From 32a7603812138663fd2d7bb1131fd079df34a986 Mon Sep 17 00:00:00 2001 From: Shuaib Hasan Akib Date: Thu, 1 Oct 2026 00:45:39 +0600 Subject: [PATCH] docs: add NG0904 error reference page for UNSAFE_VALUE_IN_RESOURCE_URL Adds a reference page for `NG0904`, thrown when a plain string is bound to a resource URL attribute (such as ``, +}) +export class App { + url = 'https://example.com/embed'; +} +``` + +Passing the value through `DomSanitizer.sanitize()` with `SecurityContext.URL` does not help. `sanitize()` returns a plain string, and a plain string is never accepted in a resource URL context: + +```angular-ts +@Component({ + selector: 'app-root', + template: ``, +}) +export class App { + private readonly sanitizer = inject(DomSanitizer); + sanitized = this.sanitizer.sanitize(SecurityContext.URL, 'https://example.com/embed'); // still throws NG0904 +} +``` + +Calling `sanitize()` with `SecurityContext.RESOURCE_URL` instead throws too, but with [NG05201](errors/NG05201), because `sanitize()` cannot make an arbitrary string safe in a resource URL context. + +If the URL comes from a trusted, controlled source, mark it safe using `DomSanitizer.bypassSecurityTrustResourceUrl`: + +```angular-ts +import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser'; + +@Component({ + selector: 'app-root', + template: ``, +}) +export class App { + safeUrl: SafeResourceUrl = inject(DomSanitizer).bypassSecurityTrustResourceUrl( + 'https://example.com/embed', + ); +} +``` + +CRITICAL: `bypassSecurityTrustResourceUrl` is an escape hatch, not a sanitizer. Never pass user-supplied URLs through it — that would allow an attacker to load arbitrary content, including malicious scripts. Only use it when the value is fully under your control. + +For more information, see the [Security guide](best-practices/security#trusting-safe-values). diff --git a/adev/src/content/reference/errors/overview.md b/adev/src/content/reference/errors/overview.md index 9fd0eca85df..c18528441f9 100644 --- a/adev/src/content/reference/errors/overview.md +++ b/adev/src/content/reference/errors/overview.md @@ -30,6 +30,7 @@ | `NG0602` | [Disallowed function call inside reactive context](errors/NG0602) | | `NG0750` | [@defer dependencies failed to load](errors/NG0750) | | `NG0751` | [@defer behavior when HMR is enabled](errors/NG0751) | +| `NG0904` | [Unsafe value used in a resource URL context](errors/NG0904) | | `NG0910` | [Unsafe bindings on an iframe element](errors/NG0910) | | `NG0912` | [Component ID generation collision](errors/NG0912) | | `NG0913` | [Runtime Performance Warnings](errors/NG0913) | diff --git a/goldens/public-api/core/errors.api.md b/goldens/public-api/core/errors.api.md index bcceb171c90..4f04e07c256 100644 --- a/goldens/public-api/core/errors.api.md +++ b/goldens/public-api/core/errors.api.md @@ -198,7 +198,7 @@ export const enum RuntimeErrorCode { // (undocumented) UNSAFE_ATTRIBUTE_BINDING = -910, // (undocumented) - UNSAFE_VALUE_IN_RESOURCE_URL = 904, + UNSAFE_VALUE_IN_RESOURCE_URL = -904, // (undocumented) UNSAFE_VALUE_IN_SCRIPT = 905, // (undocumented) diff --git a/packages/core/src/errors.ts b/packages/core/src/errors.ts index 5aa20d0321f..04c69365401 100644 --- a/packages/core/src/errors.ts +++ b/packages/core/src/errors.ts @@ -118,7 +118,7 @@ export const enum RuntimeErrorCode { NO_SUPPORTING_DIFFER_FACTORY = 901, VIEW_ALREADY_ATTACHED = 902, INVALID_INHERITANCE = 903, - UNSAFE_VALUE_IN_RESOURCE_URL = 904, + UNSAFE_VALUE_IN_RESOURCE_URL = -904, UNSAFE_VALUE_IN_SCRIPT = 905, MISSING_GENERATED_DEF = 906, TYPE_IS_NOT_STANDALONE = 907,