Description
Summary
The Link atom now decides href safety correctly: #100 replaced its startsWith('/') test
with safePath, and made the href alone decide before isExternal / target="_blank"
are read (src/components/atoms/Link/Link.tsx:94,131). But the atom is not the app's only
anchor. The Button atom's href form (Button.tsx:192-209) and ActionCircle
(ActionRow.tsx:92-107) each render their own raw <a href={href}> and never reach that code.
Every href reaching those two today is safe by provenance, not by a shared gate — a
SafeUrlSchema-parsed event.website, a directionsUrl we build ourselves, a literal
mailto: / tel: prefix. This is defense-in-depth: no live hole is known. The problem is
structural — safety is asserted once per call site instead of once at the sink, so the next
component that renders an anchor, or the next caller that passes a CMS-sourced string to
Button href=, inherits nothing. Lift the predicate into src/lib/shape/ and route all three
anchors through it.
The Link atom's own comment already names this as wanting its own ticket
(Link.tsx:118-124), as does the #100 implementation report (§2, §4).
Current behavior
| Anchor | Guard today |
|---|---|
Link (atoms/Link/Link.tsx:131) |
if (!safePath(href) && !hasAllowedScheme) → reportInternalError + renders a <span> |
Button href form (atoms/Button/Button.tsx:192) |
none — <a href={href}> verbatim |
ActionCircle href form (molecules/ActionRow/ActionRow.tsx:92) |
none — <a href={href}> verbatim |
ALLOWED_SCHEME (Link.tsx:43, /^(?:https?|mailto|tel):/i) is module-private to the atom, so
the two other anchors could not reuse it even if they wanted to.
Proposed changes
- Add
isSafeHreftosrc/lib/shape/(newhref.ts, re-exported from the
src/lib/shape/index.tsbarrel). One predicate:safePath-clean OR an allowed scheme.
MoveALLOWED_SCHEMEthere; keep the case-insensitive/iand the reasoning comment
(SafeUrlSchemaintypes/event.tsandvalidateWebUrlinlib/url.tsboth use/^https?:/i,
so a case-sensitive test here would refuse aHTTPS://URL those two already passed).
Do not re-implement the same-origin test — reusesafePath(src/lib/shape/path.ts:56),
which is the repo's single definition and already rejects//evil.com,/\evil.comand the
TAB/LF/CR variants (.claude/rules/data-layer.md:93-113). Link.tsxconsumesisSafeHrefinstead of its private copy. No behaviour change — the
refusal branch, thereportInternalErrorcall and the<span>fallback all stay exactly as
they are, and the "href alone decides, before any flag" ordering must be preserved.Button's href arm andActionCircle's href arm gate through the same predicate. Match the
Linkatom's failure mode rather than inventing a second one: report viareportInternalError
and render the non-interactive form (a<button>/<span>-equivalent that is visibly present
but not a link). These render inside the error fallback path, so the predicate must take a
non-string safely —safePathalready acceptsnull/undefined.- Must never throw. A
Refusing to link to …report is the failure mode; a blank widget is not.
Acceptance criteria
-
isSafeHreflives insrc/lib/shape/href.ts, is exported from the tier barrel, and is the
only placeALLOWED_SCHEMEis defined (grep: zero other copies undersrc/) - All three anchor sites (
Link,Buttonhref form,ActionCirclehref form) call it; grep
for<aundersrc/components/finds no anchor rendering an unguarded caller-supplied href - Co-located
src/lib/shape/href.test.ts(node lane) pins:javascript:,data:,
vbscript:,file:,#top,//evil.com,/\evil.comand the TAB/LF/CR variants refused;
https:,HTTPS:,mailto:,tel:and a site-relative/gb/londonallowed;
null/undefined/non-string returns false rather than throwing -
Button.test.tsxand a newActionRow.test.tsxassert a refused href renders no<a href>
(renderToStaticMarkup, per.claude/rules/tests.md— no jsdom) -
Link.test.tsxstill passes unchanged — this is a refactor of where the predicate lives,
not of whatLinkdoes - Every href currently reaching
Button/ActionCirclestill renders as a link (no regression
to directions, website, contacttel:/mailto:, or share actions) -
.claude/rules/data-layer.md's "Server-provided routes are untrusted untilsafePath"
section namesisSafeHrefas the sink-level gate, and the now-stale prose in
Link.tsx:118-124(which says the other two anchors are not covered) is corrected - Lint + typecheck +
pnpm test:runpass - Implementation report written to
.claude/reports/<this-issue-number>-safe-href-predicate.mdin the main checkout at
/Users/devindra/Documents/Projects/SahajAtlasWeb(.claude/reports/.gitignoreis*, so a
worktree copy would be discarded)
Parallel implementation
This ticket owns these files; no other ticket in this batch touches them:
src/lib/shape/href.ts(new),src/lib/shape/href.test.ts(new),src/lib/shape/index.tssrc/components/atoms/Link/Link.tsx,src/components/atoms/Link/Link.test.tsxsrc/components/atoms/Button/Button.tsx,src/components/atoms/Button/Button.test.tsxsrc/components/molecules/ActionRow/ActionRow.tsx,.../ActionRow.test.tsx(new).claude/rules/data-layer.md
Do not edit src/lib/shape/path.ts (safePath is correct and pinned by path.test.ts),
src/lib/shape/hash.ts, src/config/mode.ts, or any view — those belong to sibling tickets.
Files affected
src/lib/shape/href.ts(new) + test,src/lib/shape/index.tssrc/components/atoms/Link/Link.tsx(:43,:94,:118-124,:131)src/components/atoms/Button/Button.tsx(:192-209)src/components/molecules/ActionRow/ActionRow.tsx(:92-107).claude/rules/data-layer.md
References
- docs(claude): refresh stale internal docs and small hygiene batch #100 / PR docs(claude): refresh stale internal docs and small hygiene batch #111 — closed the
Linkatom's flag-bypass and the//evil.comgap .claude/reports/100-refresh-internal-docs.md§2 (the provenance audit and why this was
deferred) and §4 (listed first in the suggested follow-ups).claude/rules/data-layer.md→ "Server-provided routes are untrusted untilsafePath"src/lib/shape/path.test.ts— the existingsafePathcase pins
Metadata
Metadata
Assignees
Labels
Type
Fields
Hold Until
Activity