diff --git a/.snyk b/.snyk index 84de3ac..07c99c6 100644 --- a/.snyk +++ b/.snyk @@ -9,4 +9,13 @@ ignore: limit); no npm release yet expires: 2027-09-30T00:00:00.000Z created: 2026-09-30T04:41:26.228Z + SNYK-JS-PICOMATCH-21248802: + - '*': + reason: >- + No npm release yet for CVE-2026-112366 (open upstream PRs only). + picomatch is reached only via next-sitemap / fast-glob / micromatch + at build time with developer-controlled patterns; no untrusted glob + input is accepted at runtime + expires: 2027-05-03T00:00:00.000Z + created: 2026-11-03T00:00:00.000Z patch: {} diff --git a/app/media/MediaCoverageList.tsx b/app/media/MediaCoverageList.tsx index 347d62d..00f70a1 100644 --- a/app/media/MediaCoverageList.tsx +++ b/app/media/MediaCoverageList.tsx @@ -1,6 +1,7 @@ "use client"; import { useMemo, useState } from "react"; +import { toAbsoluteHttpUrl } from "@/lib/external-url"; import { DEFAULT_SCHOLAR_ID } from "@/lib/news"; import { getMediaSources, type MediaItem } from "@/types/media"; import { ArticleImage } from "./ArticleImage"; @@ -15,18 +16,6 @@ const MEDIA_TYPE_LABELS: Record = { press: "Press release", }; -const hasValidUrl = (url?: string): boolean => { - if (!url || typeof url !== "string") return false; - const trimmed = url.trim(); - if (!trimmed) return false; - try { - const parsed = new URL(trimmed); - return parsed.protocol === "http:" || parsed.protocol === "https:"; - } catch { - return false; - } -}; - type Props = { initialItems: MediaItem[]; initialTotal: number; @@ -92,11 +81,11 @@ export function MediaCoverageList(props: Props) {
{sortedItems.map((article, index) => { const outlets = getMediaSources(article); - const urlOk = hasValidUrl(article.url); + const safeUrl = toAbsoluteHttpUrl(article.url); return (
@@ -110,9 +99,9 @@ export function MediaCoverageList(props: Props) {

- {urlOk ? ( + {safeUrl ? ( { - if (!url || typeof url !== "string") return false; - const trimmed = url.trim(); - if (!trimmed) return false; - try { - const parsed = new URL(trimmed); - return parsed.protocol === "http:" || parsed.protocol === "https:"; - } catch { - return false; - } -}; - interface SourceTagProps { outlet: MediaSource; } @@ -25,14 +14,15 @@ interface SourceTagProps { function SourceTag({ outlet }: SourceTagProps) { const { bgColor, textColor } = getSourceTagColors(outlet.name); const tagClassName = `inline-block px-2 py-1 text-sm font-medium ${bgColor} ${textColor} rounded-sm`; + const safeUrl = toAbsoluteHttpUrl(outlet.url); - if (!hasValidUrl(outlet.url)) { + if (!safeUrl) { return {outlet.name}; } return ( { it('preserves same-origin paths', () => { expect(toAbsoluteHttpUrl('/papers/example.pdf')).toBe('/papers/example.pdf'); + expect(toAbsoluteHttpUrl('/media/a.pdf?x=1#p2')).toBe('/media/a.pdf?x=1#p2'); + }); + + it('rejects paths that browsers resolve to another origin', () => { + expect(toAbsoluteHttpUrl('/\\evil.com')).toBeNull(); + expect(toAbsoluteHttpUrl('/\t/evil.com')).toBeNull(); + }); + + it('drops credentials when rebuilding absolute URLs', () => { + expect(toAbsoluteHttpUrl('https://user:pass@example.com/a')).toBe('https://example.com/a'); }); it('rejects empty, javascript, and mailto values', () => { diff --git a/lib/external-url.ts b/lib/external-url.ts index 750bb3f..afdda13 100644 --- a/lib/external-url.ts +++ b/lib/external-url.ts @@ -1,3 +1,5 @@ +const SAME_ORIGIN_BASE = 'https://same-origin.invalid'; + /** * Turn a website field into an absolute http(s) URL. * Bare domains like `sharkmeasurements.com` become `https://sharkmeasurements.com`. @@ -15,8 +17,15 @@ export const toAbsoluteHttpUrl = (value: string | undefined | null): string | nu if (candidate.startsWith('//')) { candidate = `https:${candidate}`; } else if (candidate.startsWith('/')) { - // Same-origin path — leave for callers that need internal links. - return candidate; + // Same-origin path for internal links. Browsers read `/\host` or `//host` + // as protocol-relative, so resolve it and reject anything that leaves the origin. + try { + const parsed = new URL(candidate, SAME_ORIGIN_BASE); + if (parsed.origin !== SAME_ORIGIN_BASE) return null; + return `${parsed.pathname}${parsed.search}${parsed.hash}`; + } catch { + return null; + } } else { candidate = `https://${candidate.replace(/^\/+/, '')}`; } @@ -25,7 +34,8 @@ export const toAbsoluteHttpUrl = (value: string | undefined | null): string | nu try { const parsed = new URL(candidate); if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return null; - return parsed.toString(); + // Rebuild from URL components so callers never reuse the raw input string. + return `${parsed.protocol}//${parsed.host}${parsed.pathname}${parsed.search}${parsed.hash}`; } catch { return null; } diff --git a/package.json b/package.json index 86b8447..8f6f9a2 100644 --- a/package.json +++ b/package.json @@ -68,14 +68,14 @@ "nanoid": "^5.1.16", "postcss": "^8.5.26", "postcss-selector-parser": "^6.1.3", - "undici": "^7.29.1" + "undici": "^7.30.1" }, "packageManager": "pnpm@10.33.0", "pnpm": { "overrides": { "brace-expansion@1": "1.1.21", "brace-expansion@5": "5.0.12", - "undici": "^7.29.1" + "undici": "^7.30.1" }, "patchedDependencies": { "braces@3.0.3": "patches/braces@3.0.3.patch" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d4d486d..73a479f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,7 +7,7 @@ settings: overrides: brace-expansion@1: 1.1.21 brace-expansion@5: 5.0.12 - undici: ^7.29.1 + undici: ^7.30.1 patchedDependencies: braces@3.0.3: @@ -2997,8 +2997,8 @@ packages: undici-types@8.9.0: resolution: {integrity: sha512-KTDyRTYX8sWmKXAikPHHSyc63CRPETMctyjKFupcC6OBLXT3xsN0e9aF7m+mIXutFWpUXuedtowG7iLOzp0kQg==} - undici@7.30.0: - resolution: {integrity: sha512-dkrQXeHSaoamnItlYbmzG0wFYrM0ZwDxCIg0A7aKjTyyhh9svRzCNFEzV+Vm05/yehjCzjDZ31KXfGEjYSztDQ==} + undici@7.30.1: + resolution: {integrity: sha512-03KrMbjIFDWQR5RNPrSw3bd1r12JpyFQl9/yxjUbJtqkjmllqC5lFjZh6ujeuJCKr8EOQvRW872Oy/az+nLYxg==} engines: {node: '>=20.18.1'} unified@11.0.5: @@ -4314,7 +4314,7 @@ snapshots: parse5: 7.3.0 parse5-htmlparser2-tree-adapter: 7.1.0 parse5-parser-stream: 7.1.2 - undici: 7.30.0 + undici: 7.30.1 whatwg-mimetype: 4.0.0 client-only@0.0.1: {} @@ -6229,7 +6229,7 @@ snapshots: undici-types@8.9.0: {} - undici@7.30.0: {} + undici@7.30.1: {} unified@11.0.5: dependencies: