11import { afterEach , describe , expect , it , vi } from "vitest" ;
2- import { mkdtempSync , rmSync } from "node:fs" ;
2+ import { mkdtempSync , readFileSync , readdirSync , rmSync } from "node:fs" ;
33import { tmpdir } from "node:os" ;
44import { join } from "node:path" ;
55import {
@@ -289,7 +289,20 @@ describe("drop counts — why a referenced asset is not in the capture", () => {
289289 await withTempDir ( async ( dir ) => {
290290 vi . stubGlobal (
291291 "fetch" ,
292- vi . fn ( async ( ) => new Response ( new Uint8Array ( 2048 ) , { status : 200 } ) ) ,
292+ vi . fn (
293+ async ( ) =>
294+ new Response (
295+ new Uint8Array (
296+ readFileSync (
297+ new URL (
298+ "../../../../docs/public/catalog/assets/a634cb9e7783af7e.woff2" ,
299+ import . meta. url ,
300+ ) ,
301+ ) ,
302+ ) ,
303+ { status : 200 } ,
304+ ) ,
305+ ) ,
293306 ) ;
294307 const { css, drops } = await downloadAndRewriteFonts ( fontCss ( 1 ) , dir ) ;
295308 expect ( css ) . toContain ( "assets/fonts/font-0.woff2" ) ;
@@ -303,11 +316,21 @@ describe("drop counts — why a referenced asset is not in the capture", () => {
303316 } ) ;
304317
305318 it ( "counts an image dropped for being under the raster floor" , async ( ) => {
306- // 9 KB is under the 10 KB floor. Nothing lands, and the reason is now on the record .
319+ // A valid small PNG is under the 10 KB floor; preserve the recorded drop reason .
307320 await withTempDir ( async ( dir ) => {
308321 vi . stubGlobal (
309322 "fetch" ,
310- vi . fn ( async ( ) => new Response ( new Uint8Array ( 9000 ) , { status : 200 } ) ) ,
323+ vi . fn (
324+ async ( ) =>
325+ new Response (
326+ new Uint8Array (
327+ await sharp ( { create : { width : 2 , height : 2 , channels : 4 , background : "red" } } )
328+ . png ( )
329+ . toBuffer ( ) ,
330+ ) ,
331+ { status : 200 } ,
332+ ) ,
333+ ) ,
311334 ) ;
312335 const { assets, drops } = await downloadAssets ( tokensWithNoSvgs ( ) , dir , [
313336 { type : "Image" , url : "https://cdn.example/hero.png" , contexts : [ "img[src]" ] } ,
@@ -374,7 +397,8 @@ describe("asset fetches present the same identity as the page navigation", () =>
374397 headers : { "content-type" : "text/html" } ,
375398 } ) ;
376399 }
377- const body = new Uint8Array ( 4096 ) ;
400+ const body =
401+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect width="24" height="24"/></svg>' ;
378402 return new Response ( body , { status : 200 , headers : { "content-type" : "image/svg+xml" } } ) ;
379403 } ) ;
380404 }
@@ -404,6 +428,22 @@ describe("asset fetches present the same identity as the page navigation", () =>
404428 } ) ;
405429} ) ;
406430
431+ async function testIco ( ) : Promise < Buffer > {
432+ const png = await sharp ( { create : { width : 16 , height : 16 , channels : 4 , background : "red" } } )
433+ . png ( )
434+ . toBuffer ( ) ;
435+ const header = Buffer . alloc ( 22 ) ;
436+ header . writeUInt16LE ( 1 , 2 ) ;
437+ header . writeUInt16LE ( 1 , 4 ) ;
438+ header [ 6 ] = 16 ;
439+ header [ 7 ] = 16 ;
440+ header . writeUInt16LE ( 1 , 10 ) ;
441+ header . writeUInt16LE ( 32 , 12 ) ;
442+ header . writeUInt32LE ( png . length , 14 ) ;
443+ header . writeUInt32LE ( 22 , 18 ) ;
444+ return Buffer . concat ( [ header , png ] ) ;
445+ }
446+
407447describe ( "declared icons — keep them all, headline the bare mark" , ( ) => {
408448 afterEach ( ( ) => vi . unstubAllGlobals ( ) ) ;
409449
@@ -451,7 +491,7 @@ describe("declared icons — keep them all, headline the bare mark", () => {
451491 "fetch" ,
452492 serve ( {
453493 "favicon.svg" : BADGE_SVG ,
454- "favicon.ico" : Buffer . from ( "not a decodable ico" ) ,
494+ "favicon.ico" : await testIco ( ) ,
455495 "apple-icon.png" : await solidPng ( 1 ) ,
456496 } ) ,
457497 ) ;
@@ -474,7 +514,7 @@ describe("declared icons — keep them all, headline the bare mark", () => {
474514 "fetch" ,
475515 serve ( {
476516 "favicon.svg" : BADGE_SVG ,
477- "favicon.ico" : Buffer . from ( "not a decodable ico" ) ,
517+ "favicon.ico" : await testIco ( ) ,
478518 "apple-icon.png" : await solidPng ( 1 ) ,
479519 } ) ,
480520 ) ;
@@ -537,3 +577,133 @@ describe("declared icons — keep them all, headline the bare mark", () => {
537577 } ) ;
538578 } ) ;
539579} ) ;
580+
581+ describe ( "capture download security boundaries" , ( ) => {
582+ it ( "preserves two different fonts whose URL extensions canonicalize to the same name" , async ( ) => {
583+ await withTempDir ( async ( dir ) => {
584+ const first = readFileSync (
585+ new URL ( "../../../../docs/public/catalog/assets/a634cb9e7783af7e.woff2" , import . meta. url ) ,
586+ ) ;
587+ const second = readFileSync (
588+ new URL ( "../../../../docs/public/catalog/assets/8963f64fa28dc4ae.woff2" , import . meta. url ) ,
589+ ) ;
590+ expect ( first . equals ( second ) ) . toBe ( false ) ;
591+ vi . stubGlobal (
592+ "fetch" ,
593+ vi . fn (
594+ async ( url : string ) =>
595+ new Response ( new Uint8Array ( url . endsWith ( ".ttf" ) ? first : second ) ) ,
596+ ) ,
597+ ) ;
598+ const css =
599+ "@font-face{font-family:A;src:url(https://fonts.example/site.ttf)} @font-face{font-family:B;src:url(https://fonts.example/site.woff2)}" ;
600+ const result = await downloadAndRewriteFonts ( css , dir ) ;
601+ expect ( result . css ) . toContain ( "assets/fonts/site.woff2" ) ;
602+ expect ( result . css ) . toContain ( "assets/fonts/site-2.woff2" ) ;
603+ expect ( readFileSync ( join ( dir , "assets/fonts/site.woff2" ) ) ) . toEqual ( first ) ;
604+ expect ( readFileSync ( join ( dir , "assets/fonts/site-2.woff2" ) ) ) . toEqual ( second ) ;
605+ } ) ;
606+ } ) ;
607+
608+ it ( "shares the capture byte budget between fonts and icons" , async ( ) => {
609+ await withTempDir ( async ( dir ) => {
610+ const bytes = readFileSync (
611+ new URL ( "../../../../docs/public/catalog/assets/a634cb9e7783af7e.woff2" , import . meta. url ) ,
612+ ) ;
613+ const fetchMock = vi . fn ( async ( ) => new Response ( new Uint8Array ( bytes ) ) ) ;
614+ vi . stubGlobal ( "fetch" , fetchMock ) ;
615+ const byteBudget = { remainingBytes : bytes . length } ;
616+ await downloadAndRewriteFonts (
617+ "@font-face{font-family:A;src:url(https://fonts.example/site.woff2)}" ,
618+ dir ,
619+ { byteBudget } ,
620+ ) ;
621+ const result = await downloadAssets ( tokensWithNoSvgs ( ) , dir , [ ] , OPENAI_ICONS , {
622+ byteBudget,
623+ } ) ;
624+ expect ( result . icons . icons ) . toEqual ( [ ] ) ;
625+ expect ( fetchMock ) . toHaveBeenCalledTimes ( 1 ) ;
626+ expect ( byteBudget . remainingBytes ) . toBe ( 0 ) ;
627+ } ) ;
628+ } ) ;
629+
630+ afterEach ( ( ) => vi . unstubAllGlobals ( ) ) ;
631+
632+ it ( "canonicalizes an icon ADS suffix and its promoted copy while preserving SVG bytes" , async ( ) => {
633+ await withTempDir ( async ( dir ) => {
634+ const body =
635+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect width="24" height="24"/></svg>' ;
636+ vi . stubGlobal (
637+ "fetch" ,
638+ vi . fn ( async ( ) => new Response ( body ) ) ,
639+ ) ;
640+ const { icons } = await downloadAssets (
641+ tokensWithNoSvgs ( ) ,
642+ dir ,
643+ [ ] ,
644+ [
645+ {
646+ rel : "icon" ,
647+ href : "https://public.example/favicon.svg:payload" ,
648+ sizes : null ,
649+ type : null ,
650+ } ,
651+ ] ,
652+ ) ;
653+ expect ( icons . icons [ 0 ] ?. file ) . toBe ( "assets/icon-icon-unsized.svg" ) ;
654+ expect ( icons . headline ?. file ) . toBe ( "assets/favicon.svg" ) ;
655+ expect ( readFileSync ( join ( dir , "assets/favicon.svg" ) , "utf8" ) ) . toBe ( body ) ;
656+ } ) ;
657+ } ) ;
658+
659+ it ( "does not publish arbitrary bytes as an OG image" , async ( ) => {
660+ await withTempDir ( async ( dir ) => {
661+ vi . stubGlobal (
662+ "fetch" ,
663+ vi . fn ( async ( ) => new Response ( new Uint8Array ( 6000 ) . fill ( 65 ) ) ) ,
664+ ) ;
665+ const tokens = tokensWithNoSvgs ( ) ;
666+ tokens . ogImage = "https://public.example/og.jpg::$DATA" ;
667+ const { assets, drops } = await downloadAssets ( tokens , dir ) ;
668+ expect ( assets ) . toEqual ( [ ] ) ;
669+ expect ( drops . unavailable ) . toBe ( 1 ) ;
670+ expect ( readdirSync ( join ( dir , "assets" ) ) ) . not . toContain ( "og-image.jpg::$DATA" ) ;
671+ } ) ;
672+ } ) ;
673+
674+ it . each ( [ "font.woff2:ads" , "CON.woff2" ] ) (
675+ "writes valid font %s under a safe name and rewrites CSS" ,
676+ async ( name ) => {
677+ await withTempDir ( async ( dir ) => {
678+ const bytes = readFileSync (
679+ new URL ( "../../../../docs/public/catalog/assets/a634cb9e7783af7e.woff2" , import . meta. url ) ,
680+ ) ;
681+ vi . stubGlobal (
682+ "fetch" ,
683+ vi . fn ( async ( ) => new Response ( new Uint8Array ( bytes ) ) ) ,
684+ ) ;
685+ const source = `@font-face{font-family:Demo;src:url(https://public.example/${ name } )}` ;
686+ const result = await downloadAndRewriteFonts ( source , dir ) ;
687+ const names = readdirSync ( join ( dir , "assets/fonts" ) ) ;
688+ expect ( names ) . toHaveLength ( 1 ) ;
689+ expect ( names [ 0 ] ) . not . toMatch ( / : | ^ C O N \. / i) ;
690+ expect ( result . css ) . toContain ( `assets/fonts/${ names [ 0 ] } ` ) ;
691+ expect ( readFileSync ( join ( dir , "assets/fonts" , names [ 0 ] ! ) ) ) . toEqual ( bytes ) ;
692+ } ) ;
693+ } ,
694+ ) ;
695+
696+ it ( "leaves invalid font URLs unchanged and records their rejection" , async ( ) => {
697+ await withTempDir ( async ( dir ) => {
698+ vi . stubGlobal (
699+ "fetch" ,
700+ vi . fn ( async ( ) => new Response ( new Uint8Array ( 2048 ) ) ) ,
701+ ) ;
702+ const source = "@font-face{font-family:Demo;src:url(https://public.example/font.woff2)}" ;
703+ const result = await downloadAndRewriteFonts ( source , dir ) ;
704+ expect ( result . css ) . toBe ( source ) ;
705+ expect ( result . drops . unavailable ) . toBe ( 1 ) ;
706+ expect ( readdirSync ( join ( dir , "assets/fonts" ) ) ) . toEqual ( [ ] ) ;
707+ } ) ;
708+ } ) ;
709+ } ) ;
0 commit comments