Skip to content

refactor(ui): gate every anchor href through one predicate #114

Closed

Description

@Ardnived

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 isSafeHref to src/lib/shape/ (new href.ts, re-exported from the
    src/lib/shape/index.ts barrel). One predicate: safePath-clean OR an allowed scheme.
    Move ALLOWED_SCHEME there; keep the case-insensitive /i and the reasoning comment
    (SafeUrlSchema in types/event.ts and validateWebUrl in lib/url.ts both use /^https?:/i,
    so a case-sensitive test here would refuse a HTTPS:// URL those two already passed).
    Do not re-implement the same-origin test — reuse safePath (src/lib/shape/path.ts:56),
    which is the repo's single definition and already rejects //evil.com, /\evil.com and the
    TAB/LF/CR variants (.claude/rules/data-layer.md:93-113).
  • Link.tsx consumes isSafeHref instead of its private copy. No behaviour change — the
    refusal branch, the reportInternalError call 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 and ActionCircle's href arm gate through the same predicate. Match the
    Link atom's failure mode rather than inventing a second one: report via reportInternalError
    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
    — safePath already accepts null/undefined.
  • Must never throw. A Refusing to link to … report is the failure mode; a blank widget is not.

Acceptance criteria

  • isSafeHref lives in src/lib/shape/href.ts, is exported from the tier barrel, and is the
    only place ALLOWED_SCHEME is defined (grep: zero other copies under src/)
  • All three anchor sites (Link, Button href form, ActionCircle href form) call it; grep
    for <a under src/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.com and the TAB/LF/CR variants refused;
    https:, HTTPS:, mailto:, tel: and a site-relative /gb/london allowed;
    null/undefined/non-string returns false rather than throwing
  • Button.test.tsx and a new ActionRow.test.tsx assert a refused href renders no <a href>
    (renderToStaticMarkup, per .claude/rules/tests.md — no jsdom)
  • Link.test.tsx still passes unchanged — this is a refactor of where the predicate lives,
    not of what Link does
  • Every href currently reaching Button/ActionCircle still renders as a link (no regression
    to directions, website, contact tel:/mailto:, or share actions)
  • .claude/rules/data-layer.md's "Server-provided routes are untrusted until safePath"
    section names isSafeHref as 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:run pass
  • Implementation report written to
    .claude/reports/<this-issue-number>-safe-href-predicate.md in the main checkout at
    /Users/devindra/Documents/Projects/SahajAtlasWeb (.claude/reports/.gitignore is *, 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.ts
  • src/components/atoms/Link/Link.tsx, src/components/atoms/Link/Link.test.tsx
  • src/components/atoms/Button/Button.tsx, src/components/atoms/Button/Button.test.tsx
  • src/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.ts
  • src/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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Hold Until

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions