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

Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions packages/compiler-cli/test/ngtsc/ngtsc_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8648,34 +8648,6 @@ runInEachFileSystem((os: string) => {
expect(trim(jsContents)).toContain(trim(hostBindingsFn));
});

it('should generate sanitizers for URL properties in SVG script fn in Component', () => {
env.write(
'test.ts',
`
import {Component} from '@angular/core';

@Component({
selector: 'test-cmp',
template: \`
<svg>
<script [attr.xlink:href]="attr" [attr.href]="attr"></script>
</svg>
\`,
})
export class TestCmp {
attr = './script.js';
}
`,
);

env.driveMain();

const jsContents = env.getContents('test.js');
expect(jsContents).toContain(
'i0.ɵɵattribute("href", ctx.attr, i0.ɵɵsanitizeResourceUrl, "xlink")("href", ctx.attr, i0.ɵɵsanitizeResourceUrl);',
);
});

it('should not generate sanitizers for URL properties in hostBindings fn in Component', () => {
env.write(
`test.ts`,
Expand Down
6 changes: 0 additions & 6 deletions packages/compiler/src/schema/dom_security_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ export function SECURITY_SCHEMA(): {[k: string]: SecurityContext} {
['object', ['codebase', 'data']],
]);

// The below are for Script SVG
// See: https://developer.mozilla.org/en-US/docs/Web/API/SVGScriptElement/href
registerContext(SecurityContext.RESOURCE_URL, SVG_NAMESPACE, [
['script', ['src', 'href', 'xlink:href']],
]);

// Keep this in sync with SECURITY_SENSITIVE_ELEMENTS in packages/core/src/sanitization/sanitization.ts
// The `unknown` elements refer to cases when we need to validate the input/binding in a directive (host bindings)
// and the directive can be applied to multiple different elements (with different tag names). In this case we generate
Expand Down
25 changes: 11 additions & 14 deletions packages/compiler/src/template_parser/template_preparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const LINK_ELEMENT = 'link';
const LINK_STYLE_REL_ATTR = 'rel';
const LINK_STYLE_HREF_ATTR = 'href';
const LINK_STYLE_REL_VALUE = 'stylesheet';
const STYLE_ELEMENT = 'style';
const SCRIPT_ELEMENT = 'script';
const STYLE_ELEMENTS: ReadonlySet<string> = new Set([':svg:style', 'style']);
const SCRIPT_ELEMENTS: ReadonlySet<string> = new Set([':svg:script', 'script']);
const NG_NON_BINDABLE_ATTR = 'ngNonBindable';
const NG_PROJECT_AS = 'ngProjectAs';

Expand All @@ -25,7 +25,8 @@ export function preparseElement(ast: html.Element): PreparsedElement {
let relAttr: string | null = null;
let nonBindable = false;
let projectAs = '';
ast.attrs.forEach((attr) => {

for (const attr of ast.attrs) {
const lcAttrName = attr.name.toLowerCase();
if (lcAttrName == NG_CONTENT_SELECT_ATTR) {
selectAttr = attr.value;
Expand All @@ -40,15 +41,18 @@ export function preparseElement(ast: html.Element): PreparsedElement {
projectAs = attr.value;
}
}
});
selectAttr = normalizeNgContentSelect(selectAttr);
}

// Normalize selector to '*' if empty
selectAttr ||= '*';

const nodeName = ast.name.toLowerCase();
let type = PreparsedElementType.OTHER;
if (isNgContent(nodeName)) {
type = PreparsedElementType.NG_CONTENT;
} else if (nodeName == STYLE_ELEMENT) {
} else if (STYLE_ELEMENTS.has(nodeName)) {
type = PreparsedElementType.STYLE;
} else if (nodeName == SCRIPT_ELEMENT) {
} else if (SCRIPT_ELEMENTS.has(nodeName)) {
type = PreparsedElementType.SCRIPT;
} else if (nodeName == LINK_ELEMENT && relAttr == LINK_STYLE_REL_VALUE) {
type = PreparsedElementType.STYLESHEET;
Expand All @@ -73,10 +77,3 @@ export class PreparsedElement {
public projectAs: string,
) {}
}

function normalizeNgContentSelect(selectAttr: string | null): string {
if (selectAttr === null || selectAttr.length === 0) {
return '*';
}
return selectAttr;
}
3 changes: 1 addition & 2 deletions packages/core/src/sanitization/sanitization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ const RESOURCE_MAP: Record<string, Record<string, true | undefined> | undefined>
'frame': {'src': true},
'iframe': {'src': true},
'media': {'src': true},
'script': {'src': true, 'href': true, 'xlink:href': true},
':svg:script': {'src': true, 'href': true, 'xlink:href': true},

'base': {'href': true},
'link': {'href': true},
'object': {'data': true, 'codebase': true},
Expand Down
14 changes: 14 additions & 0 deletions packages/core/test/acceptance/security_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,3 +902,17 @@ describe('Component host element validation', () => {
}
});
});

describe('SVG <script> bindings', () => {
it(`should remove svg <script> element`, () => {
@Component({
template: `<svg><script src="https://bad.com/script.js"></script></svg>`,
changeDetection: ChangeDetectionStrategy.Eager,
})
class TestCmp {}

const fixture = TestBed.createComponent(TestCmp);
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('script')).toBeFalsy();
});
});
8 changes: 4 additions & 4 deletions packages/core/test/sanitization/sanitization_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('sanitization', () => {
[SecurityContext.RESOURCE_URL, ɵɵsanitizeResourceUrl],
]);
Object.entries(schema).forEach(([key, context]) => {
if (context === SecurityContext.URL || SecurityContext.RESOURCE_URL) {
if (context === SecurityContext.URL || context === SecurityContext.RESOURCE_URL) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😱

const [tag, prop] = key.split('|');
const contexts = contextsByProp.get(prop) || new Set<number>();
contexts.add(context);
Expand All @@ -137,7 +137,7 @@ describe('sanitization', () => {
expect(getUrlSanitizer('IFRAME', 'SRC')).toEqual(ɵɵsanitizeResourceUrl);
expect(getUrlSanitizer('IFRAME', 'src')).toEqual(ɵɵsanitizeResourceUrl);
expect(getUrlSanitizer('iframe', 'SRC')).toEqual(ɵɵsanitizeResourceUrl);
expect(getUrlSanitizer('ScRiPt', 'xLiNk:HrEf')).toEqual(ɵɵsanitizeResourceUrl);
expect(getUrlSanitizer('ScRiPt', 'xLiNk:HrEf')).toEqual(ɵɵsanitizeUrl);
expect(getUrlSanitizer('A', 'HREF')).toEqual(ɵɵsanitizeUrl);
});

Expand All @@ -150,8 +150,8 @@ describe('sanitization', () => {

expect(() => ɵɵsanitizeUrlOrResourceUrl('http://server', 'iframe', 'SRC')).toThrowError(ERROR);

expect(() => ɵɵsanitizeUrlOrResourceUrl('http://server', 'ScRiPt', 'xLiNk:HrEf')).toThrowError(
ERROR,
expect(ɵɵsanitizeUrlOrResourceUrl('javascript:true', 'ScRiPt', 'xLiNk:HrEf')).toEqual(
'unsafe:javascript:true',
);

expect(ɵɵsanitizeUrlOrResourceUrl('javascript:true', 'A', 'HREF')).toEqual(
Expand Down
Loading