Skip to content

Commit c19a36c

Browse files
SkyZeroZxthePunderWoman
authored andcommitted
fix(platform-browser): disallow event handler attributes in Meta
Prevent arbitrary MetaDefinition properties from writing on* handlers directly to meta elements. Browser events can execute these handlers, including on meta elements rendered in the document body. Reject handler attributes case-insensitively before matching or mutating tags. Fixes #70126
1 parent dca0a77 commit c19a36c

4 files changed

Lines changed: 96 additions & 3 deletions

File tree

‎goldens/public-api/platform-browser/errors.api.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export const enum RuntimeErrorCode {
1313
// (undocumented)
1414
HYDRATION_CONFLICTING_FEATURES = 5001,
1515
// (undocumented)
16+
INVALID_EVENT_ATTRIBUTE = 5203,
17+
// (undocumented)
1618
NO_PLUGIN_FOR_EVENT = 5101,
1719
// (undocumented)
1820
ROOT_NODE_NOT_FOUND = -5104,

‎packages/platform-browser/src/browser/meta.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
*/
88

99
import {DOCUMENT, ɵDomAdapter as DomAdapter, ɵgetDOM as getDOM} from '@angular/common';
10-
import {Inject, Injectable} from '@angular/core';
10+
import {Inject, Injectable, ɵRuntimeError as RuntimeError} from '@angular/core';
11+
12+
import {RuntimeErrorCode} from '../errors';
1113

1214
/**
1315
* Represents the attributes of an HTML `<meta>` element. The element itself is
14-
* represented by the internal `HTMLMetaElement`.
16+
* represented by the internal `HTMLMetaElement`. Event handler attributes, which start with
17+
* `on`, are not allowed.
1518
*
1619
* @see [HTML meta tag](https://developer.mozilla.org/docs/Web/HTML/Element/meta)
1720
* @see {@link Meta}
@@ -133,6 +136,7 @@ export class Meta {
133136
*/
134137
updateTag(tag: MetaDefinition, selector?: string): HTMLMetaElement | null {
135138
if (!tag) return null;
139+
this._validateMetaDefinition(tag);
136140
selector = selector || this._parseSelector(tag);
137141
const meta: HTMLMetaElement = this.getTag(selector)!;
138142
if (meta) {
@@ -164,6 +168,7 @@ export class Meta {
164168
meta: MetaDefinition,
165169
forceCreation: boolean = false,
166170
): HTMLMetaElement {
171+
this._validateMetaDefinition(meta);
167172
if (!forceCreation) {
168173
const selector: string = this._parseSelector(meta);
169174
// It's allowed to have multiple elements with the same name so it's not enough to
@@ -197,14 +202,27 @@ export class Meta {
197202
return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
198203
}
199204

205+
private _validateMetaDefinition(tag: MetaDefinition): void {
206+
for (const prop of Object.keys(tag)) {
207+
const attributeName = this._getMetaKeyMap(prop);
208+
if (attributeName.toLowerCase().startsWith('on')) {
209+
throw new RuntimeError(
210+
RuntimeErrorCode.INVALID_EVENT_ATTRIBUTE,
211+
(typeof ngDevMode === 'undefined' || ngDevMode) &&
212+
`The Meta service does not allow setting event handler attribute '${attributeName}' for security reasons.`,
213+
);
214+
}
215+
}
216+
}
217+
200218
private _containsAttributes(tag: MetaDefinition, elem: HTMLMetaElement): boolean {
201219
return Object.keys(tag).every(
202220
(key: string) => elem.getAttribute(this._getMetaKeyMap(key)) === tag[key],
203221
);
204222
}
205223

206224
private _getMetaKeyMap(prop: string): string {
207-
return META_KEYS_MAP[prop] || prop;
225+
return Object.hasOwn(META_KEYS_MAP, prop) ? META_KEYS_MAP[prop] : prop;
208226
}
209227
}
210228

‎packages/platform-browser/src/errors.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const enum RuntimeErrorCode {
2626
SANITIZATION_UNSAFE_SCRIPT = 5200,
2727
SANITIZATION_UNSAFE_RESOURCE_URL = 5201,
2828
SANITIZATION_UNEXPECTED_CTX = 5202,
29+
INVALID_EVENT_ATTRIBUTE = 5203,
2930

3031
// Animations related errors (5300-5400 range)
3132
ANIMATION_RENDERER_ASYNC_LOADING_FAILURE = 5300,

‎packages/platform-browser/test/browser/meta_spec.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,26 @@ describe('Meta service', () => {
8989
expect(actual!.getAttribute('content')).toEqual('4321');
9090
});
9191

92+
it('should reject event handler attributes targeting a body meta tag', () => {
93+
doc.body.appendChild(defaultMeta);
94+
95+
const evil = 'alert(1)';
96+
97+
expect(metaService.getTag('property="fb:app_id"')).toBe(defaultMeta);
98+
expect(() =>
99+
metaService.updateTag({
100+
property: 'fb:app_id',
101+
style: 'content-visibility:auto',
102+
oncontentvisibilityautostatechange: evil,
103+
}),
104+
).toThrowError(
105+
/NG05203: The Meta service does not allow setting event handler attribute 'oncontentvisibilityautostatechange'/,
106+
);
107+
108+
expect(defaultMeta.getAttribute('style')).toBeNull();
109+
expect(defaultMeta.getAttribute('oncontentvisibilityautostatechange')).toBeNull();
110+
});
111+
92112
it('should not allow a custom selector to match off target elements like the body tag', () => {
93113
// This payload attempts to break out of the `meta[name="..."]` constraint entirely
94114
// and inject a comma to target arbitrary DOM elements like the `body` tag via the
@@ -143,6 +163,42 @@ describe('Meta service', () => {
143163
metaService.removeTagElement(actual);
144164
});
145165

166+
it('should reject event handler attributes without adding a tag', () => {
167+
const selector = 'name="og:title"';
168+
const evil = 'alert(1)';
169+
170+
expect(() =>
171+
metaService.addTag({
172+
name: 'og:title',
173+
style: 'content-visibility:auto',
174+
oncontentvisibilityautostatechange: evil,
175+
}),
176+
).toThrowError(
177+
/NG05203: The Meta service does not allow setting event handler attribute 'oncontentvisibilityautostatechange'/,
178+
);
179+
180+
expect(metaService.getTag(selector)).toBeNull();
181+
});
182+
183+
it('should reject event handler attributes in addTags without adding tags', () => {
184+
const selector = 'name="og:title"';
185+
const evil = 'alert(1)';
186+
187+
expect(() =>
188+
metaService.addTags([
189+
{
190+
name: 'og:title',
191+
style: 'content-visibility:auto',
192+
oncontentvisibilityautostatechange: evil,
193+
},
194+
]),
195+
).toThrowError(
196+
/NG05203: The Meta service does not allow setting event handler attribute 'oncontentvisibilityautostatechange'/,
197+
);
198+
199+
expect(metaService.getTag(selector)).toBeNull();
200+
});
201+
146202
it('should add httpEquiv meta tag as http-equiv', () => {
147203
metaService.addTag({httpEquiv: 'refresh', content: '3;url=http://test'});
148204

@@ -155,6 +211,22 @@ describe('Meta service', () => {
155211
metaService.removeTagElement(actual);
156212
});
157213

214+
it('should add attributes whose names match Object prototype keys', () => {
215+
const meta = metaService.addTag({
216+
name: 'prototype-keys',
217+
constructor: 'constructor',
218+
toString: 'toString',
219+
['__proto__']: '__proto__',
220+
})!;
221+
222+
expect(meta.getAttribute('constructor')).toEqual('constructor');
223+
expect(meta.getAttribute('toString')).toEqual('toString');
224+
expect(meta.getAttribute('__proto__')).toEqual('__proto__');
225+
226+
// clean up
227+
metaService.removeTagElement(meta);
228+
});
229+
158230
it('should escape selector values when deriving the match selector', () => {
159231
// This payload attempts to prematurely close the attribute selector
160232
// and match another attribute.

0 commit comments

Comments
 (0)