Skip to content

Commit

Permalink
Explain fromLiteral method. (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
otherdaniel committed Oct 21, 2022
1 parent 2dec77e commit 2b089c8
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions explainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,26 @@ trustedTypes.createPolicy('default', {
This mechanism complements CSP's `'unsafe-inline'`, allowing the authors to enable strong security
controls in their application even if it occasionally uses `javascript:` URLs for legitimate purposes.

### Source Literals

XSS is an unintended modification of a site's source code. Wrapping literals
from the original JavaScript resource - which by definition aren't XSS - can be
cumbersome. Trusted Types provides a way to easily wrap source literals in
Trusted Types by using the tagged template syntax and the `fromLiteral` methods,
in a way that cannot be spoofed at runtime:

``` javascript
const value = TrustedHTML.fromLiteral`<b>Hello there.</b>`;
```

Note that template literals are passed as arrays of strings to the tag functions.
`fromLiteral` checks that a passed-in value is actually a template literal
and not dynamically constructed.

``` javascript
TrustedHTML.fromLiteral(["<b>Hello there.</b>"]); // Throws.
```

### DOM Sinks

* **HTML Contexts**: Given something like `typedef (DOMString or TrustedHTML) HTMLString`, we'd
Expand Down

0 comments on commit 2b089c8

Please sign in to comment.