Skip to content

fix(risk): record a clicked host's verdict for every link on the host - #478

Open
polylane[bot] wants to merge 1 commit into
mainfrom
polylane/autofix/x5bigpo5i4ki
Open

polylane[bot] wants to merge 1 commit into
mainfrom
polylane/autofix/x5bigpo5i4ki

Conversation

@polylane

@polylane polylane Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

When somebody clicked a link to a host whose risk verdict was stale, only that one link was marked. Every other link on the same host stayed unmarked, so the admin risk view showed them as unremarkable. This change records the verdict for all of the organization's links on that host after one click. The function's own documentation and its test both say the verdict belongs to every link on the host, but the code saved it to the clicked link alone. The test now clicks one of two links on a host and checks that both carry the verdict, which fails against the old code.

flowchart LR
  A["Visitor clicks link 1 on host H"] --> B["revalidateOnRedirect"]
  B --> C["host verdict from KV risk:host:H or a fresh lookup"]
  C --> D["select stale org links whose destination host is H"]
  D --> E["update links set risk_... where id in (...)"]
  E --> F["link 1 scored"]
  E --> G["link 2 on host H scored in the same click"]
  H["link 2 risk_score stayed null before the change"] -.-> G
Loading

Fix walkthrough

Watch the fix walkthrough

Watch the fix explained (57 s). Recorded for commit 80c1315e9c06e3382370cfd22a7f71841a52eeac.

Detection evidence

Affected: int_1a7ddf18a0010py9bcmkrims

Production impact

Risk verdict write stays inside the clicked organization's links

I checked the repository node baronunread/rdyrct: it has no outgoing edges and the workspace has no connected cloud accounts, so the change reaches no deployed resource. The changed statement binds org_id = ? to the clicking link's organization and writes only the four risk_* columns, which admin routes read.

Observability

  • risk_revalidate_failed is logged when the revalidation throws (existing, unchanged).
  • After one click, every stale links row on the host carries risk_score and risk_checked_at.

Polylane assessed this pull request at 80c1315.

Causal chain
  • Signal (log template): Click-time risk revalidation labels only the clicked link, not every link on the host
  • Surfacing site: baronunread/rdyrct at src/worker/risk.ts#revalidateOnRedirect
  • Mechanism: The UPDATE filters where id = ? and binds hit.linkId, so the cached or freshly computed host verdict is written to the clicked link alone; sibling links on the same host keep a null risk_score until each is clicked.
  • Producer: baronunread/rdyrct, at src/worker/risk.ts#revalidateOnRedirect
  • Trigger: A click on one link while sibling links on the same host exist and their verdict is stale or missing.
  • What happens to the failed unit today: The function's documented contract is that a host's verdict is written to every link on that host; the current write covers the clicked link only.
  • Last observed: 2026-11-11T15:22:12Z
  • Blast radius: unknown other resource(s), unknown other tenant(s); data at risk: Unclicked links on a malicious host stay unscored and sort as unremarkable in the admin risk table, so moderation can miss them. No end-user path or org-scoped response reads these columns.
  • Producer evidence:
2 files changed (+21/-15)
  • src/worker/risk.ts: modified, +19/-11
  • tests/worker/link-risk.worker.ts: modified, +2/-4

View autofix


Generated by Polylane. You can ask follow-ups by mentioning @polylane in a comment. If you close this pull request without merging, add a comment that says why. Polylane reads it to make its next fixes better.

Co-authored-by: polylane[bot] <277585245+polylane[bot]@users.noreply.github.com>
@polylane polylane Bot added polylane severity:low Polylane autofix severity: low labels Nov 11, 2026
@coderabbitai

coderabbitai Bot commented Nov 11, 2026

Copy link
Copy Markdown

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@polylane
polylane Bot requested a review from baronunread November 11, 2026 15:52
@github-actions

Copy link
Copy Markdown

React Doctor found no new issues. 🎉

Reviewed by React Doctor for commit 80c1315.

@github-actions

Copy link
Copy Markdown

leanest: 6 of 79 playwright test files selected

The judge wasn't sure enough to skip 5 and it thinks 1 is affected.

