fix(compiler): promote constants in templates to Trusted Types (#39211)

Angular treats constant values of attributes and properties in templates
as secure. This means that these values are not sanitized, and are
instead passed directly to the corresponding setAttribute or setProperty
function. In cases where the given attribute or property is
security-sensitive, this causes a Trusted Types violation.

To address this, functions for promoting constant strings to each of the
three Trusted Types are introduced to Angular's private codegen API. The
compiler is updated to wrap constant strings with calls to these
functions as appropriate when constructing the `consts` array. This is
only done for security-sensitive attributes and properties, as
classified by Angular's dom_security_schema.

PR Close #39211
This commit is contained in:
Bjarki
2020-10-09 13:00:32 +00:00
committed by atscott
parent 9bfb508b87
commit 6e18d2dacc
5 changed files with 82 additions and 7 deletions

View File

@ -290,6 +290,9 @@ export {
ɵɵsanitizeStyle,
ɵɵsanitizeUrl,
ɵɵsanitizeUrlOrResourceUrl,
ɵɵtrustConstantHtml,
ɵɵtrustConstantResourceUrl,
ɵɵtrustConstantScript,
} from './sanitization/sanitization';
export {
noSideEffects as ɵnoSideEffects,

View File

@ -166,4 +166,7 @@ export const angularCoreEnv: {[name: string]: Function} =
'ɵɵsanitizeScript': sanitization.ɵɵsanitizeScript,
'ɵɵsanitizeUrl': sanitization.ɵɵsanitizeUrl,
'ɵɵsanitizeUrlOrResourceUrl': sanitization.ɵɵsanitizeUrlOrResourceUrl,
'ɵɵtrustConstantHtml': sanitization.ɵɵtrustConstantHtml,
'ɵɵtrustConstantScript': sanitization.ɵɵtrustConstantScript,
'ɵɵtrustConstantResourceUrl': sanitization.ɵɵtrustConstantResourceUrl,
}))();

View File

@ -10,6 +10,8 @@ import {getDocument} from '../render3/interfaces/document';
import {SANITIZER} from '../render3/interfaces/view';
import {getLView} from '../render3/state';
import {renderStringify} from '../render3/util/misc_utils';
import {TrustedHTML, TrustedScript, TrustedScriptURL} from '../util/security/trusted_type_defs';
import {trustedHTMLFromString, trustedScriptFromString, trustedScriptURLFromString} from '../util/security/trusted_types';
import {allowSanitizationBypassAndThrow, BypassType, unwrapSafeValue} from './bypass';
import {_sanitizeHtml as _sanitizeHtml} from './html_sanitizer';
@ -139,6 +141,51 @@ export function ɵɵsanitizeScript(unsafeScript: any): string {
throw new Error('unsafe value used in a script context');
}
/**
* Promotes the given constant string to a TrustedHTML.
* @param html constant string containing trusted HTML.
* @returns TrustedHTML wrapping `html`.
*
* @security This is a security-sensitive function and should only be used to
* convert constant values of attributes and properties found in
* application-provided Angular templates to TrustedHTML.
*
* @codeGenApi
*/
export function ɵɵtrustConstantHtml(html: string): TrustedHTML|string {
return trustedHTMLFromString(html);
}
/**
* Promotes the given constant string to a TrustedScript.
* @param script constant string containing a trusted script.
* @returns TrustedScript wrapping `script`.
*
* @security This is a security-sensitive function and should only be used to
* convert constant values of attributes and properties found in
* application-provided Angular templates to TrustedScript.
*
* @codeGenApi
*/
export function ɵɵtrustConstantScript(script: string): TrustedScript|string {
return trustedScriptFromString(script);
}
/**
* Promotes the given constant string to a TrustedScriptURL.
* @param url constant string containing a trusted script URL.
* @returns TrustedScriptURL wrapping `url`.
*
* @security This is a security-sensitive function and should only be used to
* convert constant values of attributes and properties found in
* application-provided Angular templates to TrustedScriptURL.
*
* @codeGenApi
*/
export function ɵɵtrustConstantResourceUrl(url: string): TrustedScriptURL|string {
return trustedScriptURLFromString(url);
}
/**
* Detects which sanitizer to use for URL property, based on tag name and prop name.
*