diff --git a/CHANGELOG.md b/CHANGELOG.md
index 83ba85d..1d94104 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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: `<x` now yields `<x`, as it already did without the
+ comment, rather than ` {
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(`text`);
+ 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(`${'&'.repeat(100_000)}${''.repeat(20_000)}`);
+ 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('<x')).toEqual({ a: '<x' });
+ });
+
+ it('keeps whitespace between text segments split by a comment', () => {
+ expect(parse(' x y ')).toEqual({ a: 'x y' });
+ });
+
+ it('stores elements named after Object.prototype members as plain data', () => {
+ const result = parse('<__proto__>xy1');
+ const r = result.r as Record;
+ 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('ab');
+ expect((result.r as Record).toString).toEqual(['a', 'b']);
+ });
+
+ it('does not let a __proto__ attribute change the prototype', () => {
+ const result = parse('v', { attributeNamePrefix: '' });
+ const r = result.r as Record;
+ 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(``);
+ expect(result).toEqual({ a: { '@_b': '1', '@_c': '2', '@_d': '3' } });
+ });
+
+ it('skips a name with no value and keeps scanning', () => {
+ const result = parse('');
+ expect(result).toEqual({ a: { '@_b': '1', '@_d': '2' } });
+ });
+
+ it('skips an unquoted value and keeps scanning', () => {
+ const result = parse('');
+ expect(result).toEqual({ a: { '@_c': '1' } });
+ });
+ });
});
diff --git a/src/parse.ts b/src/parse.ts
index 414bd5e..c1ff103 100644
--- a/src/parse.ts
+++ b/src/parse.ts
@@ -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, key: string, value: unknown): void {
+ Object.defineProperty(obj, key, { value, writable: true, enumerable: true, configurable: true });
+}
+
function addChild(parent: Record, 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;
+ /** 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 {
@@ -73,28 +85,28 @@ export function parse(xml: string, options?: ParseOptions): Record = {};
- 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('