GHSA-whqh-9pq5-c7r3 phpMyFAQ has a SVG Sanitizer Entity Decoding Depth Limit Bypass Leading to Stored XSS
SummaryThe `SvgSanitizer::decodeAllEntities()` method limits recursive entity decoding to 5 iterations. By wrapping each character of `javascript` in an `href` attribute value with 5 levels of `&amp;` encoding around numeric HTML entities (e.g., `&amp;amp;amp;amp;amp;#106;` for `j`), an attacker can bypass both `isSafe()` detection and `sanitize()` removal. The uploaded SVG is served from the application origin with `image/svg+xml` content type, and the browser's XML parser fully decodes the remaining `&#NNN;` entities, resulting in a clickable `javascript:` link that executes arbitrary JavaScript.Details**Root cause:** `decodeAllEntities()` at `phpmyfaq/src/phpMyFAQ/Helper/SvgSanitizer.php:223-249` limits entity decoding to `maxIterations=5`. Each iteration: (1) decodes `&#NNN;` numeric entities, (2) decodes `&#xHH;` hex entities, (3) calls `html_entity_decode()` which resolves one level of `&amp;` → `&`. With 5 levels of `&amp;` wrapping, all 5 iterations are consumed unwinding the `&amp;` nesting, leaving the final `&#NNN;` numeric entities unresolved.**Code path:**Authenticated user with `FAQ_EDIT` permission uploads SVG via `POST /admin/api/content/images` (`ImageController::upload()` at line 39)File extension is `svg` → `SvgSanitizer::isSafe()` called (line 114)`isSafe()` calls `decodeAllEntities()` — 5 iterations resolve `&amp;` nesting but leave `&#106;&#97;...` (numeric entities for `javascript`)Pattern matching at line 47 (`/href\s*=\s*["\'][\s]*javascript\s*:/i`) does **not** match `&#106;&#97;...``isSafe()` returns **true** — file saved **without any sanitization**SVG served directly by web server from `content/user/images/` with `image/svg+xml` MIME typeBrowser's XML parser decodes `&#106;` → `j`, `&#97;` → `a`, etc., reconstructing `javascript:alert(document.domain)`User clicks the SVG link → JavaScript executes in the phpMyFAQ originThe bypass is even simpler than initially described — no `<script>` decoy tag is needed. Since `isSafe()` itself is bypassed, the file is stored without sanitization and the `sanitize()` code path is never reached.**Relevant code in `decodeAllEntities()`:**PoCUpload an SVG file containing a `javascript:` href where each character of `javascript` is entity-encoded with 5 levels of `&amp;` nesting around numeric entities. No `<script>` decoy is required — `isSafe()` itself is bypassed.**Step 1: Create malicious SVG file (`xss.svg`):****Step 2: Upload via admin image upload endpoint:**Expected response: `{"success": true, ...}` with the uploaded file URL.**Step 3: Access the uploaded SVG directly:**The browser renders the SVG as `image/svg+xml`. The XML parser decodes `&#106;` → `j`, `&#97;` → `a`, etc., producing `href="javascript:alert(document.domain)"`. Clicking the red circle executes JavaScript in the phpMyFAQ origin.Impact**Stored XSS**: Any user (including other administrators) who views and clicks the malicious SVG link has JavaScript executed in their browser within the phpMyFAQ origin.**Session hijacking**: Attacker can steal session cookies and CSRF tokens of other admins.**Privilege escalation**: An editor-level user can execute JavaScript as a super-admin who views the image, potentially gaining full administrative control.**Data exfiltration**: Access to all FAQ content, user data, and configuration accessible through the admin interface.The blast radius is limited by the requirement that a victim must click the link within the SVG. However, the SVG can be crafted to make the clickable area cover the entire visible image (as shown in the PoC), and the attacker controls the visual appearance.Recommended FixThe root cause is that `decodeAllEntities()` can be exhausted by deeply nested `&amp;` encoding. The fix should ensure that after the decoding loop exits, a final pass of numeric/hex entity decoding is performed:Additionally, consider serving uploaded SVG files with `Content-Disposition: attachment` or `Content-Type: application/octet-stream` to prevent browser rendering, as a defense-in-depth measure.
CVSS v3.1 score: 5.4