Test Decision Reason
tests/e2e/link-risk.pw.ts RUN judge p=0.90 c=0.68
tests/e2e/abuse-hold.pw.ts RUN judge p=0.44 c=0.10
tests/e2e/link-states.pw.ts RUN judge p=0.38 c=0.20
tests/e2e/click-buffer.pw.ts RUN judge p=0.32 c=0.31
tests/e2e/privacy-and-email-abuse.pw.ts RUN judge p=0.23 c=0.46
tests/e2e/admin-moderation.pw.ts RUN judge p=0.21 c=0.49
73 skipped
Test Decision Reason
tests/e2e/new-link-live.pw.ts SKIP judge p=0.16 c=0.58
tests/e2e/click-caps.pw.ts SKIP judge p=0.12 c=0.65
tests/e2e/link-addresses.pw.ts SKIP judge p=0.07 c=0.73
tests/e2e/admin-triage.pw.ts SKIP judge p=0.06 c=0.75
tests/e2e/link-import.pw.ts SKIP judge p=0.06 c=0.75
tests/e2e/production/csp.pw.ts SKIP judge p=0.06 c=0.75
tests/e2e/production/made-with.pw.ts SKIP judge p=0.06 c=0.75
tests/e2e/utm-builder.pw.ts SKIP judge p=0.06 c=0.75
tests/e2e/abuse.pw.ts SKIP judge p=0.05 c=0.77
tests/e2e/anon-shortener.pw.ts SKIP judge p=0.04 c=0.78
tests/e2e/link-editor.pw.ts SKIP judge p=0.04 c=0.78
tests/e2e/viewer-role.pw.ts SKIP judge p=0.04 c=0.78
tests/e2e/authenticated-flows.pw.ts SKIP judge p=0.03 c=0.80
tests/e2e/billing-truth.pw.ts SKIP judge p=0.03 c=0.80
tests/e2e/downgrade.pw.ts SKIP judge p=0.03 c=0.80
tests/e2e/google-signin.pw.ts SKIP judge p=0.03 c=0.80
tests/e2e/insights.pw.ts SKIP judge p=0.03 c=0.80
tests/e2e/links-export.pw.ts SKIP judge p=0.03 c=0.80
tests/e2e/onboarding.pw.ts SKIP judge p=0.03 c=0.80
tests/e2e/first-run.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/funnel.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/graphical-ui.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/invites.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/links-bulk.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/links-paging.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/mcp-oauth.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/page-navigation.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/production/prerendered-pages.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/production/seo.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/production/status-codes.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/public-pages.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/qr-code-monkey-alternative.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/qr-generator.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/qr-types.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/self-host/no-providers.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/self-host/self-host.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/upsell.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/webmcp.pw.ts SKIP judge p=0.02 c=0.82
tests/e2e/account-deletion.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/admin-platform.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/analytics-export.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/anon-link-bar.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/api-keys.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/auth.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/billing-page.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/billing-yearly.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/branded-link-shortener.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/command-menu.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/default-domain.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/dev-scenarios.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/form-errors.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/google-url-shortener.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/header-float.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/landing-sections.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/marketing-opt-in.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/members.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/overflow-tip.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/pricing-page.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/product-showcase.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/production/app-shell.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/production/assets.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/qr-download.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/route-skeletons.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/shell-persistence.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/sidebar.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/stale-chunk.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/toasts.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/turnstile-slot.pw.ts SKIP judge p=0.01 c=0.83
tests/e2e/api-method.pw.ts SKIP judge p=0.00 c=0.85
tests/e2e/app-not-found.pw.ts SKIP judge p=0.00 c=0.85
tests/e2e/avatar.pw.ts SKIP judge p=0.00 c=0.85
tests/e2e/not-found.pw.ts SKIP judge p=0.00 c=0.85
tests/e2e/page-ending.pw.ts SKIP judge p=0.00 c=0.85

baronunread added a commit that referenced this pull request Nov 12, 2026
…#506)

## What was wrong

**#489, three D1 costs that grow with traffic or history**

1. `revalidateOnRedirect` ran an `UPDATE links ... WHERE risk_checked_at
< ?` on every click of an org link. The condition made it a no-op for a
link scored in the last day, but it was still one D1 statement per
click, on a path whose comment says it should cost one KV read.
2. `sweepDedupeIds` selects `dedupe_id IS NOT NULL AND ts < ?`. The
planner picked `idx_clicks_ts`, which walks every click older than the
cutoff (up to 400 days) on every loop, including the last empty one.
3. `withSession` queued `markSeen` on every signed-in request, and
API-key requests wrote `api_keys.last_used_at` on every call. Both were
mostly no-ops.

**#491.** `zod` was overridden to 4.6.5, which `bun audit` flags
(GHSA-mrhx-c7xc-m3w5, high, memory exhaustion on oversized collections).
Fixed in 4.6.6.

## Why this fix

