Skip to content

Commit dab8c4a

Browse files
committed
Merge branch 'bodnia-feature/add-sanitize-html'
2 parents 48e7bc1 + 87438ad commit dab8c4a

File tree

9 files changed

+66
-65
lines changed

9 files changed

+66
-65
lines changed

.jshintrc

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"SwaggerUi": false,
3333
"jsyaml": false,
3434
"define": false,
35+
"sanitizeHtml": false,
3536

3637
// Global object
3738
// TODO: remove these

dist/lib/sanitize-html.min.js

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/swagger-ui.js

+23-28
Large diffs are not rendered by default.

dist/swagger-ui.min.js

+13-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function _dist() {
5050
return es.merge(
5151
gulp.src([
5252
'./node_modules/es5-shim/es5-shim.js',
53+
'./lib/sanitize-html.min.js',
5354
'./src/main/javascript/**/*.js',
5455
'./node_modules/swagger-client/browser/swagger-client.js'
5556
]),

lib/sanitize-html.min.js

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
],
1010
"description": "Swagger UI is a dependency-free collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API",
11-
"version": "2.2.2",
11+
"version": "2.2.3",
1212
"homepage": "http://swagger.io",
1313
"license": "Apache-2.0",
1414
"main": "dist/swagger-ui.js",

src/main/javascript/helpers/handlebars.js

+14-26
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,22 @@
11
'use strict';
22
/*jslint eqeq: true*/
33

4-
var _sanitize = function(html) {
5-
// Strip the script tags from the html and inline evenhandlers
6-
html = html.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
7-
html = html.replace(/(on\w+="[^"]*")*(on\w+='[^']*')*(on\w+=\w*\(\w*\))*/gi, '');
4+
Handlebars.registerHelper('sanitize', function (text) {
5+
var result;
86

9-
return html;
10-
};
7+
if (text === undefined) { return ''; }
118

12-
var sanitize =function (html) {
13-
var _html;
14-
15-
if ( _.isUndefined(html) || _.isNull(html)) {
16-
return new Handlebars.SafeString('');
17-
}
18-
19-
if (_.isNumber(html)) {
20-
return new Handlebars.SafeString(html);
21-
}
22-
23-
if (_.isObject(html)){
24-
_html = JSON.stringify(html);
25-
return new Handlebars.SafeString(JSON.parse(_sanitize(_html)));
26-
}
27-
28-
return new Handlebars.SafeString(_sanitize(html));
29-
};
9+
result = sanitizeHtml(text, {
10+
allowedTags: [ 'div', 'span', 'b', 'i', 'em', 'strong', 'a' ],
11+
allowedAttributes: {
12+
'div': [ 'class' ],
13+
'span': [ 'class' ],
14+
'a': [ 'href' ]
15+
}
16+
});
3017

31-
Handlebars.registerHelper('sanitize', sanitize);
18+
return new Handlebars.SafeString(result);
19+
});
3220

3321
Handlebars.registerHelper('renderTextParam', function(param) {
3422
var result, type = 'text', idAtt = '';
@@ -55,7 +43,7 @@ Handlebars.registerHelper('renderTextParam', function(param) {
5543
idAtt = ' id=\'' + valueId + '\'';
5644
}
5745

58-
defaultValue = sanitize(defaultValue);
46+
defaultValue = sanitizeHtml(defaultValue);
5947

6048
if(isArray) {
6149
result = '<textarea class=\'body-textarea' + (param.required ? ' required' : '') + '\' name=\'' + name + '\'' + idAtt + dataVendorExtensions;

src/main/javascript/view/MainView.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ SwaggerUi.Views.MainView = Backbone.View.extend({
9696
id = id + '_' + counter;
9797
counter += 1;
9898
}
99-
resource.id = SwaggerUi.utils.sanitize(id);
99+
resource.id = sanitizeHtml(id);
100100
resources[id] = resource;
101101
this.addResource(resource, this.model.auths);
102102
}

0 commit comments

Comments
 (0)