The vulnerability is a cross-site scripting (XSS) issue within Svelte's Server-Side Rendering (SSR) functionality, specifically when using spread attributes ({...props}). The root cause is the improper handling of untrusted data passed into these attributes, which allows attackers to inject malicious event handlers (e.g., onclick) into the rendered HTML.
The analysis of the security patches reveals two primary vulnerable functions on the server side:
-
encode in packages/svelte/src/internal/server/hydratable.js: This function had a classic JavaScript string replacement vulnerability. When serializing data for hydration, it used String.prototype.replace with a replacement value derived from user input. This allowed attackers to use special replacement patterns to break out of the intended structure and inject malicious code.
-
attributes in packages/svelte/src/internal/server/index.js: This function is responsible for rendering HTML attributes. It failed to block attributes with empty names, creating a loophole that could be used to bypass sanitization and inject malicious attributes during SSR.
A third function, get_setters in packages/svelte/src/internal/client/dom/elements/attributes.js, was also patched as a defense-in-depth measure. This client-side function was hardened to prevent spread attributes from modifying dangerous properties like innerHTML, mitigating the risk of DOM-based XSS during hydration.
An attacker exploiting this vulnerability could execute arbitrary JavaScript in the context of the user's browser, provided that the Svelte hydration process does not clean up the malicious element before an event (like mouseover) is triggered.