Skip to content
This repository was archived by the owner on Apr 24, 2019. It is now read-only.

Commit 729cac4

Browse files
committed
Merge pull request #3 from yahoo/use-xss-filters-v1.2.0
release v1.0.7
2 parents e217307 + 3f0ee4a commit 729cac4

File tree

9 files changed

+422
-91
lines changed

9 files changed

+422
-91
lines changed

.travis.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
language: node_js
22
node_js:
3+
- '0.12'
34
- '0.11'
45
- '0.10'
6+
notifications:
7+
email:
8+
recipients:
9+
- neraliu@yahoo-inc.com
10+
- adon@yahoo-inc.com
11+
on_success: change
12+
on_failure: always
13+
after_success:
14+
- test $(cat $TRAVIS_BUILD_DIR/package.json | grep version | awk '{print $2}' | sed
15+
's/"//g' | sed 's/,//g' | awk '{print "v"$1}' ) = $TRAVIS_TAG && export VALID_VERSION=true
16+
deploy:
17+
provider: npm
18+
email: neraliu@yahoo-inc.com
19+
api_key:
20+
secure: LlhF8dO3Plt023CjeQRumw9ZgBNsE2KWanHrRY/SnMoFxWgcVYEbzX3Ze5CtoRvtm8CH+3z71L11BaL52GN0qm+0zX93h4JXTYETloEzX2pdJpiBXATzEKcscLC5bfsarh2I/UlkSPQP2b+mkAi3RJu/rjF4CBjbpwka90aF1k0=
21+
on:
22+
condition: $VALID_VERSION = true
23+
tags: true
24+
branch: master

