diff --git a/src/components/ItemArticle.test.tsx b/src/components/ItemArticle.test.tsx index 32ac28e..65a1b5d 100644 --- a/src/components/ItemArticle.test.tsx +++ b/src/components/ItemArticle.test.tsx @@ -51,6 +51,26 @@ describe('ItemArticle', () => { // Text posts render as plain text, not as a clickable link expect(screen.queryByRole('link', { name: 'A great story' })).not.toBeInTheDocument(); }); + + it.each(['javascript:alert(1)', 'data:text/html,', 'vbscript:msgbox(1)'])( + 'does not render an anchor for non-http(s) URL scheme %s', + (url) => { + render(); + + // Hostile schemes must never reach the anchor href; the title degrades + // to plain text instead. + expect(screen.queryByRole('link', { name: 'A great story' })).not.toBeInTheDocument(); + }, + ); + + it('renders the external link for ordinary http(s) URLs', () => { + render(); + + expect(screen.getByRole('link', { name: 'A great story' })).toHaveAttribute( + 'href', + 'https://example.com/article', + ); + }); }); describe('author byline link', () => { diff --git a/src/components/ItemArticle.tsx b/src/components/ItemArticle.tsx index 09452f7..2a22a32 100644 --- a/src/components/ItemArticle.tsx +++ b/src/components/ItemArticle.tsx @@ -19,6 +19,12 @@ export function ItemArticle({ item, className = 'mb-4 pb-4 border-b border-borde const viewed = useIsViewed(item.id); const hostname = getHostname(item.url); + // Gate the outbound href to http(s) so javascript:/data: URLs can't reach the + // DOM; anything else degrades to plain text. Keep it ONE `startsWith` — a `||` + // reopens the CodeQL js/xss alert (a LogOrExpr drops the guard's true outcome). + const rawUrl = item.url ?? ''; + const safeUrl = rawUrl.startsWith('http') ? rawUrl : undefined; + const itemText = item.text; const sanitizedText = useMemo( () => itemText ? sanitizeHtml(itemText) : '', @@ -31,9 +37,9 @@ export function ItemArticle({ item, className = 'mb-4 pb-4 border-b border-borde // point reads cleanly instead of cascading inside itself.

- {item.url ? ( + {safeUrl ? ( markViewedWithTime(item.id, 'title')} diff --git a/src/components/StoryCard.test.tsx b/src/components/StoryCard.test.tsx index 925ea56..81373d4 100644 --- a/src/components/StoryCard.test.tsx +++ b/src/components/StoryCard.test.tsx @@ -141,6 +141,20 @@ describe('StoryCard', () => { expect(titleLink).toHaveAttribute('href', '/item/67890'); }); + it.each(['javascript:alert(1)', 'data:text/html,', 'vbscript:msgbox(1)'])( + 'never points the title link at a non-http(s) URL scheme %s', + (url) => { + render(); + + // Hostile schemes must not become the outbound href; the title degrades + // to the in-app discussion link instead. + const titleLink = screen.getByRole('link', { + name: 'Rust Is the Future of JavaScript Infrastructure', + }); + expect(titleLink).toHaveAttribute('href', '/item/12345'); + }, + ); + it('renders comments link', () => { render(); diff --git a/src/components/StoryCard.tsx b/src/components/StoryCard.tsx index a81ce8d..a411fd2 100644 --- a/src/components/StoryCard.tsx +++ b/src/components/StoryCard.tsx @@ -31,6 +31,10 @@ interface StoryCardProps { export function StoryCard({ story, index = 0, listType = 'top', fromDomain, fromUser, onBeforeNavigate, stageIdx, appendIdx }: StoryCardProps) { const hostname = getHostname(story.url); + // http(s)-only outbound href (keeps javascript:/data: out); else use the in-app + // link below. Keep it ONE `startsWith` — a `||` reopens the CodeQL js/xss alert (see ItemArticle). + const rawUrl = story.url ?? ''; + const safeUrl = rawUrl.startsWith('http') ? rawUrl : undefined; const { startPrefetch, stopPrefetch } = usePrefetchItem(); const isPrefetchingRef = useRef(false); const wasInExitZoneRef = useRef(false); @@ -154,9 +158,9 @@ export function StoryCard({ story, index = 0, listType = 'top', fromDomain, from

- {story.url ? ( + {safeUrl ? ( typeof DOMPurify; +type DOMPurifyFactory = (w: Window) => typeof createDOMPurify; // Single source for the browser-environment guard used by the private // DOMPurify instance, runtime self-host detection, and base URL resolution. @@ -10,12 +10,12 @@ const browserLocation: Location | null = // Private DOMPurify instance so the link-rewriting hook below is scoped to // this module; importing `dompurify` elsewhere gets the unmodified default // singleton. Module load requires a DOM (browser or jsdom): in pure Node, -// the imported `DOMPurify` is a bare factory with no `.addHook`, so the -// `: DOMPurify` fallback would throw at hook registration. HackerTok runs -// only in browsers and jsdom, where `browserLocation` is never null. -const purify = browserLocation - ? (DOMPurify as unknown as DOMPurifyFactory)(window) - : DOMPurify; +// the imported `createDOMPurify` is a bare factory with no `.addHook`, so the +// `: createDOMPurify` fallback would throw at hook registration. HackerTok +// runs only in browsers and jsdom, where `browserLocation` is never null. +const DOMPurify = browserLocation + ? (createDOMPurify as unknown as DOMPurifyFactory)(window) + : createDOMPurify; const PURIFY_CONFIG: Config = { ALLOWED_TAGS: [ @@ -238,7 +238,7 @@ function isAutoLinkifiedText(text: string, parsed: URL): boolean { return text.startsWith(parsed.origin); } -purify.addHook('afterSanitizeAttributes', (node) => { +DOMPurify.addHook('afterSanitizeAttributes', (node) => { if (node.tagName === 'PRE') { // Make scrollable pre blocks keyboard-accessible (WCAG 2.1.1) node.setAttribute('tabindex', '0'); @@ -363,5 +363,11 @@ export function sanitizeHtml(html: string | null | undefined): string { if (!html || typeof html !== 'string') { return ''; } - return purify.sanitize(autoLinkUrls(html), PURIFY_CONFIG); + // Defense in depth: sanitize the raw input *before* it is ever parsed into a + // DOM by `autoLinkUrls`, so untrusted markup can never reach an `innerHTML` + // write unsanitized (and CodeQL sees the DOMPurify barrier before the sink). + // The second pass re-runs the afterSanitizeAttributes hook on the anchors + // that auto-linking just created. + const sanitized = DOMPurify.sanitize(html, PURIFY_CONFIG); + return DOMPurify.sanitize(autoLinkUrls(sanitized), PURIFY_CONFIG); }