fix: build the password reset link without path.join and encode the email [T-2539]

Merged
EV
6d ago
+55-4

What was wrong

  • api/src/services/mail.service.ts:88 built the reset link with join(config.virtualHost, 'reset-password'). path.join collapses the //, so https://t.example.com became https:/t.example.com/reset-password (on Windows hosts it even became .\http:\localhost:8080\reset-password). Mail clients usually do not turn that into a link.
  • api/src/templates/mail/password-reset-token.txt:7 printed {{endpoint}}?email={{user.email}}&resetToken={{resetToken}}: the email was not URL-encoded, and {{ }} HTML-escapes it. For jane+tr@example.com the reset page (webapp/src/app/auth/components/reset-password/reset-password.component.ts:18 and 47-49, a disabled field filled from queryParams.email) received jane tr@example.com, ResetPasswordRequest.email (@IsEmail(), api/src/domain/http.ts:621-624) answered 400, and the user could not correct the field, so they could not reset their password. An email with ' became ' inside the link, which breaks the query string.

What changed

  • New passwordResetUrl(email, token) in api/src/utils/password-reset-url.ts: ${config.virtualHost}/reset-password?email=${encodeURIComponent(email)}&resetToken=${encodeURIComponent(token)}, built the same way as verifyEmailUrl and inviteSignupUrl.
  • MailService.passwordResetToken passes that URL to the template as resetUrl. The path import stays, mustCompileTemplate still uses it.
  • The template prints {{{resetUrl}}} (unescaped), like the verify-email and invite templates.
  • docs/changelog.md: one line under ## next.

Tests

New specs, run on the unchanged code first:

  • api/src/utils/password-reset-url.spec.ts (1 test): passwordResetUrl('jane+tr@example.com', 'abc') equals ${config.virtualHost}/reset-password?email=jane%2Btr%40example.com&resetToken=abc.
  • api/src/services/mail.service.spec.ts (2 tests): spies on the private send and checks the reset email text contains that URL and no http:/ or https:/ followed by a non-slash; a second case checks that o'neil@example.com is not HTML-escaped (').

Before the fix: 2 suites failed (the util spec does not compile, TS2307; both MailService tests fail, the received text was .\http:\localhost:8080\reset-password?email=jane+tr@example.com&resetToken=abc and ...email=o'neil@example.com...). After the fix: 3 of 3 tests pass.

Checks run

  • prettier --check "**/*.ts" at the root (as bin/check_lint.sh): clean.
  • yarn --cwd api lint (tslint): clean.
  • yarn --cwd api build (tsc + copyfiles): passes; dist/src/templates/mail/password-reset-token.txt has {{{resetUrl}}}.
  • api unit tests (jest, --maxWorkers=2): 170 of 171 pass. The one failure, src/formatters/properties.spec.ts, fails only on Windows and is unrelated: the properties library writes \r\n when process.platform === 'win32', while the fixture has \n. It passes on Linux CI.
  • e2e tests were not run locally; the e2e specs that touch password reset mock passwordResetToken, so they do not read the template.

Fleet task T-2539; authored by the lead lane while the fleet is paused by its daily cost ceiling.

šŸ¤– Generated with Claude Code


Summary by cubic

Fixes the password reset email so the link actually opens the reset page and works with plus-addressed emails.

  • path.join collapsed https:// to https:/, so mail clients did not turn the link into a clickable URL.
  • The email was not URL-encoded and was HTML-escaped, so jane+tr@example.com reached the reset page as jane tr@example.com and the reset failed with a 400, with no way for the user to correct it.

The link is now built by a new passwordResetUrl helper that encodes the email and token, and the template prints it unescaped, matching how the verify-email and invite links are handled.