Repository navigation
Conversation
External images are fetched through the image proxy and served with a generic application/octet-stream content type. Browsers sniff raster formats in <img> tags, but they refuse to render SVG unless it is served as image/svg+xml, so SVG logos stayed blank and only the alt text and placeholder were shown. Detect SVG markup in the proxied response and serve it as a sanitised image/svg+xml document. A new SvgSanitizer service strips active content (scripts, event handlers, external/javascript references) and rejects DOCTYPE/entity declarations as XXE defence in depth, even though the <img> context does not execute scripts. Assisted-by: Claude:claude-opus-4-8 Signed-off-by: Joël de Jager <contact@joeldejager.nl>
|
Thanks for the contribution |
|
@coderabbitai review and scan for new vulnerabilities |
🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Get the list of changed files in this PR
gh pr view 13142 --repo nextcloud/mail --json files --jq '.files[].path'Length of output: 416 🏁 Script executed: #!/bin/bash
# Read the new SvgSanitizer and any modified proxy/controller files
fd -e php . lib/ --exec echo {} \; | grep -i 'svg\|proxy\|image' | sort
cat -n lib/Service/SvgSanitizer.php 2>/dev/null || echo "NOT FOUND"Length of output: 4248 🏁 Script executed: #!/bin/bash
# Find and read all relevant new/changed files
fd -e php . lib/ | xargs grep -l -i 'svg\|SvgSanitizer' 2>/dev/null | sort
# Also find proxy-related files
fd -e php . lib/ | xargs grep -l -i 'proxy\|imageproxy\|image_proxy' 2>/dev/null | sortLength of output: 730 🏁 Script executed: #!/bin/bash
cat -n lib/Controller/ProxyController.phpLength of output: 6202 🏁 Script executed: #!/bin/bash
cat -n tests/Unit/Service/SvgSanitizerTest.php
cat -n tests/Unit/Controller/ProxyControllerTest.phpLength of output: 14218
🔴 Security findings1.
|
| Concern | Status |
|---|---|
| HMAC validation before proxying | ✅ |
LIBXML_NONET prevents network fetch during parse |
✅ |
DOCTYPE / <!ENTITY pre-rejected by regex |
✅ |
on* event-handler attributes stripped |
✅ |
Namespace-aware element removal (local-name()) |
✅ |
External href / xlink:href stripped |
✅ |
Rate-limiting (#[UserRateLimit]) |
✅ |
LocalServerException caught (SSRF guard) |
✅ |
| Test coverage for sanitizer + controller | ✅ |
✅ Action performed
Review finished.
Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.
📝 WalkthroughWalkthroughA new ChangesSVG Sanitization Feature
Estimated code review effort: 3 (Moderate) | ~25 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 7
🧹 Nitpick comments (1)
lib/Controller/ProxyController.php (1)
137-146: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winMove SVG detection heuristic out of the controller.
looksLikeSvg()is business logic and makesProxyControllerless of a thin HTTP handler. Consider moving this heuristic intoSvgSanitizer(or a dedicated detector service) and call it from here.As per coding guidelines,
lib/Controller/**/*.php: “Controllers must be thin HTTP handlers; business logic must live in services”.Source: Coding guidelines
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 26c52dd2-524b-4c89-9e7f-305d15c812e0
📒 Files selected for processing (4)
lib/Controller/ProxyController.phplib/Service/SvgSanitizer.phptests/Unit/Controller/ProxyControllerTest.phptests/Unit/Service/SvgSanitizerTest.php
|
Yes |
- Extend URL attribute stripping to cover src, action, formaction - Strip external CSS url() references from <style> elements and style="" attributes while preserving fragment references (url(#...)) - Reject SVG payloads exceeding 2 MiB before DOM parsing (DoS guard) - Fall back to blocked-image.png instead of serving empty body when sanitizer rejects an SVG document - Drop misleading <!-- branch from looksLikeSvg() heuristic - Move looksLikeSvg() from ProxyController into SvgSanitizer service Assisted-by: Claude:claude-sonnet-4-6 Signed-off-by: Joël de Jager <contact@joeldejager.nl>
|
The recommended changes have been implemented. |
|
@coderabbitai re-review |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
cat -n lib/Service/SvgSanitizer.php
echo "---"
cat -n lib/Controller/ProxyController.phpLength of output: 11368
The implementation is clean and all security measures are in place. [approve] |
|
Hello there, We hope that the review process is going smooth and is helpful for you. We want to ensure your pull request is reviewed to your satisfaction. If you have a moment, our community management team would very much appreciate your feedback on your experience with this PR review process. Your feedback is valuable to us as we continuously strive to improve our community developer experience. Please take a moment to complete our short survey by clicking on the following link: https://cloud.nextcloud.com/apps/forms/s/i9Ago4EQRZ7TWxjfmeEpPkf6 Thank you for contributing to Nextcloud and we hope to hear from you soon! (If you believe you should not receive this message, you can add yourself to the blocklist.) |
|
The branch needs an update because there is a conflict |
Signed-off-by: Joël de Jager <contact@joeldejager.nl>
Assisted-by: Codex:gpt-5 Signed-off-by: Joël de Jager <contact@joeldejager.nl>
…oeldj-nl/ncmail into fix/render-received-svg-images
Updated! |
Assisted-by: Codex:gpt-5 Signed-off-by: Joël de Jager <contact@joeldejager.nl>
|
@coderabbitai review again |
|
✅ Action performedReview finished.
|
There was a problem hiding this comment.
Actionable comments posted: 3
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 537ec8a0-7baa-4b21-b7c6-209350983019
📒 Files selected for processing (1)
lib/Controller/ProxyController.php
Assisted-by: Codex:gpt-5 Signed-off-by: Joël de Jager <contact@joeldejager.nl>
|
@ChristophWurst the new findings by CodeRabbitAI have been resolved. |
|
Thanks for your pr 👍 We reject SVG's with XLS/Transform in server (c.f. nextcloud/server#59835) and should apply the same hardening here. |
Signed-off-by: Joël de Jager <contact@joeldejager.nl>
|
@kesselb thanks for your input. This should be fixed now. |
There was a problem hiding this comment.
Pull request overview
This PR fixes external SVG images not rendering in proxied email content by detecting SVG payloads and serving them as image/svg+xml, adding an SVG sanitization layer to reduce XSS/XXE risk when proxying untrusted markup.
Changes:
- Add
SvgSanitizerservice to detect SVG markup and strip active/dangerous content. - Update
ProxyControllerto read streamed proxy responses safely and to sanitize/serve SVG with the correct content type. - Add unit tests for the sanitizer and for proxying sanitized SVG responses.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
lib/Controller/ProxyController.php |
Reads stream bodies, detects SVG responses, sanitizes them, and serves with image/svg+xml (fallbacks to blocked image on unsafe SVG). |
lib/Service/SvgSanitizer.php |
New sanitizer that parses SVG with libxml hardening and strips scripts/event handlers/external references. |
tests/Unit/Controller/ProxyControllerTest.php |
Adds test coverage for streamed bodies and SVG sanitization/content-type behavior in the proxy. |
tests/Unit/Service/SvgSanitizerTest.php |
Adds unit tests for sanitizer security rules and SVG detection heuristics. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Joël de Jager <contact@joeldejager.nl>
Assisted-by: ClaudeCode:claude-fable-5 Signed-off-by: Joël de Jager <contact@joeldejager.nl>
Assisted-by: ClaudeCode:claude-fable-5 Signed-off-by: Joël de Jager <contact@joeldejager.nl>
|
Added new commits to resolve the issues. |
Signed-off-by: Christoph Wurst <1374172+ChristophWurst@users.noreply.github.com> # Conflicts: # lib/Controller/ProxyController.php
ChristophWurst
left a comment
There was a problem hiding this comment.
Thanks Joël, the root cause is spot on and the tests are appreciated.
Main concern: the headline case doesn't work for typical exported SVGs yet. Anything with a plain <!DOCTYPE svg PUBLIC …> header (Illustrator, older Inkscape) is rejected, files starting with a comment or DOCTYPE aren't detected, and data: images are stripped, so those logos stay blank. Second concern: the hand-rolled sanitizer has gaps (see inline notes, the CSS ones are reproducible with a short snippet). In an <img> context the impact is low, but it describes itself as defence in depth.
Please also trim the comments per AGENTS.md. Could you try an Illustrator-style export with DOCTYPE, an SVG using <style> classes and one with an embedded base64 PNG, and tell us what renders?
This touches security-relevant parsing, so a second reviewer on the sanitizer would be good.
Assisted by ClaudeCode:claude-sonnet-5-5
|
|
||
| // A DOCTYPE or entity declaration is not needed for plain SVG graphics | ||
| // and is a common XXE / entity-expansion vector. Reject such documents. | ||
| if (preg_match('/<!DOCTYPE|<!ENTITY/i', $inspectable) === 1) { |
There was a problem hiding this comment.
Any document with a DOCTYPE is rejected, including the standard <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "…svg11.dtd"> header that Illustrator and older Inkscape exports carry. Those now show the blocked placeholder. Could we reject only <!ENTITY / internal subsets ([) and tolerate the plain public DOCTYPE? The parser runs without LIBXML_NOENT/LIBXML_DTDLOAD, so it isn't resolved anyway.
| /** | ||
| * Heuristically decide whether the given bytes are an SVG document. | ||
| */ | ||
| public function looksLikeSvg(string $content): bool { |
There was a problem hiding this comment.
Two problems with the heuristic: (1) files starting with <!-- Generator: … --> or a bare <!DOCTYPE svg return false, so they stay application/octet-stream and blank. (2) Any XML that merely mentions <svg, e.g. <?xml …?><html><svg/></html>, returns true. Checking that the parsed root element is svg in the SVG namespace would be more robust than string prefixes.
| * @return string|null Null if the payload is not in an accepted encoding | ||
| */ | ||
| private function normalize(string $svg): ?string { | ||
| $encoding = mb_detect_encoding($svg, self::ALLOWED_ENCODINGS, true); |
There was a problem hiding this comment.
ISO-8859-1 matches any byte string, so this never returns false; random bytes and UTF-16 both detect as ISO-8859-1. The null branch is dead and "anything else is rejected outright" isn't true. Also, the string checks run on this normalised copy while loadXML at :80 gets the original bytes with their declared encoding, so the two can see different documents. The DOM pass is what actually protects here; I'd say so rather than claim the pre-checks are exhaustive.
|
|
||
| // Only allow same-document references; strip javascript:, external | ||
| // and data: URLs from links and resource references. | ||
| if (in_array($localName, self::URL_ATTRIBUTES, true) && !str_starts_with($value, '#')) { |
There was a problem hiding this comment.
This also strips href="data:image/png;base64,…" on <image>, so logos with an embedded bitmap render empty. data:image/* can't make a request, so it could be allowed for image elements. Related: XHTML-namespaced elements pass through untouched (<h:iframe srcdoc>, <h:object data>, <h:meta http-equiv=refresh>, srcset, poster).
| } | ||
| } | ||
|
|
||
| // Sanitise <style> element content: strip external CSS url() references. |
There was a problem hiding this comment.
The CSS url() stripping misses things. I ran these: u\72l(http://e.com/x) (CSS escape) and image-set("http://e.com/x" 1x) both survive in <style>; quoted URLs containing ) get mangled (url("http://e.com/)x") becomes nonex")). Regex CSS handling will keep having holes. Dropping <style> and style altogether is simpler but breaks class-styled SVGs, so either parse the CSS properly or use a maintained library.
| continue; | ||
| } | ||
|
|
||
| // Strip external CSS url() references from inline style attributes. |
There was a problem hiding this comment.
Only the style attribute is sanitised. Presentation attributes aren't: <rect fill="url(http://e.com/x)"/> passes through untouched, same for filter, mask, clip-path, marker-*, cursor. Apply stripCssUrls to every attribute value?
| */ | ||
| class SvgSanitizer { | ||
| /** Elements that can carry or execute active content. */ | ||
| private const FORBIDDEN_ELEMENTS = [ |
There was a problem hiding this comment.
<animate>, <animateTransform> and <animateMotion> aren't forbidden: <animate attributeName="href" values="javascript:…"/> survives (only <set> is removed). Harmless in <img>, but it contradicts the "strips javascript: references" claim.
| * not execute, but they are still sanitised as defence in depth: any document | ||
| * that cannot be parsed safely is dropped entirely. | ||
| */ | ||
| class SvgSanitizer { |
There was a problem hiding this comment.
Was a maintained library (e.g. enshrined/svg-sanitize) or an element/attribute allowlist considered instead of a blocklist? The gaps above are the typical failure mode of blocklists. nit: the class docblock mentions messages and a "CID context", but this is only used by the proxy.
nit: this PR adds a lot of explanatory comments; AGENTS.md asks for essentially none beyond a non-obvious why. The nextcloud/server#62162 reference belongs in the commit message.
| // Detect and sanitise SVG markup so external SVG logos are displayed | ||
| // instead of staying blank. Sanitising also strips any active content in | ||
| // case the response is fetched through a direct (non-<img>) navigation. | ||
| if ($this->svgSanitizer->looksLikeSvg($content)) { |
There was a problem hiding this comment.
The 4-line comment above restates what the code does; per AGENTS.md please drop or cut it down. Also worth a manual check: the inherited default-src 'none' CSP may block <style> / style="" inside the SVG when loaded through <img> (GitHub adds style-src 'unsafe-inline' to raw SVGs for this reason).
| $this->assertFalse($this->sanitizer->looksLikeSvg("\x89PNG\r\n")); | ||
| } | ||
|
|
||
| public function testLooksLikeSvgReturnsFalseForHtmlComment(): void { |
There was a problem hiding this comment.
Please add cases for the ordinary inputs and for the bypasses above: an Illustrator-style file with a public DOCTYPE, a data: image, a leading <!-- … --> comment, non-SVG XML in looksLikeSvg, escaped url(), image-set, fill="url(…)" and <animate>. nit: the new controller tests repeat the 12-line constructor call; a createController() helper would help.
Summary
Received emails containing external SVG images did not render. The message showed only the broken-image placeholder and the image's alt text or name. External images are fetched through the image proxy, which served every response as
tag regardless of content type, but they refuse to render SVG unless it is served as
application/octet-stream. Browsers happily sniff raster formats (PNG/JPEG/GIF) out of animage/svg+xml, so SVG logos and graphics stayed blank.Changes
image/svg+xmlcontent type so browsers render it.SvgSanitizerservice that strips active content (scripts, event handlers, external/javascript: references) and rejects DOCTYPE/entity declarations, as defence in depth against XXE and stored XSS even though theThe first screenshot shows the issue; the second screenshot shows it has been resolved.
Assisted-by: Claude:claude-opus-4-8
Summary by CodeRabbit
Release Notes
New Features
image/svg+xmlcontent type; unsafe/invalid content is replaced with a blocked-image fallback.Bug Fixes
Tests