/

CVE-2026-48060

Severity:

HIGH RISK

(8.1)

XSS via CSRF Cookie Handling (CWE-79) in Litestar

CVE-2026-48060

Deferred

Vulnerability type

Category:

Cross-Site Scripting (XSS)

CWE:

CWE-79

Impact:

Client-Side Code Execution / Session Compromise

Severity:

HIGH RISK

(8.1)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

Summary

Litestar applications that combine template rendering with CSRF protection may be vulnerable to HTML injection that escalates to Cross-Site Scripting (XSS).

When the CSRF cookie value is rendered inline as documented and bypasses automatic escaping, an attacker controlling page content or parameters can inject malicious script into the rendered HTML.

Litestar applications that combine template rendering with CSRF protection may be vulnerable to HTML injection that escalates to Cross-Site Scripting (XSS).

When the CSRF cookie value is rendered inline as documented and bypasses automatic escaping, an attacker controlling page content or parameters can inject malicious script into the rendered HTML.

Affected context

This vulnerability occurs in Litestar-based web applications that:

  • Use a template engine to render HTML responses, and
  • Enable CSRF protection that exposes a CSRF token via cookie, and
  • Render the CSRF token inline following the documented configuration pattern that disables or bypasses automatic escaping for that value.

The risk is higher when templates incorporate user-controlled data near the CSRF token or when layout patterns cause token output to be concatenated with untrusted content.

This vulnerability occurs in Litestar-based web applications that:

  • Use a template engine to render HTML responses, and
  • Enable CSRF protection that exposes a CSRF token via cookie, and
  • Render the CSRF token inline following the documented configuration pattern that disables or bypasses automatic escaping for that value.

The risk is higher when templates incorporate user-controlled data near the CSRF token or when layout patterns cause token output to be concatenated with untrusted content.

Exploit preconditions

  • Attack surface: Browser-based clients consuming HTML pages rendered by a Litestar application.
  • Required access: Ability to influence or inject content into a rendered page (e.g., via query parameters, form fields, stored content, or other user-controlled data that reaches the template).
  • Trigger point: Rendering of a template that emits the CSRF cookie value in an unescaped or partially unescaped context in the HTML.
  • Why exploitation is possible: The template engine configuration effectively exempts the CSRF cookie content from automatic context-aware escaping. When this unescaped output is combined with attacker-controlled data in the same HTML or JavaScript context, HTML injection can escalate into XSS execution in the victim's browser.
  • Attack surface: Browser-based clients consuming HTML pages rendered by a Litestar application.
  • Required access: Ability to influence or inject content into a rendered page (e.g., via query parameters, form fields, stored content, or other user-controlled data that reaches the template).
  • Trigger point: Rendering of a template that emits the CSRF cookie value in an unescaped or partially unescaped context in the HTML.
  • Why exploitation is possible: The template engine configuration effectively exempts the CSRF cookie content from automatic context-aware escaping. When this unescaped output is combined with attacker-controlled data in the same HTML or JavaScript context, HTML injection can escalate into XSS execution in the victim's browser.

Why this happens

The underlying issue stems from improper output encoding in the template layer, mapping directly to CWE-79: Improper Neutralization of Input During Web Page Generation (Cross-site Scripting).

In affected Litestar configurations, the CSRF cookie is inserted into templates in a way that bypasses the template engine's default escaping mechanisms, often relying on implicit trust in framework-provided token values. This breaks the invariant that all dynamic data in HTML and script contexts must be contextually escaped, especially when combined with or rendered adjacent to other user-controlled inputs. The result is an unsafe pattern where framework-generated values appear safe but can still form part of an exploitable HTML or JavaScript injection chain.

The underlying issue stems from improper output encoding in the template layer, mapping directly to CWE-79: Improper Neutralization of Input During Web Page Generation (Cross-site Scripting).

In affected Litestar configurations, the CSRF cookie is inserted into templates in a way that bypasses the template engine's default escaping mechanisms, often relying on implicit trust in framework-provided token values. This breaks the invariant that all dynamic data in HTML and script contexts must be contextually escaped, especially when combined with or rendered adjacent to other user-controlled inputs. The result is an unsafe pattern where framework-generated values appear safe but can still form part of an exploitable HTML or JavaScript injection chain.

Potential impact

  • Theft of session identifiers or other sensitive cookies accessible via JavaScript
  • Execution of arbitrary JavaScript in the context of the affected application's origin
  • Unauthorized actions performed on behalf of authenticated users (CSRF-like behavior via XSS)
  • Defacement of application pages or injection of malicious content (e.g., phishing forms)
  • Pivoting into broader account takeover or data exfiltration if the web app exposes privileged client-side APIs
  • Theft of session identifiers or other sensitive cookies accessible via JavaScript
  • Execution of arbitrary JavaScript in the context of the affected application's origin
  • Unauthorized actions performed on behalf of authenticated users (CSRF-like behavior via XSS)
  • Defacement of application pages or injection of malicious content (e.g., phishing forms)
  • Pivoting into broader account takeover or data exfiltration if the web app exposes privileged client-side APIs

General mitigation guidance

