XSS via unescaped object values in JSX and MDX elements
Description
Astro escapes dynamic values in templates before rendering them as HTML, but only when the value was a string. Other values that turn into text, such as an Error, a String object, a Node.js Buffer, or any object with a custom toString(), were converted to a string and rendered without escaping.
This affected .astro templates, JSX and MDX. In JSX and MDX it also affected set:text, which is documented to escape its value.
For example:
---
let error;
try {
await loadPost(Astro.url.searchParams.get('id'));
} catch (e) {
error = e; // e.g. new Error(`Post "${id}" not found`)
}
---
<p>{error}</p>rendered <p>Error: Post "<script>alert(1)</script>" not found</p> as live HTML. In JSX and MDX, {new String(value)}, {Buffer.from(value)} and <div set:text={new String(value)} /> were rendered unescaped in the same way.
Impact
Reflected or stored cross-site scripting, but only in applications that render untrusted input inside a non-string value, such as an error message built from a request parameter or a buffer read from user-supplied content. String values were always escaped.
Rendering a Uint8Array or Buffer in a .astro template, and passing a Response, stream or iterable to set:html, intentionally writes the raw content and is unchanged. Only use these with trusted content.
Affected versions
astro <= 7.4.13 and @astrojs/runtime <= 0.1.2.
Patches
Fixed in astro 7.4.14 and @astrojs/runtime 0.1.3. Non-string values in template expressions, JSX, MDX and set:text are now converted to a string and escaped like strings.
Workarounds
Upgrade to astro 7.4.14 or later. Before upgrading, convert values to strings before rendering them, for example {String(error)} or {error.message}, so that Astro escapes them.
Credits
Reported by @HellboundAJ.