Skip to main content
Menu
Advisories

vue-i18n escapeParameter sanitizer leaves elements open, so a translation can change how the rest of an SSR page is parsed

GHSA-786J-GFWP-VHM5Published Updated GitHub

Description

When escapeParameter is enabled (escapeParameterHtml in the Legacy API), Vue I18n sanitizes the translated HTML with sanitizeTranslatedHtml(). The sanitizer reads tags in the same way as the HTML tokenizer, but it does not follow how elements open and close. An element that a translation leaves open, or an end tag that closes an element outside the translation, stays in the result.

When the page is rendered on the server (SSR), the rest of the page follows the translation in the same HTML, and the browser parses it as a part of what is still open. An unclosed <template> hides the rest of the page, and an unclosed <select> takes it in. With <object><svg>, <table><svg> or <math><mi><svg>, the rest of the page is parsed as SVG, and a later inline <script> becomes an SVG script, whose character references are decoded before it runs. Text that the page puts in such a script as data, such as &quot; in a serialized state, can then end a JavaScript string, and run in the origin of the application.

Details

sanitizeTranslatedHtml() in packages/shared/src/sanitize.ts turns a tag, a comment, or an element whose content is text (such as <textarea>) into text when it is not closed at the end of the translation (GHSA-9qc3-h48f-7h96). Other elements are kept even when they are not closed, and end tags are kept even when they close no element of the translation.

In the HTML parser, an end tag closes an element only when the element is in scope. <object>, <table>, <template>, MathML <mi> and similar elements are boundaries of the scope, so the end tags of the element that holds the translation, and of the application after it, are ignored while they are open. <svg> inside them keeps the rest of the page in SVG until a start tag such as <div>, <p> or <span> leaves SVG.

Some shapes need no boundary:

  • In a <span>, <a>, <small> or similar element, <div><svg> or <li><svg> keeps <svg> open, because the end tag of the container is ignored while a special element such as <div> is open.
  • In an <a>, </a><svg> closes the container, and opens <svg> after it.
  • In an SVG <foreignObject> of the page, </span></foreignObject> closes the <foreignObject> of the page without opening any element.

An inline <script> in SVG is an SVG script. Its content is parsed as markup, so &quot; becomes " before the script runs.

Before 11.4.21 and 12.0.0-alpha.12, such markup can also come from an interpolation parameter that is not a string, even when the messages are trusted (GHSA-4fx6-3h3f-fh66).

Rendering on the client (v-html, which uses innerHTML) is not affected: the browser parses the translation as a fragment, and closes what is open at its end.

PoC

import { createI18n } from 'vue-i18n'

const i18n = createI18n({
  legacy: false,
  locale: 'en',
  escapeParameter: true,
  messages: {
    en: {
      footer: '© Example<object><svg>',
      greeting: 'Say &quot;-alert(document.cookie)-&quot; hi'
    }
  }
})

The footer is rendered on the server with <small v-html="t('footer')"></small>, and the page puts its translations into an inline state after the application:

<footer><small>© Example<object><svg></small></footer>
<script>window.__STATE__ = {"greeting":"Say &quot;-alert(document.cookie)-&quot; hi"}</script>

</small> and </footer> are ignored while <object> is open, so the <script> is parsed as an SVG script, &quot; becomes ", and alert(document.cookie) runs in the origin of the application. This was confirmed on Chromium, Chrome and WebKit with 11.4.20 and 12.0.0-alpha.11.

Impact

Applications are affected when they render translations as HTML on the server (SSR, including SSG and prerendering) with escapeParameter enabled, and an attacker can change the translations (for example crowdsourced or stored translations), or, before 11.4.21 and 12.0.0-alpha.12, pass a parameter that is not a string.

When an inline script after the translation holds text that the attacker can change (for example the translations in a serialized state), and no element that leaves SVG (such as <div>, <p> or <span>) comes between them, the attacker can run script in the origin of the application. Otherwise, the attacker can still hide the rest of the page (<template>), take it into a <select> or an <object>, or make the inputs of a later form belong to a form that the translation opened (<form action>).

Pages rendered only on the client are not affected.

Patches

Fixed in 11.4.22 and 12.0.0-alpha.13. sanitizeTranslatedHtml() now follows how browsers open and close elements, including SVG and MathML: an end tag that closes no element of the translation is removed, the elements inside an element that an end tag closes are closed before it, and the elements that are still open at the end of the translation are closed. A <form> that the translation does not close is turned into text, and so are <foreignObject>, <desc>, <mi> and similar SVG and MathML elements outside SVG and MathML.

Well-formed translations are not changed.

v9 and v10 are no longer supported and will not be fixed. Please migrate to v11.

Workarounds

  • Close every element in translations that are rendered with v-html, and do not accept translations from untrusted sources.
  • Do not render translations from untrusted sources with v-html on the server. Use component interpolation (<i18n-t>) instead.

References

Affected versions

References

GitHub · intlify/vue-i18n