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
2 changes: 2 additions & 0 deletions goldens/public-api/core/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,8 @@ export interface SchemaMetadata {

// @public
export enum SecurityContext {
// (undocumented)
ATTRIBUTE_NO_BINDING = 6,
// (undocumented)
HTML = 1,
// (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion integration/cli-hello-world-ivy-i18n/size.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/main.js": 135813,
"dist/main.js": 144843,
"dist/polyfills.js": 35883
}
1 change: 0 additions & 1 deletion packages/compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {publishFacade} from './jit_compiler_facade';
import * as outputAst from './output/output_ast';
import {global} from './util';

export {SECURITY_SCHEMA} from './schema/dom_security_schema';
export {CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA, SchemaMetadata} from './core';
export {core};

Expand Down
12 changes: 2 additions & 10 deletions packages/compiler/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,6 @@ export interface Type extends Function {
}
export const Type = Function;

export enum SecurityContext {
NONE = 0,
HTML = 1,
STYLE = 2,
SCRIPT = 3,
URL = 4,
RESOURCE_URL = 5,
ATTRIBUTE_NO_BINDING = 6,
}

/**
* Injection flags for DI.
*/
Expand Down Expand Up @@ -331,3 +321,5 @@ export const enum AttributeMarker {
*/
I18n = 6,
}

export {SecurityContext} from './schema/dom_security_schema';
2 changes: 0 additions & 2 deletions packages/compiler/src/schema/dom_element_schema_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,6 @@ export class DomElementSchemaRegistry extends ElementSchemaRegistry {
propName = this.getMappedPropName(propName);
}

// Make sure comparisons are case insensitive, so that case differences between attribute and
// property names do not have a security impact.
tagName = tagName.toLowerCase();
propName = propName.toLowerCase();

Expand Down
27 changes: 25 additions & 2 deletions packages/compiler/src/schema/dom_security_schema.ts
Comment thread
dgp1130 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,24 @@
* found in the LICENSE file at https://angular.dev/license
*/

import {SecurityContext} from '../core';
/**
* A SecurityContext marks a location that has dangerous security implications, e.g. a DOM property
* like `innerHTML` that could cause Cross Site Scripting (XSS) security bugs when improperly
* handled.
*
* See DomSanitizer for more details on security in Angular applications.
*
* @publicApi
*/
export enum SecurityContext {
NONE = 0,
HTML = 1,
STYLE = 2,
SCRIPT = 3,
URL = 4,
RESOURCE_URL = 5,
ATTRIBUTE_NO_BINDING = 6,
}

// =================================================================================================
// =================================================================================================
Expand All @@ -18,11 +35,17 @@ import {SecurityContext} from '../core';
//
// =================================================================================================

/** Map from tagName|propertyName to SecurityContext. Properties applying to all tags use '*'. */
/**
* Map from tagName|propertyName to SecurityContext. Properties applying to all tags use '*'.
*/
let _SECURITY_SCHEMA!: {[k: string]: SecurityContext};
const SVG_NAMESPACE = 'svg';
const MATH_ML_NAMESPACE = 'math';

/**
* @remarks Keep is a copy of DOM Security Schema.
* @see [SECURITY_SCHEMA](../../../compiler/src/schema/dom_security_schema.ts)
Comment thread
alan-agius4 marked this conversation as resolved.
*/
export function SECURITY_SCHEMA(): {[k: string]: SecurityContext} {
if (!_SECURITY_SCHEMA) {
_SECURITY_SCHEMA = {};
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler/src/template/pipeline/src/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import {resolveDeferDepsFns} from './phases/resolve_defer_deps_fns';
import {resolveDollarEvent} from './phases/resolve_dollar_event';
import {resolveI18nElementPlaceholders} from './phases/resolve_i18n_element_placeholders';
import {resolveI18nExpressionPlaceholders} from './phases/resolve_i18n_expression_placeholders';
import {resolveI18nAttrSanitizers} from './phases/resolve_i18n_attr_sanitizers';
import {resolveNames} from './phases/resolve_names';
import {resolveSanitizers} from './phases/resolve_sanitizers';
import {removeSafeNavigationMigration} from './phases/safe_navigation_migration';
Expand Down Expand Up @@ -161,6 +162,7 @@ const phases: Phase[] = [
{kind: Kind.Tmpl, fn: resolveI18nExpressionPlaceholders},
{kind: Kind.Tmpl, fn: extractI18nMessages},
{kind: Kind.Tmpl, fn: collectI18nConsts},
{kind: Kind.Tmpl, fn: resolveI18nAttrSanitizers},
{kind: Kind.Tmpl, fn: collectConstExpressions},
{kind: Kind.Both, fn: collectElementConsts},
{kind: Kind.Tmpl, fn: removeI18nContexts},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,7 @@ class ElementAttributes {
if (value === null) {
throw Error('Attribute, i18n attribute, & style element attributes must have a value');
}
if (trustedValueFn !== null) {
if (!ir.isStringLiteral(value)) {
throw Error('AssertionError: extracted attribute value should be string literal');
}
if (trustedValueFn !== null && ir.isStringLiteral(value)) {
Comment thread
alan-agius4 marked this conversation as resolved.
array.push(
o.taggedTemplate(
trustedValueFn,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @license
* Copyright Google LLC 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.dev/license
*/

import {SecurityContext} from '../../../../core';
import * as o from '../../../../output/output_ast';
import {Identifiers} from '../../../../render3/r3_identifiers';
import * as ir from '../../ir';
import {CompilationJob} from '../compilation';
import {MATH_ML_NAMESPACE, SVG_NAMESPACE} from '../namespaces';

/**
* Wraps static i18n extracted attributes in their corresponding sanitizers/validators.
*/
export function resolveI18nAttrSanitizers(job: CompilationJob): void {
const tagNamesByElement = new Map<ir.XrefId, string>();

for (const unit of job.units) {
for (const op of unit.ops()) {
if (op.kind === ir.OpKind.ElementStart || op.kind === ir.OpKind.Template) {
let tag = op.tag ?? '';
switch (op.namespace) {
case ir.Namespace.SVG:
tag = `:${SVG_NAMESPACE}:${tag}`;
break;
case ir.Namespace.Math:
tag = `:${MATH_ML_NAMESPACE}:${tag}`;
break;
}

tagNamesByElement.set(op.xref, tag);
}
}
}

for (const unit of job.units) {
for (const op of unit.create) {
if (
op.kind === ir.OpKind.ExtractedAttribute &&
op.i18nContext !== null &&
op.expression !== null
) {
const tagName = tagNamesByElement.get(op.target) ?? '';
let expr = op.expression;
switch (op.securityContext) {
case SecurityContext.HTML:
expr = o.importExpr(Identifiers.sanitizeHtml).callFn([expr]);
break;
case SecurityContext.STYLE:
expr = o.importExpr(Identifiers.sanitizeStyle).callFn([expr]);
break;
case SecurityContext.SCRIPT:
expr = o.importExpr(Identifiers.sanitizeScript).callFn([expr]);
break;
case SecurityContext.URL:
expr = o.importExpr(Identifiers.sanitizeUrl).callFn([expr]);
break;
case SecurityContext.RESOURCE_URL:
expr = o.importExpr(Identifiers.sanitizeResourceUrl).callFn([expr]);
break;
case SecurityContext.ATTRIBUTE_NO_BINDING:
expr = o
.importExpr(Identifiers.validateAttribute)
.callFn([expr, o.literal(tagName), o.literal(op.name)]);
break;
}
op.expression = expr;
}
}
}
}
Loading
Loading