Skip to content

Fix stored XSS via uploaded filenames and widen pasta ID keyspace - #378

Open
presto8 wants to merge 2 commits into
szabodanika:masterfrom
presto8:security/xss-idspace-fixes
Open

presto8 wants to merge 2 commits into
szabodanika:masterfrom
presto8:security/xss-idspace-fixes

Conversation

@presto8

@presto8 presto8 commented Nov 13, 2026

Copy link
Copy Markdown

Summary

Two security issues found while auditing a MicroBin instance, confirmed against a local build of this project.

1. Stored XSS via uploaded filenames

upload.html disables Askama's auto-escaping (escape = "none"). Paste content has a manual content_escaped() call, but the filename accessors (name(), display_name(), extension()) did not, and filenames are fully attacker-controlled (only .replace(' ', '_') is applied — nothing HTML-related).

Two independently confirmed exploitation paths:

  • Tag injection: a filename with a tab character instead of a space (the app only filters literal spaces) survives into the raw HTML, e.g. <svg[TAB]onload=alert(1)>.txt renders as a live <svg> element with a working onload handler.
  • Attribute breakout (multi-file/gallery view): a filename like x" onmouseover="alert(1)" foo=" closes the existing title="..." attribute early and injects a real onmouseover handler — browsers recover from the resulting (technically invalid per spec) markup by starting a new attribute, so no whitespace is even required for this variant.

Fix: added name_escaped()/display_name_escaped()/extension_escaped() accessors on PastaFile using html_escape::encode_quoted_attribute (escapes &, <, >, ", and '), used at every render site in upload.html and file_card.html, including the one spot embedding a filename inside a single-quoted JS string literal. Both exploitation paths were re-tested after the fix and confirmed neutralized.

2. Pasta ID keyspace was only 16 bits

id: rand::thread_rng().gen::<u16>() as u64,

Only 65,536 possible IDs. This undermines the "Unlisted" privacy level, whose entire security model is "can't be found unless you know the unique identifier" — 65,536 is trivially enumerable by brute force. Confirmed empirically by decoding real-world slugs (e.g. falcon-snail-wolf -> 37367), all well within the 16-bit range. It also meant two unrelated uploads could silently collide onto the same ID, since there was no uniqueness check at creation.

Fix: widened to u32 (~4.3 billion possible values — still fits cleanly within the existing 6-character minimum hashid length), and added a collision-avoidance loop that re-rolls the ID if it happens to match an existing pasta, performed immediately after construction and before any ID-dependent work (encryption keyed by id, attachment file paths) happens.

Testing

  • cargo build and cargo test pass.
  • Both XSS payloads manually re-verified against a local build pre/post-fix (raw tag injection and quote-breakout), confirmed neutralized post-fix.
  • New IDs verified to span the full u32 range (e.g. sloth-fox-cat-zebra-pigeon-wasp -> 3582195631), vs. the old scheme which could never exceed 65535.

Preston Hunt added 2 commits November 13, 2026 11:01
upload.html disables Askama's auto-escaping (escape = "none"), and while
paste content already goes through a manual content_escaped() call, the
uploaded filename accessors (name(), display_name(), extension()) did not,
and are fully attacker-controlled (only '.replace(' ', '_')' is applied,
nothing HTML-related).

This allowed two independently confirmed exploitation paths against a
local build:

- Tag injection: a filename containing a tab character instead of a
  space (the app only filters literal spaces) survives into the raw
  HTML, e.g. "<svg\tonload=alert(1)>.txt" renders as a live <svg> element
  with a real onload handler.
- Attribute breakout in the multi-file/gallery view: a filename like
  'x" onmouseover="alert(1)" foo="' closes the existing title="..."
  attribute early and injects a real onmouseover handler onto the
  element - all major browsers recover from the resulting (technically
  invalid) markup by starting a new attribute, so no whitespace is even
  required for this variant.

Fix: add name_escaped()/display_name_escaped()/extension_escaped()
accessors on PastaFile using html_escape::encode_quoted_attribute (which
escapes &, <, >, ", and ') and use them at every render site in
upload.html and file_card.html, including the one spot embedding a
filename inside a single-quoted JS string literal.

Both exploitation paths were re-tested after the fix and confirmed
neutralized (payload now renders as inert escaped text).
IDs were generated as rand::thread_rng().gen::<u16>() as u64 - only
65,536 possible values. This undermines the 'Unlisted' privacy level,
whose entire security model is 'can't be found unless you know the
unique identifier': 65,536 is trivially enumerable by brute force.
Confirmed empirically by decoding real-world slugs generated during
testing (e.g. falcon-snail-wolf -> 37367), all well within the 16-bit
range.

It also meant two unrelated uploads could silently collide onto the
same ID, since there was no uniqueness check at creation.

Widen to u32 (~4.3 billion possible values), which still fits cleanly
within the existing 6-character minimum hashid length, and add a
collision-avoidance loop that re-rolls the ID if it happens to match an
existing pasta, performed immediately after construction and before any
ID-dependent work (encryption keyed by id, attachment file paths)
happens.

Verified: new slugs decode to values across the full u32 range (e.g.
sloth-fox-cat-zebra-pigeon-wasp -> 3582195631), vs. the old scheme which
could never exceed 65,535.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant