Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,48 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDE
},
}]
}] });
export class HostBindingCustomSrcdocDir {
evil = 'evil';
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: HostBindingCustomSrcdocDir, deps: [], target: i0.ɵɵFactoryTarget.Directive });
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "0.0.0-PLACEHOLDER", type: HostBindingCustomSrcdocDir, isStandalone: true, selector: "safe-srcdoc-carrier", host: { properties: { "attr.srcdoc": "evil" } }, ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: HostBindingCustomSrcdocDir, decorators: [{
type: Directive,
args: [{
selector: 'safe-srcdoc-carrier',
host: {
'[attr.srcdoc]': 'evil',
},
}]
}] });
export class HostBindingCustomSrcDir {
evil = 'evil';
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: HostBindingCustomSrcDir, deps: [], target: i0.ɵɵFactoryTarget.Directive });
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "0.0.0-PLACEHOLDER", type: HostBindingCustomSrcDir, isStandalone: true, selector: "safe-src-carrier", host: { properties: { "attr.src": "evil" } }, ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: HostBindingCustomSrcDir, decorators: [{
type: Directive,
args: [{
selector: 'safe-src-carrier',
host: {
'[attr.src]': 'evil',
},
}]
}] });
export class HostBindingCustomDataDir {
evil = 'evil';
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: HostBindingCustomDataDir, deps: [], target: i0.ɵɵFactoryTarget.Directive });
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "0.0.0-PLACEHOLDER", type: HostBindingCustomDataDir, isStandalone: true, selector: "safe-data-carrier", host: { properties: { "attr.data": "evil" } }, ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: HostBindingCustomDataDir, decorators: [{
type: Directive,
args: [{
selector: 'safe-data-carrier',
host: {
'[attr.data]': 'evil',
},
}]
}] });

/****************************************************************************************************
* PARTIAL FILE: sanitization.d.ts
Expand Down Expand Up @@ -1008,6 +1050,21 @@ export declare class HostBindingSvgAnimateDir {
static ɵfac: i0.ɵɵFactoryDeclaration<HostBindingSvgAnimateDir, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<HostBindingSvgAnimateDir, "animateMotion[hostBindingSvgAnimateDir]", never, {}, {}, never, never, true, never>;
}
export declare class HostBindingCustomSrcdocDir {
evil: string;
static ɵfac: i0.ɵɵFactoryDeclaration<HostBindingCustomSrcdocDir, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<HostBindingCustomSrcdocDir, "safe-srcdoc-carrier", never, {}, {}, never, never, true, never>;
}
export declare class HostBindingCustomSrcDir {
evil: string;
static ɵfac: i0.ɵɵFactoryDeclaration<HostBindingCustomSrcDir, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<HostBindingCustomSrcDir, "safe-src-carrier", never, {}, {}, never, never, true, never>;
}
export declare class HostBindingCustomDataDir {
evil: string;
static ɵfac: i0.ɵɵFactoryDeclaration<HostBindingCustomDataDir, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<HostBindingCustomDataDir, "safe-data-carrier", never, {}, {}, never, never, true, never>;
}

/****************************************************************************************************
* PARTIAL FILE: security_sensitive_constant_attributes.js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
hostBindings: function HostBindingLinkDir_HostBindings(rf, ctx) {
if (rf & 2) {
$r3$.ɵɵdomProperty("innerHTML", ctx.evil, $r3$.ɵɵsanitizeHtml)("href", ctx.evil, $r3$.ɵɵsanitizeUrl);
$r3$.ɵɵdomProperty("innerHTML", ctx.evil, $r3$.ɵɵsanitizeHtml)("href", ctx.evil, $r3$.ɵɵsanitizeUrlOrResourceUrl);
$r3$.ɵɵattribute("style", ctx.evil, $r3$.ɵɵsanitizeStyle);
}
}
hostBindings: function HostBindingImageDir_HostBindings(rf, ctx) {
if (rf & 2) {
i0.ɵɵdomProperty("innerHTML", ctx.evil, i0.ɵɵsanitizeHtml)("src", ctx.nonEvil, i0.ɵɵsanitizeUrl);
i0.ɵɵdomProperty("innerHTML", ctx.evil, i0.ɵɵsanitizeHtml)("src", ctx.nonEvil, i0.ɵɵsanitizeUrlOrResourceUrl);
i0.ɵɵattribute("style", ctx.evil, i0.ɵɵsanitizeStyle);
}
}
hostBindings: function HostBindingIframeDir_HostBindings(rf, ctx) {
if (rf & 2) {
$r3$.ɵɵdomProperty("innerHTML", ctx.evil, $r3$.ɵɵsanitizeHtml)("src", ctx.evil, i0.ɵɵsanitizeResourceUrl)("sandbox", ctx.evil, $r3$.ɵɵvalidateAttribute);
$r3$.ɵɵattribute("style", ctx.evil, $r3$.ɵɵsanitizeStyle)("attributeName", ctx.nonEvil);
$r3$.ɵɵdomProperty("innerHTML", ctx.evil, $r3$.ɵɵsanitizeHtml)("src", ctx.evil, i0.ɵɵsanitizeUrlOrResourceUrl)("sandbox", ctx.evil, $r3$.ɵɵvalidateAttribute);
$r3$.ɵɵattribute("style", ctx.evil, $r3$.ɵɵsanitizeStyle)("attributeName", ctx.nonEvil, i0.ɵɵvalidateAttribute);
}
}
Expand All @@ -24,3 +24,21 @@ hostBindings: function HostBindingSvgAnimateDir_HostBindings(rf, ctx) {
i0.ɵɵattribute("attributeName", ctx.evil, i0.ɵɵvalidateAttribute);
}
}
hostBindings: function HostBindingCustomSrcdocDir_HostBindings(rf, ctx) {
if (rf & 2) {
i0.ɵɵattribute("srcdoc", ctx.evil, i0.ɵɵsanitizeHtml);
}
}
hostBindings: function HostBindingCustomSrcDir_HostBindings(rf, ctx) {
if (rf & 2) {
i0.ɵɵattribute("src", ctx.evil, i0.ɵɵsanitizeUrlOrResourceUrl);
}
}
hostBindings: function HostBindingCustomDataDir_HostBindings(rf, ctx) {
if (rf & 2) {
i0.ɵɵattribute("data", ctx.evil, i0.ɵɵsanitizeUrlOrResourceUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,33 @@ export class HostBindingIframeDir {
export class HostBindingSvgAnimateDir {
evil = 'evil';
}

@Directive({
selector: 'safe-srcdoc-carrier',
host: {
'[attr.srcdoc]': 'evil',
},
})
export class HostBindingCustomSrcdocDir {
evil = 'evil';
}

@Directive({
selector: 'safe-src-carrier',
host: {
'[attr.src]': 'evil',
},
})
export class HostBindingCustomSrcDir {
evil = 'evil';
}

@Directive({
selector: 'safe-data-carrier',
host: {
'[attr.data]': 'evil',
},
})
export class HostBindingCustomDataDir {
evil = 'evil';
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,18 @@ export declare class HostBindingSvgAnimateDir {
static ɵfac: i0.ɵɵFactoryDeclaration<HostBindingSvgAnimateDir, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<HostBindingSvgAnimateDir, "animateMotion[hostBindingSvgAnimateDir]", never, {}, {}, never, never, true, never>;
}

export declare class HostBindingCustomSrcdocDir {
evil: string;
static ɵfac: i0.ɵɵFactoryDeclaration<HostBindingCustomSrcdocDir, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<HostBindingCustomSrcdocDir, "safe-srcdoc-carrier", never, {}, {}, never, never, true, never>;
}
export declare class HostBindingCustomSrcDir {
evil: string;
static ɵfac: i0.ɵɵFactoryDeclaration<HostBindingCustomSrcDir, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<HostBindingCustomSrcDir, "safe-src-carrier", never, {}, {}, never, never, true, never>;
}
export declare class HostBindingCustomDataDir {
evil: string;
static ɵfac: i0.ɵɵFactoryDeclaration<HostBindingCustomDataDir, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<HostBindingCustomDataDir, "safe-data-carrier", never, {}, {}, never, never, true, never>;
}
42 changes: 37 additions & 5 deletions packages/compiler-cli/test/ngtsc/ngtsc_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8614,7 +8614,7 @@ runInEachFileSystem((os: string) => {
hostVars: 6,
hostBindings: function UnsafeAttrsDirective_HostBindings(rf, ctx) {
if (rf & 2) {
i0.ɵɵattribute("href", ctx.attrHref, i0.ɵɵsanitizeUrlOrResourceUrl)("src", ctx.attrSrc, i0.ɵɵsanitizeUrlOrResourceUrl)("action", ctx.attrAction, i0.ɵɵsanitizeUrl)("profile", ctx.attrProfile)("innerHTML", ctx.attrInnerHTML, i0.ɵɵsanitizeHtml)("title", ctx.attrSafeTitle);
i0.ɵɵattribute("href", ctx.attrHref, i0.ɵɵsanitizeUrlOrResourceUrl)("src", ctx.attrSrc, i0.ɵɵsanitizeUrlOrResourceUrl)("action", ctx.attrAction, i0.ɵɵsanitizeUrlOrResourceUrl)("profile", ctx.attrProfile)("innerHTML", ctx.attrInnerHTML, i0.ɵɵsanitizeHtml)("title", ctx.attrSafeTitle);
}
}
`;
Expand Down Expand Up @@ -8660,14 +8660,14 @@ runInEachFileSystem((os: string) => {
hostVars: 3,
hostBindings: function UnsafePropsDirective_HostBindings(rf, ctx) {
if (rf & 2) {
i0.ɵɵdomProperty("href", ctx.propHref, i0.ɵɵsanitizeUrl)("innerHTML", ctx.propInnerHTML, i0.ɵɵsanitizeHtml)("title", ctx.propSafeTitle);
i0.ɵɵdomProperty("href", ctx.propHref, i0.ɵɵsanitizeUrlOrResourceUrl)("innerHTML", ctx.propInnerHTML, i0.ɵɵsanitizeHtml)("title", ctx.propSafeTitle);
}
}
`;
expect(trim(jsContents)).toContain(trim(hostBindingsFn));
});

it('should not generate sanitizers for URL properties in hostBindings fn in Component', () => {
it('should generate concrete-host URL sanitizers in hostBindings fn in Component', () => {
env.write(
`test.ts`,
`
Expand Down Expand Up @@ -8700,8 +8700,40 @@ runInEachFileSystem((os: string) => {
hostVars: 5,
hostBindings: function FooCmp_HostBindings(rf, ctx) {
if (rf & 2) {
i0.ɵɵdomProperty("href", ctx.hrefProp, i0.ɵɵsanitizeUrl)("title", ctx.titleProp);
i0.ɵɵattribute("src", ctx.srcAttr)("href", ctx.hrefAttr, i0.ɵɵsanitizeUrl)("title", ctx.titleAttr);
i0.ɵɵdomProperty("href", ctx.hrefProp, i0.ɵɵsanitizeUrlOrResourceUrl)("title", ctx.titleProp);
i0.ɵɵattribute("src", ctx.srcAttr, i0.ɵɵsanitizeUrlOrResourceUrl)("href", ctx.hrefAttr, i0.ɵɵsanitizeUrlOrResourceUrl)("title", ctx.titleAttr);
}
}
`;
expect(trim(jsContents)).toContain(trim(hostBindingsFn));
});

it('should generate sanitizers for pure :not selector host bindings', () => {
env.write(
`test.ts`,
`
import {Component} from '@angular/core';

@Component({
selector: ':not(iframe)',
template: '',
host: {
'[attr.srcdoc]': 'srcdoc',
}
})
class FooCmp {
srcdoc: any;
}
`,
);

env.driveMain();
const jsContents = env.getContents('test.js');
const hostBindingsFn = `
hostVars: 1,
hostBindings: function FooCmp_HostBindings(rf, ctx) {
if (rf & 2) {
i0.ɵɵattribute("srcdoc", ctx.srcdoc, i0.ɵɵsanitizeHtml);
}
}
`;
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/src/schema/dom_security_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ type SecuritySchema = Record<
>;

let _SECURITY_SCHEMA!: SecuritySchema;
const SVG_NAMESPACE = 'svg';
const MATH_ML_NAMESPACE = 'math';
export const SVG_NAMESPACE = 'svg';
export const MATH_ML_NAMESPACE = 'math';
const NO_NAMESPACE = '';
const MATCH_ALL_ELEMENTS = '*';
const createNullObj = () => Object.create(null);
Expand Down
60 changes: 49 additions & 11 deletions packages/compiler/src/template/pipeline/src/ingest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '../../../render3/view/api';
import {icuFromI18nMessage} from '../../../render3/view/i18n/util';
import {DomElementSchemaRegistry} from '../../../schema/dom_element_schema_registry';
import {BindingParser} from '../../../template_parser/binding_parser';
import {BindingParser, calcPossibleSecurityContexts} from '../../../template_parser/binding_parser';
import * as ir from '../ir';

import {
Expand Down Expand Up @@ -125,19 +125,21 @@ export function ingestHostBinding(
if (property.isAnimation) {
bindingKind = ir.BindingKind.Animation;
}
const securityContexts = bindingParser
.calcPossibleSecurityContexts(
input.componentSelector,
property.name,
bindingKind === ir.BindingKind.Attribute,
)
.filter((context) => context !== SecurityContext.NONE);
const securityContexts = calcHostBindingSecurityContexts(
bindingParser,
input.componentSelector,
property.name,
bindingKind === ir.BindingKind.Attribute,
);
ingestDomProperty(job, property, bindingKind, securityContexts);
}
for (const [name, expr] of Object.entries(input.attributes) ?? []) {
const securityContexts = bindingParser
.calcPossibleSecurityContexts(input.componentSelector, name, true)
.filter((context) => context !== SecurityContext.NONE);
const securityContexts = calcHostBindingSecurityContexts(
bindingParser,
input.componentSelector,
name,
true,
);
ingestHostAttribute(job, name, expr, securityContexts);
}
for (const event of input.events ?? []) {
Expand All @@ -146,6 +148,42 @@ export function ingestHostBinding(
return job;
}

function calcHostBindingSecurityContexts(
bindingParser: BindingParser,
selector: string,
name: string,
isAttribute: boolean,
): SecurityContext[] {
const declaringSelectorContexts = bindingParser.calcPossibleSecurityContexts(
selector,
name,
isAttribute,
);
const concreteHostContexts = calcPossibleSecurityContexts(
domSchema,
null,
domSchema.getMappedPropName(name),
isAttribute,
);
const concreteHostNonNoneContexts = concreteHostContexts.filter(
(context) => context !== SecurityContext.NONE,
);
const concreteHostNonNoneCount = concreteHostNonNoneContexts.length;
const hasConcreteHostNoneContext = concreteHostNonNoneCount !== concreteHostContexts.length;

// Host bindings can run against a concrete host whose element name differs from the declaring
// selector, including dynamic root components whose TNode name is `#host`.
if (hasConcreteHostNoneContext && concreteHostNonNoneCount > 0) {
return concreteHostContexts;
}

if (concreteHostNonNoneContexts.some((context) => !declaringSelectorContexts.includes(context))) {
return concreteHostContexts;
}

return declaringSelectorContexts.filter((context) => context !== SecurityContext.NONE);
}

Comment thread
SkyZeroZx marked this conversation as resolved.
// TODO: We should refactor the parser to use the same types and structures for host bindings as
// with ordinary components. This would allow us to share a lot more ingestion code.
export function ingestDomProperty(
Expand Down
Loading
Loading