-
Notifications
You must be signed in to change notification settings - Fork 29.2k
docs: add NG0904 error reference page for UNSAFE_VALUE_IN_RESOURCE_URL #71105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MeAkib
wants to merge
1
commit into
angular:main
Choose a base branch
from
MeAkib:docs/ng0904-error-page
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # Unsafe value used in a resource URL context | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
| The attributes that trigger this check are: | ||
|
|
||
| | Element | Attribute(s) | | ||
| | ---------- | ------------------ | | ||
| | `<base>` | `href` | | ||
| | `<embed>` | `src` | | ||
| | `<frame>` | `src` | | ||
| | `<iframe>` | `src` | | ||
| | `<link>` | `href` | | ||
| | `<object>` | `codebase`, `data` | | ||
|
|
||
| ## Debugging the error | ||
|
|
||
| The error is thrown when any plain string is bound to one of the attributes above, even a static-looking URL: | ||
|
|
||
| ```angular-ts | ||
| @Component({ | ||
| selector: 'app-root', | ||
| template: `<iframe [src]="url"></iframe>`, | ||
| }) | ||
| 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: `<iframe [src]="sanitized"></iframe>`, | ||
| }) | ||
| 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: `<iframe [src]="safeUrl"></iframe>`, | ||
| }) | ||
| 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). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.