-
Notifications
You must be signed in to change notification settings - Fork 28.2k
docs: add NG05201 #69504
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
Merged
Merged
docs: add NG05201 #69504
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,53 @@ | ||
| # Unsafe value used in a resource URL context | ||
|
|
||
| Angular throws this error when you bind a value to an attribute that loads an external resource — like `<iframe src>`, or `<link href>` — and that value hasn't been explicitly marked as trusted. | ||
|
|
||
| Resource URLs are treated differently from regular URLs. Angular can sanitize a plain URL (stripping `javascript:` schemes and so on), 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 | ||
|
|
||
| This error can also be thrown when calling `DomSanitizer.sanitize()` directly with `SecurityContext.RESOURCE_URL` and a plain string: | ||
|
|
||
| ```typescript | ||
| inject(DomSanitizer).sanitize(SecurityContext.RESOURCE_URL, 'https://example.com'); // throws NG05201 | ||
| ``` | ||
|
|
||
| `sanitize()` cannot make an arbitrary string safe in a resource URL context. Either use `bypassSecurityTrustResourceUrl` if the value is fully under your control, or bind the value to a regular URL attribute that Angular can sanitize. | ||
|
|
||
| Look for a binding to one of the attributes above or a direct `sanitize()` call: | ||
|
|
||
| ```html | ||
| <iframe [src]="userUrl"></iframe> | ||
| ``` | ||
|
|
||
| If the URL comes from a trusted, controlled source, mark it safe using `DomSanitizer.bypassSecurityTrustResourceUrl`: | ||
|
|
||
| ```typescript | ||
| import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser'; | ||
|
|
||
| @Component({ | ||
| selector: 'my-app', | ||
| }) | ||
| export class App { | ||
| safeUrl: SafeResourceUrl = inject(DomSanitizer).bypassSecurityTrustResourceUrl( | ||
| 'https://example.com/embed', | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ```html | ||
| <iframe [src]="safeUrl"></iframe> | ||
| ``` | ||
|
|
||
| `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. | ||
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.