Skip to content

bug(platform): a shortened two-factor grace period never blocks anyone earlier; the deadline slides on every check #4424

Closed

Description

@yannickmonney

Found in the post-merge review of #4420 (TALE-947, agent #6, run 2d1106e9), at merge 464e1d9b / head 1948429b. The cited files are unchanged on main 8b750caf30.

Summary

When an admin shortens the two-factor grace period, nobody who already has a grace anchor is blocked any earlier. The deadline the account is shown moves forward on every evaluation, so the account is still blocked only when its original anchor passes.

The domain spec states the opposite. #4420 added a test that asserts the spec's claim, but at one frozen instant, so it passes.

Where

  • Code: services/platform/backend/domains/two_factor/service.ts:267-283
    • cap = now + gracePeriodDays * 24 * 60 * 60 * 1000 (:268) is recomputed from the current time on every evaluation.
    • deadline = Math.min(anchor, cap) (:276) and the block test deadline <= now (:277) follow.
    • With gracePeriodDays > 0, cap is always later than now, so the account is blocked only once the stored anchor itself has passed.
    • The anchor is written once and only when absent, by setGraceUntilIfAbsent with ON CONFLICT (user_id) DO NOTHING (:294-304, :302). Nothing shortens it.
    • GitHub code search for two_factor_grace finds only DELETE statements elsewhere: members/service.ts:733 and erasure/service.ts:1401.
    • The comment at :274-275 says "a shortened grace takes effect immediately".
  • Spec: services/platform/backend/domains/two_factor/spec.md:37-44 (TFA-R4)
    • The rule says: "A policy that is made shorter applies at once."
    • The example says: "Mia first signs in on Monday → she has until the next Monday. An admin then cuts the period to 2 days → she has until Wednesday."
  • Test: services/platform/backend/domains/two_factor/service.enforcement.test.ts:154-169, titled "never restarts a running grace period, and applies a shorter one at once [TFA-R4]".
    • It evaluates once at the frozen NOW, with anchor NOW+10d and policy 2 days, and asserts graceDeadline: NOW + 2 days.
    • It never advances time, so it cannot see that the 2-day deadline is never reached.

Scenario (the test's own data)

The anchor is NOW+10d (a 10-day policy at first sign-in). The policy is then cut to 2 days.

When cap Deadline shown Decision
NOW NOW+2d NOW+2d grace
NOW+3d NOW+5d NOW+5d grace (the deadline slid)
NOW+9d NOW+11d NOW+10d grace
NOW+10d — — blocked, the original anchor

In the spec's own example, Mia is blocked next Monday, not on Wednesday.

Expected (per TFA-R4)

A shortened policy moves the effective block time earlier. For example, the deadline becomes min(anchor, <start> + newDays) with the start of the grace stored, or the anchors are capped when the policy is saved. Owners decide which "at once" means.

Fix notes

I ran no tests; this comes from reading the code. A check phase can confirm it with the probe above.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions