prevent unsafe replacements of preloaded data etc

This commit is contained in:
Rich Harris
2018-07-14 20:56:05 -04:00
parent 0e3775397f
commit 74acf93c7a
3 changed files with 24 additions and 5 deletions

View File

@@ -360,11 +360,11 @@ function get_page_handler(App: Component, routes: RouteObject[], store_getter: (
}
const page = template()
.replace('%sapper.base%', `<base href="${req.baseUrl}/">`)
.replace('%sapper.scripts%', `<script>${inline_script}</script>${scripts}`)
.replace('%sapper.html%', html)
.replace('%sapper.head%', `<noscript id='sapper-head-start'></noscript>${head}<noscript id='sapper-head-end'></noscript>`)
.replace('%sapper.styles%', (css && css.code ? `<style>${css.code}</style>` : ''));
.replace('%sapper.base%', () => `<base href="${req.baseUrl}/">`)
.replace('%sapper.scripts%', () => `<script>${inline_script}</script>${scripts}`)
.replace('%sapper.html%', () => html)
.replace('%sapper.head%', () => `<noscript id='sapper-head-start'></noscript>${head}<noscript id='sapper-head-end'></noscript>`)
.replace('%sapper.styles%', () => (css && css.code ? `<style>${css.code}</style>` : ''));
res.statusCode = status;
res.end(page);

View File

@@ -0,0 +1,9 @@
$&
<script>
export default {
preload() {
return '$&';
}
};
</script>

View File

@@ -619,6 +619,16 @@ function run({ mode, basepath = '' }) {
assert.equal(name, 'BODY');
});
});
it('replaces %sapper.xxx% tags safely', () => {
return nightmare
.goto(`${base}/unsafe-replacement`)
.init()
.page.html()
.then(html => {
assert.equal(html.indexOf('%sapper'), -1);
});
});
});
describe('headers', () => {