fix: escape IRIREF-forbidden characters and full control range in Writer (#659) - #661
Draft
jeswr wants to merge 1 commit into
Draft
fix: escape IRIREF-forbidden characters and full control range in Writer (#659)#661jeswr wants to merge 1 commit into
jeswr wants to merge 1 commit into
Conversation
…ter (rdfjs#659) The Writer emitted NamedNode values verbatim between angle brackets, so an IRI containing a `>` (or space, `<`, `"`, `{`, `}`, `|`, `^`, backtick, backslash, or a C0 control) closed its own `<...>` and injected forged triples (RDF-injection). Literals also passed U+001A-U+001F, DEL and the C1 block (U+0080-U+009F) through raw. - `_encodeIriOrBlank` now escapes the Turtle/N-Triples IRIREF-forbidden set (U+0000-U+0020 and `<>"{}|^` + backtick + `\`) as UCHAR (`\uXXXX`/`\UXXXXXXXX`), the only escape an IRIREF permits. Valid IRIs are untouched; astral chars still serialise as `\U...`. `@prefix` IRIs are escaped the same way. - Literal escaping is widened from U+0000-U+0019 to the full C0 range plus DEL and C1 (U+0000-U+001F, U+007F-U+009F). UCHAR escaping is identity-preserving (unlike percent-encoding), so round-trip of valid terms is unchanged. Adds regression tests covering the injection vectors and control-char escaping.
jeswr
commented
Jul 8, 2026
jeswr
left a comment
Member
Author
There was a problem hiding this comment.
We need to discuss the following RDF/JS assumptions before this PR is merged:
- Do we assume named node IRI's should be pre-escaped
We need to discuss the following N3.js assumption:
- Do we assume prefixes to be sanitised
The following N3.js assumption is known: We expect named nodes to be valid RDF/JS terms.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Draft — addresses #659.
N3WriteremittedNamedNodevalues verbatim between angle brackets, so a value containing anIRIREF-forbidden character (>,<,",{,},|,^, backtick,\, space, or a C0 control) closed its own<…>and injected forged triples (repro 1 in the issue). Separately, literal escaping only covered U+0000–U+0019, so U+001A–U+001F, DEL (U+007F) and the C1 block (U+0080–U+009F) passed through raw (repro 2).The fix (
src/N3Writer.js):_encodeIriOrBlank(and@prefixlines) escape theIRIREF-forbidden set (U+0000–U+0020 and< > " { } | ^ ` \) as UCHAR (\uXXXX/\UXXXXXXXX), via a dedicated replacer becauseIRIREFpermits only UCHAR escapes, not ECHAR._encodeLiteralwidens the escaped ranges to the full C0 plus DEL and C1 (U+0000–U+001F, U+007F–U+009F).Why escape (UCHAR) rather than percent-encode or throw: percent-encoding changes IRI identity (
>→%3Edoesn't reverse), whereas UCHAR escaping round-trips byte-identically; a valid IRI contains no forbidden characters, so well-formed data is byte-for-byte unchanged; and re-reading escaped malformed input with N3's own strict parser rejects the invalid IRI instead of materialising an injected quad. If validate-and-throw is preferred instead, that's a small swap — the main reason this is a draft. (DEL/C1 are grammar-permitted raw inside anIRIREF, so they're only escaped in literals; escaping them in IRIs too is a one-line range addition if wanted.)Tests: a
Writer term-serialisation safety (#659)block covers the issue's repros (re-parsing never yields the forged quad, an injected newline can't start a statement), every forbidden character emitted as UCHAR, valid IRIs (%20, query, fragment, punycode host,urn:,did:) byte-for-byte unchanged, astral\U…behaviour preserved, the full C0+DEL+C1 set in literals, and a hostile@prefix.npm testgreen (6512 tests, 100% coverage onN3Writer.js), lint clean.Closes #659