Skip to content

fix(anti-spam): the URL pattern's bare-domain arm matches an email address - #922

Merged
sydevs-bot merged 4 commits into
mainfrom
claude/fix-921-url-email-boundary
Oct 11, 2026
Merged

sydevs-bot merged 4 commits into
mainfrom
claude/fix-921-url-email-boundary

Conversation

@sydevs-bot

@sydevs-bot sydevs-bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • URL_PATTERN's bare-domain arm had no left boundary, so an email address's domain half read as a link.
  • The arm now matches the whole dotted run, from a position no @ 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).

 URL_PATTERN, third arm                        src/lib/antiSpam/antiSpamGuard.ts
-  <label>.<listed-tld>, anywhere in the value
-    "[email protected]"         → gmail.com            # an address, read as a link
-    "see buy-now.example.com"   → example.com
+  <label>(.<label>)*.<listed-tld>, starting where no `@` or domain char precedes
+    "[email protected]"         → no match             # the run starts after `@`
+    "see buy-now.example.com"   → buy-now.example.com  # whole run, so still caught
+    "[email protected] buy at cheap.shop" → cheap.shop           # per position, not per value
+    "join us at .cheap.shop"    → .cheap.shop          # leading dots consumed first
   arms 1 and 2 (`https?://`, `www.`) untouched
     "https://[email protected]"     → still a link

Contract — what a public write observes

 POST /api/user-submissions → urls_not_allowed        docs/rules/api-clients.md
 - exempt by key: path, hostUrl, error, userAgent, locale
+- exempt by shape: an address's domain half, in every scanned field
+- still refused: a value carrying both an address and a link

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.

  • Forms list — pick a contact form and take its id for the request
  • User Submissions list — where an accepted submission lands, with its stored submissionData pairs

Test results

  • Lean gate: ✓ pnpm lint, pnpm typecheck, pnpm typecheck:tests, pnpm test:unit — 2320 passed
  • Targeted spec: 32 passed — tests/int/user-submissions-create.int.spec.ts
  • Build: N/A — GitHub Actions does not build this app; Railway builds the preview

Each 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.org was also matched by the bare-domain arm, so deleting the www. arm left the suite green. It is now www.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 a contact form id from the Forms list:

  1. submissionData: [{field:"email",value:"[email protected]"},{field:"message",value:"hello"}] → 201. Before this change, 400 urls_not_allowed.
  2. The same body with message set to see buy-now.example.com → still 400 urls_not_allowed.
  3. The same body with message set to [email protected] buy at cheap.shop → still 400.
  4. The same body with message set to join 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.shop and ..cheap.shop had 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 runs beforeValidate, so maxLength has not run yet, and proposed.* is a jsonField with 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.com stops being refused, which is acceptance criterion 2. tests/unit/anti-spam-guard.spec.ts now pins that string in the pattern's own suite, not only in submission-screening.spec.ts, since the pattern is where someone would try this.

displayNameFor is 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 any word.tld; it now says word.tld in a local part, which is what makes the dot-rewrite load-bearing.

No overlap with #919. That PR routes an authored email answer through checkEmailAllowed, which is additive capability (disposable_email), not an exemption this subsumes. It touches prepareUserSubmission.ts and submissionData.ts; this touches neither, bar the one docblock clause above. #920 is also untouched — website and onlineUrl hold 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():

input (1 MB) old (?<!@…{0,255}) whole run
dot run 4.98 ms 1263 ms 4.48 ms
a. repeated 5.58 ms 1366 ms 28.5 ms
a.b-c. repeated 7.19 ms 1284 ms 21.4 ms
@ + dot run 3.41 ms 1156 ms 4.64 ms
ordinary prose 10.2 ms 25.7 ms 7.17 ms

The 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 on a-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 from src/collections/UserSubmissions/fields.ts (z.looseObject({}).meta({ maxProperties: 60 }), no string bound) and src/collections/Users/Users.ts (name, type: 'text', no maxLength), with Payload's hook order in node_modules/payload/dist/collections/operations/create.js putting collection beforeValidate ahead of field validate.

Not a sibling fix. src/lib/eventQuality/heuristics.ts:12 holds 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-review was not run as a separate pass — the toolkit plugin is not installed in a routine. /security-review did not trigger: no changed path matches securityReview.triggerPattern.

Closes #921

… 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
@railway-app

railway-app Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

🚅 Deployed to the SahajCloud-pr-922 environment in sahajcloud

Service Status Web Updated
SahajCloud ✅ Success (View Logs) Web Oct 11, 2026 at 4:45 pm UTC
1 service not affected by this PR
  • mailpit

@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-922 October 11, 2026 16:10 Destroyed
@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Oct 11, 2026
@sydevs-bot

sydevs-bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 no session running

recent dispatches
  • 16:36Z address-review · attempt 1 · session · done
  • 16:36Z address-review · attempt 1 · session
  • 16:23Z review-pr · attempt 1 · session · done
  • 16:23Z review-pr · attempt 1 · session

Written by the sydevs dispatcher. Sessions never edit this comment.

@sydevs-bot sydevs-bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🧐 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 in prepareUserSubmission.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.com alone is still refused, so the reworded docblock is accurate.
  • Consumer side followed: SahajAtlasWeb's ReportIssueForm and mutate.ts treat urls_not_allowed as actionable, and this change only ever refuses less, so nothing there breaks. WeMeditateWeb names the code and no behaviour. docs/rules/api-clients.md is 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

Comment thread src/lib/antiSpam/antiSpamGuard.ts Resolved
Comment thread tests/unit/anti-spam-guard.spec.ts Resolved
@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Oct 11, 2026
`.` 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
@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-922 October 11, 2026 16:43 Destroyed
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Both review findings adopted. Pushed 5da7642.

Body corrected on one point the review raised: the pre-existing quadratic is retired by this diff, not unchanged. Measured old vs. shipped on a-runs — 64 KB, 3625 ms → 0.46 ms. That is the larger of the two DoS exposures here, and not the one the ticket asked about.

Nothing rebutted, nothing asked.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Oct 11, 2026
@sydevs-bot
sydevs-bot marked this pull request as ready for review October 11, 2026 16:51
@sydevs-bot
sydevs-bot requested a review from Ardnived October 11, 2026 16:51
@sydevs-bot
sydevs-bot enabled auto-merge October 11, 2026 16:51
@sydevs-bot
sydevs-bot added this pull request to the merge queue Oct 11, 2026
Merged via the queue into main with commit fc16753 Oct 11, 2026
43 of 45 checks passed
@sydevs-bot
sydevs-bot deleted the claude/fix-921-url-email-boundary branch October 11, 2026 19:39
@sydevs-bot sydevs-bot moved this from Revising to Done in SYDevelopers Technical Roadmap Oct 11, 2026
sydevs-bot added a commit that referenced this pull request Oct 12, 2026
`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

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-922 — 5da76425 Deployed Oct 11, 2026 by railway-app[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

fix(anti-spam): the URL pattern's bare-domain arm matches an email address

3 participants