Skip to content

feat(console): add periodic password change policy card - #14492

Closed
HarshMN2345 wants to merge 4 commits into
mainfrom
claude/console-password-rotation
Closed

HarshMN2345 wants to merge 4 commits into
mainfrom
claude/console-password-rotation

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Adds the Periodic password change card to Auth → Policies → Passwords, so the policy shipped in #14321 is configurable from the Console. Today it has no UI and can only be set through the API.

The card is a toggle plus a maximum-age value and unit (Days / Weeks / Months / Years), reusing SessionLengthCard's duration pattern and time-unit-converter so nobody types 31536000. It registers with the policies settings-search index like its siblings.

Three behaviours worth a reviewer's eye:

  • maxAge is omitted from the PATCH while the toggle is off, matching the handler's "options left out keep their current value", so turning the policy off doesn't destroy the stored age.
  • The card hides itself when listPolicies returns no password-rotation entry, so a server older than feat: add password rotation policy #14321 shows the tab unchanged rather than offering a setting it cannot save. Same conditional spread PasswordPwnedCard and MfaFactorsCard already use.
  • min/max on the number field are the API's range converted into the unit on screen, not hardcoded whole units, and each end is rounded inwards (Years 0.0028–10, Months 0.034–121.66) so every number the field advertises is an age the API accepts. A 1-day policy viewed in Years renders 0.0027; it used to render 0 with no error while the helper text still said "Between 1 day and 10 years".

The copy is scoped to what the policy enforces by design: a reset at the next email/password sign-in, other methods unaffected.

Test Plan

Live, against production Cloud (2.5.0): the card renders on Auth → Policies → Passwords between "Personal data" and "Breached passwords", with the range helper text and a default of 1 Years.

Automated — apps/console:

bun test tests/unit/password-rotation.test.ts
  28 pass, 0 fail, 83 expect() calls

bunx playwright test --project=console-mocked e2e/console.mocked.password-rotation.spec.ts
  10 passed

The mocked spec covers: reading back a stored policy and saving a new age (asserting seconds, not the unit on screen); the card being absent when the server reports no rotation policy, while the Strength card still renders and no request touches the endpoint; the range validation at both ends; turning the policy off sending exactly {enabled: false} and the stored age surviving; switching units repeatedly without the value drifting; a refused save keeping the edit; a short age rendered in a large unit; typing the bound the field itself advertises and saving it, at both ends; and a refetch the card did not ask for (a sibling policy's save) leaving an edit alone while a stored policy that really changed is adopted.

Writing the tests turned up two defects in the first draft of this card, both fixed here and both covered by tests that fail without the fix:

  • Switching the time period drifted the stored value (1 Year → 12.17 Months → 52.14 Weeks…, each conversion rounding on the previous one).
  • The card could open already dirty, with Update enabled before any edit.

Review round

The advertised bounds could not be saved — fixed, with tests that fail without it.

min/max are the API's range converted into the unit on screen, and rounding them to the field's precision moved both ends outside the range the card then validates in seconds: 0.0027 years is 85,147s (under the 86,400 minimum) and 121.67 months is 315,368,640s (over the 315,360,000 maximum). Typing the minimum the field itself showed produced "Minimum is 1 day" and left Update disabled, so the card displayed a value as valid and then refused it. Both ends now round inwards.

A stored age at an end of the range is still shown as it converts, so a stored day read in Years is 0.0027 against a min of 0.0028. That is the one place a displayed value sits a display step outside the field's own bound; the card's check is in seconds and still accepts it, and the exact stored age is what gets saved.

An in-progress edit is now held by value, not by object identity — no behaviour change, so nothing fails without it.

The card adopted the stored policy whenever the currentPolicy object changed, and useAuthSecuritySnapshot reparses the policy list on every refetch while every sibling policy's save invalidates that query. This was reviewed as silently discarding an edit in progress. It does not: React Query's structural sharing reuses the reference for a policy whose values did not change (replaceEqualDeep keeps authPasswordRotation even when the snapshot around it is new, and the two new e2e cases pass on the old code too). The comparison is now on the values the card actually uses, so holding an edit does not rest on a default this repo already turns off on other queries (hooks/functions.ts), and the comment matches what the code does. The two e2e cases pin both halves of that contract rather than leaving them resting on the default.

Both ends of the round were checked by reverting the source and re-running. On the old code the unit test fails at 85147.20000000001 >= 86400; the mocked lane reports 2 failed, 8 passed, the advertised min resolving to 0.0027 and a typed bound rendering "Minimum is 1 day" instead of the helper text. Restoring the source turns all 10 green. The two refetch cases pass either way, as described above.

tsc --noEmit is red on main (1824 errors) and so cannot gate anything; bun run lint:types reports 1824 known error(s) in 410 file(s), none new. eslint and prettier are clean on the changed files. No copy changed in this round, so the dictionaries are untouched; i18n:audit:he and i18n:audit:ja both report no issues.

Related PRs and Issues

Not verified

  • No live save. The card was confirmed rendering against Cloud, but PATCH /v1/project/policies/password-rotation and an actually-expired password were exercised only against the mocked lane.
  • Only the console-mocked Playwright project was run locally.

Scope: email/password sign-ins, and that is intended

Confirmed with Matej: the email/password-only enforcement is deliberate. Sign-ins are the only thing this policy is meant to affect, so what follows is the feature's scope, not something to close before this ships.

Enforcement sits in POST /v1/account/sessions/email: a password past maxAge is refused there, and the user gets back in through password recovery (PUT /v1/account/recovery sets passwordUpdate). Existing sessions are not revoked, PATCH /v1/account/sessions/:sessionId extends a session without the check, and OTP, magic URL, OAuth and passkey sign-ins keep working with an older password. The card's copy says exactly that, so the toggle cannot be read as "force a reset everywhere":

Require users to set a new password once the one they have reaches the maximum age. An email/password sign-in with a password past that age is refused until the user resets it through password recovery. Sign-ins with other methods are not affected.

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs? — n/a, Console only

🤖 Generated with Claude Code

HarshMN2345 and others added 3 commits October 14, 2026 19:41
Adds the password rotation policy to Auth > Policies > Passwords: a toggle
plus a maximum password age control (value + time period, days through
years), wired to PATCH /v1/project/policies/password-rotation through the
pinned SDK's project.updatePasswordRotationPolicy.

The card validates the API's 1 day to 10 years range inline and keeps the
maximum age out of the request while the policy is off, so the stored value
survives a toggle. Copy says what the policy does: an email/password
sign-in with an expired password is refused until the user resets it
through password recovery.

Servers that do not report a password-rotation policy from listPolicies
hide the card, the same way the breached-password and MFA-factors cards do,
so self-hosted installs on older versions do not see defaults they cannot
save.

Co-Authored-By: Claude Opus 5 <[email protected]>
…itch drifting it

Moves the card's pure logic into `src/lib/password-rotation.ts`, next to
`password-strength.ts` and `passkey-policy.ts`, so the range, the conversion,
the PATCH body, and the older-server path can be tested without a browser.
`parseProjectAuthSecurity` and the mutation's client-side guard now read the
range from there instead of repeating 86400 and 315360000.

Fixes a bug found while writing the tests: the card converted the maximum age
from the number on screen rather than from the stored age, and the field shows
two decimals, so each Time period change rounded on top of the last one. One
year read as 12.17 months, then 52.16 weeks, then 365.21 days. Update lit up
from nothing but opening the dropdown and offered to save an age hours off the
stored one. The card now keeps the age in seconds, the way the API stores it,
and the unit only decides how it is written: switching is lossless and is not
an edit. The same change stops the card opening dirty when the stored age is
not a whole number of the unit it reads back in.

Tests:

- `tests/unit/password-rotation.test.ts` pins the range against the spec the
  SDK is generated from, the unit pair both ways, the range messages, the
  omitted `maxAge`, and what counts as an edit.
- `e2e/console.mocked.password-rotation.spec.ts` drives the real page with the
  API mocked: the card renders and saves, it is absent when `listPolicies`
  reports no `password-rotation` policy, the range disables Update, turning the
  policy off sends `enabled` alone and the server keeps the stored age, a
  refused save keeps the edit, and a Time period change is not an edit.

The copy was re-checked against the API: the rotation check runs only in
`Http::post('/v1/account/sessions/email')`, and `PUT /v1/account/recovery` sets
`passwordUpdate`, so "refused until the user resets it through password
recovery" and "sign-ins with other methods are not affected" both hold.

Co-Authored-By: Claude Opus 5 <[email protected]>
…t the tests that could not fail

The value field's `min`/`max` were whole display units while the policy is
validated in seconds, so they disagreed with the field's own value at both
ends. A 1-day policy — the smallest age the API takes — switched to Years
rendered `0`: `maxAgeValueInUnit(86400, 'years')` rounded 0.0027 to two
decimals, the helper text still read "Between 1 day and 10 years", no error
showed, and `min={1}` called the stored age out of range. At the other end a
10-year policy in Months rendered 121.67 against a floored `max` of 121. The
field now takes its range from `maxAgeRangeInUnit`, which writes the API's
`Range(86400, 315360000)` through the same conversion as the value itself, and
a value under one unit is written to two significant digits rather than two
decimals, so the stored day reads as 0.0027 years. Nothing out of range could
ever be sent — the seconds check already caught that — but the number on screen
was simply not the stored age, which contradicted the point of keeping it in
seconds.

Also in `src/lib/password-rotation.ts`:

- Drop `policy.enabled ?? false` and `policy.maxAge ?? DEFAULT_...`. Both are
  unreachable: `Models.PolicyPasswordRotation` types `enabled` and `maxAge` as
  required, so the narrowing already guarantees them.
- Make the discriminator a numeric `maxAge` instead of key presence, so a
  response carrying `maxAge: undefined` hides the card like any other
  non-rotation policy rather than being read as a policy with an invented
  default. `DEFAULT_PASSWORD_MAX_AGE_SECONDS` had no production consumer and
  goes with it.
- `PASSWORD_MAX_AGE_UNITS` is now a const tuple with a `PasswordMaxAgeUnit`
  type, and the Time period dropdown renders from it through an exhaustive
  label map. The list the test pins is the list the dropdown shows; removing a
  unit from the constant no longer leaves it on screen.
- `hasPasswordRotationChanges` compares `Math.round(draft.maxAgeSeconds)` to
  the stored age — the whole second `passwordRotationUpdate` actually sends —
  instead of a 0.5 tolerance that needed a comment to defend.

Tests, after a review pass for assertions that cannot fail:

- "round trips through the display without drifting" asserted
  `round(round(x)) === round(x)`, which holds for any rounding at any
  precision, and was named after the drift bug it could not catch. It now
  records the one-way loss instead: a year read back from Months gains 8,640s
  and from Weeks loses 1,728s, which is why the card derives the display from
  the stored seconds rather than converting it.
- The `PASSWORD_MAX_AGE_UNITS` test asserted the constant against its own
  literal. It now asserts the property the comment claimed — every offered
  unit can write an age the API accepts, and `hours` cannot.
- The spec check matched English prose ("86400 seconds"). It matches the bare
  numbers now, with a note that the schema carries no machine-readable
  `minimum`/`maximum`, so nobody tightens it back.
- The `maxAge: undefined` test claimed a fallback the code did not have
  (`'maxAge' in { maxAge: undefined }` is `true`). It pins the branch the code
  takes.
- New coverage for the display defect: `maxAgeValueInUnit(ONE_DAY, 'years')` is
  0.0027, `maxAgeRangeInUnit` is pinned in all four units, and the mocked e2e
  loads a 1-day policy, switches to Years and asserts the rendered value and
  `min` attribute, then types 10 years and reads it back in Months against
  `max`. All three fail on the old code.

Co-Authored-By: Claude Opus 5 <[email protected]>
@HarshMN2345
HarshMN2345 marked this pull request as ready for review October 15, 2026 08:28
@luxor-code-review

luxor-code-review Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 2 medium

💬 Inline comments (2)

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Decimal input is rewritten on each keystroke (PasswordRotationCard.tsx:139) — The maximum age input is controlled from seconds, and onChange parses the typed text with parseFloat (apps/console/src/components/pages/projects/$projectId/auth/policies/PasswordRotationCard.tsx:139).

This PR introduces a password-rotation policy editor in the console: pure helpers for parsing/serializing the policy in seconds, a new card in the auth policies page with a unit dropdown and value input, React Query mutation hooks against the project auth endpoint, a settings-search index entry, a mocked Playwright e2e spec, unit tests, and Hebrew/Japanese dictionary entries.

Files Change
password-rotation.ts, password-rotation.test.ts Pure parse/serialize/rounding helpers for the rotation policy plus their unit tests.
PasswordRotationCard.tsx, Passwords.tsx New policy card UI: enable toggle, numeric value with unit selection, save gating, and integration into the passwords policy page.
project-settings.ts, react-query/hooks/auth.ts Snapshot type growth and a updatePasswordRotationPolicy mutation with cache invalidation.
policies-settings-cards.ts Settings-search index entry for the new card.
console.mocked.password-rotation.spec.ts Mocked-network Playwright coverage of the editor flow.
dictionaries/he/auth-storage.ts, dictionaries/ja/auth-storage.ts Hebrew and Japanese translations for the new copy.

Reviewed commit: 9c37bb6

@ghost

ghost commented Oct 15, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The updated bounds are saveable across the offered units, and the edit synchronization handles both unchanged and externally changed policy values.

Adds a periodic password-change policy card to the project Auth → Policies → Passwords page, with unit-based age editing, validation, and policy updates. The card is shown only when the server reports the policy, and the project auth-security snapshot and settings-search index include it. Hebrew and Japanese translations and unit and mocked-browser coverage accompany the feature.

Latest changes: The newest commits round advertised age bounds inward so they can be saved, synchronize edits by comparing policy values across refetches, and add coverage for bounds and sibling-policy refetches.

Verdict New comments Fixed Still open
✅ Approved 0 1 0
📂 Walkthrough · 10
File Change
apps/console/e2e/console.mocked.password-rotation.spec.ts Adds mocked-browser coverage for policy visibility, conversions, updates, validation, failed saves, and refetch behavior.
apps/console/src/components/pages/projects/$projectId/auth/policies/PasswordRotationCard.tsx Adds the periodic password-change policy card with its toggle, age and unit fields, and update behavior.
apps/console/src/components/pages/projects/$projectId/auth/policies/Passwords.tsx Shows the card only when the server reports the password-rotation policy.
apps/console/src/lib/i18n/dictionaries/he/auth-storage.ts Adds Hebrew translations for the new policy card.
apps/console/src/lib/i18n/dictionaries/ja/auth-storage.ts Adds Japanese translations for the new policy card.
apps/console/src/lib/password-rotation.ts Adds policy parsing, age conversion, inward-rounded saveable bounds, validation, and PATCH-body helpers.
apps/console/src/lib/project-settings.ts Includes password-rotation policy data in the project auth-security snapshot.
apps/console/src/lib/react-query/hooks/auth.ts Adds the project password-rotation policy update mutation and validation.
apps/console/src/lib/settings-search/policies-settings-cards.ts Registers the new card in policies settings search.
apps/console/tests/unit/password-rotation.test.ts Tests policy parsing, unit conversion, saveable bounds, range validation, and update payload behavior.
✅ Fixed since the last review · 1
  • Rounded range endpoints can’t be entered as valid ages · apps/console/src/lib/password-rotation.ts:93

Reviewed the commits since 9c37bb6 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · See the inline comments. Summary

@luxor-code-review luxor-code-review Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds a periodic password-rotation policy card to the console auth settings with unit conversion helpers, mutation wiring, mocked e2e, and he/ja translations; a bounds-check mismatch and two UI state issues worth addressing.

Key findings

Comment on lines +88 to +96
export function maxAgeRangeInUnit(unit: TimeUnit): {
min: number
max: number
} {
return {
min: maxAgeValueInUnit(MIN_PASSWORD_MAX_AGE_SECONDS, unit),
max: maxAgeValueInUnit(MAX_PASSWORD_MAX_AGE_SECONDS, unit),
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 bug · medium

Advertised min/max bounds fail the seconds range check

The range shown in the value field is rounded to two decimals (maxAgeRangeInUnit at apps/console/src/lib/password-rotation.ts:88), but the error check compares raw seconds (passwordMaxAgeRangeError at apps/console/src/lib/password-rotation.ts:115). Rounding makes the weeks and years minimums fall below one day, and the weeks and months maxima rise above ten years. A user who types an advertised bound is shown the min/max error and Update stays disabled, so an age the card just displayed as valid cannot be saved.

📋 Prompt for AI Agents

In apps/console/src/lib/password-rotation.ts around lines 88-96 (maxAgeRangeInUnit), the rounded min/max can be outside the API range in seconds: years min 0.0027 (~85147s), months min 0.033 (~85536s) and weeks min 0.14 (~84672s) are below 86400, and months max 121.67 (~315368640s) and weeks max 521.43 (~315408864s) are above 315360000. Change the function so the advertised min rounds up and the max rounds down to the two-decimal precision, so every displayed bound passes passwordMaxAgeRangeError at around line 115. Update the unit test expectations in apps/console/tests/unit/password-rotation.test.ts accordingly.

Comment on lines +52 to +59
useEffect(() => {
// Adopt the stored policy only when it changes, so a refused save keeps the
// edit on screen. The unit stays the one the user picked.
if (mutation.isPending || syncedPolicy.current === currentPolicy) return
syncedPolicy.current = currentPolicy
setEnabled(currentPolicy.enabled)
setMaxAgeSeconds(currentPolicy.maxAge)
}, [currentPolicy, mutation.isPending])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 bug · medium

Draft resets on any refetch, not real changes

PasswordRotationCard adopts the stored policy whenever the currentPolicy object reference changes (PasswordRotationCard.tsx:55), and the auth snapshot hook returns a fresh parsed object on every refetch of the auth query (Security.tsx:63). Any unrelated save on the same page — every sibling policy mutation calls invalidateProjectAuthQueries — or a window-focus refetch produces a new object and silently wipes the user's in-progress rotation edit, contrary to the comment's stated intent of adopting the policy "only when it changes". Users lose typed work without any indication.

📋 Prompt for AI Agents

In apps/console/src/components/pages/projects/$projectId/auth/policies/PasswordRotationCard.tsx around lines 52-59, change the adoption effect so it only calls setEnabled/setMaxAgeSeconds when the incoming policy's enabled or maxAge actually differ from syncedPolicy.current's values (compare fields, not the object reference), so unrelated refetches of the auth snapshot do not discard a draft the user is editing.

Two findings from review of the periodic password change card.

`maxAgeRangeInUnit` wrote the API's `Range(86400, 315360000)` through the same
rounding as the value field, which moved both ends outwards: 0.0027 years is
85,147s, under the 86,400 minimum, and 121.67 months is 315,368,640s, over the
315,360,000 maximum. The error check compares seconds, so typing the minimum the
field itself advertised showed "Minimum is 1 day" and left Update disabled — the
card displayed a value as valid and then refused to save it. Each end is now
rounded towards the inside of the range (Years 0.0028–10, Months 0.034–121.66,
Weeks 0.15–521.42, Days 1–3650), so every bound on screen is an age that can be
saved. A stored age at an end of the range is still written as it converts, so a
stored day read in Years is 0.0027 against a `min` of 0.0028: the one place a
displayed value sits a display step outside the field's own bound, and the
seconds check still accepts it.

`PasswordRotationCard` adopted the stored policy whenever the `currentPolicy`
object changed. The snapshot hook reparses the policy list on every refetch and
every sibling policy's save invalidates it, so that comparison was reported as
discarding an edit in progress. It does not: React Query's structural sharing
reuses the reference for a policy whose values did not change, which
`replaceEqualDeep` confirms and which the two new e2e cases show by passing on
the old code too. The comparison is now on the values the card actually uses, so
holding an edit no longer rests on a default this repo already turns off on
other queries (`hooks/functions.ts`), and the comment matches the code.

Tests:

- The `maxAgeRangeInUnit` property test asserts that, for every offered unit,
  both advertised bounds read back inside the API's range and raise no range
  error. It fails on the old code at 85,147s >= 86,400. The `min > 0` test it
  replaces is subsumed by it: a bound of 0 fails the same assertion.
- The mocked spec types the bound the field advertises at both ends and saves it
  (88,301s and 315,342,720s), and pins the `min`/`max` attributes that a stored
  day and 10 years are shown against. Both fail on the old code.
- Two cases drive a refetch the card did not ask for, by saving the Dictionary
  policy next to it: an unchanged stored policy leaves the edit alone, a changed
  one is adopted. Neither fails on the old code, for the structural sharing
  reason above; they pin the contract rather than leave it resting on that
  default.

Co-Authored-By: Claude Opus 5 <[email protected]>

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@HarshMN2345
HarshMN2345 deleted the claude/console-password-rotation branch October 16, 2026 12:12
@HarshMN2345

Copy link
Copy Markdown
Member Author

Replaced by #14602.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant