Skip to content

Commit a7e2e38

Browse files
authored
fix(media): validate every download redirect target (#3841)
* fix(media): validate every download redirect target * fix(media): apply redirect policy to logo HEAD probes
1 parent d393541 commit a7e2e38

9 files changed

Lines changed: 300 additions & 38 deletions

File tree

‎skills-manifest.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
"files": 12
5151
},
5252
"media-use": {
53-
"hash": "332f69eaeabc945f",
54-
"files": 156
53+
"hash": "28948e640a5d133a",
54+
"files": 158
5555
},
5656
"motion-graphics": {
5757
"hash": "de8ec5464028ec67",

‎skills/media-use/audio/scripts/lib/heygen.mjs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { fetchMedia } from "../../../scripts/lib/media-fetch.mjs";
12
// heygen.mjs — vendored HeyGen REST helpers (auth + transport) for the audio
23
// pipeline. The credential resolver matches the hyperframes CLI auth: first
34
// usable source wins — $HEYGEN_API_KEY / $HYPERFRAMES_API_KEY → a nearby .env → ~/.heygen/
@@ -125,7 +126,7 @@ export async function heygenJSON(path, { method = "GET", headers = {}, body } =
125126

126127
// Download a (presigned) URL to destPath; returns byte length.
127128
export async function downloadTo(url, destPath) {
128-
const res = await fetch(url);
129+
const res = await fetchMedia(url);
129130
if (!res.ok) throw new Error(`download HTTP ${res.status}: ${String(url).slice(0, 80)}`);
130131
const bytes = Buffer.from(await res.arrayBuffer());
131132
mkdirSync(dirname(destPath), { recursive: true });

‎skills/media-use/audio/scripts/lib/tts.mjs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { fetchMedia } from "../../../scripts/lib/media-fetch.mjs";
12
// tts.mjs — multi-provider TTS for the media audio engine. The provider chain,
23
// auto-detected from env, is the one documented in ../SKILL.md:
34
//
@@ -312,7 +313,7 @@ export async function synthesizeHeygen({ text, voiceId, lang, speed, wavAbs }, d
312313
if (!inner.audio_url) {
313314
return { ok: false, words: null, error: "HeyGen /voices/speech returned no audio_url" };
314315
}
315-
const res = await fetchImpl(inner.audio_url);
316+
const res = await fetchMedia(inner.audio_url, { fetchImpl });
316317
if (!res.ok) {
317318
return { ok: false, words: null, error: `audio_url fetch failed: HTTP ${res.status}` };
318319
}

‎skills/media-use/scripts/lib/freeze.mjs‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { fetchMedia, isPublicMediaUrl } from "./media-fetch.mjs";
12
import { writeFileSync, copyFileSync, mkdirSync } from "node:fs";
23
import { dirname } from "node:path";
34

@@ -7,7 +8,7 @@ const MAX_FREEZE_BYTES = 256 * 1024 * 1024;
78

89
export async function freezeUrl(url, destPath) {
910
const where = String(url).slice(0, 80);
10-
const res = await fetch(url);
11+
const res = await fetchMedia(url);
1112
if (!res.ok) throw new Error(`freeze failed: HTTP ${res.status} for ${where}`);
1213

1314
// Fail fast on an advertised oversize body before reading a single byte.
@@ -47,15 +48,6 @@ const PLATFORM_HOSTS =
4748
/(^|\.)(youtube\.com|youtu\.be|vimeo\.com|tiktok\.com|instagram\.com|twitter\.com|x\.com|facebook\.com|dailymotion\.com)$/i;
4849
const MEDIA_EXT = /\.(mp3|wav|m4a|aac|ogg|flac|mp4|mov|webm|mkv|png|jpe?g|webp|gif|svg|avif)$/i;
4950

50-
// SSRF guard (m11): a user-supplied --from URL must not point at the local host
51-
// or a private network. Blocks loopback/localhost, RFC1918, link-local, and the
52-
// IPv6 equivalents on the literal hostname.
53-
// ponytail: literal-host check only; a DNS name that *resolves* to a private IP
54-
// (rebinding) still passes — add resolve-then-check if --from ever fetches from
55-
// untrusted hostnames at scale.
56-
const PRIVATE_HOST =
57-
/^(localhost|.*\.local|.*\.internal|127\.|10\.|0\.|169\.254\.|192\.168\.|172\.(1[6-9]|2\d|3[01])\.|\[?(::1|::ffff:127\.|f[cd][0-9a-f]{2}:|fe80:))/i;
58-
5951
export function isDirectMediaUrl(u) {
6052
let url;
6153
try {
@@ -65,6 +57,6 @@ export function isDirectMediaUrl(u) {
6557
}
6658
if (url.protocol !== "http:" && url.protocol !== "https:") return false;
6759
if (PLATFORM_HOSTS.test(url.hostname)) return false;
68-
if (PRIVATE_HOST.test(url.hostname)) return false;
60+
if (!isPublicMediaUrl(url)) return false;
6961
return MEDIA_EXT.test(url.pathname);
7062
}

‎skills/media-use/scripts/lib/heygen-video-provider.test.mjs‎

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async function freshGenerate() {
3636
return module.heygenVideoGenerate;
3737
}
3838

39-
async function listenVideoServer() {
39+
async function listenVideoServer(t) {
4040
const server = http.createServer((req, res) => {
4141
if (req.url !== "/video.mp4") {
4242
res.writeHead(404).end();
@@ -51,23 +51,33 @@ async function listenVideoServer() {
5151
await new Promise((resolve) => server.listen(0, resolve));
5252
const address = server.address();
5353
assert.ok(address && typeof address !== "string");
54-
return {
55-
server,
56-
url: `http://127.0.0.1:${address.port}/video.mp4`,
57-
};
54+
// Keep the provider URL public; only the test transport reaches the local fixture.
55+
const url = "https://media.example.com/video.mp4";
56+
const originalFetch = globalThis.fetch;
57+
t.mock.method(globalThis, "fetch", (requested, options) => {
58+
assert.equal(requested, url);
59+
assert.equal(options.redirect, "manual");
60+
return originalFetch(`http://127.0.0.1:${address.port}/video.mp4`, options);
61+
});
62+
return { server, url };
5863
}
5964

60-
async function listenFailingVideoServer() {
65+
async function listenFailingVideoServer(t) {
6166
const server = http.createServer((req, res) => {
6267
res.writeHead(500).end();
6368
});
6469
await new Promise((resolve) => server.listen(0, resolve));
6570
const address = server.address();
6671
assert.ok(address && typeof address !== "string");
67-
return {
68-
server,
69-
url: `http://127.0.0.1:${address.port}/video.mp4`,
70-
};
72+
// Keep the provider URL public; only the test transport reaches the local fixture.
73+
const url = "https://media.example.com/video.mp4";
74+
const originalFetch = globalThis.fetch;
75+
t.mock.method(globalThis, "fetch", (requested, options) => {
76+
assert.equal(requested, url);
77+
assert.equal(options.redirect, "manual");
78+
return originalFetch(`http://127.0.0.1:${address.port}/video.mp4`, options);
79+
});
80+
return { server, url };
7181
}
7282

7383
function closeServer(server) {
@@ -144,8 +154,8 @@ function bodyFromInvocation(invocation) {
144154
return JSON.parse(invocation.slice(start + marker.length));
145155
}
146156

147-
test("downloads a generated avatar video and returns the generated MP4 result", async () => {
148-
const { server, url } = await listenVideoServer();
157+
test("downloads a generated avatar video and returns the generated MP4 result", async (t) => {
158+
const { server, url } = await listenVideoServer(t);
149159
let localPath;
150160
try {
151161
await withFakeHeygen(
@@ -191,8 +201,8 @@ test("downloads a generated avatar video and returns the generated MP4 result",
191201
}
192202
});
193203

194-
test("tags video creation but not avatar or voice discovery", async () => {
195-
const { server, url } = await listenVideoServer();
204+
test("tags video creation but not avatar or voice discovery", async (t) => {
205+
const { server, url } = await listenVideoServer(t);
196206
let localPath;
197207
try {
198208
await withFakeHeygen(
@@ -218,8 +228,8 @@ test("tags video creation but not avatar or voice discovery", async () => {
218228
}
219229
});
220230

221-
test("uses explicit avatar and voice overrides without discovery", async () => {
222-
const { server, url } = await listenVideoServer();
231+
test("uses explicit avatar and voice overrides without discovery", async (t) => {
232+
const { server, url } = await listenVideoServer(t);
223233
let localPath;
224234
try {
225235
await withFakeHeygen(
@@ -249,8 +259,8 @@ test("uses explicit avatar and voice overrides without discovery", async () => {
249259
}
250260
});
251261

252-
test("caches discovered avatar and voice IDs for the process", async () => {
253-
const { server, url } = await listenVideoServer();
262+
test("caches discovered avatar and voice IDs for the process", async (t) => {
263+
const { server, url } = await listenVideoServer(t);
254264
const localPaths = new Set();
255265
try {
256266
await withFakeHeygen(
@@ -349,8 +359,8 @@ test("onboards and returns null when avatar/voice discovery itself is unauthenti
349359
});
350360
});
351361

352-
test("download failure after a successful create returns null and logs a diagnostic", async () => {
353-
const { server, url } = await listenFailingVideoServer();
362+
test("download failure after a successful create returns null and logs a diagnostic", async (t) => {
363+
const { server, url } = await listenFailingVideoServer(t);
354364
try {
355365
await withFakeHeygen({ response: JSON.stringify({ data: { video_url: url } }) }, async () => {
356366
const heygenVideoGenerate = await freshGenerate();

‎skills/media-use/scripts/lib/logo-provider.mjs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { fetchMedia } from "./media-fetch.mjs";
12
// Official brand marks — the `logo` type's provider tiers, tried in registry
23
// order. Every tier was verified against a 54-brand stress test (2026-07,
34
// 100% cascade hit). Hit counts below are a snapshot of that run — they
@@ -105,13 +106,13 @@ export function faviconDomainFor(entity) {
105106
}
106107

107108
async function fetchJson(url) {
108-
const res = await fetch(url, { signal: AbortSignal.timeout(10_000) });
109+
const res = await fetchMedia(url, { signal: AbortSignal.timeout(10_000) });
109110
if (!res.ok) return null;
110111
return res.json();
111112
}
112113

113114
async function urlExists(url) {
114-
const res = await fetch(url, { method: "HEAD", signal: AbortSignal.timeout(10_000) });
115+
const res = await fetchMedia(url, { method: "HEAD", signal: AbortSignal.timeout(10_000) });
115116
return res.ok;
116117
}
117118

@@ -196,7 +197,7 @@ export async function faviconSearch(intent, ctx = {}) {
196197
const url = `https://icons.duckduckgo.com/ip3/${domain}.ico`;
197198
let body;
198199
try {
199-
const res = await fetch(url, { signal: AbortSignal.timeout(10_000) });
200+
const res = await fetchMedia(url, { signal: AbortSignal.timeout(10_000) });
200201
if (!res.ok) return null;
201202
body = Buffer.from(await res.arrayBuffer());
202203
} catch {

‎skills/media-use/scripts/lib/logo-provider.test.mjs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,20 @@ test("the real logo cascade falls through tier by tier to the first hit", async
135135
assert.ok(res, "cascade must land on the favicon tier");
136136
assert.equal(res.metadata.provider, "favicon.ddg");
137137
});
138+
139+
for (const search of [simpleIconsSearch, githubAvatarSearch]) {
140+
test(`${search.name} rejects private HEAD redirects`, async (t) => {
141+
const seen = [];
142+
t.mock.method(globalThis, "fetch", async (url, options) => {
143+
seen.push(url);
144+
assert.equal(options.method, "HEAD");
145+
assert.ok(options.signal);
146+
return options.redirect === "manual"
147+
? new Response(null, { status: 302, headers: { location: "http://127.0.0.1/private" } })
148+
: new Response(null, { status: 200 });
149+
});
150+
assert.equal(await search("vercel logo"), null);
151+
assert.equal(seen.length, 1);
152+
assert.ok(!seen[0].includes("127.0.0.1"));
153+
});
154+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Media downloads use public HTTP(S) URLs. Validate every redirect target;
2+
// a provider result must meet the same host policy as a direct ingest URL.
3+
// Public HTTPS-to-HTTP redirects are allowed, matching direct HTTP support.
4+
// This is a literal-host policy, not DNS pinning: DNS resolution remains trusted.
5+
6+
import { BlockList, isIP } from "node:net";
7+
8+
const blocked = new BlockList();
9+
for (const [network, prefix] of [
10+
["0.0.0.0", 8],
11+
["10.0.0.0", 8],
12+
["100.64.0.0", 10],
13+
["127.0.0.0", 8],
14+
["169.254.0.0", 16],
15+
["172.16.0.0", 12],
16+
["192.0.0.0", 24],
17+
["192.0.2.0", 24],
18+
["192.88.99.0", 24],
19+
["192.168.0.0", 16],
20+
["198.18.0.0", 15],
21+
["198.51.100.0", 24],
22+
["203.0.113.0", 24],
23+
["224.0.0.0", 4],
24+
["240.0.0.0", 4],
25+
])
26+
blocked.addSubnet(network, prefix, "ipv4");
27+
for (const [network, prefix] of [
28+
["::", 128],
29+
["::1", 128],
30+
["fc00::", 7],
31+
["fe80::", 10],
32+
["fec0::", 10],
33+
["ff00::", 8],
34+
["2001:db8::", 32],
35+
])
36+
blocked.addSubnet(network, prefix, "ipv6");
37+
38+
export function isPublicMediaUrl(value) {
39+
try {
40+
const url = new URL(value);
41+
if (url.protocol !== "http:" && url.protocol !== "https:") return false;
42+
const host = url.hostname.replace(/\.$/, "");
43+
if (
44+
host === "localhost" ||
45+
host.endsWith(".localhost") ||
46+
host.endsWith(".local") ||
47+
host.endsWith(".internal")
48+
)
49+
return false;
50+
const address = host.replace(/^\[|\]$/g, "");
51+
const family = isIP(address);
52+
return family === 0 || !blocked.check(address, family === 4 ? "ipv4" : "ipv6");
53+
} catch {
54+
return false;
55+
}
56+
}
57+
58+
export async function fetchMedia(url, { method = "GET", signal, fetchImpl = fetch } = {}) {
59+
let current = String(url);
60+
for (let hop = 0; hop <= 5; hop++) {
61+
if (!isPublicMediaUrl(current))
62+
throw new Error("Media download blocked: URL is not public HTTP(S)");
63+
const response = await fetchImpl(current, { method, signal, redirect: "manual" });
64+
if (!(response.status >= 300 && response.status < 400)) return response;
65+
const location = response.headers.get("location");
66+
if (!location) return response;
67+
await response.body?.cancel();
68+
current = new URL(location, current).href;
69+
}
70+
throw new Error("Media download exceeded redirect limit");
71+
}

0 commit comments

Comments
 (0)