Public: anyone can read this; the crew can change it.
Every file renderer is fuzzed, or named as excluded with its reason: a renderer added without a sample fails the suite #14043
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14042-every-file-renderer-is-fuzzed-or-named-a → main 3 commits fork
1 file
changed +192
−49
test/fuzz-renderers.test.ts +192 −49
| @@ -8,30 +8,14 @@ | ||
| 8 | 8 | * to be kept in test/fuzz-found.test.ts. |
| 9 | 9 | */ |
| 10 | 10 | import { describe, expect, test } from "bun:test"; |
| 11 | import type { Renderer } from "../src/web/render/renderers.ts"; | |
| 12 | import { csv } from "../src/web/render/csv.ts"; | |
| 13 | import { log } from "../src/web/render/log.ts"; | |
| 14 | import { ics } from "../src/web/render/ics.ts"; | |
| 15 | import { eml } from "../src/web/render/eml.ts"; | |
| 16 | import { vcf } from "../src/web/render/vcf.ts"; | |
| 17 | import { gpx } from "../src/web/render/gpx.ts"; | |
| 18 | import { dxf } from "../src/web/render/dxf.ts"; | |
| 19 | import { drawio, excalidraw, mermaid } from "../src/web/render/diagram.ts"; | |
| 20 | import { geojson } from "../src/web/render/geojson.ts"; | |
| 21 | import { subtitles } from "../src/web/render/subtitles.ts"; | |
| 22 | import { patch } from "../src/web/render/patch.ts"; | |
| 23 | import { notebook } from "../src/web/render/notebook.ts"; | |
| 24 | import { makefile } from "../src/web/render/makefile.ts"; | |
| 25 | import { lcov } from "../src/web/render/lcov.ts"; | |
| 26 | import { sarif } from "../src/web/render/sarif.ts"; | |
| 27 | import { gcode } from "../src/web/render/gcode.ts"; | |
| 28 | import { config } from "../src/web/render/config.ts"; | |
| 29 | import { markdown } from "../src/web/render/markdown.ts"; | |
| 11 | import { RENDERERS, type Renderer } from "../src/web/render/renderers.ts"; | |
| 30 | 12 | import { fuzz, int, mix, pick, rng, type Rand } from "./helpers/fuzz.ts"; |
| 31 | 13 | import { blobBody, rawResponse } from "../src/web/pages/repo.ts"; |
| 32 | 14 | import { fileKind, HEAD_BYTES, mediaType, RENDER_CAP, sniffType, TEXT_CAP } from "../src/web/render/kinds.ts"; |
| 33 | 15 | import { securityFloor } from "../src/http/respond.ts"; |
| 34 | 16 | |
| 17 | const encoder = new TextEncoder(); | |
| 18 | ||
| 35 | 19 | /** What a hostile file tries to smuggle into the page. */ |
| 36 | 20 | export const HOSTILE = [ |
| 37 | 21 | "<script>alert(1)</script>", |
| @@ -62,22 +46,23 @@ export const HOSTILE = [ | ||
| 62 | 46 | "x".repeat(5000), |
| 63 | 47 | ]; |
| 64 | 48 | |
| 65 | /** A valid file of each kind; a case is one of these with hostile text spliced in. */ | |
| 66 | const SAMPLES: Record<string, { path: string; render: Renderer; files: readonly string[] }> = { | |
| 49 | /** | |
| 50 | * A valid file of each kind; a case is one of these with hostile text spliced in. The kind is what | |
| 51 | * `fileKind` reads from the path and the first file, and the renderer fuzzed is the one `RENDERERS` | |
| 52 | * names for it: the one the blob page draws with. | |
| 53 | */ | |
| 54 | export const SAMPLES: Readonly<Record<string, { path: string; files: readonly string[] }>> = { | |
| 67 | 55 | csv: { |
| 68 | 56 | path: "data.csv", |
| 69 | render: csv, | |
| 70 | 57 | files: ['name,url,note\nAda,https://a.test,"quoted, cell"\nBob,javascript:alert(1),x\n'], |
| 71 | 58 | }, |
| 72 | tsv: { path: "data.tsv", render: csv, files: ["a\tb\tc\n1\t2\t3\n"] }, | |
| 59 | tsv: { path: "data.tsv", files: ["a\tb\tc\n1\t2\t3\n"] }, | |
| 73 | 60 | jsonl: { |
| 74 | 61 | path: "rows.jsonl", |
| 75 | render: csv, | |
| 76 | 62 | files: ['{"a":1,"b":"<b>x</b>"}\n{"a":2,"href":"javascript:alert(1)"}\n'], |
| 77 | 63 | }, |
| 78 | 64 | log: { |
| 79 | 65 | path: "build.log", |
| 80 | render: log, | |
| 81 | 66 | files: [ |
| 82 | 67 | "\u001b[1;32mok\u001b[0m step one\n\u001b[38;5;196mfail\u001b[0m\n\u001b]8;;https://a.test\u0007link\u001b]8;;\u0007\n", |
| 83 | 68 | "INFO start\nWARN slow\nERROR boom\nFATAL end\n", |
| @@ -85,129 +70,265 @@ const SAMPLES: Record<string, { path: string; render: Renderer; files: readonly | ||
| 85 | 70 | }, |
| 86 | 71 | ics: { |
| 87 | 72 | path: "cal.ics", |
| 88 | render: ics, | |
| 89 | 73 | files: [ |
| 90 | 74 | "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nSUMMARY:Meet\r\nDTSTART:20260101T100000Z\r\nDTEND:20260101T110000Z\r\nLOCATION:Room 1\r\nURL:https://a.test/\r\nDESCRIPTION:Line\\nTwo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n", |
| 91 | 75 | ], |
| 92 | 76 | }, |
| 93 | 77 | vcf: { |
| 94 | 78 | path: "people.vcf", |
| 95 | render: vcf, | |
| 96 | 79 | files: [ |
| 97 | 80 | "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Ada Lovelace\r\nEMAIL:[email protected]\r\nTEL:+1 555\r\nURL:https://a.test/\r\nORG:Engines\r\nNOTE:hi\r\nEND:VCARD\r\n", |
| 98 | 81 | ], |
| 99 | 82 | }, |
| 100 | 83 | eml: { |
| 101 | 84 | path: "mail.eml", |
| 102 | render: eml, | |
| 103 | 85 | files: [ |
| 104 | 86 | "From: Ada <[email protected]>\r\nSubject: =?utf-8?Q?<script>?=\r\nMIME-Version: 1.0\r\nContent-Type: text/html\r\n\r\n<p onclick=x>hi</p>\r\n", |
| 105 | 87 | ], |
| 106 | 88 | }, |
| 107 | 89 | gpx: { |
| 108 | 90 | path: "track.gpx", |
| 109 | render: gpx, | |
| 110 | 91 | files: [ |
| 111 | 92 | '<?xml version="1.0"?><gpx version="1.1" creator="x"><metadata><name>Walk</name><link href="https://a.test"/></metadata><trk><name>T</name><trkseg><trkpt lat="51.5" lon="-0.1"><ele>10</ele><time>2026-01-01T10:00:00Z</time></trkpt><trkpt lat="51.6" lon="-0.2"><ele>12</ele></trkpt></trkseg></trk><wpt lat="51.5" lon="-0.1"><name>W</name><desc>d</desc></wpt></gpx>', |
| 112 | 93 | ], |
| 113 | 94 | }, |
| 114 | 95 | dxf: { |
| 115 | 96 | path: "part.dxf", |
| 116 | render: dxf, | |
| 117 | 97 | files: ["0\nSECTION\n2\nENTITIES\n0\nLINE\n8\n<script>alert(1)</script>\n10\n0\n20\n0\n11\n5\n21\n5\n0\nCIRCLE\n10\n1\n20\n1\n40\n2\n0\nENDSEC\n0\nEOF\n"], |
| 118 | 98 | }, |
| 119 | 99 | drawio: { |
| 120 | 100 | path: "a.drawio", |
| 121 | render: drawio, | |
| 122 | 101 | files: [ |
| 123 | 102 | `<mxfile><diagram><mxGraphModel><root><mxCell id="2" value="<script>alert(1)</script>" style="ellipse;" vertex="1"><mxGeometry x="1" y="2" width="30" height="40"/></mxCell><mxCell id='3' value="b" vertex="1"><mxGeometry x="90" y="9" width="30" height="40"/></mxCell><mxCell id="4" edge="1" source="2" target="3"><mxGeometry><mxPoint x="5" y="5"/></mxGeometry></mxCell></root></mxGraphModel></diagram></mxfile>`, |
| 124 | 103 | ], |
| 125 | 104 | }, |
| 126 | 105 | excalidraw: { |
| 127 | 106 | path: "a.excalidraw", |
| 128 | render: excalidraw, | |
| 129 | 107 | files: ['{"elements":[{"type":"rectangle","x":0,"y":0,"width":9,"height":9},{"type":"text","x":1,"y":1,"text":"<script>a</script>\\nb","fontSize":20},{"type":"arrow","x":0,"y":0,"points":[[0,0],[5,5]]}]}'], |
| 130 | 108 | }, |
| 131 | 109 | mermaid: { |
| 132 | 110 | path: "a.mmd", |
| 133 | render: mermaid, | |
| 134 | 111 | files: ["---\ntitle: <b>x</b>\n---\nflowchart TD\n A[<script>alert(1)</script>] --> B\n"], |
| 135 | 112 | }, |
| 136 | 113 | geojson: { |
| 137 | 114 | path: "map.geojson", |
| 138 | render: geojson, | |
| 139 | 115 | files: [ |
| 140 | 116 | '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"name":"A","url":"javascript:alert(1)"},"geometry":{"type":"Point","coordinates":[0.1,51.5]}},{"type":"Feature","properties":{"k":"v"},"geometry":{"type":"LineString","coordinates":[[0,0],[1,1]]}}]}', |
| 141 | 117 | ], |
| 142 | 118 | }, |
| 143 | 119 | sarif: { |
| 144 | 120 | path: "scan.sarif", |
| 145 | render: sarif, | |
| 146 | 121 | files: ['{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"T"}},"results":[{"ruleId":"R","level":"error","message":{"text":"m"},"locations":[{"physicalLocation":{"artifactLocation":{"uri":"src/a.ts"},"region":{"startLine":3}}}]}]}]}'], |
| 147 | 122 | }, |
| 148 | 123 | lcov: { |
| 149 | 124 | path: "lcov.info", |
| 150 | render: lcov, | |
| 151 | 125 | files: ["TN:\nSF:src/a.ts\nFNF:2\nFNH:1\nDA:1,1\nDA:2,0\nLF:2\nLH:1\nBRF:4\nBRH:2\nend_of_record\n"], |
| 152 | 126 | }, |
| 153 | 127 | makefile: { |
| 154 | 128 | path: "Makefile", |
| 155 | render: makefile, | |
| 156 | 129 | files: [".PHONY: all clean\nCC := <b>gcc</b>\nall: a.o | dir\n\t$(CC) -o x \\\n\t a.o\nclean:\n\trm -f *.o\ninclude <script>.mk\n"], |
| 157 | 130 | }, |
| 158 | 131 | srt: { |
| 159 | 132 | path: "talk.srt", |
| 160 | render: subtitles, | |
| 161 | 133 | files: ["1\n00:00:01,000 --> 00:00:02,000\n<i>Hello</i> there\n\n2\n00:00:03,000 --> 00:00:04,500\n<b>Bye</b>\n"], |
| 162 | 134 | }, |
| 163 | 135 | vtt: { |
| 164 | 136 | path: "talk.vtt", |
| 165 | render: subtitles, | |
| 166 | 137 | files: ["WEBVTT\n\n00:00:01.000 --> 00:00:02.000\n<v Ada>Hello <c.x>there</c>\n\nNOTE x\n\n00:00:03.000 --> 00:00:04.000 align:start\nBye\n"], |
| 167 | 138 | }, |
| 168 | 139 | patch: { |
| 169 | 140 | path: "fix.patch", |
| 170 | render: patch, | |
| 171 | 141 | files: [ |
| 172 | 142 | "From abc Mon Sep 17 00:00:00 2001\nFrom: Ada <[email protected]>\nSubject: [PATCH] fix\n\n---\ndiff --git a/x.ts b/x.ts\nindex 1..2 100644\n--- a/x.ts\n+++ b/x.ts\n@@ -1,2 +1,2 @@\n-old\n+new\n context\n", |
| 173 | 143 | ], |
| 174 | 144 | }, |
| 175 | 145 | notebook: { |
| 176 | 146 | path: "nb.ipynb", |
| 177 | render: notebook, | |
| 178 | 147 | files: [ |
| 179 | 148 | '{"nbformat":4,"nbformat_minor":5,"metadata":{"kernelspec":{"name":"python3","language":"python"}},"cells":[{"cell_type":"markdown","source":["# Title\\n","[x](https://a.test)"]},{"cell_type":"code","execution_count":1,"source":"print(1)","outputs":[{"output_type":"stream","name":"stdout","text":["1\\n"]},{"output_type":"execute_result","data":{"text/html":["<b>x</b>"],"text/plain":["x"],"image/png":"iVBORw0KGgo="}},{"output_type":"error","ename":"E","evalue":"v","traceback":["\\u001b[31mE\\u001b[0m"]}]}]}', |
| 180 | 149 | ], |
| 181 | 150 | }, |
| 182 | 151 | gcode: { |
| 183 | 152 | path: "part.gcode", |
| 184 | render: gcode, | |
| 185 | 153 | files: [";FLAVOR:Marlin\n;LAYER:0\nG28\nG1 Z0.2 F300\nG1 X10 Y10 E1 F1200\nG1 X20 Y10 E2\n;LAYER:1\nG1 Z0.4\nG1 X20 Y20 E3\nM104 S200 ; <b>hot</b>\n"], |
| 186 | 154 | }, |
| 187 | 155 | json: { |
| 188 | 156 | path: "settings.json", |
| 189 | render: config, | |
| 190 | 157 | files: ['{"name":"x","port":8080,"debug":true,"tags":["a","b"],"nested":{"url":"javascript:alert(1)","n":null}}'], |
| 191 | 158 | }, |
| 192 | 159 | yaml: { |
| 193 | 160 | path: "settings.yaml", |
| 194 | render: config, | |
| 195 | 161 | files: ["name: x\nport: 8080\ntags:\n - a\n - b\nnested:\n url: \"javascript:alert(1)\"\n"], |
| 196 | 162 | }, |
| 197 | 163 | toml: { |
| 198 | 164 | path: "settings.toml", |
| 199 | render: config, | |
| 200 | 165 | files: ['name = "x"\nport = 8080\n[nested]\nurl = "javascript:alert(1)"\n'], |
| 201 | 166 | }, |
| 202 | 167 | markdown: { |
| 203 | 168 | path: "docs/README.md", |
| 204 | render: markdown, | |
| 205 | 169 | files: [ |
| 206 | 170 | "# Title\n\nSome *text* and a [link](https://a.test) and .\n\n```js\ncode()\n```\n\n| a | b |\n|---|---|\n| 1 | 2 |\n\n<details><summary>x</summary>y</details>\n", |
| 207 | 171 | ], |
| 208 | 172 | }, |
| 173 | env: { | |
| 174 | path: ".env.example", | |
| 175 | files: ["# Server\n\n# the port\nPORT=8080\nAPI_SECRET=hunter2 # never\nURL=\"javascript:alert(1)\"\n"], | |
| 176 | }, | |
| 177 | sql: { | |
| 178 | path: "schema.sql", | |
| 179 | files: [ | |
| 180 | "-- users\nCREATE TABLE users (\n id INTEGER PRIMARY KEY,\n name TEXT NOT NULL DEFAULT '<b>x</b>'\n);\nINSERT INTO users VALUES (1, 'a;b');\n", | |
| 181 | ], | |
| 182 | }, | |
| 183 | graphql: { | |
| 184 | path: "schema.graphql", | |
| 185 | files: ["# c\ntype Query {\n user(id: ID!): User\n}\ntype User {\n name: String\n}\nenum Role { A B }\nunion X = User\n"], | |
| 186 | }, | |
| 187 | mbox: { | |
| 188 | path: "inbox.mbox", | |
| 189 | files: [ | |
| 190 | "From [email protected] Mon Jan 1 00:00:00 2024\nFrom: Ann <[email protected]>\nSubject: =?UTF-8?B?aGVsbG8=?=\n <b>world</b>\nDate: Mon, 1 Jan 2024\n\n>From body line\n\nFrom [email protected] Tue\nFrom: Bob\nSubject: two\n\nx\n", | |
| 191 | ], | |
| 192 | }, | |
| 193 | proto: { | |
| 194 | path: "user.proto", | |
| 195 | files: [ | |
| 196 | '// c\nsyntax = "proto3";\npackage demo.v1;\nimport "other.proto";\nmessage User {\n string name = 1; // <b>x</b>\n repeated int32 ids = 2 [packed = true];\n map<string, Role> roles = 3;\n oneof pick { string a = 4; int32 b = 5; }\n reserved 9, 10;\n message Inner { bool ok = 1; }\n}\nenum Role { OTHER = 0; ADMIN = 1 [deprecated = true]; }\nservice Users {\n rpc Get (GetReq) returns (User);\n rpc Watch (stream GetReq) returns (stream User) { option deprecated = true; }\n}\n', | |
| 197 | ], | |
| 198 | }, | |
| 199 | openapi: { | |
| 200 | path: "openapi.json", | |
| 201 | files: [ | |
| 202 | '{"openapi":"3.0.0","info":{"title":"<b>T</b>","version":"1"},"paths":{"/a":{"get":{"tags":["t"],"summary":"s","deprecated":true}},"/b":{"post":{"summary":"javascript:alert(1)"}}}}', | |
| 203 | ], | |
| 204 | }, | |
| 205 | package: { | |
| 206 | path: "package.json", | |
| 207 | files: [ | |
| 208 | '{"name":"x","version":"1.0.0","type":"module","license":"MIT","scripts":{"test":"bun test"},"dependencies":{"a":"^1"},"devDependencies":{"b":"javascript:alert(1)"}}', | |
| 209 | ], | |
| 210 | }, | |
| 211 | dockerfile: { | |
| 212 | path: "Dockerfile", | |
| 213 | files: ["ARG V=1\nFROM oven/bun:1 AS build\nENV SECRET=x\nEXPOSE 3000\nRUN echo <b>x</b> \\\n && true\nCOPY . .\nFROM scratch\nCMD [\"x\"]\n"], | |
| 214 | }, | |
| 215 | workflow: { | |
| 216 | path: ".github/workflows/ci.yml", | |
| 217 | files: [ | |
| 218 | "on: [push]\njobs:\n test:\n runs-on: ubuntu-latest\n needs: []\n strategy:\n matrix:\n os: [a, b]\n steps:\n - uses: actions/checkout@v4\n - name: <b>run</b>\n run: bun test\n", | |
| 219 | ], | |
| 220 | }, | |
| 221 | compose: { | |
| 222 | path: "compose.yaml", | |
| 223 | files: [ | |
| 224 | "services:\n web:\n image: nginx\n ports: [\"80:80\"]\n volumes: [\"./a:/a\"]\n depends_on: [db]\n environment:\n SECRET: x\n db:\n build: ./db\n", | |
| 225 | ], | |
| 226 | }, | |
| 227 | kml: { | |
| 228 | path: "places.kml", | |
| 229 | files: [ | |
| 230 | '<?xml version="1.0"?><kml xmlns="http://www.opengis.net/kml/2.2"><Document><Placemark><name>A</name><description><b>x</b></description><Point><coordinates>-0.1,51.5,0</coordinates></Point></Placemark></Document></kml>', | |
| 231 | ], | |
| 232 | }, | |
| 233 | har: { | |
| 234 | path: "session.har", | |
| 235 | files: [ | |
| 236 | '{"log":{"version":"1.2","entries":[{"startedDateTime":"2026-01-01T10:00:00.000Z","time":50,"request":{"method":"GET","url":"https://a.test/","headers":[{"name":"x","value":"<b>y</b>"}]},"response":{"status":200,"statusText":"OK","headers":[],"content":{"size":10}},"timings":{"wait":20,"receive":30}}]}}', | |
| 237 | ], | |
| 238 | }, | |
| 239 | otio: { | |
| 240 | path: "cut.otio", | |
| 241 | files: [ | |
| 242 | '{"OTIO_SCHEMA":"Timeline.1","name":"T","tracks":{"OTIO_SCHEMA":"Stack.1","children":[{"OTIO_SCHEMA":"Track.1","name":"V1","kind":"Video","children":[{"OTIO_SCHEMA":"Clip.2","name":"<b>c</b>","source_range":{"OTIO_SCHEMA":"TimeRange.1","start_time":{"OTIO_SCHEMA":"RationalTime.1","value":0,"rate":24},"duration":{"OTIO_SCHEMA":"RationalTime.1","value":48,"rate":24}}}]}]}}', | |
| 243 | ], | |
| 244 | }, | |
| 245 | edl: { | |
| 246 | path: "cut.edl", | |
| 247 | files: [ | |
| 248 | "TITLE: <b>T</b>\nFCM: NON-DROP FRAME\n\n001 AX V C 00:00:00:00 00:00:02:00 01:00:00:00 01:00:02:00\n* FROM CLIP NAME: a.mov\n", | |
| 249 | ], | |
| 250 | }, | |
| 251 | fcpxml: { | |
| 252 | path: "cut.fcpxml", | |
| 253 | files: [ | |
| 254 | '<?xml version="1.0"?><fcpxml version="1.9"><resources><format id="r1" frameDuration="1/24s"/><asset id="r2" name="A" src="file:///a.mov"/></resources><library><event><project name="P"><sequence format="r1"><spine><asset-clip ref="r2" name="<b>c</b>" offset="0s" start="0s" duration="2s"/></spine></sequence></project></event></library></fcpxml>', | |
| 255 | ], | |
| 256 | }, | |
| 257 | junit: { | |
| 258 | path: "results.junit.xml", | |
| 259 | files: [ | |
| 260 | '<?xml version="1.0"?><testsuites tests="2" failures="1"><testsuite name="S" tests="2" failures="1" time="0.1"><testcase name="ok" classname="C" time="0.01"/><testcase name="bad" classname="C"><failure message="<b>m</b>">trace\nline</failure></testcase></testsuite></testsuites>', | |
| 261 | ], | |
| 262 | }, | |
| 263 | opml: { | |
| 264 | path: "subs.opml", | |
| 265 | files: [ | |
| 266 | '<?xml version="1.0"?><opml version="2.0"><head><title>Subs</title></head><body><outline text="A" xmlUrl="https://a.test/feed"/><outline text="B" htmlUrl="javascript:alert(1)"/></body></opml>', | |
| 267 | ], | |
| 268 | }, | |
| 269 | feed: { | |
| 270 | path: "news.rss", | |
| 271 | files: [ | |
| 272 | '<?xml version="1.0"?><rss version="2.0"><channel><title>N</title><item><title><b>x</b></title><link>https://a.test/1</link><pubDate>Thu, 01 Jan 2026 10:00:00 GMT</pubDate></item><item><title>J</title><link>javascript:alert(1)</link></item></channel></rss>', | |
| 273 | ], | |
| 274 | }, | |
| 275 | step: { | |
| 276 | path: "part.step", | |
| 277 | files: [ | |
| 278 | "ISO-10303-21;\nHEADER;\nFILE_DESCRIPTION(('<b>d</b>'),'2;1');\nFILE_NAME('part.step','2026-01-01',('Ada'),(''),'','','');\nFILE_SCHEMA(('AUTOMOTIVE_DESIGN'));\nENDSEC;\nDATA;\n#1=PRODUCT('P','<script>','',(#2));\n#2=CARTESIAN_POINT('',(0.,0.,0.));\nENDSEC;\nEND-ISO-10303-21;\n", | |
| 279 | ], | |
| 280 | }, | |
| 281 | model3d: { | |
| 282 | path: "cube.obj", | |
| 283 | files: ["# <b>cube</b>\no Cube\nv 0 0 0\nv 1 0 0\nv 0 1 0\nvn 0 0 1\nf 1 2 3\n"], | |
| 284 | }, | |
| 209 | 285 | }; |
| 210 | 286 | |
| 287 | /** | |
| 288 | * Kinds the fuzz does not draw from generated text, each with why. A kind here draws no byte of the | |
| 289 | * file into the page (the media route serves it under its own policy), or reads a binary format a | |
| 290 | * generator cannot splice text into without a fixture to start from. | |
| 291 | */ | |
| 292 | export const EXCLUDED: Readonly<Record<string, string>> = { | |
| 293 | image: "an <img> pointing at the media route; only the header's width and height are read", | |
| 294 | svg: "an <img> pointing at the media route, served under a sandbox policy; no byte enters the page", | |
| 295 | pdf: "a viewer pointing at the media route; no byte enters the page", | |
| 296 | html: "a sandboxed <iframe> pointing at the media route; no byte enters the page", | |
| 297 | audio: "an <audio> pointing at the media route; binary, needs a fixture", | |
| 298 | video: "a <video> pointing at the media route; binary, needs a fixture", | |
| 299 | sqlite: "binary database pages; needs a fixture to splice into", | |
| 300 | parquet: "binary columnar file; needs a fixture to splice into", | |
| 301 | archive: "zip/tar bytes; needs a fixture to splice into", | |
| 302 | sheet: "a zip of XML (xlsx); needs a fixture to splice into", | |
| 303 | document: "a zip of XML (docx); needs a fixture to splice into", | |
| 304 | slides: "a zip of XML (pptx); needs a fixture to splice into", | |
| 305 | layers: "a zip of images (ora); needs a fixture to splice into", | |
| 306 | font: "binary font tables; needs a fixture to splice into", | |
| 307 | psd: "binary Photoshop layers; needs a fixture to splice into", | |
| 308 | }; | |
| 309 | ||
| 310 | /** The kind a sample is: what the blob page reads from its path and first file. */ | |
| 311 | export const sampleKind = (sample: { path: string; files: readonly string[] }): string => | |
| 312 | fileKind(sample.path, encoder.encode(sample.files[0] ?? "").subarray(0, HEAD_BYTES)); | |
| 313 | ||
| 314 | /** | |
| 315 | * The kinds a registry draws that are neither sampled nor excluded, with a message naming each and | |
| 316 | * where to add it; empty when every kind is accounted for. | |
| 317 | */ | |
| 318 | export function unguarded( | |
| 319 | registry: Readonly<Record<string, unknown>>, | |
| 320 | sampled: ReadonlySet<string>, | |
| 321 | excluded: Readonly<Record<string, string>>, | |
| 322 | ): string[] { | |
| 323 | return Object.keys(registry) | |
| 324 | .filter((kind) => !sampled.has(kind) && !(kind in excluded)) | |
| 325 | .map( | |
| 326 | (kind) => | |
| 327 | `renderer "${kind}" is not fuzzed: add a valid file of it to SAMPLES in test/fuzz-renderers.test.ts, ` + | |
| 328 | `or a line to EXCLUDED there saying why it cannot be`, | |
| 329 | ); | |
| 330 | } | |
| 331 | ||
| 211 | 332 | /** One case: a sample cut at random places with hostile pieces put in. */ |
| 212 | 333 | const caseFor = |
| 213 | 334 | (files: readonly string[]) => |
| @@ -254,7 +375,6 @@ export function assertInert(html: string): void { | ||
| 254 | 375 | } |
| 255 | 376 | } |
| 256 | 377 | |
| 257 | const encoder = new TextEncoder(); | |
| 258 | 378 | |
| 259 | 379 | describe("fuzz: file renderers", () => { |
| 260 | 380 | test("the inertness check catches what it is for", () => { |
| @@ -273,11 +393,34 @@ describe("fuzz: file renderers", () => { | ||
| 273 | 393 | ); |
| 274 | 394 | }); |
| 275 | 395 | |
| 396 | test("every kind RENDERERS draws is sampled here or excluded with its reason", () => { | |
| 397 | const sampled = new Set(Object.values(SAMPLES).map(sampleKind)); | |
| 398 | expect(unguarded(RENDERERS, sampled, EXCLUDED)).toEqual([]); | |
| 399 | // An exclusion for a kind that is sampled, or that no longer exists, is stale. | |
| 400 | expect(Object.keys(EXCLUDED).filter((k) => sampled.has(k) || !(k in RENDERERS))).toEqual([]); | |
| 401 | for (const reason of Object.values(EXCLUDED)) expect(reason.trim().length).toBeGreaterThan(0); | |
| 402 | }); | |
| 403 | ||
| 404 | test("the guard names a kind whose sample is taken away, and where to add it", () => { | |
| 405 | const stub: Record<string, Renderer> = { csv: RENDERERS.csv!, log: RENDERERS.log!, font: RENDERERS.font! }; | |
| 406 | const both = new Set(["csv", "log"]); | |
| 407 | expect(unguarded(stub, both, { font: "binary" })).toEqual([]); | |
| 408 | const missing = unguarded(stub, new Set(["csv"]), { font: "binary" }); | |
| 409 | expect(missing).toHaveLength(1); | |
| 410 | expect(missing[0]).toContain('"log"'); | |
| 411 | expect(missing[0]).toContain("SAMPLES in test/fuzz-renderers.test.ts"); | |
| 412 | expect(unguarded(stub, both, {})[0]).toContain('"font"'); | |
| 413 | }); | |
| 414 | ||
| 276 | 415 | for (const [name, sample] of Object.entries(SAMPLES)) |
| 277 | 416 | test(`${name}: never throws, never lets a tag or a javascript: URL through`, async () => { |
| 417 | const kind = sampleKind(sample); | |
| 418 | const render = RENDERERS[kind as keyof typeof RENDERERS]; | |
| 419 | if (render === undefined) throw new Error(`${sample.path} reads as "${kind}", which no renderer draws`); | |
| 278 | 420 | // The valid sample itself draws, so the cases built on it reach the drawing. |
| 279 | 421 | for (const file of sample.files) { |
| 280 | const out = sample.render(input(sample.path, file)); | |
| 422 | const out = render(input(sample.path, file)); | |
| 423 | expect(out.fallback).toBeUndefined(); | |
| 281 | 424 | expect(out.content.text.length).toBeGreaterThan(0); |
| 282 | 425 | } |
| 283 | 426 | await fuzz( |
| @@ -285,7 +428,7 @@ describe("fuzz: file renderers", () => { | ||
| 285 | 428 | caseFor(sample.files), |
| 286 | 429 | (text) => { |
| 287 | 430 | const all = text.length % 2 === 0; |
| 288 | const out = sample.render(input(sample.path, text, all)); | |
| 431 | const out = render(input(sample.path, text, all)); | |
| 289 | 432 | assertInert(out.content.text); |
| 290 | 433 | }, |
| 291 | 434 | Math.min(Number(process.env["FUZZ_RUNS"] ?? 250), 250), |
new above
offline