Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 69a7470

Browse files
authored
Merge 0d438f5 into eb5c5d2
2 parents eb5c5d2 + 0d438f5 commit 69a7470

File tree

7 files changed

+49
-0
lines changed

7 files changed

+49
-0
lines changed

.changeset/red-cows-trade.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'lit-html': patch
3+
---
4+
5+
Added an additional check to prevent spoofing of internal lit types in data bindings.

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ lerna-debug.log
88
*.tgz
99
*.tsbuildinfo
1010
.DS_Store
11+
.vscode/
1112

1213
packages/benchmarks/generated/
1314
packages/benchmarks/generator/build/

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ lerna-debug.log
33
*.tgz
44
*.tsbuildinfo
55
.DS_Store
6+
.vscode/

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ lerna-debug.log
88
*.tgz
99
*.tsbuildinfo
1010
.DS_Store
11+
.vscode/
1112

1213
packages/benchmarks/generated/
1314
packages/benchmarks/generator/build/

packages/lit-html/src/lit-html.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,22 @@ const getTemplateHtml = (
653653
const htmlResult: string | TrustedHTML =
654654
html + (strings[l] || '<?>') + (type === SVG_RESULT ? '</svg>' : '');
655655

656+
// A security check to prevent spoofing of Lit template results.
657+
// In the future, we may be able to replace this with Array.isTemplateObject,
658+
// though we might need to make that check inside of the html and svg
659+
// functions, because precompiled templates don't come in as
660+
// TemplateStringArray objects.
661+
if (!Array.isArray(strings) || !strings.hasOwnProperty('raw')) {
662+
let message = 'invalid template strings array';
663+
if (DEV_MODE) {
664+
message =
665+
`Internal Error: expected template strings to be an array ` +
666+
`with a 'raw' field. Please file a bug at ` +
667+
`https://github.com/lit/lit/issues/new?template=bug_report.md ` +
668+
`and include information about your build tooling, if any.`;
669+
}
670+
throw new Error(message);
671+
}
656672
// Returned as an array for terseness
657673
return [
658674
policy !== undefined

packages/lit-html/src/static.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ export const withStatic =
105105
const key = staticStrings.join('$$lit$$');
106106
strings = stringsCache.get(key)!;
107107
if (strings === undefined) {
108+
// Beware: in general this pattern is unsafe, and doing so may bypass
109+
// lit's security checks and allow an attacker to execute arbitrary
110+
// code and inject arbitrary content.
111+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
112+
(staticStrings as any).raw = staticStrings;
108113
stringsCache.set(
109114
key,
110115
(strings = staticStrings as unknown as TemplateStringsArray)

packages/lit-html/src/test/lit-html_test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3062,6 +3062,26 @@ suite('lit-html', () => {
30623062
});
30633063
});
30643064

3065+
test(`don't render simple spoof template results`, () => {
3066+
const spoof = {
3067+
['_$litType$']: 1,
3068+
strings: ['<div>spoofed string</div>'],
3069+
values: [],
3070+
};
3071+
const template = html`<div>${spoof}</div>`;
3072+
let threwError = false;
3073+
try {
3074+
render(template, container);
3075+
} catch {
3076+
threwError = true;
3077+
}
3078+
assert.equal(stripExpressionMarkers(container.innerHTML), '');
3079+
assert.isTrue(
3080+
threwError,
3081+
`Expected an error when rendering a spoofed template result`
3082+
);
3083+
});
3084+
30653085
const warningsSuiteFunction = DEV_MODE ? suite : suite.skip;
30663086

30673087
warningsSuiteFunction('warnings', () => {

0 commit comments

Comments
 (0)