Astro: Reflected/Stored XSS via unescaped <script>/<style> tag content in JSX/MDX rendering
Affected releases · astro
- 7.x7.0.0 – 7.3.1Fixed in 7.3.2
- 6.x6.0.0 – 6.4.8No fix on this line yet
- 5.x5.0.0 – 5.18.2No fix on this line yet
- 4.x and older0.0.1 – 4.16.19No fix, no longer updated
On 7.x, the newest line:
npm install astro@7.3.2Affected
7.0.0 – 7.3.1
30 releases
Fixed
7.3.2 – 7.4.2
10 releases, latest
Affected
6.0.0 – 6.4.8
41 releases
Affected
5.0.0 – 5.18.2
137 releases
Affected
4.0.0 – 4.16.19
145 releases
Affected
3.0.0 – 3.6.5
48 releases
Affected
2.0.0 – 2.10.15
93 releases
Affected
1.0.0 – 1.9.2
65 releases
Affected
0.0.1 – 0.26.1
162 releases
Is your project affected?
Paste your package.json, or type the range you declare for astro. It is read in your browser and sent nowhere.
What happens
Summary
Astro's JSX and MDX rendering pipeline treated string children of <script> and <style> elements as trusted HTML. When a dynamic value was passed through a JSX expression, such as <script>{value}</script>, an attacker-controlled value containing </script> or </style> could terminate the element and inject arbitrary HTML and JavaScript.
Other JSX expression contexts escaped dynamic strings automatically. This behavior was limited to <script> and <style> children rendered through Astro's astro:jsx renderer, including MDX components.
Details
The affected rendering paths marked all string children of <script> and <style> elements as safe HTML, without distinguishing literal content generated by the compiler from dynamic expression values. An application was vulnerable when attacker-controlled data, such as a query parameter or CMS field, was interpolated directly into one of these elements.
For example, a value containing </script><script>alert(1)</script> passed to <script>{value}</script> was emitted without escaping. The browser interpreted the first closing tag as markup and executed the injected script.
The fix preserves raw rendering for literal script and style content while escaping dynamic string children like other JSX expression values.
Impact
Affected applications could be vulnerable to reflected or stored cross-site scripting. Exploitation required an application to pass attacker-controlled data directly as a dynamic child of a <script> or <style> element in MDX or through the astro:jsx runtime. Successful exploitation allowed arbitrary JavaScript execution in the victim's browser under the application's origin.
Patches
This issue is patched in astro@7.3.2.
Workarounds
Do not interpolate untrusted dynamic values directly into <script> or <style> element children. Applications that cannot upgrade should validate and contextually encode these values before rendering them.
Credit
Reported by @RKasai127.