Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

escapeParameterHtml flag: Don't escape ampersand #1019 #1020

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ export function looseEqual (a: any, b: any): boolean {
*/
function escapeHtml(rawText: string): string {
return rawText
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
Expand Down
13 changes: 7 additions & 6 deletions test/unit/escape_parameter_html.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ describe('escapeParameterHtml', () => {
messages,
escapeParameterHtml: true
})
assert(i18n.t('nameformat', { key: '<&"\'>' }) === '&lt;&amp;&quot;&apos;&gt;')
assert(i18n.t('listformat', ['<&"\'>']) === '&lt;&amp;&quot;&apos;&gt;')
assert(i18n.tc('nameformat', 1, { key: '<&"\'>' }).toString() === '&lt;&amp;&quot;&apos;&gt;')
assert(i18n.tc('listformat', 1, ['<&"\'>']).toString() === '&lt;&amp;&quot;&apos;&gt;')
// We should not escape the ampersand (&).
assert(i18n.t('nameformat', { key: '<&"\'>' }) === '&lt;&&quot;&apos;&gt;')
assert(i18n.t('listformat', ['<&"\'>']) === '&lt;&&quot;&apos;&gt;')
assert(i18n.tc('nameformat', 1, { key: '<&"\'>' }).toString() === '&lt;&&quot;&apos;&gt;')
assert(i18n.tc('listformat', 1, ['<&"\'>']).toString() === '&lt;&&quot;&apos;&gt;')
})
it('Replacement parameters are not escaped when escapeParameterHtml: undefined.', () => {
const i18n = new VueI18n({
locale: 'en',
messages,
})
assert(i18n.t('nameformat', { key: '<&"\'>' }) === '<&"\'>')
assert(i18n.t('listformat', ['<&"\'>']) === '<&"\'>')
assert(i18n.t('nameformat', { key: '<"\'>' }) === '<"\'>')
assert(i18n.t('listformat', ['<"\'>']) === '<"\'>')

})
})
2 changes: 1 addition & 1 deletion vuepress/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ If `escapeParameterHtml` is configured as true then interpolation parameters are
This is useful when translation output is used in `v-html` and the translation resource contains html markup (e.g. `<b>`
around a user provided value). This usage pattern mostly occurs when passing precomputed text strings into UI compontents.

The escape process involves replacing the following symbols with their respective HTML character entities: `<`, `>`, `"`, `'`, `&`.
The escape process involves replacing the following symbols with their respective HTML character entities: `<`, `>`, `"`, `'`.

Setting `escapeParameterHtml` as true should not break existing functionality but provides a safeguard against a subtle
type of XSS attack vectors.
Expand Down