Repository navigation
fix(forms): auto-refresh challenge and honor minSubmitSeconds on submit (#690) - #694
Merged
DavidBabinec merged 2 commits intoNov 16, 2026
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:CHALLENGE_TTL_MS = 5 * 60 * 1000).takeChallenge()checkschallengeIsFresh(). If expired, it discards the stale challenge and requests a new challenge on demand from/_instatic/form/challenge.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.Changes
Auto-refresh before expiry (
formRuntimeJs.ts):scheduleChallengeRefresh(form, challenge)to automatically prefetch a fresh challenge 30 seconds before expiration (expiresAt - 30_000), bounded by a 32-bit signed timeout ceiling.Honor
minSubmitSecondson stale submit (handler.ts&formRuntimeJs.ts):handleChallengenow includesminSubmitSeconds: snapshot.minSubmitSecondsin the challenge response.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 ofminSubmitSecondswhile the submit button displays"Sending...".Tests:
src/__tests__/publisher/formRuntime.test.tsverifying that stale challenges are re-fetched andminSubmitSecondsis honored on submit.src/__tests__/server/publicForms.test.tsasserting thatminSubmitSecondsis 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)