Skip to content

Commit fea25d1

Browse files
alan-agius4pkozlowski-opensource
authored andcommitted
fix(compiler): register SVG animation attributes in URL security context (#67797)
This change is a security hardening measure to prevent potentially unsafe attribute value manipulation through SVG animations. By mapping `animate|to`, `animate|from`, `animate|values`, and `set|to` to the `SecurityContext.URL`, Angular will now automatically sanitize these attributes. PR Close #67797
1 parent f916531 commit fea25d1

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

packages/compiler/src/schema/dom_security_schema.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ export function SECURITY_SCHEMA(): {[k: string]: SecurityContext} {
105105
'none|href',
106106
'none|xlink:href',
107107

108+
// SVG animation value attributes — may animate URL-bearing attrs (e.g. attributeName="href")
109+
// https://www.w3.org/TR/SVG11/animate.html#ToAttribute
110+
'animate|to',
111+
'animate|from',
112+
'animate|values',
113+
'set|to',
114+
108115
// The below two items are safe and should be removed but they require a G3 clean-up as a small number of tests fail.
109116
'img|src',
110117
'video|src',

packages/compiler/test/schema/dom_element_schema_registry_spec.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@ import {
1111
DomElementSchemaRegistry,
1212
SCHEMA,
1313
} from '../../src/schema/dom_element_schema_registry';
14-
import {CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA, SecurityContext} from '@angular/core';
15-
import {isNode} from '@angular/private/testing';
14+
import {CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA, SecurityContext} from '../../src/core';
1615

1716
import {Element} from '../../src/ml_parser/ast';
1817
import {HtmlParser} from '../../src/ml_parser/html_parser';
1918

20-
import {extractSchema} from './schema_extractor';
21-
2219
describe('DOMElementSchema', () => {
2320
let registry: DomElementSchemaRegistry;
2421
beforeEach(() => {
@@ -157,6 +154,12 @@ If 'onAnything' is a directive input, make sure the directive is imported by the
157154
expect(registry.securityContext('a', 'href', false)).toBe(SecurityContext.URL);
158155
expect(registry.securityContext('a', 'style', false)).toBe(SecurityContext.STYLE);
159156
expect(registry.securityContext('base', 'href', false)).toBe(SecurityContext.RESOURCE_URL);
157+
158+
// SVG animate and set attributes
159+
expect(registry.securityContext('animate', 'to', false)).toBe(SecurityContext.URL);
160+
expect(registry.securityContext('animate', 'from', false)).toBe(SecurityContext.URL);
161+
expect(registry.securityContext('animate', 'values', false)).toBe(SecurityContext.URL);
162+
expect(registry.securityContext('set', 'to', false)).toBe(SecurityContext.URL);
160163
});
161164

162165
it('should detect properties on namespaced elements', () => {

packages/core/test/sanitization/sanitization_spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,22 @@ describe('sanitization', () => {
166166
expect(
167167
ɵɵsanitizeUrlOrResourceUrl(bypassSanitizationTrustUrl('javascript:true'), 'a', 'href'),
168168
).toEqual('javascript:true');
169+
170+
// SVG animate and set attributes
171+
expect(ɵɵsanitizeUrlOrResourceUrl('javascript:alert(1)', 'animate', 'to')).toEqual(
172+
'unsafe:javascript:alert(1)',
173+
);
174+
expect(ɵɵsanitizeUrlOrResourceUrl('0.2', 'animate', 'to')).toEqual('0.2');
175+
expect(ɵɵsanitizeUrlOrResourceUrl('javascript:alert(1)', 'animate', 'from')).toEqual(
176+
'unsafe:javascript:alert(1)',
177+
);
178+
expect(ɵɵsanitizeUrlOrResourceUrl('javascript:alert(1)', 'animate', 'values')).toEqual(
179+
'unsafe:javascript:alert(1)',
180+
);
181+
expect(ɵɵsanitizeUrlOrResourceUrl('javascript:alert(1)', 'set', 'to')).toEqual(
182+
'unsafe:javascript:alert(1)',
183+
);
184+
expect(ɵɵsanitizeUrlOrResourceUrl('0.2', 'set', 'to')).toEqual('0.2');
169185
});
170186

171187
it('should only trust constant strings from template literal tags without interpolation', () => {

0 commit comments

Comments
 (0)