Skip to content

Commit 7b25bba

Browse files
committed
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 `<iframe src>`) without being marked trusted via `bypassSecurityTrustResourceUrl`. The error code is made negative so the error message links to the new page. Fixes angular#71092
1 parent 9028705 commit 7b25bba

4 files changed

Lines changed: 68 additions & 2 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Unsafe value used in a resource URL context
2+
3+
Angular throws this error, with the message `unsafe value used in a resource URL context`, when a template or host binding sets an attribute that loads an external resource — like `<iframe src>` or `<link href>` — to a value that hasn't been explicitly marked as trusted.
4+
5+
Resource URLs are treated differently from regular URLs. Angular can sanitize a plain URL, but it can't make an arbitrary resource URL safe because these attributes cause the browser to fetch and execute external content. So Angular rejects untrusted values rather than trying to sanitize them.
6+
7+
The attributes that trigger this check are:
8+
9+
| Element | Attribute(s) |
10+
| ---------- | ------------------ |
11+
| `<base>` | `href` |
12+
| `<embed>` | `src` |
13+
| `<frame>` | `src` |
14+
| `<iframe>` | `src` |
15+
| `<link>` | `href` |
16+
| `<object>` | `codebase`, `data` |
17+
18+
## Debugging the error
19+
20+
The error is thrown when any plain string is bound to one of the attributes above, even a static-looking URL:
21+
22+
```angular-ts
23+
@Component({
24+
selector: 'app-root',
25+
template: `<iframe [src]="url"></iframe>`,
26+
})
27+
export class App {
28+
url = 'https://example.com/embed';
29+
}
30+
```
31+
32+
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:
33+
34+
```angular-ts
35+
@Component({
36+
selector: 'app-root',
37+
template: `<iframe [src]="sanitized"></iframe>`,
38+
})
39+
export class App {
40+
private readonly sanitizer = inject(DomSanitizer);
41+
sanitized = this.sanitizer.sanitize(SecurityContext.URL, 'https://example.com/embed'); // still throws NG0904
42+
}
43+
```
44+
45+
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.
46+
47+
If the URL comes from a trusted, controlled source, mark it safe using `DomSanitizer.bypassSecurityTrustResourceUrl`:
48+
49+
```angular-ts
50+
import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';
51+
52+
@Component({
53+
selector: 'app-root',
54+
template: `<iframe [src]="safeUrl"></iframe>`,
55+
})
56+
export class App {
57+
safeUrl: SafeResourceUrl = inject(DomSanitizer).bypassSecurityTrustResourceUrl(
58+
'https://example.com/embed',
59+
);
60+
}
61+
```
62+
63+
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.
64+
65+
For more information, see the [Security guide](best-practices/security#trusting-safe-values).

‎adev/src/content/reference/errors/overview.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
| `NG0602` | [Disallowed function call inside reactive context](errors/NG0602) |
3131
| `NG0750` | [@defer dependencies failed to load](errors/NG0750) |
3232
| `NG0751` | [@defer behavior when HMR is enabled](errors/NG0751) |
33+
| `NG0904` | [Unsafe value used in a resource URL context](errors/NG0904) |
3334
| `NG0910` | [Unsafe bindings on an iframe element](errors/NG0910) |
3435
| `NG0912` | [Component ID generation collision](errors/NG0912) |
3536
| `NG0913` | [Runtime Performance Warnings](errors/NG0913) |

‎goldens/public-api/core/errors.api.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export const enum RuntimeErrorCode {
198198
// (undocumented)
199199
UNSAFE_ATTRIBUTE_BINDING = -910,
200200
// (undocumented)
201-
UNSAFE_VALUE_IN_RESOURCE_URL = 904,
201+
UNSAFE_VALUE_IN_RESOURCE_URL = -904,
202202
// (undocumented)
203203
UNSAFE_VALUE_IN_SCRIPT = 905,
204204
// (undocumented)

‎packages/core/src/errors.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export const enum RuntimeErrorCode {
118118
NO_SUPPORTING_DIFFER_FACTORY = 901,
119119
VIEW_ALREADY_ATTACHED = 902,
120120
INVALID_INHERITANCE = 903,
121-
UNSAFE_VALUE_IN_RESOURCE_URL = 904,
121+
UNSAFE_VALUE_IN_RESOURCE_URL = -904,
122122
UNSAFE_VALUE_IN_SCRIPT = 905,
123123
MISSING_GENERATED_DEF = 906,
124124
TYPE_IS_NOT_STANDALONE = 907,

0 commit comments

Comments
 (0)