mirror of
https://github.com/angular/angular.git
synced 2026-11-22 22:55:25 -05:00
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.
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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, '<a href="{{url}}" i18n-href></a>');
|
||||
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, `<div i18n><a href="{{url}}"></a></div>`);
|
||||
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,
|
||||
`<div i18n>{count, plural,
|
||||
=0 {no <strong>link</strong> yet}
|
||||
other {{{count}} Here is the <a href="{{url}}">link</a>!}
|
||||
}</div>`,
|
||||
);
|
||||
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,
|
||||
|
||||
@@ -258,6 +258,7 @@
|
||||
"\\u0275\\u0275sanitizeResourceUrl",
|
||||
"\\u0275\\u0275sanitizeScript",
|
||||
"\\u0275\\u0275sanitizeStyle",
|
||||
"\\u0275\\u0275sanitizeUrl",
|
||||
"\\u0275\\u0275validateAttribute",
|
||||
"_DOM",
|
||||
"_SECURITY_SCHEMA",
|
||||
|
||||
Reference in New Issue
Block a user