Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .snyk
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
21 changes: 5 additions & 16 deletions app/media/MediaCoverageList.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -15,18 +16,6 @@ const MEDIA_TYPE_LABELS: Record<MediaItem["type"], string> = {
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;
Expand Down Expand Up @@ -92,11 +81,11 @@ export function MediaCoverageList(props: Props) {
<div className="grid gap-8">
{sortedItems.map((article, index) => {
const outlets = getMediaSources(article);
const urlOk = hasValidUrl(article.url);
const safeUrl = toAbsoluteHttpUrl(article.url);

return (
<article
key={`${article.sourceType}-${article.url || article.title}-${index}`}
key={`${article.sourceType}-${safeUrl || article.title}-${index}`}
className="view-reveal overflow-hidden rounded-xl border border-gray-200/60 bg-surface-elevated shadow-lg transition-all duration-300 hover-lift dark:border-gray-800/60"
style={{ animationDelay: `${index * 60}ms` }}
>
Expand All @@ -110,9 +99,9 @@ export function MediaCoverageList(props: Props) {
</div>
<MediaSourceTags outlets={outlets} />
<h3 className="text-xl font-semibold text-gray-900 dark:text-gray-100 mb-2">
{urlOk ? (
{safeUrl ? (
<a
href={article.url}
href={safeUrl}
className="hover:text-blue-600 dark:hover:text-blue-400 transition-colors duration-200"
target="_blank"
rel="noopener noreferrer"
Expand Down
18 changes: 4 additions & 14 deletions app/media/MediaSourceTags.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
"use client";

import { useEffect, useRef, useState } from "react";
import { toAbsoluteHttpUrl } from "@/lib/external-url";
import type { MediaSource } from "@/types/media";
import { getSourceTagColors } from "./sourceTagColors";

const VISIBLE_SOURCE_LIMIT = 4;

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;
}
};

interface SourceTagProps {
outlet: MediaSource;
}

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 <span className={tagClassName}>{outlet.name}</span>;
}

return (
<a
href={outlet.url}
href={safeUrl}
className={`${tagClassName} hover:opacity-80 transition-opacity`}
target="_blank"
rel="noopener noreferrer"
Expand Down
10 changes: 10 additions & 0 deletions lib/external-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ describe('toAbsoluteHttpUrl', () => {

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', () => {
Expand Down
16 changes: 13 additions & 3 deletions lib/external-url.ts
Original file line number Diff line number Diff line change
@@ -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`.
Expand All @@ -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 `/<tab>/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(/^\/+/, '')}`;
}
Expand All @@ -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}`;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rebuild is good, but the earlier candidate.startsWith('/') branch still returns the raw string. new URL('/\evil.com', base) and a /+TAB+/evil.com value both resolve to https://evil.com/, so those become cross-origin links. Suggest resolving same-origin paths against a sentinel base and returning only pathname + search + hash when the origin is unchanged.

} catch {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading