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",