Skip to content

Commit

Permalink
fix ssr xss (fix vuejs#5351)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 authored and awamwang committed Jun 15, 2017
1 parent cde0c59 commit 38ef0d9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/core/vdom/helpers/resolve-async-component.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
/* @flow */

// () => ({
// component: import('./xxx.vue'),
// delay: 200,
// loading: LoadingComponent,
// error: ErrorComponent
// })

import {
warn,
isObject
Expand Down
4 changes: 3 additions & 1 deletion src/platforms/web/server/modules/attrs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* @flow */

import { escape } from 'he'

import {
isBooleanAttr,
isEnumeratedAttr,
Expand Down Expand Up @@ -40,7 +42,7 @@ export function renderAttr (key: string, value: string): string {
} else if (isEnumeratedAttr(key)) {
return ` ${key}="${isFalsyAttrValue(value) || value === 'false' ? 'false' : 'true'}"`
} else if (!isFalsyAttrValue(value)) {
return ` ${key}="${value}"`
return ` ${key}="${typeof value === 'string' ? escape(value) : value}"`
}
return ''
}
3 changes: 2 additions & 1 deletion src/platforms/web/server/modules/class.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* @flow */

import { escape } from 'he'
import { genClassForVnode } from 'web/util/index'

export default function renderClass (node: VNodeWithData): ?string {
const classList = genClassForVnode(node)
if (classList) {
return ` class="${classList}"`
return ` class="${escape(classList)}"`
}
}
4 changes: 3 additions & 1 deletion src/platforms/web/server/modules/style.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* @flow */

import { escape } from 'he'
import { hyphenate } from 'shared/util'
import { getStyle } from 'web/util/style'

Expand All @@ -14,6 +16,6 @@ function genStyleText (vnode: VNode): string {
export default function renderStyle (vnode: VNodeWithData): ?string {
const styleText = genStyleText(vnode)
if (styleText) {
return ` style=${JSON.stringify(styleText)}`
return ` style=${JSON.stringify(escape(styleText))}`
}
}
15 changes: 15 additions & 0 deletions test/ssr/ssr-string.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,21 @@ describe('SSR: renderToString', () => {
})
expect(vm.a).toBe(func)
})

it('should prevent xss in attribtues', () => {
renderVmWithOptions({
data: {
xss: '"><script>alert(1)</script>'
},
template: `
<div>
<a :title="xss" :style="{ color: xss }" :class="[xss]">foo</a>
</div>
`
}, res => {
expect(res).not.toContain(`<script>alert(1)</script>`)
})
})
})

function renderVmWithOptions (options, cb) {
Expand Down

0 comments on commit 38ef0d9

Please sign in to comment.