Skip to content

fix(forms): auto-refresh challenge and honor minSubmitSeconds on submit (#690) - #694

Merged
DavidBabinec merged 2 commits into
CoreBunch:mainfrom
ghazy2001:fix/form-challenge-refresh-time-trap
Nov 16, 2026
Merged

DavidBabinec merged 2 commits into
CoreBunch:mainfrom
ghazy2001:fix/form-challenge-refresh-time-trap

Conversation

@ghazy2001

Copy link
Copy Markdown
Contributor

Summary

Fixes #690.

A visitor who stayed on a page with a CMS form for ~5 minutes (e.g. while drafting a message or filling out fields) would receive a 400 "Form submitted too quickly" rejection on their first submit attempt:

  1. The initial form challenge prefetched on page load expires after 5 minutes (CHALLENGE_TTL_MS = 5 * 60 * 1000).
  2. At submit time, takeChallenge() checks challengeIsFresh(). If expired, it discards the stale challenge and requests a new challenge on demand from /_instatic/form/challenge.
  3. The server measures elapsedMs = Date.now() - challenge.issuedAt. Because the newly requested challenge was only milliseconds old, the server's time-trap (elapsedMs < snapshot.minSubmitSeconds * 1000) rejected the submission.
  4. The user was forced to submit a second time once a few seconds elapsed.

Changes

  1. Auto-refresh before expiry (formRuntimeJs.ts):

    • Added scheduleChallengeRefresh(form, challenge) to automatically prefetch a fresh challenge 30 seconds before expiration (expiresAt - 30_000), bounded by a 32-bit signed timeout ceiling.
    • For users actively spending minutes on a page, a fresh challenge is constantly pre-warmed and ready without submit-time network delay.
    • Timers are cleaned up when the form disconnects, submits, or receives a replacement challenge.
  2. Honor minSubmitSeconds on stale submit (handler.ts & formRuntimeJs.ts):

    • handleChallenge now includes minSubmitSeconds: snapshot.minSubmitSeconds in the challenge response.
    • When takeChallenge() must fetch a fresh challenge on demand at submit time (e.g. due to background tab hibernation, network reconnect, or prefetch failure), it pauses for the remaining duration of minSubmitSeconds while the submit button displays "Sending...".
    • The server's time-trap check remains fully intact to reject instant bot submissions, while preventing false-positive rejections for human visitors.
  3. Tests:

    • Added test in src/__tests__/publisher/formRuntime.test.ts verifying that stale challenges are re-fetched and minSubmitSeconds is honored on submit.
    • Updated src/__tests__/server/publicForms.test.ts asserting that minSubmitSeconds is returned in the challenge response.

Verification

  • bun test src/__tests__/publisher/formRuntime.test.ts (5 pass, 0 fail)
  • bun test src/__tests__/server/publicForms.test.ts (13 pass, 0 fail)
  • bun test src/__tests__/forms/ (41 pass, 0 fail)
  • bunx tsc --noEmit (0 errors)
  • bunx eslint server/forms/handler.ts src/modules/base/forms/formRuntimeJs.ts src/__tests__/publisher/formRuntime.test.ts src/__tests__/server/publicForms.test.ts (0 errors)

@ghazy2001
ghazy2001 marked this pull request as ready for review November 14, 2026 13:56
…e fill time on refreshed challenges

The background refresh compared the server's absolute expiry with the
browser clock, so a visitor whose clock ran fast re-requested a challenge
every second. The challenge response now carries expiresInSeconds and the
runtime measures the lifetime from when the response arrived.

A submit made right after a background refresh still held a challenge
younger than minSubmitSeconds and was rejected. The runtime now waits out
the remaining fill time whenever the visitor has had the form for at least
minSubmitSeconds, and still posts earlier submits straight away so the
server's time trap rejects them.
@DavidBabinec
DavidBabinec merged commit 3a48841 into CoreBunch:main Nov 16, 2026
5 checks passed
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.

[Bug]: CMS form shows "Form submitted too quickly" on first submit after the page is open ~5 minutes (stale challenge refetched at submit)

2 participants