Injection sinks outside templates, as data; path-sink where outside input reaches; brands a project defines #129
Loading…
Reference in a new issue
No description provided.
Delete branch "sinks-and-brands"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Based on
tests-that-cannot-fail(#128), and contains it, #127 and #126: the diff shown is this piece alone.SAFE-TS has brands: a value made safe for one use carries a type that says so, and the framework rules demand the brand where markup or a URL is rendered (
vue-v-html,vue-unsafe-url,react-dangerous-html). The same sinks reached from plain code were open, and the set of brands was fixed in the checker. Each of these passedtscstrict,safe-ts check --deny-warningsand oxlint with the preset:What this adds
Sinks are data. Which member takes markup, a URL the browser may run, code, a shell command, a file path, a regular expression's source or SQL is a
sinkssection in the language's map and in the browser and Node packs, with a list of members considered and rejected, each with its reason. Every browser sink was shown to run script in Chromium and Firefox, and all but one in WebKit. A pack for a library declares its own (a database client'squery).One rule per kind, so each has its own level:
markup-sink,url-sink,code-sink,command-sink,pattern-sink,sql-sink,brand-sink(errors) andpath-sink(idiomatic, a warning). A sink accepts a constant the source shows (also built up in alet, or read from a table written in the source), a number, the sink's brand, and the forms that make one (escapeHtml, a declared sanitizer, a URL with a safe written start,pathUnder,escapeRegExp). The framework rules now judge "safe" by the same code.The operator's input is the operator's. A path, module or command from
process.argv, the environment,import.meta, a listing of such a directory, or a config file read from one is accepted where the code shows it. Past that,fromOperator(text)makes the brand and is counted as an escape hatch.Brands a project defines.
Brand<"Sql">,brandConstructor(counted as an escape hatch) andbrandSinksin@evobug_com/safe-ts-std/brand: only the declared constructors make the brand, and the declared sinks accept only it. Two worked examples in the docs: an SQL fragment that can only come from a tagged template, and an id that can only come from a parser.New in std:
SafePathwithpathUnder(root, ...parts), andFromOperator.path-sinkasks who can reach the codeThe first version reported every unbranded
stringthat reaches the file system. On an adopter's project that was 61 findings, and reading them showed none was injectable: every path came from configuration, the environment, the command line, or a check of the project's own. What makes a path dangerous is that something outside the program chose it, and the checker knows something close to that: which code an outside party can cause to run.So the rule reports only in functions that are, or are called from, a handler the runtime invokes with outside data: an HTTP, HTTPS or HTTP/2 request handler, a TCP or UDP socket's data, a WebSocket message, a worker's or child process's message. Those slots are data in the Node pack (and the
wspack), each shown on the running engine to receive../secret.txtfrom a client. Reach follows the existing call graph once per run, including a handler returned by a factory and a call through a function-typed field or a typed route table.On that project: 61 findings became 2, both in its static file server on a request's path, where the name from the URL is checked by a function of the project's own that returns
string. The page has a section on turning such a function into the place where theSafePathis made.Evidence
instanceof, a field typed with a platform class,querySelector(...)!.||=,&&=,??=andObject.assign(el, { innerHTML })are assignments. On a server of my own, a read reached from acreateServerhandler, one behind a hand-written check, and one in a handler registered by name are reported; the same read behind aSafePathparameter, or in a tool function no handler reaches, is silent.pattern-sinkfindings, nothing else (its packs predate thesinkssection). With this branch's packs:path-sink2,code-sink2,pattern-sink2,url-sink1,markup-sink1. All were read; one is injectable, by the server a smoke script talks to.What turns red on upgrade
sanitizeHtmlis no longer trusted byreact-dangerous-html: it must be DOMPurify,sanitize-htmlorxssimported from its package, a function whose everyreturnis one, or declared withdefineSanitizer.Decided along the way
DOMParseris not a sink: the document it makes is inert in all three browsers.fetchof a built URL is not one either; where a request may go is a different question.new RegExp(text)stays withunhandled-failurewhere that rule already reports it;pattern-sinkreports what it misses (str.match(text), a handlednew RegExp).readlinehandlers are not outside input. A program that takes hostile lines on stdin declares that slot in a pack.setAttribute(name, value)with a name whose written type cannot be a sink attribute (`aria-${string}` | `data-${string}`) leaves the value alone.What stays unseen
.bind; a route picked through a value with no written type; an event name that is not a constant.Object.assign(el, props)with an object not written in the call; ashelloption built elsewhere.Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.
Open: which members take markup, a URL that may run script, code, a shell command, a file path, a pattern or SQL was written nowhere; std had no brand for a path and no way for a project to declare a brand of its own. Closes: - `sinks` in ecmascript-api.json (eval, Function, RegExp, String#match/matchAll/search, import()), browser-api.json (every member that parses markup into the live document, every IDL attribute that reflects a URL the browser navigates to, every one that runs script, setAttribute and setAttributeNS by the attribute's name, execCommand("insertHTML")) and node-api.json (child_process, vm, worker_threads, module.register, node:sqlite, and every node:fs and node:fs/promises function @types/node declares with a path parameter, at each one), each with an `inert` list of members considered and why they aren't sinks. The packs' generators copy the section into safe-ts.json, and the browser pack lists the classes and globals on the way to a sink (Location, Window, HTMLScriptElement, `location`, ...). - Shown on the engines: ecmascript/sinks.test.ts and feature-node/test/sinks.test.ts give each kind text that does more than meant (code runs, `../` leaves the directory, the shell runs a second command, the statement deletes every row); feature-node checks the node:fs list against @types/node; feature-browser/test/sinks.test.ts holds every key to lib.dom.d.ts and the pack's copy to the map; test/browsers/sinks.test.ts (npm run test:browsers) makes every browser sink run script in Chromium and Firefox (WebKit all but execCommand) and no inert member run any. - std/path: SafePath and pathUnder(root, ...parts), with a test that what it accepts stays under the root on the file system and what it refuses would leave it. - std/brand: Brand<"Name">, brandConstructor("Name", make) (a Result or Option it returns keeps its Err and undefined), brandSinks("Name", members). Compile-time tests in brand.check.ts. Co-Authored-By: Claude Opus 5.5 <[email protected]>Open: `el.innerHTML = user`, `link.href = user`, `setAttribute("onclick", user)`, `setTimeout(user)`, `new Worker(user)`, `exec(`ls ${user}`)`, `readFile(`/var/data/${user}`)`, `text.search(user)` passed every gate; the framework rules each had their own idea of safe HTML (react-dangerous-html trusted a function for its name); a project couldn't hold its own brands' constructors and sinks to them. Closes: - Rules markup-sink, url-sink, code-sink, command-sink, path-sink (idiomatic, a warning), pattern-sink, sql-sink and brand-sink (sinks.rs, analyze/sinks.rs, project/sinks.rs). The analysis records where a value reaches what may be a sink, through every spelling the failure rules follow (aliases, `.call`, `.bind`, destructured and dynamic imports), and splits it into the parts the sink reads; the whole program resolves the member through the packs' class chains (and `document.createElement("a")`, a binding typed with a package's class, a receiver of unknown class by name for innerHTML and kin) and judges each part by its type. Constants in disguise found on an adopter are accepted: path.join of import.meta.dirname, an imported const bound to a constant (LocalAnalysis::constant_tops), process.cwd(), os.tmpdir(), a mkdtemp directory, new URL(c, import.meta.url), a blob: URL, a URL made from the page's own address, String(n), numbers and literal types. Test files' platform sinks aren't checked. eval and new Function are left to the preset's oxlint no-eval and no-new-func (`lint` in the map). - vue-v-html and react-dangerous-html are judged by the same code as markup-sink (vue.rs and react.rs no longer decide; react's name trust is gone); both now accept a template whose parts are each sanitized or Html. Shapes for sinks are kept apart (FileShapes::sinks) so the other rules' questions are unchanged. - Brands of your own: Brand<"Name"> is a brand to the evaluator; a brandConstructor call is a function returning it; brandSinks are read from every file (safe-ts check) or from the file's import closure (the editor); brand-constructor reports a cast, trust<Name> and a type argument making one, also of a brand declared in another file (LocalAnalysis::brand_aliases, brand_casts). brandConstructor is counted with the escape hatches (summary, --json, baseline). - Fixtures sinks/ and brands/; sinks::tests hold the three maps' sections and the packs' copies together. Soundness corpus: code-from-text, path-escapes-root, shell-command-injection, pattern-from-text, each escaping at the starting commit, all rejected now. - Docs: a page per rule, Libraries "Sinks", Proof types SafePath and brands of your own (an SQL fragment from a tagged template or a placeholder; an id from a parser), Security scope, the React and Vue pages, README, CHANGELOG, the migration skill, census RULE_SEE, the oxlint preset. Doc examples that read a path given as a string now take a SafePath. Co-Authored-By: Claude Opus 5.5 <[email protected]>