Title: [Critical][security] mXSS-detection fallback returns unescaped textContent → XSS via all HTML sinks · Issue #162 · bQuery/bQuery · GitHub
Open Graph Title: [Critical][security] mXSS-detection fallback returns unescaped textContent → XSS via all HTML sinks · Issue #162 · bQuery/bQuery
X Title: [Critical][security] mXSS-detection fallback returns unescaped textContent → XSS via all HTML sinks · Issue #162 · bQuery/bQuery
Description: Severity: 🔴 Critical (verified exploit) Location src/security/sanitize-core.ts:356-361 — reached through every HTML sink: $el.html() / $$.html() (src/core/dom.ts, src/core/element.ts:236, src/core/collection.ts:229), .append()/.before()/...
Open Graph Description: Severity: 🔴 Critical (verified exploit) Location src/security/sanitize-core.ts:356-361 — reached through every HTML sink: $el.html() / $$.html() (src/core/dom.ts, src/core/element.ts:236, src/core/...
X Description: Severity: 🔴 Critical (verified exploit) Location src/security/sanitize-core.ts:356-361 — reached through every HTML sink: $el.html() / $$.html() (src/core/dom.ts, src/core/element.ts:236, src/core/...
Opengraph URL: https://github.com/bQuery/bQuery/issues/162
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Critical][security] mXSS-detection fallback returns unescaped textContent → XSS via all HTML sinks","articleBody":"## Severity: 🔴 Critical (verified exploit)\n\n## Location\n[`src/security/sanitize-core.ts:356-361`](../blob/main/src/security/sanitize-core.ts#L356-L361) — reached through **every** HTML sink: `$el.html()` / `$$.html()` ([`src/core/dom.ts`](../blob/main/src/core/dom.ts), [`src/core/element.ts:236`](../blob/main/src/core/element.ts#L236), [`src/core/collection.ts:229`](../blob/main/src/core/collection.ts#L229)), `.append()/.before()/.after()`, and the **default-sanitized `bq-html` directive** ([`src/view/directives/html.ts:15`](../blob/main/src/view/directives/html.ts#L15)).\n\n## Description\nThe anti-mutation-XSS double-parse ends with a fallback that returns raw, **un-escaped** `textContent`:\n\n```ts\nif (firstPass !== secondPass) {\n // Content mutated during re-parse - potential mXSS detected.\n return fragment.textContent ?? ''; // \u003c-- RAW, UNESCAPED\n}\n```\n\nEvery caller assigns the return value of `sanitizeHtmlCore` to `element.innerHTML` / `insertAdjacentHTML`. Everywhere else the function returns *serialized HTML* (safe), but this branch returns *plain text* that is not HTML-escaped. When an attacker supplies the payload HTML-entity-encoded, the parser decodes it into a text node whose data is literally `\u003cimg src=x onerror=alert(1)\u003e`. If the same input also contains a construct that makes serialize→re-parse unstable (trivial via HTML `\u003ctable\u003e` foster-parenting), the \"mXSS detected\" branch fires and hands that raw markup back to the caller, which re-parses it as HTML and executes the payload. **The defense meant to stop mXSS is itself the injection sink.**\n\n## Reproduction (verified against the real `sanitizeHtml` + a real innerHTML sink under happy-dom)\n```ts\nconst payload = '\u003ca\u003e\u003ctable\u003e\u003ca\u003e\u0026lt;img src=x onerror=alert(1)\u0026gt;';\nsanitizeHtmlCore(payload); // =\u003e \"\u003cimg src=x onerror=alert(1)\u003e\"\nhost.innerHTML = sanitizeHtml(payload); // =\u003e live \u003cimg src=\"x\" onerror=\"alert(1)\"\u003e fires\n```\nObserved DOM: `\u003cimg src=\"x\" onerror=\"alert(1)\"\u003e` (img count = 1). The `\u003ca\u003e\u003ctable\u003e\u003ca\u003e…` prefix makes the two serialization passes differ (nested-anchor + table foster-parenting), so the fallback returns `fragment.textContent` = the entity-decoded `\u003cimg …\u003e` string. This is standard HTML behavior (identical across DOMParser and `innerHTML` in all standards browsers), not a happy-dom artifact. Reachable in the **default, no-opt-out** configuration.\n\n## Suggested fix\nThe fallback value is destined for an HTML sink, so it must be HTML-escaped (or empty):\n```ts\nif (firstPass !== secondPass) {\n return escapeHtml(fragment.textContent ?? '');\n}\n```\nMore broadly, reconsider the \"return text on instability\" design in favour of a re-sanitize-to-fixpoint loop (DOMPurify-style), which also avoids silent content destruction. Note the `stripAllTags` path at `sanitize-core.ts:250` returns raw `textContent` too — audit any caller that treats a `stripTags()` result as HTML.\n\n---\n_Filed as part of a full-codebase security \u0026 correctness audit._\n","author":{"url":"https://github.com/JosunLP","@type":"Person","name":"JosunLP"},"datePublished":"2026-07-05T17:02:28.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/162/bQuery/issues/162"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:46dd6101-480d-7150-1907-6c0d02463a7c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AF6C:3BEF12:492588:6B29EB:6A4E0A0A |
| html-safe-nonce | 11dfdbbe5724438494625a74ca8f96e292169277b2676e849644b4ca7c44d7ba |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRjZDOjNCRUYxMjo0OTI1ODg6NkIyOUVCOjZBNEUwQTBBIiwidmlzaXRvcl9pZCI6IjQ5MjE4NjY1MjY4MDg0NzYxNzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d8aafb814343bd0443057d1e8284cd6c11849fa37d641e513041305f74039ad6 |
| hovercard-subject-tag | issue:4813691770 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/bQuery/bQuery/162/issue_layout |
| twitter:image | https://opengraph.githubassets.com/925d88f92a4f995923fb295f2f6d3b134be695adc78d0678d506107dc5b4970c/bQuery/bQuery/issues/162 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/925d88f92a4f995923fb295f2f6d3b134be695adc78d0678d506107dc5b4970c/bQuery/bQuery/issues/162 |
| og:image:alt | Severity: 🔴 Critical (verified exploit) Location src/security/sanitize-core.ts:356-361 — reached through every HTML sink: $el.html() / $$.html() (src/core/dom.ts, src/core/element.ts:236, src/core/... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JosunLP |
| hostname | github.com |
| expected-hostname | github.com |
| None | df0492960db29b4938cb72070351d6b1d0c6c0767b27ceb8394bbf4fcc0223c6 |
| turbo-cache-control | no-preview |
| go-import | github.com/bQuery/bQuery git https://github.com/bQuery/bQuery.git |
| octolytics-dimension-user_id | 256381806 |
| octolytics-dimension-user_login | bQuery |
| octolytics-dimension-repository_id | 1139284244 |
| octolytics-dimension-repository_nwo | bQuery/bQuery |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1139284244 |
| octolytics-dimension-repository_network_root_nwo | bQuery/bQuery |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 51470c353b8a1f52a88d3e3cc2014b17ab8cc1ce |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width