Mitigation focuses on restoring strict output encoding guarantees and minimizing trust in framework-managed tokens when rendered in templates:

  • Ensure that all template variables, including CSRF tokens, are subject to context-appropriate escaping (HTML, attribute, URL, JavaScript) with auto-escaping enabled by default.
  • Avoid marking CSRF token values or related template variables as "safe" or disabling escaping directives, even if the values are framework-generated.
  • Isolate security tokens from user-controlled data in templates; do not concatenate them into the same inline script or HTML attribute string as attacker-influenced input.
  • Prefer dedicated templating helpers or tags that generate complete, pre-escaped CSRF form fields or meta tags rather than manually embedding raw token values.
  • Apply a principle of least privilege at the client side: do not expose more token data in templates than needed, and avoid reusing CSRF tokens for non-CSRF purposes.

Mitigation focuses on restoring strict output encoding guarantees and minimizing trust in framework-managed tokens when rendered in templates:

  • Ensure that all template variables, including CSRF tokens, are subject to context-appropriate escaping (HTML, attribute, URL, JavaScript) with auto-escaping enabled by default.
  • Avoid marking CSRF token values or related template variables as "safe" or disabling escaping directives, even if the values are framework-generated.
  • Isolate security tokens from user-controlled data in templates; do not concatenate them into the same inline script or HTML attribute string as attacker-influenced input.
  • Prefer dedicated templating helpers or tags that generate complete, pre-escaped CSRF form fields or meta tags rather than manually embedding raw token values.
  • Apply a principle of least privilege at the client side: do not expose more token data in templates than needed, and avoid reusing CSRF tokens for non-CSRF purposes.

How teams detect issues like this

Effective detection usually involves a combination of code review and automated analysis:

  • Static analysis rules targeting templates for unescaped output in HTML, attribute, or JavaScript contexts, especially where CSRF-related variables or cookies are referenced.
  • Search-based checks (e.g., grep) for template constructs that disable auto-escaping or mark CSRF tokens as "safe," "raw," or equivalent in the chosen templating language.
  • Security-focused code reviews of authentication and CSRF-handling templates to verify that framework-recommended patterns do not bypass escaping.
  • Dynamic security testing (DAST) or browser-based fuzzing that injects XSS payload markers into any user-controllable input that reaches CSRF-protected views, observing rendered HTML for reflection.
  • Integration of templating-security linters or SAST tools into CI pipelines to flag new instances of unescaped output related to security-critical tokens.

Effective detection usually involves a combination of code review and automated analysis:

  • Static analysis rules targeting templates for unescaped output in HTML, attribute, or JavaScript contexts, especially where CSRF-related variables or cookies are referenced.
  • Search-based checks (e.g., grep) for template constructs that disable auto-escaping or mark CSRF tokens as "safe," "raw," or equivalent in the chosen templating language.
  • Security-focused code reviews of authentication and CSRF-handling templates to verify that framework-recommended patterns do not bypass escaping.
  • Dynamic security testing (DAST) or browser-based fuzzing that injects XSS payload markers into any user-controllable input that reaches CSRF-protected views, observing rendered HTML for reflection.
  • Integration of templating-security linters or SAST tools into CI pipelines to flag new instances of unescaped output related to security-critical tokens.

Sources

SourceLink
github.comgithub.com/litestar-org/litestar/releases/tag/v2.20.0
github.comgithub.com/litestar-org/litestar/security/advisories/GHSA-542p-wvx7-72m4

Frequently asked

questions

Yes. An attacker who can cause a victim to visit a crafted URL or view attacker-controlled content that flows into the vulnerable template can remotely exploit the issue through the victim's browser.

Typically, the attacker only needs the ability to influence data that is reflected or stored and later rendered by the application in views that include the unescaped CSRF token. No direct server access is required; exploitation occurs via normal web requests.

The safest pattern is to ensure that all CSRF-related values rendered in templates go through the template engine's standard auto-escaping, and to remove any configuration or template directives that treat these values as pre-sanitized. Using dedicated helper tags or components that generate fully escaped CSRF form fields is preferable to manual insertion.

Adopt a strict templating security model where every dynamic value is escaped according to its rendering context by default, and disallow "safe"/raw output in project guidelines. Combine this with security-focused code review for authentication and CSRF flows, template linting rules for unescaped output, and automated tests that exercise pages with injected XSS markers to catch regressions early.

Yes. An attacker who can cause a victim to visit a crafted URL or view attacker-controlled content that flows into the vulnerable template can remotely exploit the issue through the victim's browser.

Typically, the attacker only needs the ability to influence data that is reflected or stored and later rendered by the application in views that include the unescaped CSRF token. No direct server access is required; exploitation occurs via normal web requests.

The safest pattern is to ensure that all CSRF-related values rendered in templates go through the template engine's standard auto-escaping, and to remove any configuration or template directives that treat these values as pre-sanitized. Using dedicated helper tags or components that generate fully escaped CSRF form fields is preferable to manual insertion.

Adopt a strict templating security model where every dynamic value is escaped according to its rendering context by default, and disallow "safe"/raw output in project guidelines. Combine this with security-focused code review for authentication and CSRF flows, template linting rules for unescaped output, and automated tests that exercise pages with injected XSS markers to catch regressions early.

Find out what's already
exploitable in your codebase.