Skip to content

Commit 5c0d8fa

Browse files
Copilotthonatos
andauthored
fix: validate homepage/tarball URL schemes to prevent javascript: injection; merge main
Co-authored-by: thonatos <958063+thonatos@users.noreply.github.com>
1 parent dce1945 commit 5c0d8fa

4 files changed

Lines changed: 35 additions & 6 deletions

File tree

‎apps/web/app/components/cnpm/PkgHeader.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from "~/components/ui/select";
1010
import { Badge } from "~/components/ui/badge";
1111
import { Button } from "~/components/ui/button";
12-
import { sortVersions, getVersionTags, repoUrl, type PkgTab } from "~/lib/registry/parse";
12+
import { sortVersions, getVersionTags, repoUrl, safeExternalUrl, type PkgTab } from "~/lib/registry/parse";
1313
import type { RegistryManifest } from "~/lib/registry/types";
1414
import { PkgTabs } from "./PkgTabs";
1515

@@ -80,9 +80,9 @@ export function PkgHeader({
8080
<GitFork className="size-3.5" /> 源码
8181
</a>
8282
)}
83-
{manifest.homepage && (
83+
{safeExternalUrl(manifest.homepage) && (
8484
<a
85-
href={manifest.homepage}
85+
href={safeExternalUrl(manifest.homepage)}
8686
target="_blank"
8787
rel="noopener noreferrer"
8888
className="inline-flex items-center gap-1 text-xs text-muted-foreground transition-colors hover:text-primary"

‎apps/web/app/components/cnpm/PkgSidebar.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ import { Download, ExternalLink, GitFork, Globe, Package as PackageIcon } from "
22
import { Card, CardContent, CardHeader, CardTitle } from "~/components/ui/card";
33
import { DownloadCard } from "./DownloadCard";
44
import { MaintainersCard } from "./MaintainersCard";
5-
import { formatBytes, repoUrl } from "~/lib/registry/parse";
5+
import { formatBytes, repoUrl, safeExternalUrl } from "~/lib/registry/parse";
66
import type { RegistryManifest } from "~/lib/registry/types";
77

88
export function PkgSidebar({ manifest, version }: { manifest: RegistryManifest; version: string }) {
99
const repo = repoUrl(manifest.repository);
1010
const dist = manifest.versions?.[version]?.dist;
1111
const links: Array<{ label: string; href?: string; icon: React.ReactNode } | null> = [
1212
{ label: "仓库", href: repo, icon: <GitFork className="size-4 shrink-0" /> },
13-
{ label: "主页", href: manifest.homepage, icon: <Globe className="size-4 shrink-0" /> },
13+
{ label: "主页", href: safeExternalUrl(manifest.homepage), icon: <Globe className="size-4 shrink-0" /> },
1414
{ label: "npmjs.com", href: `https://www.npmjs.com/package/${manifest.name}`, icon: <PackageIcon className="size-4 shrink-0" /> },
1515
{ label: "unpkg", href: `https://unpkg.com/${manifest.name}@${version}`, icon: <ExternalLink className="size-4 shrink-0" /> },
1616
dist?.tarball
1717
? {
1818
label: `tarball${dist.size !== undefined ? ` · ${formatBytes(dist.size)}` : ""}`,
19-
href: dist.tarball,
19+
href: safeExternalUrl(dist.tarball),
2020
icon: <Download className="size-4 shrink-0" />,
2121
}
2222
: null,

‎apps/web/app/lib/registry/parse.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
normalizeVersionSpec,
66
parsePkgPath,
77
repoUrl,
8+
safeExternalUrl,
89
sortVersions,
910
} from "./parse";
1011
import type { RegistryVersion } from "./types";
@@ -109,3 +110,21 @@ describe("repoUrl", () => {
109110
expect(repoUrl("svn://example.com/foo")).toBeUndefined();
110111
});
111112
});
113+
114+
describe("safeExternalUrl", () => {
115+
it("允许 https 和 http 地址", () => {
116+
expect(safeExternalUrl("https://example.com")).toBe("https://example.com");
117+
expect(safeExternalUrl("http://example.com")).toBe("http://example.com");
118+
});
119+
120+
it("拒绝非 http(s) 协议", () => {
121+
expect(safeExternalUrl("javascript:alert(1)")).toBeUndefined();
122+
expect(safeExternalUrl("data:text/html,<h1>x</h1>")).toBeUndefined();
123+
expect(safeExternalUrl("ftp://example.com")).toBeUndefined();
124+
});
125+
126+
it("无效或空地址返回 undefined", () => {
127+
expect(safeExternalUrl(undefined)).toBeUndefined();
128+
expect(safeExternalUrl("not-a-url")).toBeUndefined();
129+
});
130+
});

‎apps/web/app/lib/registry/parse.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ export function formatBytes(bytes: number | undefined) {
9595
return `${value.toFixed(decimals)} ${units[Math.min(index, units.length - 1)]}`;
9696
}
9797

98+
export function safeExternalUrl(url: string | undefined): string | undefined {
99+
if (!url) return undefined;
100+
try {
101+
const { protocol } = new URL(url);
102+
return protocol === "https:" || protocol === "http:" ? url : undefined;
103+
} catch {
104+
return undefined;
105+
}
106+
}
107+
98108
export function formatCompactNumber(value: number | undefined) {
99109
if (value === undefined || Number.isNaN(value)) return "-";
100110
const compact = (scale: number, suffix: string) => {

0 commit comments

Comments
 (0)