Skip to content

fix: v6 broken imports, entity crash on out-of-range codepoints, DOCTYPE entity replacement - #923

Closed
Goldloli wants to merge 1 commit into
NaturalIntelligence:masterfrom
Goldloli:fix/v6-import-and-entity-fixes-922
Closed

Goldloli wants to merge 1 commit into
NaturalIntelligence:masterfrom
Goldloli:fix/v6-import-and-entity-fixes-922

Conversation

@Goldloli

Copy link
Copy Markdown

Refs #922 — fixes all six findings from the v6 audit. Each was reproduced against current master (5.12.1, 88b816b) before and after the change.

What was broken (all in src/v6/, unreachable from the package entry today)

  1. OptionsBuilder.js imported the named export JsObjOutputBuilder, but OutputBuilders/JsObjBuilder.js only has a default export → any import of src/v6/XMLParser.js failed at link time.
  2. TagPathMatcher.js:1 did a named import of TagPath, which likewise only has a default export → Xml2JsParser.js failed to import.
  3. spec/importTests/v6/import_test.js had the same import line twice (syntax error), so the one test that would have caught Wrong output #1/Update .codeclimate.yml #2 never ran.
  4. EntitiesParser.js called String.fromCodePoint() without a range check; the regexes allow up to 7 decimal / 6 hex digits, so � (12 bytes of untrusted input) threw RangeError: Invalid code point and crashed the parse. Now range-checked, and out-of-range references are left untouched — mirroring v5's fromCodePoint() in OrderedObjParser.js.
  5. EntitiesParser.js:78 read entity.regx (typo) instead of entity.regex. String.prototype.replace(undefined, x) treats undefined as the literal string "undefined", so DOCTYPE-declared entities were never replaced and any literal word undefined in the text was silently replaced by the entity value.
  6. EntitiesParser.js:42 called reportWarning(), which is not defined or imported anywhere, so registering an external entity whose value contains & threw ReferenceError. Replaced with console.warn, the warning channel used elsewhere in the codebase (src/cli/cli.js).

One extra fix the import repairs uncovered

With #1/#2 fixed, module evaluation of OptionsBuilder.js reaches new JsObjOutputBuilder() → buildOptions(), which read this.preserveOrder in a plain exported function (this is undefined in ESM) → TypeError on import. Changed to read options.preserveOrder (undefined-safe). Without this, finding #3's import test still fails.

Out of scope, for later: src/v6/valueParsers/EntitiesParser.js is a stale duplicate of EntitiesParser.js with the same reportWarning bug, but nothing imports it; and buildOptions's shallow { ...defaultOptions } clone aliases the shared tags.valueParsers array, so repeated parser construction accumulates duplicate value parsers. Happy to follow up on either if you want.

Verification

  • npm test → 343 specs, 0 failures (2 pending are pre-existing xit)
  • v6 end-to-end parse works: new XMLParser().parse('<root><item id="1">hello &amp; world</item>…') → correct tree
  • Each issue repro now behaves correctly: &#9999999; / &#xFFFFFF; left as-is (no RangeError), in-range &#65;/&#x41; still decode to A; DOCTYPE entity &myent; replaced, literal undefined untouched; addExternalEntity('bad', 'a&b') warns and skips instead of throwing
  • The two ESLint parse errors on the changed v6 files (... spread) exist identically on master — no new lint issues

…YPE entity replacement

Refs NaturalIntelligence#922

- OptionsBuilder/TagPathMatcher: use default imports (the modules only
  have default exports), so src/v6 is importable again
- importTests/v6: drop the duplicate import (syntax error)
- EntitiesParser: range-check numeric character references before
  String.fromCodePoint() and leave out-of-range references untouched,
  mirroring v5's fromCodePoint() (&#9999999; threw RangeError)
- EntitiesParser: fix entity.regx typo so DOCTYPE-declared entities are
  replaced (and the literal text "undefined" is no longer corrupted)
- EntitiesParser: replace the undefined reportWarning() call in
  addExternalEntity with console.warn (external entity with & in value
  threw ReferenceError)
- ParserOptionsBuilder: read options.preserveOrder instead of
  this.preserveOrder in buildOptions (TypeError on import of v6)
@amitguptagwl

Copy link
Copy Markdown
Member

Please read docs. Use Flexible XML Parser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants