Skip to content

Commit 3df30ae

Browse files
phr4nkyamishne
authored andcommitted
fix(core): use the custom Sanitizer for URLs in i18n attributes
Translated attributes with interpolations in a URL context (e.g. `<a i18n-href href="{{url}}">` 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. (cherry picked from commit ca92b96)
1 parent 0083cd0 commit 3df30ae

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎packages/core/src/render3/i18n/i18n_parse.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import {
1818
ɵɵsanitizeResourceUrl as _sanitizeResourceUrl,
1919
ɵɵsanitizeScript as _sanitizeScript,
2020
ɵɵsanitizeStyle as _sanitizeStyle,
21+
ɵɵsanitizeUrl as _sanitizeUrl,
2122
ɵɵvalidateAttribute as _validateAttribute,
2223
} from '../../sanitization/sanitization';
23-
import {_sanitizeUrl} from '../../sanitization/url_sanitizer';
2424
import {
2525
assertDefined,
2626
assertEqual,

‎packages/core/test/acceptance/i18n_spec.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import {
3636
PipeTransform,
3737
provideZoneChangeDetection,
3838
QueryList,
39+
Sanitizer,
40+
SecurityContext,
3941
signal,
4042
TemplateRef,
4143
Type,
@@ -3569,6 +3571,45 @@ describe('runtime i18n', () => {
35693571
expect(link.getAttribute('href')).toMatch(/^unsafe:/);
35703572
});
35713573

3574+
describe('with a custom Sanitizer', () => {
3575+
const customSanitizer: Sanitizer = {
3576+
sanitize: (context: SecurityContext, value: unknown) =>
3577+
context === SecurityContext.URL ? `custom:${value}` : String(value),
3578+
};
3579+
3580+
beforeEach(() => {
3581+
TestBed.configureTestingModule({
3582+
providers: [{provide: Sanitizer, useValue: customSanitizer}],
3583+
});
3584+
});
3585+
3586+
it('should use the custom sanitizer for translated interpolation', () => {
3587+
const fixture = initWithTemplate(SanitizeAppComp, '<a href="{{url}}" i18n-href></a>');
3588+
const link: HTMLAnchorElement = fixture.nativeElement.querySelector('a');
3589+
expect(link.getAttribute('href')).toBe('custom:javascript:alert("oh no")');
3590+
});
3591+
3592+
it('should use the custom sanitizer for interpolation inside translated element', () => {
3593+
const fixture = initWithTemplate(SanitizeAppComp, `<div i18n><a href="{{url}}"></a></div>`);
3594+
const link: HTMLAnchorElement = fixture.nativeElement.querySelector('a');
3595+
expect(link.getAttribute('href')).toBe('custom:javascript:alert("oh no")');
3596+
});
3597+
3598+
it('should use the custom sanitizer for interpolation inside an ICU', () => {
3599+
const fixture = initWithTemplate(
3600+
SanitizeAppComp,
3601+
`<div i18n>{count, plural,
3602+
=0 {no <strong>link</strong> yet}
3603+
other {{{count}} Here is the <a href="{{url}}">link</a>!}
3604+
}</div>`,
3605+
);
3606+
fixture.componentInstance.count = 1;
3607+
fixture.detectChanges();
3608+
const link: HTMLAnchorElement = fixture.nativeElement.querySelector('a');
3609+
expect(link.getAttribute('href')).toBe('custom:javascript:alert("oh no")');
3610+
});
3611+
});
3612+
35723613
it('should sanitize action binding', () => {
35733614
const fixture = initWithTemplate(
35743615
SanitizeAppComp,

‎packages/core/test/bundling/i18n/bundle.golden_symbols.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@
258258
"\\u0275\\u0275sanitizeResourceUrl",
259259
"\\u0275\\u0275sanitizeScript",
260260
"\\u0275\\u0275sanitizeStyle",
261+
"\\u0275\\u0275sanitizeUrl",
261262
"\\u0275\\u0275validateAttribute",
262263
"_DOM",
263264
"_SECURITY_SCHEMA",

0 commit comments

Comments
 (0)