Skip to content
Closed
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
30 changes: 23 additions & 7 deletions packages/compiler/src/render3/view/i18n/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import * as i18n from '../../../i18n/i18n_ast';
import {createI18nMessageFactory, VisitNodeFn} from '../../../i18n/i18n_parser';
import * as html from '../../../ml_parser/ast';
import {ParseTreeResult} from '../../../ml_parser/parser';
import {splitNsName} from '../../../ml_parser/tags';
import * as o from '../../../output/output_ast';
import {SecurityContext} from '../../../core';
import {DomElementSchemaRegistry} from '../../../schema/dom_element_schema_registry';
import {isTrustedTypesSink} from '../../../schema/trusted_types_sinks';

import {hasI18nAttrs, I18N_ATTR, I18N_ATTR_PREFIX, icuFromI18nMessage} from './util';
Expand Down Expand Up @@ -49,6 +52,25 @@ const setI18nRefs = (originalNodeMap: Map<html.Node, html.Node>): VisitNodeFn =>
};
};

const domSchema = new DomElementSchemaRegistry();

function isSecuritySensitiveI18nAttribute(
node: html.Element | html.Component,
attrName: string,
): boolean {
const tagName = node instanceof html.Component ? node.tagName : node.name;
if (tagName === null) {
return false;
}

const elementName = splitNsName(tagName)[1];
return (
isTrustedTypesSink(elementName, attrName) ||
domSchema.securityContext(elementName, attrName, /* isAttribute */ true) ===
SecurityContext.RESOURCE_URL
);
}

/**
* This visitor walks over HTML parse tree and converts information stored in
* i18n-related attributes ("i18n" and "i18n-*") into i18n meta object that is
Expand Down Expand Up @@ -201,14 +223,8 @@ export class I18nMetaVisitor implements html.Visitor {
} else if (attr.name.startsWith(I18N_ATTR_PREFIX)) {
// 'i18n-*' attributes
const name = attr.name.slice(I18N_ATTR_PREFIX.length);
let isTrustedType: boolean;
if (node instanceof html.Component) {
isTrustedType = node.tagName === null ? false : isTrustedTypesSink(node.tagName, name);
} else {
isTrustedType = isTrustedTypesSink(node.name, name);
}

if (isTrustedType) {
if (isSecuritySensitiveI18nAttribute(node, name)) {
this._reportError(
attr,
`Translating attribute '${name}' is disallowed for security reasons.`,
Expand Down
13 changes: 13 additions & 0 deletions packages/core/test/linker/security_integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,18 @@ describe('security integration tests', function () {
/Translating attribute 'innerHTML' is disallowed for security reasons./,
);
});

it('should throw error on translated SVG script ResourceURL attributes', () => {
const template = `
<svg>
<script href="/safe-svg-script.js" i18n-href></script>
</svg>
`;
TestBed.overrideComponent(SecuredComponent, {set: {template}});

expect(() => TestBed.createComponent(SecuredComponent)).toThrowError(
/Translating attribute 'href' is disallowed for security reasons./,
);
});
});
});
Loading