fix(core): allow binding null and undefined to resource URL properties

Binding `null` or `undefined` to a resource URL property such as
`<iframe [src]>` throws NG0904 ("unsafe value used in a resource URL
context"), even though there is nothing unsafe about clearing the binding.
The inlined `ɵɵsanitizeResourceUrl` only accepts values marked trusted with
`bypassSecurityTrustResourceUrl` and rejects everything else, including
nullish values. This forces users to bind a trusted placeholder such as
`about:blank` instead of simply using `undefined`.

The same binding already works when a custom `Sanitizer` is provided,
because `DomSanitizer.sanitize` returns `null` for nullish values, which is
then mapped to an empty string. Apply the same handling to the inlined
sanitizer: `null` and `undefined` are mapped to an empty string, which the
browser treats as `about:blank` for `<iframe src>`. Since
`setElementAttribute` runs the sanitizer before removing an attribute for a
nullish value, this also fixes `[attr.src]` bindings on resource URL
attributes.

Fixes #42527

(cherry picked from commit 088cdd2cab)
This commit is contained in:
aparziale
2026-11-10 23:59:34 +00:00
committed by Jessica Janiuk
parent 14cf46b79a
commit 7c883e02bd
3 changed files with 56 additions and 2 deletions
@@ -124,10 +124,12 @@ export function ɵɵsanitizeUrl(unsafeUrl: any): string {
* A `url` sanitizer which only lets trusted `url`s through.
*
* This passes only `url`s marked trusted by calling {@link bypassSanitizationTrustResourceUrl}.
* `null` and `undefined` are not considered unsafe and are mapped to an empty string, so that a
* resource URL binding can be cleared without having to provide a trusted placeholder value.
*
* @param unsafeResourceUrl untrusted `url`, typically from the user.
* @returns `url` string which is safe to bind to the `src` properties such as `<img src>`, because
* only trusted `url`s have been allowed to pass.
* @returns `url` string which is safe to bind to the `src` properties such as `<iframe src>`,
* because only trusted `url`s have been allowed to pass.
*
* @codeGenApi
*/
@@ -138,6 +140,11 @@ export function ɵɵsanitizeResourceUrl(unsafeResourceUrl: any): TrustedScriptUR
sanitizer.sanitize(SecurityContext.RESOURCE_URL, unsafeResourceUrl) || '',
);
}
if (unsafeResourceUrl == null) {
// Consistent with `DomSanitizer.sanitize`, which returns `null` for nullish values: there is
// nothing to sanitize, and setting the property to `''` avoids a request to `'undefined'`.
return trustedScriptURLFromStringBypass('');
}
if (allowSanitizationBypassAndThrow(unsafeResourceUrl, BypassType.ResourceUrl)) {
return trustedScriptURLFromStringBypass(unwrapSafeValue(unsafeResourceUrl));
}
@@ -201,6 +201,48 @@ describe('security integration tests', function () {
checkEscapeOfHrefProperty(fixture);
});
function checkNullishResourceUrl(
fixture: ComponentFixture<any>,
expectedNullishSrc: string | null,
) {
const e = fixture.debugElement.children[0].nativeElement;
const ci = fixture.componentInstance;
ci.ctxProp = undefined;
fixture.changeDetectorRef.markForCheck();
expect(() => fixture.detectChanges()).not.toThrow();
expect(e.getAttribute('src')).toEqual(expectedNullishSrc);
ci.ctxProp = null;
fixture.changeDetectorRef.markForCheck();
expect(() => fixture.detectChanges()).not.toThrow();
expect(e.getAttribute('src')).toEqual(expectedNullishSrc);
ci.ctxProp = 'https://example.com/embed';
fixture.changeDetectorRef.markForCheck();
expect(() => fixture.detectChanges()).toThrowError(
/NG0904: unsafe value used in a resource URL context/,
);
}
it('should allow binding null and undefined to resource URL properties', () => {
const template = `<iframe [src]="ctxProp"></iframe>`;
TestBed.overrideComponent(SecuredComponent, {set: {template}});
const fixture = TestBed.createComponent(SecuredComponent);
// The property is set to an empty string, which the browser treats as `about:blank`.
checkNullishResourceUrl(fixture, '');
});
it('should allow binding null and undefined to resource URL attributes', () => {
const template = `<iframe [attr.src]="ctxProp"></iframe>`;
TestBed.overrideComponent(SecuredComponent, {set: {template}});
const fixture = TestBed.createComponent(SecuredComponent);
// Nullish attribute values remove the attribute without going through the sanitizer.
checkNullishResourceUrl(fixture, null);
});
it('should escape unsafe attributes', () => {
const template = `<a [attr.href]="ctxProp">Link Title</a>`;
TestBed.overrideComponent(SecuredComponent, {set: {template}});
@@ -91,6 +91,11 @@ describe('sanitization', () => {
).toEqual('javascript:true');
});
it('should map null and undefined resourceUrls to an empty string', () => {
expect(ɵɵsanitizeResourceUrl(null).toString()).toEqual('');
expect(ɵɵsanitizeResourceUrl(undefined).toString()).toEqual('');
});
it('should sanitize style', () => {
expect(ɵɵsanitizeStyle('red')).toEqual('red');
expect(ɵɵsanitizeStyle(new Wrap('red'))).toEqual('red');