- **Risk update (1):** a per-isolate memo (`recentlyStamped` in
`risk.ts`) of link + destination → "skip until". A click that finds it
skips the statement. It adds no D1 read (that would replace the write it
saves) and no KV write (the issue's other option, carrying the check
time in the link's KV value, costs a KV write per link per day). A write
that changed the row is skipped for the whole 24 h verdict TTL; a write
that changed nothing (scored at a time we did not read) is skipped for
an hour. A cache miss always writes. The destination is in the key, so
an edit does not inherit the skip. The cost is that each isolate writes
a given link at most once per window instead of never skipping; a cold
isolate still sends one statement. The memo is bounded (10 000 entries,
oldest dropped).
- **Dedupe sweep (2):** partial index `idx_clicks_live_dedupe_ts ON
clicks(ts) WHERE dedupe_id IS NOT NULL` (migration 0058). I chose it
over a cursor because a cursor is state to lose and the index is the
same fix in one line. Only about a day of clicks ever has a dedupe id,
so the index stays small. Cost: one extra index entry per click insert
(and its removal at sweep). That is one more D1 row written per click,
against a scan that read all of history on each loop; at any real volume
the scan is the bigger cost (reads of 400 days of clicks times the
number of loops, daily).
- **Last-seen and key writes (3):** the marks are read with the caller
(`user.lastSeenAt` is now a better-auth user field, so `getSession`
returns it; the API key and OAuth lookups already select the row).
`markSeen` returns nothing if the mark is under an hour old. The key's
`last_used_at` is rewritten at most every 5 minutes; the keys page shows
a relative date, so it can lag by that much. The SQL conditions stay, so
two concurrent requests with a stale mark still produce one change.
- **zod (#491):** override bumped, `bun install`, `bun audit` no longer
lists zod (the remaining undici and other advisories are unrelated and
untouched). Built Worker: one `function $constructor` and one `var
$ZodType` definition (both in `assets/schemas-*.js`), so there is still
a single copy.
- `bun run db:migrate:local` added (ship-issue and this change both tell
you to run it; no script existed).

## Query plan, before and after (local D1, `explain query plan`)

```
BEFORE  SEARCH clicks USING INDEX idx_clicks_ts (ts<?)
AFTER   SEARCH clicks USING INDEX idx_clicks_live_dedupe_ts (ts<?)
```

Same statement as `DEDUPE_SWEEP_SQL` in `clicks.ts`. Local D1 returns no
`rows_read` meta, so I did not measure row counts; a worker test asserts
the plan uses the new index.

## Interaction with #478

#478 rewrites the same block in `revalidateOnRedirect`: on every click
it selects the org's stale links on the clicked host and updates them
all. That is a better fix for the "other links on the host stay
unscored" problem, but it also makes the per-click statement a `SELECT
... LIKE` instead of a no-op `UPDATE`, so it needs the same skip.
Whichever lands second should key the memo by `orgId` + hostname instead
of link + destination, and skip the select and update on a memo hit (the
memo helpers are separate from the SQL, so this is a small change). #478
also deletes the "second click on another link" half of an existing
test, which this PR leaves alone, so expect a trivial conflict in
`tests/worker/link-risk.worker.ts`. Not done here to avoid pre-empting
#478's design.

## Left out

- Nothing carried in the KV link value (see above).
- better-auth (1.7.14 → 1.7.21) and hono bumps mentioned in #491:
separate change, different review.
- No UI changes. Nothing to screenshot; the keys page reads the same
field.

## Tests

- `tests/worker/request-writes.worker.ts` (new): last-seen and key
writes with `recordingEnv()` (new helper in `support.ts`) that records
the SQL the Worker prepares.
- `link-risk.worker.ts`: a repeat click sends no SQL; an edited
destination is still stamped.
- `clicks.worker.ts`: the sweep's plan uses the new index.
- Mutation-tested: dropped the memo check, the `markSeen` check, the key
check, the destination from the memo key, and the migration. Each broke
exactly its own tests.
- One thing the throttle surfaced: `lastSeenAt` as a user field changed
the shape of the sign-up reply, which the account-enumeration test (#53)
caught. `pendingSignUpResponse` now carries `lastSeenAt: null`.
- `bun run verify` on a loaded machine (two runs competing) timed out in
unrelated worker tests and failed the enumeration test, which was a real
regression and is fixed above. Final head: `bun run test:worker` passed
1008 of 1009 run tests; the one failure, `mcp-oauth` "a granted access
token authenticates a real tool call", was a 5 s timeout under load and
passes alone (11/11). `format:check`, `lint`, `check`, `test`,
`test:worker:self-host` and `bun run fallow` pass.

Closes #489
Closes #491

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

polylane severity:low Polylane autofix severity: low

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant