Repository navigation
fix(anti-spam): the URL pattern's bare-domain arm matches an email address - #922
Conversation
… link `URL_PATTERN`'s bare-domain arm had no left boundary, so it matched the domain half of an email address: `[email protected]` read as a link. Every consumer of `checkNoUrls` inherited it — the write guard's `users` name policy, the `user-submissions` intake, and the proposal screening job. The arm now matches the whole dotted run, from a position no `@` or domain character precedes. Two shapes were measured and rejected: A variable-length lookbehind over the prefix (`(?<!@[a-z0-9.-]{0,255})`) is the same rule, but it is the arm's first token, so it re-runs at every scan position instead of once per candidate. Measured against the old pattern on a 1 MB dot-run: 4.98ms to 1263ms, and `proposed.*` has no length cap at scan time because the write guard runs `beforeValidate`, ahead of `maxLength`. That is a denial-of-service amplification on a public write path, not a constant. A one-character lookbehind (`(?<![@a-z0-9.-])`) is cheap and wrong: it also exempts a domain sitting behind a dot, so `see buy-now.example.com` would stop being refused. "Skip a value containing `@`" stays rejected as the ticket says — a scan bypass rather than a narrowing, since `checkNoUrls` tests whole field values. `[email protected] buy at cheap.shop` is still refused. One loosening is accepted and stated at the pattern: a domain parked behind a plausible address (`[email protected]`) now passes. The arm-removal matrix the acceptance criteria ask for is now real — removing the boundary, or any one of the three arms, fails a case no other covers. `www.spam-site.org` did not do that, since the bare-domain arm matched it too. `displayNameFor`'s docblock claimed the pattern matches any `word.tld`. It matches one in a local part, which is what still makes that rewrite load-bearing (`[email protected]` is refused either way, measured). Refs #921 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01DTYt2MDg15hX5MeCgZiUk4
The client contract lists what `urls_not_allowed` scans and its one per-key exemption. The pattern-level narrowing is observable the same way, in every scanned field, so it belongs beside it. Refs #921 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01DTYt2MDg15hX5MeCgZiUk4
|
🚅 Deployed to the SahajCloud-pr-922 environment in sahajcloud
1 service not affected by this PR
|
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 Adversarial review
Four files, 38 lines, nothing outside #921 — the scope is right and there is no refactor riding along. Deviating from the ticket's variable-length lookbehind is well earned: that form is ~250x slower, and proposed.* carries no scan-time length cap, so an unbounded value could block the loop. The whole-run form also retires a pre-existing quadratic the body says it leaves unchanged — 64KB of a costs 6342ms under the old pattern and 0.4ms under this one. One cost of the deviation is unstated: the arm's left boundary now exempts more than an address's domain.
Findings
- should-fix — a leading dot defeats the bare-domain arm outright.
src/lib/antiSpam/antiSpamGuard.ts:144. - consider — the new suite's justification comment asserts the atlas email field is required; that repo documents it as optional.
tests/unit/anti-spam-guard.spec.ts:42.
What was checked
- Read whole:
antiSpamGuard.ts,writeGuard/policies.ts,submissionData.ts(urlScannablePairs,URL_EXEMPT_KEYS), the scan call site inprepareUserSubmission.ts, both touched specs. - Ran old vs. new over 48 strings, including every acceptance criterion. Only verdict changes are the 7 address shapes the ticket wants, plus 5 leading-dot bypasses nobody asked for.
- Timed both patterns on 1 MB of dot runs,
a.,a.b-c., prose and addresses, and on a no-dot run. - Confirmed
displayNameFor's dot-rewrite is still load-bearing:foo.comalone is still refused, so the reworded docblock is accurate. - Consumer side followed: SahajAtlasWeb's
ReportIssueFormandmutate.tstreaturls_not_allowedas actionable, and this change only ever refuses less, so nothing there breaks. WeMeditateWeb names the code and no behaviour.docs/rules/api-clients.mdis the only contract doc that states the rule, and it is updated.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
`.` is in the bare-domain arm's own left-boundary class, so a dotted run sitting behind a dot had no legal match start: `join us at .cheap.shop` and `..cheap.shop` both passed the scan, where the old pattern refused them. `\.*` after the lookbehind consumes the leading dots so the run starts where it really starts. Verdict-identical on every other case measured — the 7 address shapes the ticket exempts stay exempt, and all four acceptance criteria still refuse. Cost is immaterial: 1 MB of pure dots goes 2.6 ms to 4.7 ms, against the 1263 ms the variable-length lookbehind cost on the same input. Both new assertions were watched failing with the fix reverted. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01TFwvv6FKSxXsCKzm2RHzsE
The comment claimed the atlas report form's email field is required. `required` is per-form configuration, and that form's own email field is optional — SahajAtlasWeb `ReportIssueForm.test.tsx:81` asserts it carries no required marker. The justification does not need the claim: a sender hitting this refusal has to delete their address either way. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01TFwvv6FKSxXsCKzm2RHzsE
|
Both review findings adopted. Pushed
Body corrected on one point the review raised: the pre-existing quadratic is retired by this diff, not unchanged. Measured old vs. shipped on Nothing rebutted, nothing asked. 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
`still URL-scans an address question authored as a text block` asserted that `[email protected]` in a `text` block raises `urls_not_allowed`. #922 removed that outcome everywhere: the bare-domain arm no longer reads the domain half of an address as a link. #919 landed the case on top of it, so `main` is red at f823484 too — the failure is not this branch's, and fixing it here is what the handler does with one. The property under test is the branch, not the value: a block the author made `text` gets the URL scan, while an `email` one gets the address check. `see example.com` carries that, and is the same input the `email`-block case above judges `invalid_email`, so the pair reads as one contrast keyed on block type. Reintroducing a `text` exemption in `valueSchemaFor` turns the case red, so it is not vacuous. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_0136exQRmeiDqDAMeS3tj9t9
Summary
URL_PATTERN's bare-domain arm had no left boundary, so an email address's domain half read as a link.@or domain character precedes.Change outline
Behaviour — what the bare-domain arm matches
The arm is given a left boundary. It matches the domain forward from its true start rather than looking back over it, which is what keeps the scan cheap (see Notes).
Contract — what a public write observes
Preview
The change is server-side, so these are the two pages that let you drive the Manual verification below rather than pages that show the change.
contactform and take its id for the requestsubmissionDatapairsTest results
pnpm lint,pnpm typecheck,pnpm typecheck:tests,pnpm test:unit— 2320 passedtests/int/user-submissions-create.int.spec.tsEach new assertion was watched failing before it was committed. Removing the left boundary fails 9 address cases; removing the protocol arm fails 3, the
www.arm 1, the bare-domain arm 4. That last part is acceptance criterion 6, and it was not satisfied by the suite as written —www.spam-site.orgwas also matched by the bare-domain arm, so deleting thewww.arm left the suite green. It is nowwww.spam-site.example. Removing\.*fails the two leading-dot cases.Manual verification
The behaviour lives behind
POST, not in the admin, so it needs one request each way. Against the preview, with an active client key and acontactform id from the Forms list:submissionData: [{field:"email",value:"[email protected]"},{field:"message",value:"hello"}]→ 201. Before this change, 400urls_not_allowed.messageset tosee buy-now.example.com→ still 400urls_not_allowed.messageset to[email protected] buy at cheap.shop→ still 400.messageset tojoin us at .cheap.shop→ still 400.Notes for reviewer
One judgement call worth your eye. Giving the arm a left boundary necessarily exempts anything that looks like an address's domain, so a domain parked behind a plausible address —
[email protected]— now passes the scan. That is intrinsic to the approach the ticket specifies, not to this particular spelling of it; any left boundary has it. It is stated at the pattern rather than hidden. If you would rather refuse that shape, the scan is the wrong layer for it and it needs its own ticket.A leading dot had to be consumed, not just bounded.
.is in the boundary class itself, so.cheap.shopand..cheap.shophad no legal match start and passed the scan — shapes the old pattern refused.\.*after the lookbehind consumes them, costs nothing measurable (1 MB of pure dots: 2.6 ms → 4.7 ms), and changes no other verdict in the suite or the ticket.The shape of the boundary changed twice under review, and the measurements are why. The ticket proposes a variable-length lookbehind,
(?<!@[a-z0-9.-]{0,255}). It is the correct rule and I shipped it first, then replaced it: because the lookbehind is the arm's first token, it re-runs at every scan position instead of once per candidate match. Measured on a 1 MB dot-run, against the old pattern — 4.98 ms → 1263 ms. That is not a constant factor to accept: the write guard runsbeforeValidate, somaxLengthhas not run yet, andproposed.*is ajsonFieldwith no string-length bound at all at scan time. A single allowed create could block the event loop for over a second. The whole-run form costs 4.48 ms on the same input and is verdict-identical on all 16 cases in the suite.Dismissed: a one-character lookbehind on its own, without the whole-run form. It is wrong — it also exempts a domain sitting behind a dot, so
see buy-now.example.comstops being refused, which is acceptance criterion 2.tests/unit/anti-spam-guard.spec.tsnow pins that string in the pattern's own suite, not only insubmission-screening.spec.ts, since the pattern is where someone would try this.displayNameForis not made redundant, and its docblock needed one clause corrected. It guards the local part, and[email protected]is still refused after this change (measured against the real export). The docblock claimed the pattern matches anyword.tld; it now saysword.tldin a local part, which is what makes the dot-rewrite load-bearing.No overlap with #919. That PR routes an authored
emailanswer throughcheckEmailAllowed, which is additive capability (disposable_email), not an exemption this subsumes. It touchesprepareUserSubmission.tsandsubmissionData.ts; this touches neither, bar the one docblock clause above. #920 is also untouched —websiteandonlineUrlhold real URLs that any correct pattern matches.Detail
Why the cost difference is structural, not incidental. A negative lookbehind placed first makes the engine walk back up to the bound at each of the n start positions, before the cheap
[a-z0-9-]+test can reject. Matching the run forward does that work only where a candidate actually begins. Measured, old vs. lookbehind vs. whole-run, single.test():(?<!@…{0,255})a.repeateda.b-c.repeated@+ dot runThe pre-existing quadratic in
[a-z0-9-]+\.is retired by this diff, not carried over. A one-character left boundary rejects at every interior position of a no-dot run, so the run is never re-scanned. Measured ona-runs, old against shipped: 8 KB 53.5 ms → 0.56 ms, 16 KB 198.6 ms → 0.07 ms, 32 KB 968.5 ms → 0.23 ms, 64 KB 3625 ms → 0.46 ms. That is the larger of the two denial-of-service exposures this PR closes, and it was not the one the ticket asked about.Why the bound is gone rather than tuned. With the lookbehind,
{0,63}— a single label's limit — refused an address whose domain carries more than 63 characters before its final label, so the bound had to be a domain's 255. The whole-run form needs no bound at all, and the suite keeps a long multi-label address as a case regardless.Fixture pre-mortem. The new cases are plain strings handed to an exported pure function; there is no fixture asserting anything about the real configuration. The one claim about configuration is in the reasoning above — that
proposed.*has no scan-time length cap — read fromsrc/collections/UserSubmissions/fields.ts(z.looseObject({}).meta({ maxProperties: 60 }), no string bound) andsrc/collections/Users/Users.ts(name,type: 'text', nomaxLength), with Payload's hook order innode_modules/payload/dist/collections/operations/create.jsputting collectionbeforeValidateahead of fieldvalidate.Not a sibling fix.
src/lib/eventQuality/heuristics.ts:12holds a second link matcher,URL_RE. It has no bare-domain arm, so it never carried this defect and needs no matching change.Review passes run:
/simplify(4 agents) and the deterministic comment lint./code-reviewwas not run as a separate pass — the toolkit plugin is not installed in a routine./security-reviewdid not trigger: no changed path matchessecurityReview.triggerPattern.Closes #921