Skip to content

Commit

Permalink
Merge branch 'bodnia-feature/add-sanitize-html'
Browse files Browse the repository at this point in the history
  • Loading branch information
fehguy committed Sep 1, 2016
2 parents 48e7bc1 + 87438ad commit dab8c4a
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 65 deletions.
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"SwaggerUi": false,
"jsyaml": false,
"define": false,
"sanitizeHtml": false,

// Global object
// TODO: remove these
Expand Down
6 changes: 6 additions & 0 deletions dist/lib/sanitize-html.min.js

Large diffs are not rendered by default.

51 changes: 23 additions & 28 deletions dist/swagger-ui.js

Large diffs are not rendered by default.

22 changes: 13 additions & 9 deletions dist/swagger-ui.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function _dist() {
return es.merge(
gulp.src([
'./node_modules/es5-shim/es5-shim.js',
'./lib/sanitize-html.min.js',
'./src/main/javascript/**/*.js',
'./node_modules/swagger-client/browser/swagger-client.js'
]),
Expand Down
6 changes: 6 additions & 0 deletions lib/sanitize-html.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
],
"description": "Swagger UI is a dependency-free collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API",
"version": "2.2.2",
"version": "2.2.3",
"homepage": "http://swagger.io",
"license": "Apache-2.0",
"main": "dist/swagger-ui.js",
Expand Down
40 changes: 14 additions & 26 deletions src/main/javascript/helpers/handlebars.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
'use strict';
/*jslint eqeq: true*/

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

return html;
};
if (text === undefined) { return ''; }

var sanitize =function (html) {
var _html;

if ( _.isUndefined(html) || _.isNull(html)) {
return new Handlebars.SafeString('');
}

if (_.isNumber(html)) {
return new Handlebars.SafeString(html);
}

if (_.isObject(html)){
_html = JSON.stringify(html);
return new Handlebars.SafeString(JSON.parse(_sanitize(_html)));
}

return new Handlebars.SafeString(_sanitize(html));
};
result = sanitizeHtml(text, {
allowedTags: [ 'div', 'span', 'b', 'i', 'em', 'strong', 'a' ],
allowedAttributes: {
'div': [ 'class' ],
'span': [ 'class' ],
'a': [ 'href' ]
}
});

Handlebars.registerHelper('sanitize', sanitize);
return new Handlebars.SafeString(result);
});

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

defaultValue = sanitize(defaultValue);
defaultValue = sanitizeHtml(defaultValue);

if(isArray) {
result = '<textarea class=\'body-textarea' + (param.required ? ' required' : '') + '\' name=\'' + name + '\'' + idAtt + dataVendorExtensions;
Expand Down
2 changes: 1 addition & 1 deletion src/main/javascript/view/MainView.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ SwaggerUi.Views.MainView = Backbone.View.extend({
id = id + '_' + counter;
counter += 1;
}
resource.id = SwaggerUi.utils.sanitize(id);
resource.id = sanitizeHtml(id);
resources[id] = resource;
this.addResource(resource, this.model.auths);
}
Expand Down

0 comments on commit dab8c4a

Please sign in to comment.