From ca92b968b8ec43ec2037302ac5e1104948aea924 Mon Sep 17 00:00:00 2001 From: phr4nky Date: Fri, 13 Nov 2026 17:37:30 +0100 Subject: [PATCH] fix(core): use the custom Sanitizer for URLs in i18n attributes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Translated attributes with interpolations in a URL context (e.g. `` or an `href` inside an ICU case) were sanitized with the raw `_sanitizeUrl` function, which ignores a `Sanitizer` provided by the application. All other security contexts already go through the `ɵɵsanitize*` instructions, as does the compile-time path for static i18n attributes. Use `ɵɵsanitizeUrl` for the URL context as well, so a custom `Sanitizer` is consulted consistently. --- packages/core/src/render3/i18n/i18n_parse.ts | 2 +- packages/core/test/acceptance/i18n_spec.ts | 41 +++++++++++++++++++ .../bundling/i18n/bundle.golden_symbols.json | 1 + 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/core/src/render3/i18n/i18n_parse.ts b/packages/core/src/render3/i18n/i18n_parse.ts index dd79db59cd0..adf333dfc75 100644 --- a/packages/core/src/render3/i18n/i18n_parse.ts +++ b/packages/core/src/render3/i18n/i18n_parse.ts @@ -18,9 +18,9 @@ import { ɵɵsanitizeResourceUrl as _sanitizeResourceUrl, ɵɵsanitizeScript as _sanitizeScript, ɵɵsanitizeStyle as _sanitizeStyle, + ɵɵsanitizeUrl as _sanitizeUrl, ɵɵvalidateAttribute as _validateAttribute, } from '../../sanitization/sanitization'; -import {_sanitizeUrl} from '../../sanitization/url_sanitizer'; import { assertDefined, assertEqual, diff --git a/packages/core/test/acceptance/i18n_spec.ts b/packages/core/test/acceptance/i18n_spec.ts index 352847d90d7..0dd65706d37 100644 --- a/packages/core/test/acceptance/i18n_spec.ts +++ b/packages/core/test/acceptance/i18n_spec.ts @@ -36,6 +36,8 @@ import { PipeTransform, provideZoneChangeDetection, QueryList, + Sanitizer, + SecurityContext, signal, TemplateRef, Type, @@ -3569,6 +3571,45 @@ describe('runtime i18n', () => { expect(link.getAttribute('href')).toMatch(/^unsafe:/); }); + describe('with a custom Sanitizer', () => { + const customSanitizer: Sanitizer = { + sanitize: (context: SecurityContext, value: unknown) => + context === SecurityContext.URL ? `custom:${value}` : String(value), + }; + + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [{provide: Sanitizer, useValue: customSanitizer}], + }); + }); + + it('should use the custom sanitizer for translated interpolation', () => { + const fixture = initWithTemplate(SanitizeAppComp, ''); + const link: HTMLAnchorElement = fixture.nativeElement.querySelector('a'); + expect(link.getAttribute('href')).toBe('custom:javascript:alert("oh no")'); + }); + + it('should use the custom sanitizer for interpolation inside translated element', () => { + const fixture = initWithTemplate(SanitizeAppComp, `
`); + const link: HTMLAnchorElement = fixture.nativeElement.querySelector('a'); + expect(link.getAttribute('href')).toBe('custom:javascript:alert("oh no")'); + }); + + it('should use the custom sanitizer for interpolation inside an ICU', () => { + const fixture = initWithTemplate( + SanitizeAppComp, + `
{count, plural, + =0 {no link yet} + other {{{count}} Here is the link!} + }
`, + ); + fixture.componentInstance.count = 1; + fixture.detectChanges(); + const link: HTMLAnchorElement = fixture.nativeElement.querySelector('a'); + expect(link.getAttribute('href')).toBe('custom:javascript:alert("oh no")'); + }); + }); + it('should sanitize action binding', () => { const fixture = initWithTemplate( SanitizeAppComp, diff --git a/packages/core/test/bundling/i18n/bundle.golden_symbols.json b/packages/core/test/bundling/i18n/bundle.golden_symbols.json index 2de3b126331..fb162d2f2b4 100644 --- a/packages/core/test/bundling/i18n/bundle.golden_symbols.json +++ b/packages/core/test/bundling/i18n/bundle.golden_symbols.json @@ -258,6 +258,7 @@ "\\u0275\\u0275sanitizeResourceUrl", "\\u0275\\u0275sanitizeScript", "\\u0275\\u0275sanitizeStyle", + "\\u0275\\u0275sanitizeUrl", "\\u0275\\u0275validateAttribute", "_DOM", "_SECURITY_SCHEMA",