Commit dec2d0ff by PLN (Algolia)

hydra-assets: reject downloads that are not images (content-type + magic bytes)…

hydra-assets: reject downloads that are not images (content-type + magic bytes) — NASA hst_bday URLs now 301 to a landing page and three .jpg files shipped as saved HTML, silently black forever
parent 4f9a5cda
...@@ -46,6 +46,22 @@ async function download(url, dest) { ...@@ -46,6 +46,22 @@ async function download(url, dest) {
if (!res.ok) throw new Error(`HTTP ${res.status} for ${url}`); if (!res.ok) throw new Error(`HTTP ${res.status} for ${url}`);
const buf = Buffer.from(await res.arrayBuffer()); const buf = Buffer.from(await res.arrayBuffer());
if (buf.length > 30 * 1024 * 1024) throw new Error(`asset too big: ${url}`); if (buf.length > 30 * 1024 * 1024) throw new Error(`asset too big: ${url}`);
// An error page saved as .jpg never decodes — hydra sets no img.onerror,
// so the sketch silently renders black (three NASA hst_bday assets shipped
// exactly like that: the URLs now 301 to a landing page). Reject anything
// that isn't an image by content-type AND magic bytes.
const ct = res.headers.get('content-type') || '';
if (/text\/html|application\/json|text\/plain/.test(ct)) {
throw new Error(`not an image (content-type ${ct}): ${url}`);
}
const isJpeg = buf[0] === 0xff && buf[1] === 0xd8;
const isPng = buf[0] === 0x89 && buf[1] === 0x50;
const isGif = buf.subarray(0, 3).toString('latin1') === 'GIF';
const isRiff = buf.subarray(0, 4).toString('latin1') === 'RIFF'; // webp
const isAvif = buf.subarray(4, 8).toString('latin1') === 'ftyp';
if (!isJpeg && !isPng && !isGif && !isRiff && !isAvif) {
throw new Error(`not an image (magic bytes): ${url}`);
}
// heavy originals get the NASA-set treatment: 1920px JPEG keeps the 404 // heavy originals get the NASA-set treatment: 1920px JPEG keeps the 404
// gallery light (Webb's deepest field is 28MB as PNG) // gallery light (Webb's deepest field is 28MB as PNG)
if (buf.length > 5 * 1024 * 1024) { if (buf.length > 5 * 1024 * 1024) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment