Summary
YOURLS contains a stored cross-site scripting vulnerability in its statistics charts, where attacker-controlled HTTP Referer data is inserted into inline JavaScript without appropriate escaping. An unauthenticated attacker may poison statistics for an existing short URL, causing JavaScript to execute when an administrator or other viewer opens the affected statistics page.
Affected context
The vulnerability occurs in YOURLS deployments that collect redirect referrer data and display derived domains in statistics charts. Risk is present when a crafted referrer is accepted, stored, and later incorporated into a statistics page's inline JavaScript without JavaScript-string encoding. Administrators and users with access to public or authenticated statistics pages may be affected when viewing poisoned data.
Exploit preconditions
- Attack surface: HTTP redirect handling and statistics-page rendering
- Required access: No YOURLS account is generally required; the attacker needs a way to cause a short URL to receive a crafted referrer
- Trigger point: An administrator or statistics viewer opens the affected chart page
- Why exploitation is possible: Stored referrer-derived values are inserted into inline JavaScript without context-appropriate escaping
Why this happens
The underlying bug is a failure to treat HTTP Referer data as untrusted content throughout its storage and presentation lifecycle. Although the value is processed as a URL or domain, the chart-generation logic concatenates it into JavaScript source without applying JavaScript-string escaping or using a safe data-transfer mechanism. This behavior maps to CWE-79: Improper Neutralization of Input During Web Page Generation, specifically stored cross-site scripting caused by unsafe output encoding in a script context.
Potential impact
- Attacker-controlled JavaScript may execute in the YOURLS origin
- Administrator-visible information may be exposed
- API signature tokens or other sensitive same-origin data could be accessed
- Privileged same-origin actions could be performed through a victim's session
- Statistics and related application data may be modified or poisoned
General mitigation guidance
Mitigation generally focuses on preserving untrusted-data boundaries and preventing dynamic values from becoming executable script.
Common defensive patterns include:
- Apply context-appropriate JavaScript-string encoding to every untrusted value rendered into script
- Prefer JSON serialization into non-executable data elements or server-rendered text over string concatenation
- Validate and normalize referrer-derived domains using an allowlisted schema before storage and display
- Avoid inline JavaScript where secure event handling and external scripts are practical
- Use restrictive Content Security Policy controls as defense in depth
- Limit API-token exposure and run administrative sessions with least privilege
- Treat stored analytics and logging fields as untrusted even when they appear to contain URLs or domains
How teams detect issues like this
High-level detection can include:
- Static analysis searches for concatenation of request headers or stored database values into JavaScript, HTML attributes, or script blocks
- Code review checks for missing output encoding at each rendering context
- Tests that place benign marker strings containing script-context metacharacters in referrer and analytics fields, then verify they remain inert
- Dynamic application testing of redirect logging and statistics pages under both authenticated and unauthenticated viewing paths
- Content Security Policy reporting to identify unexpected inline script execution or violations
- Policy enforcement requiring templating encoders, safe JSON serializers, and review of inline-script changes
- Automated security review tools may help identify common reflected and stored XSS patterns, but should be supplemented with context-aware testing

