Skip to content
Closed
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
7 changes: 6 additions & 1 deletion packages/platform-browser/src/browser/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,12 @@ function buildMetaSelector(attrSelector: string): string {
}

function setMetaElementAttributes(tag: MetaDefinition, el: HTMLMetaElement) {
Object.keys(tag).forEach((prop: string) => el.setAttribute(getMetaKeyMap(prop), tag[prop]));
Object.keys(tag)
// Event-handler attributes (e.g. `onerror`, `onsecuritypolicyviolation`) must never be
// written to the DOM: unlike template bindings, `MetaDefinition` keys aren't validated by
// Angular's compiler, so an attacker-controlled key could otherwise execute script.
.filter((prop: string) => !prop.toLowerCase().startsWith('on'))
.forEach((prop: string) => el.setAttribute(getMetaKeyMap(prop), tag[prop]));
}

function parseSelector(tag: MetaDefinition): string {
Expand Down
26 changes: 26 additions & 0 deletions packages/platform-browser/test/browser/meta_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,32 @@ describe('Meta service', () => {
metaService.removeTagElement(actual);
});

it('should not write event-handler attributes onto the meta element', () => {
const meta = metaService.addTag({
name: 'og:title',
content: 'Content Title',
onerror: 'alert(document.cookie)',
OnClick: 'alert(1)',
})!;

expect(meta.getAttribute('onerror')).toBeNull();
expect(meta.getAttribute('OnClick')).toBeNull();
expect(meta.getAttribute('onclick')).toBeNull();
expect(meta.getAttribute('content')).toEqual('Content Title');

// clean up
metaService.removeTagElement(meta);
});

it('should not add event-handler attributes when updating an existing meta tag', () => {
const selector = 'property="fb:app_id"';
metaService.updateTag({content: '4321', onsecuritypolicyviolation: 'alert(1)'}, selector);

const actual = metaService.getTag(selector)!;
expect(actual.getAttribute('onsecuritypolicyviolation')).toBeNull();
expect(actual.getAttribute('content')).toEqual('4321');
});

it('should escape selector values when deriving the match selector', () => {
// This payload attempts to prematurely close the attribute selector
// and match another attribute.
Expand Down