You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+104-4Lines changed: 104 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,9 +10,9 @@ I've experimented with other existing libraries (ejs templates, etc.) but wanted
10
10
11
11
## Getting Started
12
12
13
-
1.copy the /dist/faintly.js file to the scripts directory of your project
14
-
2.in the folder for your block, add a `blockName.html` file for the block template
15
-
3.in your block javascript, call the `renderBlock` function:
13
+
1.Copy the `/dist/faintly.js` and `/dist/faintly.security.js` files to the scripts directory of your project
14
+
2.In the folder for your block, add a `blockName.html` file for the block template
15
+
3.In your block javascript, call the `renderBlock` function:
16
16
17
17
```
18
18
import { renderBlock } from '../scripts/faintly.js';
@@ -55,6 +55,7 @@ The rendering context is a javascript object used to provide data to the templat
55
55
* template
56
56
* path - the path to the template being rendered
57
57
* name - the template name, if there is one
58
+
* security - security configuration (see Security section below)
58
59
59
60
When in a repeat loop, it will also include:
60
61
@@ -70,6 +71,105 @@ When in a repeat loop, it will also include:
70
71
> [!NOTE]
71
72
> Because element attributes are case-insensitive, context names are converted to lower case. e.g. `data-fly-test.myTest` will be set in the context as `mytest`.
72
73
74
+
## Security
75
+
76
+
Faintly includes built-in security features to help protect against XSS (Cross-Site Scripting) attacks. By default, security is **enabled** and provides:
77
+
78
+
***Attribute sanitization** - Blocks dangerous attributes like event handlers (`onclick`, `onerror`, etc.) and `srcdoc`
79
+
***URL scheme validation** - Restricts URLs in attributes like `href` and `src` to safe schemes (`http:`, `https:`, `mailto:`, `tel:`)
80
+
***Same-origin enforcement** - Template includes are restricted to same-origin URLs only
81
+
82
+
### Default Security
83
+
84
+
When you call `renderBlock()` without a security context, default security is automatically applied:
- ✅ Malicious URL schemes (`javascript:`, `data:` by default)
160
+
- ✅ Cross-origin template includes
161
+
162
+
**Trusted (by design):**
163
+
- The rendering context you provide is fully trusted
164
+
- Templates fetched from your same-origin are trusted
165
+
- DOM Node objects provided in context are inserted directly
166
+
167
+
> [!WARNING]
168
+
> **Be extremely careful when adding user-supplied data to the rendering context.** URL parameters, form inputs, cookies, and other user-controlled data should be validated and sanitized before adding to the context. The context is fully trusted, so untrusted data placed in it can bypass security protections.
169
+
170
+
> [!TIP]
171
+
> Security works best in layers. Faintly's security helps prevent common XSS vectors, but you should also: validate and sanitize user input before adding it to context, use Content Security Policy headers, and follow secure coding practices.
172
+
73
173
## Directives
74
174
75
175
Faintly supports the following directives.
@@ -101,4 +201,4 @@ For `data-fly-include`, HTML text, and normal attributes, wrap your expression i
101
201
102
202
Escaping: use a leading backslash to prevent evaluation of an expression in text/attributes, e.g. `\${some.value}` will remain literal `${some.value}`.
103
203
104
-
In all other `data-fly-*` attributes, just set the expression directly as the attribute value, no wrapping needed.
204
+
In all other `data-fly-*` attributes, just set the expression directly as the attribute value, no wrapping needed.
0 commit comments