Skip to content
This repository was archived by the owner on Sep 5, 2018. It is now read-only.

Commit fd71e14

Browse files
committed
Merge pull request #109 from yahoo/complex-contexts-resolution-docs
Update README for resolution regarding complex contexts
2 parents 3b99bed + 53388d4 commit fd71e14

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

README.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,51 @@ npm test
103103

104104
### Warnings and Workarounds
105105
When output expressions are found inside dangerous (yet-to-be-supported) contexts, we echo warnings and gracefully fallback to apply the default Handlebars [`escapeExpression()`](http://handlebarsjs.com/#html-escaping). These warnings are indications of potential security exploits, and thus require closer inspections. Instead of simply abusing `{{{raw_expression}}}` to suppress the warnings, here are some alternative suggestions to secure your applications.
106-
- Output expression in the `<script>` tag: <br/>`[WARNING] SecureHandlebars: Unsafe output expression found at scriptable <script> tag`
107-
```html
106+
- [WARNING] SecureHandlebars: Unsafe output expression found at scriptable `<script>` tag
107+
108+
```html
108109
<!-- Rewrite <script>var strJS = {{strJS}};</script> as: -->
109110
<input type="hidden" id="strJS" value="{{strJS}}">
110111
<script>var strJS = document.getElementById('strJS').value;</script>
111112
```
112-
- Output expression in an event attribute (e.g., `onclick=""`): <br/>`[WARNING] SecureHandlebars: Unsafe output expression found at onclick JavaScript event attribute`
113-
```html
113+
- [WARNING] SecureHandlebars: Unsafe output expression found at onclick JavaScript event attribute
114+
115+
- *Case 1.* the data is trusted, or will not be used as URI/HTML output
116+
117+
```html
114118
<!-- Rewrite <div onclick="hello({{name}})"> as: -->
115119
<div onclick="hello(this.getAttribute('data-name'))" data-name="{{name}}">
116120
```
117121

122+
- *Case 2A.* the data will be used as URI/HTML output<br/>The contextual analyzer does not (cannot) evaluate your JavaScript code, and thus lacks the information on which contexts the data will be ultimately used. Therefore, you must manually apply the escaping filters including `uriData` (a patched `encodeURI()`), `uriComponentData` (alias of `encodeURIComponent()`), and the [xss-filters](https://github.com/yahoo/xss-filters#client-side-browser) that are already registered as Handlebars helpers.
123+
```html
124+
<script>
125+
function search(url, keyword) {
126+
var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); // ...
127+
document.getElementById('status').innerHTML = 'Searching for ' + keyword;
128+
}
129+
</script>
130+
<!-- Rewrite <div onclick="search('/query?q={{keyword}}&lang=us', '{{keyword}}')"> as: -->
131+
<div onclick="search(this.getAttribute('data-url'), this.getAttribute('data-keyword'))"
132+
data-url="/query?q={{uriComponentData keyword}}&lang=us"
133+
data-keyword="{{inHTMLData keyword}}">
134+
```
135+
The manually-applied filters here are to pre-escape `{{keyword}}` depending on the ultimate output contexts, while the `{{` `}}` is still needed (**NOT** `{{{ }}}`) to let `secure-handlebars` automatically applies the escaping filter for the immediate attribute value context.
136+
137+
- *Case 2B.* Alternatively, just in case the output pre-escaping is what you want to avoid, please embed the [xss-filters](https://github.com/yahoo/xss-filters/#client-side-browser) on the client-side for filtering.
138+
```html
139+
<script src="dist/xss-filters.min.js"></script>
140+
<script>
141+
function search(keyword) {
142+
// ...
143+
document.getElementById('status').innerHTML = 'Searching for ' + xssFilters.inHTMLData(keyword);
144+
}
145+
</script>
146+
<div onclick="search(this.getAttribute('data-keyword'))" data-keyword="{{keyword}}">
147+
```
148+
149+
150+
118151

119152
## License
120153

0 commit comments

Comments
 (0)