Public: anyone can read this; the crew can change it.

XML hostile-input test measures growth, not milliseconds: a quadratic parser still fails, a slow runner doesn't #16943

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

fix/xml-hostile-bound → main 0 commits 1 thread · 1 unresolved

  • Merged. fix/xml-hostile-bound was merged into main as 9d5f923.

Review

Sign in to review this change.
1 file changed +37 −12
test/render-ini-rtf-xml.test.ts +37 −12
@@ -101,22 +101,47 @@ describe("xml", () => {
101101 });
102102 });
103103
104 /**
105 * The CPU time of `fn`, the least of three runs: CPU, not the wall clock, so a busy host's waiting
106 * does not count, and the least, so a collection or a stray tick in one run does not either.
107 */
108 function leastCpuMs(fn: () => void): number {
109 let least = Number.POSITIVE_INFINITY;
110 for (let i = 0; i < 3; i++) {
111 const t = process.cpuUsage();
112 fn();
113 const used = process.cpuUsage(t);
114 least = Math.min(least, (used.user + used.system) / 1000);
115 }
116 return least;
117 }
118
104119 describe("xml: hostile repeated input costs a bounded amount", () => {
105120 const MB5 = 5 * 1024 * 1024;
106 const cases: Record<string, string> = {
107 "unterminated quoted starts": '<r>' + '<a "'.repeat(MB5 / 4),
108 "unterminated doctype subsets": "<!DOCTYPE a [".repeat(MB5 / 13),
109 "one huge attribute run": "<a " + "x".repeat(MB5) + "/>",
110 "unterminated comments": "<r>" + "<!-- ".repeat(MB5 / 5),
111 "bare angle brackets": "<r>" + "< ".repeat(MB5 / 2),
112 "deep opens": "<a>".repeat(MB5 / 3),
121 // Each case at `n` characters, so it can be read at two sizes.
122 const cases: Record<string, (n: number) => string> = {
123 "unterminated quoted starts": (n) => '<r>' + '<a "'.repeat(n / 4),
124 "unterminated doctype subsets": (n) => "<!DOCTYPE a [".repeat(n / 13),
125 "one huge attribute run": (n) => "<a " + "x".repeat(n) + "/>",
126 "unterminated comments": (n) => "<r>" + "<!-- ".repeat(n / 5),
127 "bare angle brackets": (n) => "<r>" + "< ".repeat(n / 2),
128 "deep opens": (n) => "<a>".repeat(n / 3),
113129 };
114 for (const [name, text] of Object.entries(cases))
130 // The bound is the growth, not a number of milliseconds: twice the input costs about twice the
131 // work for a linear parser and four times for a quadratic one, and a busy host slows both sizes
132 // alike. A fixed 500 ms failed at 717 ms on a loaded runner with no XML change (#16913).
133 for (const [name, make] of Object.entries(cases))
115134 test(name, () => {
116 const t = performance.now();
117 parseXml(text);
118 xml(input("x.xml", text));
119 expect(performance.now() - t).toBeLessThan(500);
135 const run = (text: string) => () => {
136 parseXml(text);
137 xml(input("x.xml", text));
138 };
139 const half = make(MB5 / 2);
140 const whole = make(MB5);
141 const small = leastCpuMs(run(half));
142 const big = leastCpuMs(run(whole));

The 3x + 5 ms bound is sound for the flake. Keep the comment that says why. Next step: show once that a quadratic parse fails it.

143 // 3x is between linear (2x) and quadratic (4x); 5 ms covers a size too quick to read a ratio.
144 expect(big).toBeLessThan(3 * small + 5);
120145 });
121146 test("a tag after a hostile prefix is still read", () => {
122147 expect(parseXml('<!-- a --><r k="v"><c/></r>')?.root.children.length).toBe(1);

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs