fix(docx-preview): sanitize hyperlink URIs to prevent XSS#158
Open
Mark-Zampedroni wants to merge 1 commit intomeganz:masterfrom
Open
fix(docx-preview): sanitize hyperlink URIs to prevent XSS#158Mark-Zampedroni wants to merge 1 commit intomeganz:masterfrom
Mark-Zampedroni wants to merge 1 commit intomeganz:masterfrom
Conversation
diegocr
requested changes
Mar 16, 2026
Contributor
diegocr
left a comment
There was a problem hiding this comment.
Hi @Mark-Zampedroni, thanks for your PR!
Ideally however, you should be submitting this on the upstream repository at https://github.com/VolodymyrBaydalka/docxjs
Later on, once your changes are merged, we will sync our fork with them.
You're editing a vendor file whose changes might be lost, since there's a bundling process with that file automatically generated.
Alternatively, if they don't accept your changes, you could submit this on our fork, but we will anyway consider applying them there either way - thanks again!
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.
What
Adds protocol validation to
renderHyperlinkindocx-preview.jsto prevent potential XSS via crafted DOCX hyperlinks.Why
The
renderHyperlinkfunction setsresult.hrefdirectly from the DOCX relationship target without validating the URI scheme. A DOCX file with ajavascript:hyperlink gets rendered as a clickable<a href="javascript:...">in the preview iframe.The preview iframe is
about:blank, same-origin, and has nosandboxattribute, so any script execution would run in the context ofmega.nzwith full access to the session.Currently CSP blocks execution, but there's no code-level protection. For reference,
renderSymbolin the same file already has an explicit XSS prevention check (with the commentmega.nz production build: prevent xss/html-injection), so this is an oversight in an adjacent function.Fix
Instead of assigning
hrefimmediately, the value is first validated against a protocol whitelist (http:,https:,mailto:). Non-matching URIs (likejavascript:,data:,vbscript:) are silently dropped. The<a>element is still rendered but without anhref, so the document structure is preserved.Example
A minimal DOCX with a malicious hyperlink relationship in
word/_rels/document.xml.rels:Referenced in
word/document.xml:Before fix: the preview iframe renders
<a href="javascript:void(alert('XSS: '+document.domain))">Click here</a>After fix: the
<a>tag is rendered without the dangeroushrefsincejavascript:doesn't match the allowed protocols.