README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
secure-handlebars-helpers
22
=========================
3-
This handy *client-side* script registers the required secure XSS output filters as handlebars' helpers, and is designed ONLY for
4-
- templates processed with context-sensitive filters automatically inserted (e.g., `<title>{{{yd title}}}</title>`) using [context-parser-handlebars](https://www.npmjs.com/package/context-parser-handlebars).
5-
63
[![npm version][npm-badge]][npm]
74
[![dependency status][dep-badge]][dep-status]
5+
[![Build Status](https://travis-ci.org/yahoo/secure-handlebars-helpers.svg?branch=master)](https://travis-ci.org/yahoo/secure-handlebars-helpers)
6+
7+
This handy *client-side* script registers the required [XSS output filtering functions](https://www.npmjs.com/package/xss-filters) as handlebars' helpers, and is designed ONLY for templates that already have the context-sensitive filter markup (e.g., `<title>{{{yd title}}}</title>`) automatically inserted using [secure-handlebars](https://www.npmjs.com/package/secure-handlebars).
88

9-
[npm]: https://www.npmjs.org/package/secure-handlebars-helpers
10-
[npm-badge]: https://img.shields.io/npm/v/secure-handlebars-helpers.svg?style=flat-square
11-
[dep-status]: https://david-dm.org/yahoo/secure-handlebars-helpers
12-
[dep-badge]: https://img.shields.io/david/yahoo/secure-handlebars-helpers.svg?style=flat-square
13-
149
## Quick Start
1510

1611
### Client-side (browser)
@@ -35,9 +30,13 @@ To contribute, you will make changes in [`src/`](./src) and [`tests/`](./tests),
3530
- ```$ npm run-script build``` to build the standalone JavaScript for client-side use
3631
- ```$ npm test``` to run the tests
3732

38-
### Build
39-
[![Build Status](https://travis-ci.org/yahoo/secure-handlebars-helpers.svg?branch=master)](https://travis-ci.org/yahoo/secure-handlebars-helpers)
40-
4133
## License
4234
This software is free to use under the Yahoo BSD license.
4335
See the [LICENSE file](./LICENSE) for license text and copyright information.
36+
37+
38+
39+
[npm]: https://www.npmjs.org/package/secure-handlebars-helpers
40+
[npm-badge]: https://img.shields.io/npm/v/secure-handlebars-helpers.svg?style=flat-square
41+
[dep-status]: https://david-dm.org/yahoo/secure-handlebars-helpers
42+
[dep-badge]: https://img.shields.io/david/yahoo/secure-handlebars-helpers.svg?style=flat-square

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "secure-handlebars-helpers",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"main": "dist/secure-handlebars-helpers.min.js",
55
"authors": [
66
"adon <adon@yahoo-inc.com>"

dist/secure-handlebars-helpers.js

Lines changed: 287 additions & 68 deletions
Large diffs are not rendered by default.

dist/secure-handlebars-helpers.min.1.0.7.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/secure-handlebars-helpers.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "secure-handlebars-helpers",
33
"description": "Client-side XSS filters for templates processed by context-parser-handlebars",
4-
"version": "1.0.6",
4+
"version": "1.0.7",
55
"licenses": [
66
{
77
"type": "BSD",
@@ -42,7 +42,7 @@
4242
"test": "grunt test"
4343
},
4444
"dependencies": {
45-
"xss-filters": "^1.0.6"
45+
"xss-filters": "^1.2.0"
4646
},
4747
"devDependencies": {
4848
"grunt": "^0.4.5",

src/secure-handlebars-helpers.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,26 @@ Authors: Nera Liu <neraliu@yahoo-inc.com>
1111
/* global Handlebars, privFilters */
1212

1313
(function(Handlebars, filterNames, i, name){
14-
if (!Handlebars || !Handlebars.registerHelper) {
14+
15+
if (!Handlebars || !Handlebars.registerHelper || !Handlebars.Utils.escapeExpression) {
1516
throw new ReferenceError('Handlebars is not defined');
1617
}
18+
19+
Handlebars.registerHelper('y', Handlebars.Utils.escapeExpression);
20+
21+
// don't escape SafeStrings, since they're already safe according to Handlebars
22+
// Reference: https://github.com/wycats/handlebars.js/blob/master/lib/handlebars/utils.js#L63-L82
23+
function safeStringCompatibleFilter (filterName) {
24+
return function (s) {
25+
// Unlike escapeExpression(), return s instead of s.toHTML() since downstream
26+
// filters of the same chain has to be disabled too.
27+
// Handlebars will invoke SafeString.toString() at last during data binding
28+
return (s && s.toHTML) ? s : privFilters[filterName](s);
29+
};
30+
}
31+
1732
// expect privFilters are available
1833
for (; (name = filterNames[i]); i++) {
19-
Handlebars.registerHelper(name, privFilters[name]);
34+
Handlebars.registerHelper(name, safeStringCompatibleFilter(name));
2035
}
21-
})(Handlebars, ['y','yd','yc','yavd','yavs','yavu','yu','yuc','yubl','yufull'], 0);
36+
})(Handlebars, ['yd', 'yc', 'yavd', 'yavs', 'yavu', 'yu', 'yuc', 'yubl', 'yufull', 'yceu', 'yced', 'yces', 'yceuu', 'yceud', 'yceus'], 0);

tests/integration/spec/helpers.js

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ var filterNames = [
1515
'yd', 'yc',
1616
'yavd', 'yavs', 'yavu',
1717
'yu', 'yuc',
18-
'yubl', 'yufull'
18+
'yubl', 'yufull',
19+
'yceu', 'yced', 'yces',
20+
'yceuu', 'yceud', 'yceus'
1921
];
2022

2123
console.log('Integration test with Handlebars v' + Handlebars.VERSION);
@@ -54,12 +56,42 @@ describe("secure handlebars helpers: error tests", function() {
5456
// yubl will not be independently used
5557
// expect(filter.yubl()).to.eql('undefined');
5658

59+
expect(filter.yceu()).to.eql('undefined');
60+
expect(filter.yced()).to.eql('undefined');
61+
expect(filter.yces()).to.eql('undefined');
62+
expect(filter.yceuu()).to.eql('undefined');
63+
expect(filter.yceud()).to.eql('undefined');
64+
expect(filter.yceus()).to.eql('undefined');
5765

58-
expect(filter.y()).to.eql('undefined');
66+
expect(filter.y()).to.eql('');
5967
});
6068

61-
});
69+
it('filters handling of null input', function() {
70+
71+
expect(filter.yd(null)).to.eql('null');
72+
expect(filter.yc(null)).to.eql('null');
73+
74+
expect(filter.yavd(null)).to.eql('null');
75+
expect(filter.yavs(null)).to.eql('null');
76+
expect(filter.yavu(null)).to.eql('null');
77+
78+
expect(filter.yu(null)).to.eql('null');
79+
expect(filter.yuc(null)).to.eql('null');
80+
expect(filter.yufull(null)).to.eql('null');
81+
// yubl will not be independently used
82+
// expect(filter.yubl()).to.eql('null');
83+
84+
expect(filter.yceu(null)).to.eql('null');
85+
expect(filter.yced(null)).to.eql('null');
86+
expect(filter.yces(null)).to.eql('null');
87+
expect(filter.yceuu(null)).to.eql('null');
88+
expect(filter.yceud(null)).to.eql('null');
89+
expect(filter.yceus(null)).to.eql('null');
6290

91+
expect(filter.y(null)).to.eql('');
92+
});
93+
94+
});
6395

6496
/* the functional test is tested against in the contextparse-filters */
6597
describe("secure handlebars helpers: compilation tests", function() {
@@ -108,6 +140,47 @@ describe("secure handlebars helpers: compilation tests", function() {
108140
expect(output).to.eql('<div id="divid"></div>');
109141
});
110142

143+
144+
it('filter yceu test', function() {
145+
var html = '<div style="background: {{{yceu value}}}"></div>';
146+
var json = {value: "red"};
147+
var output = compilation_test(html, json);
148+
expect(output).to.eql('<div style="background: red"></div>');
149+
});
150+
it('filter yced test', function() {
151+
var html = '<div style="background: &quot;{{{yced value}}}&quot;"></div>';
152+
var json = {value: "red"};
153+
var output = compilation_test(html, json);
154+
expect(output).to.eql('<div style="background: &quot;red&quot;"></div>');
155+
});
156+
it('filter yces test', function() {
157+
var html = '<div style="background: \'{{{yces value}}}\'"></div>';
158+
var json = {value: "red"};
159+
var output = compilation_test(html, json);
160+
expect(output).to.eql('<div style="background: \'red\'"></div>');
161+
});
162+
163+
164+
it('filter yceuu test', function() {
165+
var html = '<div style="background: url({{{yceuu value}}})"></div>';
166+
var json = {value: "javascript:alert(1)"};
167+
var output = compilation_test(html, json);
168+
expect(output).to.eql('<div style="background: url(##javascript:alert\\28 1\\29 )"></div>');
169+
});
170+
it('filter yceud test', function() {
171+
var html = '<div style="background: url(&quot;{{{yceud value}}}&quot;)"></div>';
172+
var json = {value: "javascript:alert(1)"};
173+
var output = compilation_test(html, json);
174+
expect(output).to.eql('<div style="background: url(&quot;##javascript:alert(1)&quot;)"></div>');
175+
});
176+
it('filter yceus test', function() {
177+
var html = '<div style="background: url(\'{{{yceus value}}}\')"></div>';
178+
var json = {value: "javascript:alert(1)"};
179+
var output = compilation_test(html, json);
180+
expect(output).to.eql('<div style="background: url(\'##javascript:alert(1)\')"></div>');
181+
});
182+
183+
111184
it('chained filter - yubl yavd yufull test', function() {
112185
var html = '<a href="{{{yubl (yavd (yufull url))}}}">link</a>';
113186
var json = {url: "javascript :alert(0);"};
@@ -175,7 +248,7 @@ describe("secure handlebars helpers: compilation tests", function() {
175248
var html = '<script>{{{y script}}}</script>';
176249
var json = {script: "&<>'\"&<>'\""};
177250
var output = compilation_test(html, json);
178-
expect(output).to.eql('<script>&amp;&lt;&gt;&#39;&quot;&amp;&lt;&gt;&#39;&quot;</script>');
251+
expect(output).to.eql('<script>&amp;&lt;&gt;&#x27;&quot;&amp;&lt;&gt;&#x27;&quot;</script>');
179252
});
180253

181254

0 commit comments

Comments
 (0)