-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
60 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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,31 @@ | ||
# Expression Context | ||
|
||
Datastar is _not_ exposed in the global scope. Everything you need to do should be possible via Datastar expressions in [attribute plugins](/reference/attribute_plugins) and [`datastar-execute-script`](/reference/sse_events#datastar-execute-script) SSE events. | ||
|
||
Every Datastar expression is evaluated using a context `ctx`. This means that `ctx` is exposed in expressions and can be used to access properties and methods within the current context. | ||
|
||
You should use `ctx` sparingly, and only when you can't achieve the desired behavior using signals. | ||
|
||
### `ctx.el` | ||
|
||
The current element being processed. | ||
|
||
### `ctx.plugin` | ||
|
||
The current plugin being processed. | ||
|
||
### `ctx.signals` | ||
|
||
The signals root object that contains functions for accessing and modifying signals. Signals can be accessed using the `signal` method. The following is the equivalent of an expression containing only `$foo`. | ||
|
||
```html | ||
<div data-signals-foo="1"> | ||
<div data-text="ctx.signals.signal('foo').value"></div> | ||
</div> | ||
``` | ||
|
||
Here is how you can output the existing signals in JSON format, which can be useful when troubleshooting an issue. | ||
|
||
```html | ||
<pre data-text="ctx.signals.JSON()"></pre> | ||
``` |
This file contains 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,23 @@ | ||
# Security | ||
|
||
[Datastar expressions](/guide/datastar_expressions) are strings that are evaluated in a sandboxed context, in which `ctx` represents the Datastar context. This means that JavaScript can be used in Datastar expressions. | ||
|
||
## Escape User Input | ||
|
||
The first rule of security is to never trust user input. This is especially true when using Datastar expressions, which can execute arbitrary JavaScript. | ||
|
||
When using Datastar expressions, you should always escape user input. This is to prevent, among other issues, Cross Site Scripting (XSS) attacks. | ||
|
||
## Ignore Unsafe Input | ||
|
||
If, for some reason, you cannot escape unsafe user input, you should ignore it using the [`data-star-ignore`](/reference/attribute_plugins#data-star-ignore) attribute. This tells Datastar to ignore an element and its descendants when processing DOM nodes. | ||
|
||
## Content Security Policy | ||
|
||
When using a [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) (CSP), `unsafe-eval` must be allowed for scripts, since Datastar evaluates expressions inline. | ||
|
||
```html | ||
<meta http-equiv="Content-Security-Policy" | ||
content="script-src 'self' 'unsafe-eval';" | ||
> | ||
``` |