GHSA-542p-wvx7-72m4: Litestar has HTML Injection Through its CSRF Token
OverviewLitestar instances which use a template engine in conjunction with CSRF protection are vulnerable to HTML Injection which can be escalated to Cross Site Scripting due to the contents of the CSRF cookie being excluded from automatic escaping by the template engine when configured inline with documentation recommendations.We used the latest Litestar version available via PyPI for this disclosure. At the time of writing, that is version 2.21.0 and we have not validated this against the current latest commit on the main branch.Special Configurations RequiredFor a web application to be vulnerable to this issue, it must:Use templates to render the content which is returned to the user (e.g. Jinja, Mako, MiniJinja)Have CSRF protection enabledHave CSRF inputs enabled (i.e. a hidden form field which contains the CSRF token)**Links to relevant documentation for the above configurations:**https://docs.litestar.dev/2/usage/templating.htmlhttps://docs.litestar.dev/latest/usage/middleware/builtin-middleware.html#csrfhttps://docs.litestar.dev/latest/usage/templating.html#adding-csrf-inputsReproduction StepsVisit an application which contains a form, uses templating, has CSRF protection enabled, and which inserts the CSRF token as a hidden `input` field on forms. A proof of concept application demonstrating this configuration is included later in this disclosure for ease of reproduction.Observe that the server sets the `csrftoken` cookie when the page loads.Set the value of the `csrftoken` cookie to `"><h1>HTML Injection Test</h1>`.Refresh the page.Observe that the contents of the cookie is rendered on the page.Exploit CodeThere are two Proof-of-Concepts (PoC) included here. The first demonstrates that arbitrary HTML is injected into the page when the user supplies a malicious `csrftoken` cookie. The second demonstrates how an attacker could deliver an attack using the vulnerability to an unsuspecting user.The proof-of-concept applications can be started using these commands:PoC 1 - Minimum Vulnerable ApplicationThis proof-of-concept demonstrates that a crafted `csrftoken` cookie will be rendered on the vulnerable page as HTML.**poc1.py****test.jinja**Sending the following request to the vulnerable page will result in the HTML included in the `csrftoken` cookie being injected and rendered on the page.PoC 2 - Simulated Attack DeliveryThis proof-of-concept demonstrates how an attacker could deliver an attack using this vulnerability to an unsuspecting user. We are using this example as it provides for easy local reproduction, it is possible that in end applications there may be various ways to trigger this vulnerability which differ from the example provided.First, the user must visit a malicious application (the `/first_site` endpoint in `poc2.py`) which sets the `csrftoken` cookie value to a malicious payload. This app must be hosted on the *same top level domain* as the vulnerable application so that the poisoned cookie will automatically be sent by the user's browser to the vulnerable page.Next, the malicious app *redirects* the user to the vulnerable app (the `/second_site` endpoint in `poc2.py`). The victim's browser will automatically send the poisoned cookie to the vulnerable app. This causes the vulnerable application to unsafely write the cookie content to the page and return it to the user, executing the attack in the victim user's browser**poc2.py****page1.jinja****page2.jinja**ImpactThis vulnerability affects all Litestar instances that use templates along with CSRF protection that has been configured inline with the documentation section of "Adding CSRF inputs" within the "Templating" page. An attacker that can successfully exploit this issue can inject arbitrary HTML tags into the page which is then rendered in the victim user's browser. This includes `script` tags, allowing the attacker to escalate the attack to a Cross Site Scripting attack, thus executing arbitrary JavaScript code in the victim's browser.Depending on the configuration of the site, this could result in the theft of cookies or session tokens. This issue can also allow the attacker to change the appearance of the site. This could enable possible phishing attacks by injecting fake forms into the page or even skimming the information that a user enters into a legitimate form.Resourceshttps://cwe.mitre.org/data/definitions/79.htmlhttps://docs.litestar.dev/2/usage/templating.htmlhttps://docs.litestar.dev/latest/usage/middleware/builtin-middleware.html#csrfhttps://docs.litestar.dev/latest/usage/templating.html#adding-csrf-inputs
CVSS v3.1: 8.1