Skip to content

perf(xmlenc): decrypt without redundant document parses - #307

Merged
polaz merged 7 commits into
mainfrom
perf/#306-decrypt-reparse
Oct 26, 2026
Merged

polaz merged 7 commits into
mainfrom
perf/#306-decrypt-reparse

Conversation

@polaz

@polaz polaz commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

Summary

Document decryption parsed the whole document four times. It now parses it twice: once for the input and once for the generation it commits.

  • Replacement validation on one parse. Element and fragment node replacement no longer parse a wrapped validation candidate before the commit candidate. The commit candidate is checked directly:

    • the original parent spans exactly its projected range, so every end tag of the replacement stays inside it;
    • every child that intersects the replaced range lies inside it. The only exception is text folded with a neighbour, whose part inside the range must end on a token boundary, with no CDATA section or reference left open for a neighbour to close;
    • an Element replacement holds exactly one element, beside whitespace, comments and processing instructions. Text that folds into a neighbour is judged by its decoded value: the neighbours keep their decoded values, so the replacement's own text is what remains between them.

    Bytes no node covers must be whitespace or complete references and CDATA sections (an empty entity leaves no node). When an internal DTD is allowed and the candidate holds nodes expanded from a declared entity, whose ranges may point into the declaration rather than at the reference, the replacement is validated inside the previous wrapper instead; documents without a DTD always take the single parse.

    This covers XmlDocument::replace_element, XmlDocument::replace_node_with_fragment and the XMLEnc Element/Content replacement.

  • Metadata from the parsed view. Document decryption reads the selected EncryptedData, its cipher references and the key-source association from the retained document's parsed view. It no longer reparses the document's serialization.

Performance

Measured on DecryptContext::decrypt_document with a 598 KB EncryptedData (AES-256-GCM, RSA-OAEP) that decrypts to a 448 KB Element with 16384 children (macOS, release, median of 15, same session):

decrypt
main 45.8 ms
this PR 25.0 ms

Behaviour notes

  • Every refusal stays a refusal. A replacement that closes its parent now fails the structural check (InvalidReplacement) instead of failing the wrapper parse, because the document as a whole can still be well-formed.
  • Decryption consumes less of the operation's XML parse-work allowance, since it no longer parses the source twice.

Testing

  • cargo fmt --check.
  • cargo clippy --workspace --all-targets --all-features -D warnings on stable.
  • check-features.sh lint for the xmlenc-roxmltree, dsig-roxmltree and dom-xmloxide profiles on 1.92.
  • Replacement and decryption tests on the roxmltree backend.
  • New tests in src/document/replacement_tests.rs, written first and passing against the previous wrapper implementation too:
    • accepted shapes, including whitespace that folds into neighbouring text, also when written as a character reference or a CDATA section;
    • refused Element shapes;
    • fragments that try to absorb a neighbour through an open CDATA section, processing instruction or reference, or that close the parent. Each of these candidates is well-formed as a whole document.

Closes #306

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-25T23:47:17.518748Z 6142f26 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 21 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
Commits

Reviewing files that changed from the base of the PR and between bfa636c and 6142f26.

Files selected for processing: 3
  • src/document.rs
  • src/document/replacement_tests.rs
  • src/xmlenc/decrypt.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: caa8d4249a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/document.rs Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 908b10f06c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/document.rs Resolved
polaz added 4 commits October 25, 2026 22:44
Element and fragment node replacement parsed the whole document twice:
once with the replacement inside a validation wrapper and once for the
generation to commit. The commit candidate is now the only parse, and
the replacement is checked on it:

- the original parent spans exactly its projected range, so every end
  tag of the replacement stays inside it;
- every child that intersects the replaced range lies inside it, except
  text folded with a neighbour, whose part inside the range must end on
  a token boundary (no CDATA section or reference left open);
- an element replacement holds exactly one element beside whitespace,
  comments and processing instructions.

Refs #306
Document decryption parsed the retained document's serialization again
to read the selected EncryptedData. The retained generation already
satisfies the operation's XML policy, so its parsed view now supplies
the metadata, cipher references and key-source association directly.

Closes #306
An element replacement whose surrounding whitespace folds into the
neighbouring text was checked on its raw bytes, so whitespace written as
a character reference or a CDATA section was refused next to text while
accepted elsewhere. Once its tokens are complete, the neighbours keep
their decoded values, so the replacement's own text is the folded value
without their known lengths; that decoded text must be whitespace.

Regression test: element_replacement_reads_folded_whitespace_by_value.
A reference to an empty entity (XML 1.0 section 2.3 [9] permits an
empty value) produces no node on the roxmltree backend, so its bytes
were left uncovered and treated as markup escaping the replaced range.
Uncovered bytes may now be whitespace or complete references and CDATA
sections; anything left open still fails.

Regression test: replacement_may_hold_references_that_expand_to_nothing,
run on every compiled backend.
@polaz
polaz force-pushed the perf/#306-decrypt-reparse branch from 908b10f to 5d2bd31 Compare October 25, 2026 20:50

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5d2bd31842

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/document.rs Resolved
The one-parse slot check reads node source ranges. With an internal DTD
the roxmltree backend gives nodes expanded from a declared entity a
range inside the declaration, not at the reference, so an element
replacement of `<x/>&extra;` passed with two elements and `&extra;`
alone was refused. When the internal DTD is allowed and the candidate
has such nodes, the replacement is validated inside a wrapper instead,
which recognizes expanded markup where it occurs (XML 1.0 section
4.4.2). Documents without a DTD keep the single parse: predefined and
character references keep the reference's own range.

Regression test: entity_expansions_count_toward_the_replacement_shape,
on every compiled backend.
@polaz
polaz enabled auto-merge (squash) October 25, 2026 21:30
@polaz
polaz merged commit 632edd5 into main Oct 26, 2026
30 checks passed
@polaz
polaz deleted the perf/#306-decrypt-reparse branch October 26, 2026 01:17
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.

perf(xmlenc): decrypt without redundant document parses

1 participant