8000 Percent-encode and decode PartName in [Content_Types].xml by DominikPalo · Pull Request #4 · DominikPalo/OOXML-Toolkit · GitHub
[go: up one dir, main page]

Skip to content
8000

Percent-encode and decode PartName in [Content_Types].xml - #4

Merged
DominikPalo merged 1 commit into
mainfrom
fix/content-types-partname-encoding
Oct 7, 2026
Merged

DominikPalo merged 1 commit into
mainfrom
fix/content-types-partname-encoding

Conversation

@DominikPalo
Copy link
Copy Markdown
Owner

What

Part names in <Override PartName="…"> are URIs and must be percent-encoded (/word/media/my%20logo.png), but the code treated them inconsistently.

Write side. renamePart() wrote '/' + next unencoded, so renaming ppt/slides/slide1.xml to ppt/slides/my slide.xml produced PartName="/ppt/slides/my slide.xml", which is not a valid part-name URI and can make Office report the file as corrupt. The relationship Target in the same function was already encoded with encodePartUri.

Read side. parseContentTypes() only stripped the leading slash. A correctly encoded override such as /xl/embeddings/Dokument%20Word.docx never matched the real part name xl/embeddings/Dokument Word.docx, so contentTypeOf() returned undefined and the package check reported a false "No content type is defined" warning plus a false override-missing.

Changes

  • parseContentTypes() now percent-decodes PartName through the existing safeDecode. Malformed % sequences fall back to the raw string and never throw.
  • Renaming writes '/' + encodePartUri(next).
  • renamePart() also looked up the override by comparing the raw PartName to the decoded part name, so it could not find an encoded override and would have left a stale entry behind. The lookup now decodes the same way.
  • The override rewrite moved from actions.ts into a small core helper, renameOverride(doc, from, to) in src/core/package/opc.ts. tests/ only covers src/core, so this is what makes the write path unit-testable. It is also where CLAUDE.md says the logic belongs. actions.ts shrinks by about 10 lines.

ensureContentType() needed no change: it only writes <Default Extension="…">, never a PartName.

Reviewer notes

  • Seven new tests in tests/package.test.ts (content type part names): encoded read-side match, exact encoded output on rename, a round trip with a space, % and non-ASCII in one name, an unencoded PartName written by another tool, a splice that leaves the rest of the source byte-identical, malformed % sequences (100%, %E0%A4%A, %zz), and validatePackage on a real XLSX with an encoded override.
  • I checked the tests catch the bugs: with the old read side restored, 4 fail; with the old write side restored, 2 fail.
  • npm run typecheck, npm test (116 tests) and npm run build pass.
  • ODF code is untouched (that work is on feature/odf-support).
  • Not changed: Default Extension values are still neither encoded nor decoded. That is outside what this PR set out to fix.

🤖 Generated with Claude Code

Part names in <Override PartName="..."> are URIs, but the code treated them
inconsistently:

- Renaming a part wrote the raw name ("/ppt/slides/my slide.xml"), which is
  not a valid part-name URI and can make Office report the file as corrupt.
  Relationship targets were already encoded in the same function.
- parseContentTypes() only stripped the leading slash, so a correctly
  encoded override ("/xl/embeddings/Dokument%20Word.docx") never matched the
  real part name. contentTypeOf() returned undefined and the package check
  reported a false "no-content-type" and "override-missing".

parseContentTypes() now percent-decodes PartName (malformed % sequences fall
back to the raw string), and renaming writes the encoded name. The override
rewrite moves into a small core helper, renameOverride(), so it can be unit
tested; it decodes when looking up the existing override, so encoded entries
are found and updated on rename.

ensureContentType() only writes <Default Extension>, never a PartName, so it
needed no change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@DominikPalo
DominikPalo merged commit 16449e4 into main Oct 7, 2026
1 check passed
@chatgpt-codex-connector
chatgpt-codex-connector Bot commented Oct 7, 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-07T08:37:58.528213Z a1dd6c9 PR opened
ℹ️ 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.

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.

1 participant

0