Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Fixed

- **`parse()` no longer takes quadratic time on a tag with a long run of name characters and no
`=`.** The attribute regex retried from every offset in the run, so a ~100 KB tag could block the
event loop for tens of seconds. Attributes are now read with a single linear scan that accepts
exactly the same `name = "value"` / `name = 'value'` pairs
- **`parse()` no longer re-decodes accumulated text at every `<`.** Text interrupted by many
comments, processing instructions, CDATA sections or child tags was re-trimmed and re-decoded
each time, quadratic in the text length. Only the new text is processed now. This also fixes
double decoding: `<a>&amp;lt;<!---->x</a>` now yields `&lt;x`, as it already did without the
comment, rather than `<x`
- **Element and attribute names such as `__proto__`, `constructor` and `toString` are stored as
ordinary own properties.** Previously `parse()` swapped the result's prototype for `__proto__`,
dropped its value, or wrapped the inherited `constructor` or `toString` in an array, which could
make consumers throw or misbehave. Duplicate detection now checks own properties only

### Changed

- **Replaced Prettier with [oxfmt](https://oxc.rs/docs/guide/usage/formatter.html) and added
Expand Down
61 changes: 61 additions & 0 deletions src/__tests__/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,4 +500,65 @@ describe('parse', () => {
expect(result.root).toBe(longText);
});
});
describe('hostile input', () => {
it('parses a tag with a very long attribute-like token and no "=" in linear time', () => {
const start = performance.now();
const result = parse(`<a ${'x'.repeat(200_000)}>text</a>`);
expect(result).toEqual({ a: 'text' });
expect(performance.now() - start).toBeLessThan(1000);
});

it('parses long text interrupted by many comments in linear time', () => {
const start = performance.now();
const result = parse(`<a>${'&amp;'.repeat(100_000)}${'<!---->'.repeat(20_000)}</a>`);
expect(result).toEqual({ a: '&'.repeat(100_000) });
expect(performance.now() - start).toBeLessThan(1000);
});

it('decodes entities exactly once when comments interrupt the text', () => {
expect(parse('<a>&amp;lt;<!---->x</a>')).toEqual({ a: '&lt;x' });
});

it('keeps whitespace between text segments split by a comment', () => {
expect(parse('<a> x <!--c--> y </a>')).toEqual({ a: 'x y' });
});

it('stores elements named after Object.prototype members as plain data', () => {
const result = parse('<r><__proto__>x</__proto__><constructor>y</constructor><hasOwnProperty>1</hasOwnProperty></r>');
const r = result.r as Record<string, unknown>;
expect(Object.getPrototypeOf(r)).toBe(Object.prototype);
expect(Object.getOwnPropertyDescriptor(r, '__proto__')?.value).toBe('x');
expect(r.constructor).toBe('y');
expect(r.hasOwnProperty).toBe(1);
});

it('collects repeated elements with a prototype-member name into an array', () => {
const result = parse('<r><toString>a</toString><toString>b</toString></r>');
expect((result.r as Record<string, unknown>).toString).toEqual(['a', 'b']);
});

it('does not let a __proto__ attribute change the prototype', () => {
const result = parse('<r __proto__="x">v</r>', { attributeNamePrefix: '' });
const r = result.r as Record<string, unknown>;
expect(Object.getPrototypeOf(r)).toBe(Object.prototype);
expect(Object.getOwnPropertyDescriptor(r, '__proto__')?.value).toBe('x');
});
});

describe('attribute scanning', () => {
it('parses single-quoted, double-quoted and spaced attributes', () => {
const result = parse(`<a b = "1" c='2' d="3"/>`);
expect(result).toEqual({ a: { '@_b': '1', '@_c': '2', '@_d': '3' } });
});

it('skips a name with no value and keeps scanning', () => {
const result = parse('<a disabled b="1" c=oops d="2"/>');
expect(result).toEqual({ a: { '@_b': '1', '@_d': '2' } });
});

it('skips an unquoted value and keeps scanning', () => {
const result = parse('<a b=x"y" c="1"/>');
expect(result).toEqual({ a: { '@_c': '1' } });
});
});
});
103 changes: 74 additions & 29 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,35 @@ function parseValue(val: string, coerce: boolean): string | number | boolean {
return val;
}

// Define the key as an own data property. A plain `obj[key] = value` would run the inherited
// `__proto__` setter for a document-controlled key, swapping the prototype (or silently dropping
// the value) instead of storing it.
function setOwn(obj: Record<string, unknown>, key: string, value: unknown): void {
Object.defineProperty(obj, key, { value, writable: true, enumerable: true, configurable: true });
}

function addChild(parent: Record<string, unknown>, key: string, value: unknown): void {
if (key in parent) {
// Own properties only: `key in parent` is also true for inherited names such as `constructor`,
// `toString` and `__proto__`, which would be wrapped into an array instead of stored.
if (Object.hasOwn(parent, key)) {
const existing = parent[key];
if (Array.isArray(existing)) {
existing.push(value);
} else {
parent[key] = [existing, value];
setOwn(parent, key, [existing, value]);
}
} else {
parent[key] = value;
setOwn(parent, key, value);
}
}

interface StackEntry {
tagName: string;
children: Record<string, unknown>;
/** Text already trimmed and entity-decoded. */
textContent: string;
/** Raw text (and CDATA) seen since the last flush, not yet trimmed or decoded. */
pendingText: string;
}

export function parse(xml: string, options?: ParseOptions): Record<string, unknown> {
Expand All @@ -73,28 +85,28 @@ export function parse(xml: string, options?: ParseOptions): Record<string, unkno
let i = 0;

const root: Record<string, unknown> = {};
const stack: StackEntry[] = [{ tagName: '', children: root, textContent: '' }];
const stack: StackEntry[] = [{ tagName: '', children: root, textContent: '', pendingText: '' }];

function current(): StackEntry {
return stack[stack.length - 1]!;
}

// Move the raw text seen since the last flush into `textContent`, trimmed and decoded. Only the
// new text is processed, so the cost stays linear however many comments, processing
// instructions or child tags interrupt a long run of text.
function flushText(entry: StackEntry): void {
let text = entry.pendingText;
if (!text) return;
entry.pendingText = '';
if (trimValues) text = entry.textContent === '' ? text.trim() : text.trimEnd();
if (processEnt) text = decodeEntities(text);
// Text will be set as the value when the tag closes, unless there are also child elements
if (text) entry.textContent += text;
}

while (i < len) {
if (xml[i] === '<') {
// Flush any pending text content
const cur = current();
if (cur.textContent) {
let text = cur.textContent;
if (trimValues) text = text.trim();
if (processEnt) text = decodeEntities(text);
if (text) {
// Text will be set as the value when the tag closes,
// unless there are also child elements
cur.textContent = text;
} else {
cur.textContent = '';
}
}
flushText(current());

// Check what kind of tag
if (xml.startsWith('<!--', i)) {
Expand Down Expand Up @@ -136,7 +148,7 @@ export function parse(xml: string, options?: ParseOptions): Record<string, unkno
const end = xml.indexOf(']]>', i + 9);
if (end === -1) break;
const cdataText = xml.slice(i + 9, end);
current().textContent += cdataText;
current().pendingText += cdataText;
i = end + 3;
continue;
}
Expand Down Expand Up @@ -167,6 +179,8 @@ export function parse(xml: string, options?: ParseOptions): Record<string, unkno
}

const entry = stack.pop()!;
// Decoding can introduce leading or trailing whitespace (e.g. `&#32;`), so trim the result
if (trimValues && entry.textContent) entry.textContent = entry.textContent.trim();
const parent = current();
const childKeys = Object.keys(entry.children);

Expand All @@ -179,7 +193,7 @@ export function parse(xml: string, options?: ParseOptions): Record<string, unkno
value = entry.children;
if (entry.textContent !== '') {
// Mixed content: add text as #text
(value as Record<string, unknown>)['#text'] = parseValue(entry.textContent, parseTagVal);
setOwn(value as Record<string, unknown>, '#text', parseValue(entry.textContent, parseTagVal));
}
}

Expand Down Expand Up @@ -224,7 +238,7 @@ export function parse(xml: string, options?: ParseOptions): Record<string, unkno
if (stack.length > maxDepth) {
throw new Error(`Maximum nesting depth of ${maxDepth} exceeded`);
}
stack.push({ tagName, children: attrs, textContent: '' });
stack.push({ tagName, children: attrs, textContent: '', pendingText: '' });
}

i = tagEnd + 1;
Expand All @@ -234,10 +248,10 @@ export function parse(xml: string, options?: ParseOptions): Record<string, unkno
// Text content — scan ahead to next '<' instead of char-by-char
const nextTag = xml.indexOf('<', i);
if (nextTag === -1) {
current().textContent += xml.slice(i);
current().pendingText += xml.slice(i);
i = len;
} else {
current().textContent += xml.slice(i, nextTag);
current().pendingText += xml.slice(i, nextTag);
i = nextTag;
}
}
Expand Down Expand Up @@ -272,17 +286,48 @@ function findTagEnd(xml: string, start: number): number {
return -1;
}

const SPACE_RE = /\s/;

function isSpace(ch: string): boolean {
return SPACE_RE.test(ch);
}

// Single left-to-right scan for `name = "value"` / `name = 'value'` pairs. This replaces the regex
// /([^\s=]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g, which retried from every offset inside a long run of
// name characters with no `=` and so took quadratic time. When a name has no valid `= "value"`
// after it, every suffix of that name fails identically, so the scan resumes after the name.
function parseAttributes(str: string, attrs: Record<string, unknown>, prefix: string, rmNS: boolean, processEnt: boolean): void {
const re = /([^\s=]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
let match: RegExpExecArray | null;
while ((match = re.exec(str)) !== null) {
const rawName = match[1]!;
let value: string = (match[2] ?? match[3])!;
const len = str.length;
let i = 0;
while (i < len) {
const ch = str[i]!;
if (ch === '=' || isSpace(ch)) {
i++;
continue;
}

const nameStart = i;
while (i < len && str[i] !== '=' && !isSpace(str[i]!)) i++;
const nameEnd = i;

let j = nameEnd;
while (j < len && isSpace(str[j]!)) j++;
if (str[j] !== '=') continue;
j++;
while (j < len && isSpace(str[j]!)) j++;
const quote = str[j];
if (quote !== '"' && quote !== "'") continue;
const close = str.indexOf(quote, j + 1);
if (close === -1) continue;

const rawName = str.slice(nameStart, nameEnd);
let value = str.slice(j + 1, close);
i = close + 1;
// When stripping namespace prefixes, drop xmlns and xmlns:* declarations entirely
if (rmNS && (rawName === 'xmlns' || rawName.startsWith('xmlns:'))) continue;
const name = rmNS ? removeNS(rawName) : rawName;
if (processEnt) value = decodeEntities(value);
// Attribute values are always strings — never coerce
attrs[prefix + name] = value;
setOwn(attrs, prefix + name, value);
}
}
Loading