2018-03-01 17:14:01 -08:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2018-11-21 21:14:06 -08:00
|
|
|
import {SANITIZER} from '../render3/interfaces/view';
|
|
|
|
import {getLView} from '../render3/state';
|
2019-02-20 14:21:20 -08:00
|
|
|
import {renderStringify} from '../render3/util/misc_utils';
|
2018-03-01 17:14:01 -08:00
|
|
|
|
2019-07-31 13:15:50 -07:00
|
|
|
import {BypassType, allowSanitizationBypassAndThrow, unwrapSafeValue} from './bypass';
|
2018-03-01 17:14:01 -08:00
|
|
|
import {_sanitizeHtml as _sanitizeHtml} from './html_sanitizer';
|
2019-07-31 13:15:50 -07:00
|
|
|
import {Sanitizer} from './sanitizer';
|
|
|
|
import {SecurityContext} from './security';
|
2019-05-24 13:49:57 -07:00
|
|
|
import {StyleSanitizeFn, StyleSanitizeMode, _sanitizeStyle as _sanitizeStyle} from './style_sanitizer';
|
2018-03-01 17:14:01 -08:00
|
|
|
import {_sanitizeUrl as _sanitizeUrl} from './url_sanitizer';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An `html` sanitizer which converts untrusted `html` **string** into trusted string by removing
|
|
|
|
* dangerous content.
|
|
|
|
*
|
|
|
|
* This method parses the `html` and locates potentially dangerous content (such as urls and
|
|
|
|
* javascript) and removes it.
|
|
|
|
*
|
|
|
|
* It is possible to mark a string as trusted by calling {@link bypassSanitizationTrustHtml}.
|
|
|
|
*
|
|
|
|
* @param unsafeHtml untrusted `html`, typically from the user.
|
|
|
|
* @returns `html` string which is safe to display to user, because all of the dangerous javascript
|
|
|
|
* and urls have been removed.
|
2019-04-04 11:41:52 -07:00
|
|
|
*
|
|
|
|
* @publicApi
|
2018-03-01 17:14:01 -08:00
|
|
|
*/
|
2019-05-17 18:49:21 -07:00
|
|
|
export function ɵɵsanitizeHtml(unsafeHtml: any): string {
|
2018-11-28 15:54:38 -08:00
|
|
|
const sanitizer = getSanitizer();
|
2018-11-21 21:14:06 -08:00
|
|
|
if (sanitizer) {
|
|
|
|
return sanitizer.sanitize(SecurityContext.HTML, unsafeHtml) || '';
|
2018-05-09 15:30:16 -07:00
|
|
|
}
|
2019-07-31 13:15:50 -07:00
|
|
|
if (allowSanitizationBypassAndThrow(unsafeHtml, BypassType.Html)) {
|
|
|
|
return unwrapSafeValue(unsafeHtml);
|
2018-03-01 17:14:01 -08:00
|
|
|
}
|
2019-01-12 00:59:48 -08:00
|
|
|
return _sanitizeHtml(document, renderStringify(unsafeHtml));
|
2018-03-01 17:14:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A `style` sanitizer which converts untrusted `style` **string** into trusted string by removing
|
|
|
|
* dangerous content.
|
|
|
|
*
|
|
|
|
* This method parses the `style` and locates potentially dangerous content (such as urls and
|
|
|
|
* javascript) and removes it.
|
|
|
|
*
|
|
|
|
* It is possible to mark a string as trusted by calling {@link bypassSanitizationTrustStyle}.
|
|
|
|
*
|
|
|
|
* @param unsafeStyle untrusted `style`, typically from the user.
|
|
|
|
* @returns `style` string which is safe to bind to the `style` properties, because all of the
|
|
|
|
* dangerous javascript and urls have been removed.
|
2019-04-04 11:41:52 -07:00
|
|
|
*
|
|
|
|
* @publicApi
|
2018-03-01 17:14:01 -08:00
|
|
|
*/
|
2019-05-17 18:49:21 -07:00
|
|
|
export function ɵɵsanitizeStyle(unsafeStyle: any): string {
|
2018-11-28 15:54:38 -08:00
|
|
|
const sanitizer = getSanitizer();
|
2018-11-21 21:14:06 -08:00
|
|
|
if (sanitizer) {
|
|
|
|
return sanitizer.sanitize(SecurityContext.STYLE, unsafeStyle) || '';
|
2018-05-09 15:30:16 -07:00
|
|
|
}
|
2019-07-31 13:15:50 -07:00
|
|
|
if (allowSanitizationBypassAndThrow(unsafeStyle, BypassType.Style)) {
|
|
|
|
return unwrapSafeValue(unsafeStyle);
|
2018-03-01 17:14:01 -08:00
|
|
|
}
|
2019-01-12 00:59:48 -08:00
|
|
|
return _sanitizeStyle(renderStringify(unsafeStyle));
|
2018-03-01 17:14:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A `url` sanitizer which converts untrusted `url` **string** into trusted string by removing
|
|
|
|
* dangerous
|
|
|
|
* content.
|
|
|
|
*
|
|
|
|
* This method parses the `url` and locates potentially dangerous content (such as javascript) and
|
|
|
|
* removes it.
|
|
|
|
*
|
|
|
|
* It is possible to mark a string as trusted by calling {@link bypassSanitizationTrustUrl}.
|
|
|
|
*
|
|
|
|
* @param unsafeUrl untrusted `url`, typically from the user.
|
|
|
|
* @returns `url` string which is safe to bind to the `src` properties such as `<img src>`, because
|
|
|
|
* all of the dangerous javascript has been removed.
|
2019-04-04 11:41:52 -07:00
|
|
|
*
|
|
|
|
* @publicApi
|
2018-03-01 17:14:01 -08:00
|
|
|
*/
|
2019-05-17 18:49:21 -07:00
|
|
|
export function ɵɵsanitizeUrl(unsafeUrl: any): string {
|
2018-11-28 15:54:38 -08:00
|
|
|
const sanitizer = getSanitizer();
|
2018-11-21 21:14:06 -08:00
|
|
|
if (sanitizer) {
|
|
|
|
return sanitizer.sanitize(SecurityContext.URL, unsafeUrl) || '';
|
2018-05-09 15:30:16 -07:00
|
|
|
}
|
2019-07-31 13:15:50 -07:00
|
|
|
if (allowSanitizationBypassAndThrow(unsafeUrl, BypassType.Url)) {
|
|
|
|
return unwrapSafeValue(unsafeUrl);
|
2018-03-01 17:14:01 -08:00
|
|
|
}
|
2019-01-12 00:59:48 -08:00
|
|
|
return _sanitizeUrl(renderStringify(unsafeUrl));
|
2018-03-01 17:14:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A `url` sanitizer which only lets trusted `url`s through.
|
|
|
|
*
|
|
|
|
* This passes only `url`s marked trusted by calling {@link bypassSanitizationTrustResourceUrl}.
|
|
|
|
*
|
|
|
|
* @param unsafeResourceUrl untrusted `url`, typically from the user.
|
|
|
|
* @returns `url` string which is safe to bind to the `src` properties such as `<img src>`, because
|
|
|
|
* only trusted `url`s have been allowed to pass.
|
2019-04-04 11:41:52 -07:00
|
|
|
*
|
|
|
|
* @publicApi
|
2018-03-01 17:14:01 -08:00
|
|
|
*/
|
2019-05-17 18:49:21 -07:00
|
|
|
export function ɵɵsanitizeResourceUrl(unsafeResourceUrl: any): string {
|
2018-11-28 15:54:38 -08:00
|
|
|
const sanitizer = getSanitizer();
|
2018-11-21 21:14:06 -08:00
|
|
|
if (sanitizer) {
|
|
|
|
return sanitizer.sanitize(SecurityContext.RESOURCE_URL, unsafeResourceUrl) || '';
|
2018-05-09 15:30:16 -07:00
|
|
|
}
|
2019-07-31 13:15:50 -07:00
|
|
|
if (allowSanitizationBypassAndThrow(unsafeResourceUrl, BypassType.ResourceUrl)) {
|
|
|
|
return unwrapSafeValue(unsafeResourceUrl);
|
2018-03-01 17:14:01 -08:00
|
|
|
}
|
|
|
|
throw new Error('unsafe value used in a resource URL context (see http://g.co/ng/security#xss)');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A `script` sanitizer which only lets trusted javascript through.
|
|
|
|
*
|
2018-11-21 21:14:06 -08:00
|
|
|
* This passes only `script`s marked trusted by calling {@link
|
|
|
|
* bypassSanitizationTrustScript}.
|
2018-03-01 17:14:01 -08:00
|
|
|
*
|
|
|
|
* @param unsafeScript untrusted `script`, typically from the user.
|
|
|
|
* @returns `url` string which is safe to bind to the `<script>` element such as `<img src>`,
|
2018-11-21 21:14:06 -08:00
|
|
|
* because only trusted `scripts` have been allowed to pass.
|
2019-04-04 11:41:52 -07:00
|
|
|
*
|
|
|
|
* @publicApi
|
2018-03-01 17:14:01 -08:00
|
|
|
*/
|
2019-05-17 18:49:21 -07:00
|
|
|
export function ɵɵsanitizeScript(unsafeScript: any): string {
|
2018-11-28 15:54:38 -08:00
|
|
|
const sanitizer = getSanitizer();
|
2018-11-21 21:14:06 -08:00
|
|
|
if (sanitizer) {
|
|
|
|
return sanitizer.sanitize(SecurityContext.SCRIPT, unsafeScript) || '';
|
2018-05-09 15:30:16 -07:00
|
|
|
}
|
2019-07-31 13:15:50 -07:00
|
|
|
if (allowSanitizationBypassAndThrow(unsafeScript, BypassType.Script)) {
|
|
|
|
return unwrapSafeValue(unsafeScript);
|
2018-03-01 17:14:01 -08:00
|
|
|
}
|
|
|
|
throw new Error('unsafe value used in a script context');
|
|
|
|
}
|
|
|
|
|
2019-01-03 10:04:06 -08:00
|
|
|
/**
|
|
|
|
* Detects which sanitizer to use for URL property, based on tag name and prop name.
|
|
|
|
*
|
|
|
|
* The rules are based on the RESOURCE_URL context config from
|
|
|
|
* `packages/compiler/src/schema/dom_security_schema.ts`.
|
|
|
|
* If tag and prop names don't match Resource URL schema, use URL sanitizer.
|
|
|
|
*/
|
|
|
|
export function getUrlSanitizer(tag: string, prop: string) {
|
|
|
|
if ((prop === 'src' && (tag === 'embed' || tag === 'frame' || tag === 'iframe' ||
|
|
|
|
tag === 'media' || tag === 'script')) ||
|
|
|
|
(prop === 'href' && (tag === 'base' || tag === 'link'))) {
|
2019-05-17 18:49:21 -07:00
|
|
|
return ɵɵsanitizeResourceUrl;
|
2019-01-03 10:04:06 -08:00
|
|
|
}
|
2019-05-17 18:49:21 -07:00
|
|
|
return ɵɵsanitizeUrl;
|
2019-01-03 10:04:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitizes URL, selecting sanitizer function based on tag and property names.
|
|
|
|
*
|
|
|
|
* This function is used in case we can't define security context at compile time, when only prop
|
|
|
|
* name is available. This happens when we generate host bindings for Directives/Components. The
|
|
|
|
* host element is unknown at compile time, so we defer calculation of specific sanitizer to
|
|
|
|
* runtime.
|
|
|
|
*
|
|
|
|
* @param unsafeUrl untrusted `url`, typically from the user.
|
|
|
|
* @param tag target element tag name.
|
|
|
|
* @param prop name of the property that contains the value.
|
|
|
|
* @returns `url` string which is safe to bind.
|
2019-04-04 11:41:52 -07:00
|
|
|
*
|
|
|
|
* @publicApi
|
2019-01-03 10:04:06 -08:00
|
|
|
*/
|
2019-05-17 18:49:21 -07:00
|
|
|
export function ɵɵsanitizeUrlOrResourceUrl(unsafeUrl: any, tag: string, prop: string): any {
|
2019-01-03 10:04:06 -08:00
|
|
|
return getUrlSanitizer(tag, prop)(unsafeUrl);
|
|
|
|
}
|
|
|
|
|
2018-03-01 17:14:01 -08:00
|
|
|
/**
|
2018-07-11 10:58:18 -07:00
|
|
|
* The default style sanitizer will handle sanitization for style properties by
|
|
|
|
* sanitizing any CSS property that can include a `url` value (usually image-based properties)
|
2019-04-04 11:41:52 -07:00
|
|
|
*
|
|
|
|
* @publicApi
|
2018-03-01 17:14:01 -08:00
|
|
|
*/
|
2019-05-24 13:49:57 -07:00
|
|
|
export const ɵɵdefaultStyleSanitizer =
|
|
|
|
(function(prop: string, value: string|null, mode?: StyleSanitizeMode): string | boolean | null {
|
|
|
|
mode = mode || StyleSanitizeMode.ValidateAndSanitize;
|
|
|
|
let doSanitizeValue = true;
|
|
|
|
if (mode & StyleSanitizeMode.ValidateProperty) {
|
|
|
|
doSanitizeValue = prop === 'background-image' || prop === 'background' ||
|
|
|
|
prop === 'border-image' || prop === 'filter' || prop === 'list-style' ||
|
|
|
|
prop === 'list-style-image' || prop === 'clip-path';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode & StyleSanitizeMode.SanitizeOnly) {
|
|
|
|
return doSanitizeValue ? ɵɵsanitizeStyle(value) : value;
|
|
|
|
} else {
|
|
|
|
return doSanitizeValue;
|
|
|
|
}
|
|
|
|
} as StyleSanitizeFn);
|
2018-11-28 15:54:38 -08:00
|
|
|
|
2019-02-04 21:42:55 -08:00
|
|
|
export function validateAgainstEventProperties(name: string) {
|
2019-01-10 13:34:39 -08:00
|
|
|
if (name.toLowerCase().startsWith('on')) {
|
|
|
|
const msg = `Binding to event property '${name}' is disallowed for security reasons, ` +
|
|
|
|
`please use (${name.slice(2)})=...` +
|
|
|
|
`\nIf '${name}' is a directive input, make sure the directive is imported by the` +
|
|
|
|
` current module.`;
|
|
|
|
throw new Error(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 21:42:55 -08:00
|
|
|
export function validateAgainstEventAttributes(name: string) {
|
2019-01-10 13:34:39 -08:00
|
|
|
if (name.toLowerCase().startsWith('on')) {
|
|
|
|
const msg = `Binding to event attribute '${name}' is disallowed for security reasons, ` +
|
|
|
|
`please use (${name.slice(2)})=...`;
|
|
|
|
throw new Error(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 15:54:38 -08:00
|
|
|
function getSanitizer(): Sanitizer|null {
|
|
|
|
const lView = getLView();
|
|
|
|
return lView && lView[SANITIZER];
|
2019-01-22 13:15:10 +02:00
|
|
|
}
|