Title: Security: Angular production mode allows `[attr.onclick]` and host `attr.onclick` bindings to become click-triggered XSS · Issue #68419 · angular/angular · GitHub
Open Graph Title: Security: Angular production mode allows `[attr.onclick]` and host `attr.onclick` bindings to become click-triggered XSS · Issue #68419 · angular/angular
X Title: Security: Angular production mode allows `[attr.onclick]` and host `attr.onclick` bindings to become click-triggered XSS · Issue #68419 · angular/angular
Description: Which @angular/* package(s) are the source of the bug? Don't known / other Is this a regression? No Description Report description Angular production mode allows [attr.onclick] and host attr.onclick bindings to become click-triggered XSS...
Open Graph Description: Which @angular/* package(s) are the source of the bug? Don't known / other Is this a regression? No Description Report description Angular production mode allows [attr.onclick] and host attr.onclic...
X Description: Which @angular/* package(s) are the source of the bug? Don't known / other Is this a regression? No Description Report description Angular production mode allows [attr.onclick] and host attr.on...
Opengraph URL: https://github.com/angular/angular/issues/68419
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Security: Angular production mode allows `[attr.onclick]` and host `attr.onclick` bindings to become click-triggered XSS","articleBody":"### Which @angular/* package(s) are the source of the bug?\n\nDon't known / other\n\n### Is this a regression?\n\nNo\n\n### Description\n\n## Report description\nAngular production mode allows `[attr.onclick]` and host `attr.onclick` bindings to become click-triggered XSS\n\n## The problem\n### Please describe the technical details of the vulnerability\n\n#### 1. technical details\n\nAngular already has explicit validation that rejects `on*` attribute and property bindings:\n\n```ts\noverride validateProperty(name: string): {error: boolean; msg?: string} {\n if (name.toLowerCase().startsWith('on')) {\n const msg =\n `Binding to event property '${name}' is disallowed for security reasons, ` +\n `please use (${name.slice(2)})=...` +\n `\\nIf '${name}' is a directive input, make sure the directive is imported by the` +\n ` current module.`;\n return {error: true, msg: msg};\n } else {\n return {error: false};\n }\n}\n\noverride validateAttribute(name: string): {error: boolean; msg?: string} {\n if (name.toLowerCase().startsWith('on')) {\n const msg =\n `Binding to event attribute '${name}' is disallowed for security reasons, ` +\n `please use (${name.slice(2)})=...`;\n return {error: true, msg: msg};\n } else {\n return {error: false};\n }\n}\n```\n\nHowever, the main R3 template path explicitly bypasses that validation for bound element properties and attributes:\n\n```ts\nconst bep = this.bindingParser.createBoundElementProperty(\n elementName,\n prop,\n /* skipValidation */ true,\n /* mapPropertyName */ false,\n);\n```\n\nHost bindings have a similar gap. `createBoundHostProperties()` parses host properties, but does not reuse the same validation path:\n\n```ts\ncreateBoundHostProperties(\n properties: HostProperties,\n sourceSpan: ParseSourceSpan,\n): ParsedProperty[] | null {\n const boundProps: ParsedProperty[] = [];\n for (const propName of Object.keys(properties)) {\n const expression = properties[propName];\n if (typeof expression === 'string') {\n this.parsePropertyBinding(\n propName,\n expression,\n true,\n false,\n sourceSpan,\n sourceSpan.start.offset,\n undefined,\n [],\n boundProps,\n sourceSpan,\n );\n }\n }\n return boundProps;\n}\n```\n\nThe remaining runtime protection is only enforced in `ngDevMode`. In production mode, Angular writes the attribute directly into the DOM:\n\n```ts\nexport function elementAttributeInternal(\n tNode: TNode,\n lView: LView,\n name: string,\n value: any,\n sanitizer: SanitizerFn | null | undefined,\n namespace: string | null | undefined,\n) {\n if (ngDevMode) {\n assertNotSame(value, NO_CHANGE as any, 'Incoming value should never be NO_CHANGE.');\n validateAgainstEventAttributes(name);\n assertTNodeType(\n tNode,\n TNodeType.Element,\n `Attempted to set attribute \\`${name}\\` on a container node. ` +\n `Host bindings are not valid on ng-container or ng-template.`,\n );\n }\n const element = getNativeByTNode(tNode, lView) as RElement;\n setElementAttribute(lView[RENDERER], element, namespace, tNode.value, name, value, sanitizer);\n}\n\nexport function setElementAttribute(\n renderer: Renderer,\n element: RElement,\n namespace: string | null | undefined,\n tagName: string | null,\n name: string,\n value: any,\n sanitizer: SanitizerFn | null | undefined,\n) {\n if (value == null) {\n renderer.removeAttribute(element, name, namespace);\n } else {\n const strValue =\n sanitizer == null ? renderStringify(value) : sanitizer(value, tagName || '', name);\n\n renderer.setAttribute(element, name, strValue as string, namespace);\n }\n}\n```\n\nAs a result, if an application binds attacker-controlled data to `[attr.onclick]` in a template, or to `@HostBinding('attr.onclick')` / `host: {'[attr.onclick]': ...}`, production builds can emit a real inline event handler attribute. When the victim clicks the element, the browser executes attacker-controlled JavaScript in the page origin.\n\n#### 2. vulnerability reproduction\n\nThe attached browser PoC uses a real Angular template with a realistic application pattern: campaign data from a CMS is rendered into a marketing CTA button.\n\n```ts\n@Component({\n selector: 'marketing-cta-page',\n standalone: false,\n changeDetection: ChangeDetectionStrategy.Eager,\n template: `\n \u003csection>\n \u003ch1>{{ campaign.title }}\u003c/h1>\n \u003cp id=\"campaign-origin\">{{ campaign.origin }}\u003c/p>\n \u003cbutton id=\"buy-now\" type=\"button\" [attr.onclick]=\"campaign.clickAction\">\n {{ campaign.ctaLabel }}\n \u003c/button>\n \u003c/section>\n `,\n})\nclass MarketingCtaPageComponent {\n campaign = {\n title: 'Spring Sale',\n origin: 'trusted CMS entry',\n ctaLabel: 'Buy now',\n clickAction: '',\n };\n}\n```\n\nThe PoC verifies that development mode blocks the binding:\n\n```ts\nit('blocks the binding in development mode', () => {\n expect(() => {\n const fixture = TestBed.createComponent(MarketingCtaPageComponent);\n fixture.componentInstance.campaign.clickAction =\n \"document.documentElement.setAttribute('data-realistic-xss','fired')\";\n fixture.componentInstance.campaign.origin = 'attacker-controlled CMS entry';\n fixture.detectChanges();\n }).toThrowError(\n /Binding to event attribute 'onclick' is disallowed for security reasons, please use \\(click\\)=.../,\n );\n});\n```\n\nThe same PoC then switches to production mode and shows that Angular renders the dangerous attribute and the browser executes it on click:\n\n```ts\nit('turns attacker-controlled CMS data into browser-executed XSS in production mode', () => {\n globalRef.ngDevMode = false;\n\n const fixture = TestBed.createComponent(MarketingCtaPageComponent);\n fixture.componentInstance.campaign.origin = 'attacker-controlled CMS entry';\n fixture.componentInstance.campaign.clickAction =\n \"document.documentElement.setAttribute('data-realistic-xss','fired')\";\n fixture.detectChanges();\n\n const button = fixture.nativeElement.querySelector('#buy-now') as HTMLButtonElement;\n expect(button.getAttribute('onclick')).toBe(\n \"document.documentElement.setAttribute('data-realistic-xss','fired')\",\n );\n\n button.click();\n\n expect(document.documentElement.getAttribute('data-realistic-xss')).toBe('fired');\n});\n```\n\nThe same test suite also models a benign trusted CMS handler string to show that the affected code path is a realistic application pattern:\n\n```ts\nit('renders a trusted CMS handler string in production mode and executes it on click', () => {\n globalRef.ngDevMode = false;\n globalRef.demoSafeHandler = (campaignId: string) => {\n document.documentElement.setAttribute('data-safe-click', campaignId);\n };\n\n const fixture = TestBed.createComponent(MarketingCtaPageComponent);\n fixture.componentInstance.campaign.origin = 'trusted CMS entry';\n fixture.componentInstance.campaign.clickAction = `demoSafeHandler('spring-sale')`;\n fixture.detectChanges();\n\n const button = fixture.nativeElement.querySelector('#buy-now') as HTMLButtonElement;\n expect(button.getAttribute('onclick')).toBe(`demoSafeHandler('spring-sale')`);\n\n button.click();\n\n expect(document.documentElement.getAttribute('data-safe-click')).toBe('spring-sale');\n});\n```\n\nThe browser PoC is available in `./work/angular/r3_template_transform_xss/realistic_attr_onclick_spec.ts` and was executed with:\n\n```bash\nbazelisk test //r3_template_transform_xss:test_web --test_output=errors\n```\n\nObserved result:\n\n```text\n//r3_template_transform_xss:test_web_chromium PASSED\n//r3_template_transform_xss:test_web_firefox PASSED\n```\n\nThis demonstrates a practical path from attacker-controlled application data to a real inline `onclick` attribute and browser-executed JavaScript when the user clicks the element.\n\n#### 3. patch\n\n```diff\ndiff --git a/packages/compiler/src/render3/r3_template_transform.ts b/packages/compiler/src/render3/r3_template_transform.ts\nindex d031c50053..c3f9e72b94 100644\n--- a/packages/compiler/src/render3/r3_template_transform.ts\n+++ b/packages/compiler/src/render3/r3_template_transform.ts\n@@ -594,7 +594,7 @@ class HtmlAstToIvyAst implements html.Visitor {\n const bep = this.bindingParser.createBoundElementProperty(\n elementName,\n prop,\n- /* skipValidation */ true,\n+ /* skipValidation */ !prop.name.startsWith('attr.'),\n /* mapPropertyName */ false,\n );\n bound.push(t.BoundAttribute.fromBoundElementProperty(bep, i18n));\ndiff --git a/packages/compiler/src/template_parser/binding_parser.ts b/packages/compiler/src/template_parser/binding_parser.ts\nindex db7d694166..cb4b589ed5 100644\n--- a/packages/compiler/src/template_parser/binding_parser.ts\n+++ b/packages/compiler/src/template_parser/binding_parser.ts\n@@ -72,6 +72,7 @@ export class BindingParser {\n for (const propName of Object.keys(properties)) {\n const expression = properties[propName];\n if (typeof expression === 'string') {\n+ const initialPropCount = boundProps.length;\n this.parsePropertyBinding(\n propName,\n expression,\n@@ -90,6 +91,14 @@ export class BindingParser {\n boundProps,\n sourceSpan,\n );\n+ for (let i = initialPropCount; i \u003c boundProps.length; i++) {\n+ this.createBoundElementProperty(\n+ null,\n+ boundProps[i],\n+ /* skipValidation */ false,\n+ /* mapPropertyName */ false,\n+ );\n+ }\n } else {\n this._reportError(\n `Value of the host property binding \"${propName}\" needs to be a string representing an expression but got \"${expression}\" (${typeof expression})`,\ndiff --git a/packages/core/test/linker/security_integration_spec.ts b/packages/core/test/linker/security_integration_spec.ts\nindex 860ab92d9b..f82d3f4e5c 100644\n--- a/packages/core/test/linker/security_integration_spec.ts\n+++ b/packages/core/test/linker/security_integration_spec.ts\n@@ -97,6 +97,27 @@ describe('security integration tests', function () {\n expect(div.nativeElement.onclick).not.toBe(value);\n expect(div.nativeElement.hasAttribute('onclick')).toEqual(false);\n });\n+\n+ it('should disallow binding to attr.on* in host bindings', () => {\n+ @Directive({\n+ selector: '[dirOnclick]',\n+ standalone: false,\n+ })\n+ class HostOnclickDirective {\n+ @HostBinding('attr.onclick') @Input() dirOnclick: string | undefined;\n+ }\n+\n+ const template = `\u003cbutton [dirOnclick]=\"ctxProp\">\u003c/button>`;\n+ TestBed.configureTestingModule({declarations: [HostOnclickDirective]});\n+ TestBed.overrideComponent(SecuredComponent, {set: {template}});\n+\n+ expect(() => {\n+ const cmp = TestBed.createComponent(SecuredComponent);\n+ cmp.detectChanges();\n+ }).toThrowError(\n+ /Binding to event attribute 'onclick' is disallowed for security reasons, please use \\(click\\)=.../,\n+ );\n+ });\n });\n \n describe('safe HTML values', function () {\n```\n\n### Impact analysis – Please briefly explain who can exploit the vulnerability, and what they gain when doing so\n\nAn attacker who can influence application data that is bound to `[attr.onclick]` or to host `attr.onclick` bindings can exploit this issue against users of a production Angular application.\n\nSuccessful exploitation causes the browser to execute attacker-controlled JavaScript when the victim clicks the affected element, giving the attacker same-origin DOM XSS in the application page.\n\n[r3_template_transform_xss.tar.gz](https://github.com/user-attachments/files/27165211/r3_template_transform_xss.tar.gz)\n\n### Please provide a link to a minimal reproduction of the bug\n\n_No response_\n\n### Please provide the exception or error you saw\n\n```true\n\n```\n\n### Please provide the environment you discovered this bug in (run `ng version`)\n\n```true\n\n```\n\n### Anything else?\n\n_No response_","author":{"url":"https://github.com/foxllb","@type":"Person","name":"foxllb"},"datePublished":"2026-04-28T12:39:22Z","url":"https://github.com/angular/angular/issues/68419"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:2c9b0932-ee56-15d6-81fc-322ee4b3d703 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8654:1AE20C:AB5EFE:DA1C58:6B00F3D0 |
| html-safe-nonce | bc3c1c7ad36969a671ca7fcee31eaa52591dc034f8c7739be9f701a17a654904 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NjU0OjFBRTIwQzpBQjVFRkU6REExQzU4OjZCMDBGM0QwIiwidmlzaXRvcl9pZCI6IjQxMjUzNzAxNzkzOTg1MDE5MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 0ddca6683ddbdcc74ff5087d77a4621648912bf2c888738285c351e4f61e2b56 |
| hovercard-subject-tag | issue:4343191193 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/68419/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ff0781d085b01e5f16359381eab864d2a4839dc30532eb6764f2f1d4a69c7f5b/angular/angular/issues/68419 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ff0781d085b01e5f16359381eab864d2a4839dc30532eb6764f2f1d4a69c7f5b/angular/angular/issues/68419 |
| og:image:alt | Which @angular/* package(s) are the source of the bug? Don't known / other Is this a regression? No Description Report description Angular production mode allows [attr.onclick] and host attr.onclic... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | foxllb |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width