Skip to content

Commit a906cff

Browse files
authored
Merge pull request #2334 from bodnia/feature/xss
fix for xss issue
2 parents 826bfcc + a1aea70 commit a906cff

28 files changed

+610
-532
lines changed

dist/css/print.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,11 @@
832832
.swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.heading ul.options li a {
833833
text-decoration: none;
834834
}
835+
.swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.heading ul.options li a .markdown p {
836+
color: inherit;
837+
padding: 0;
838+
line-height: inherit;
839+
}
835840
.swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.heading ul.options li.access {
836841
color: black;
837842
}

dist/css/screen.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,11 @@
832832
.swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.heading ul.options li a {
833833
text-decoration: none;
834834
}
835+
.swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.heading ul.options li a .markdown p {
836+
color: inherit;
837+
padding: 0;
838+
line-height: inherit;
839+
}
835840
.swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.heading ul.options li.access {
836841
color: black;
837842
}

dist/swagger-ui.js

Lines changed: 245 additions & 210 deletions
Large diffs are not rendered by default.

dist/swagger-ui.min.js

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

src/main/html/css/print.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,11 @@
832832
.swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.heading ul.options li a {
833833
text-decoration: none;
834834
}
835+
.swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.heading ul.options li a .markdown p {
836+
color: inherit;
837+
padding: 0;
838+
line-height: inherit;
839+
}
835840
.swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.heading ul.options li.access {
836841
color: black;
837842
}

src/main/html/css/screen.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,11 @@
832832
.swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.heading ul.options li a {
833833
text-decoration: none;
834834
}
835+
.swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.heading ul.options li a .markdown p {
836+
color: inherit;
837+
padding: 0;
838+
line-height: inherit;
839+
}
835840
.swagger-section .swagger-ui-wrap ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operation div.heading ul.options li.access {
836841
color: black;
837842
}

src/main/javascript/helpers/handlebars.js

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,43 @@
11
'use strict';
22
/*jslint eqeq: true*/
33

4-
Handlebars.registerHelper('sanitize', function(html) {
5-
// Strip the script tags from the html, and return it as a Handlebars.SafeString
4+
var _sanitize = function(html) {
5+
// Strip the script tags from the html and inline evenhandlers
66
html = html.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
7-
return new Handlebars.SafeString(html);
8-
});
7+
html = html.replace(/(on\w+="[^"]*")*(on\w+='[^']*')*(on\w+=\w*\(\w*\))*/gi, '');
8+
9+
return html;
10+
};
11+
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+
};
30+
31+
Handlebars.registerHelper('sanitize', sanitize);
932

1033
Handlebars.registerHelper('renderTextParam', function(param) {
1134
var result, type = 'text', idAtt = '';
1235
var paramType = param.type || param.schema && param.schema.type || '';
1336
var isArray = paramType.toLowerCase() === 'array' || param.allowMultiple;
1437
var defaultValue = isArray && Array.isArray(param.default) ? param.default.join('\n') : param.default;
38+
var name = Handlebars.Utils.escapeExpression(param.name);
39+
var valueId = Handlebars.Utils.escapeExpression(param.valueId);
40+
paramType = Handlebars.Utils.escapeExpression(paramType);
1541

1642
var dataVendorExtensions = Object.keys(param).filter(function(property) {
1743
// filter X-data- properties
@@ -21,24 +47,18 @@ Handlebars.registerHelper('renderTextParam', function(param) {
2147
return result += ' ' + property.substring(2, property.length) + '=\'' + param[property] + '\'';
2248
}, '');
2349

24-
if (typeof defaultValue === 'undefined') {
25-
defaultValue = '';
26-
}
27-
2850
if(param.format && param.format === 'password') {
2951
type = 'password';
3052
}
3153

32-
if(param.valueId) {
33-
idAtt = ' id=\'' + param.valueId + '\'';
54+
if(valueId) {
55+
idAtt = ' id=\'' + valueId + '\'';
3456
}
3557

36-
if (typeof defaultValue === 'string' || defaultValue instanceof String) {
37-
defaultValue = defaultValue.replace(/'/g,'&apos;');
38-
}
58+
defaultValue = sanitize(defaultValue);
3959

4060
if(isArray) {
41-
result = '<textarea class=\'body-textarea' + (param.required ? ' required' : '') + '\' name=\'' + param.name + '\'' + idAtt + dataVendorExtensions;
61+
result = '<textarea class=\'body-textarea' + (param.required ? ' required' : '') + '\' name=\'' + name + '\'' + idAtt + dataVendorExtensions;
4262
result += ' placeholder=\'Provide multiple values in new lines' + (param.required ? ' (at least one required).' : '.') + '\'>';
4363
result += defaultValue + '</textarea>';
4464
} else {
@@ -47,7 +67,7 @@ Handlebars.registerHelper('renderTextParam', function(param) {
4767
parameterClass += ' required';
4868
}
4969
result = '<input class=\'' + parameterClass + '\' minlength=\'' + (param.required ? 1 : 0) + '\'';
50-
result += ' name=\'' + param.name +'\' placeholder=\'' + (param.required ? '(required)' : '') + '\'' + idAtt + dataVendorExtensions;
70+
result += ' name=\'' + name +'\' placeholder=\'' + (param.required ? '(required)' : '') + '\'' + idAtt + dataVendorExtensions;
5171
result += ' type=\'' + type + '\' value=\'' + defaultValue + '\'/>';
5272
}
5373
return new Handlebars.SafeString(result);
@@ -76,3 +96,9 @@ Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
7696
return options.inverse(this);
7797
}
7898
});
99+
100+
Handlebars.registerHelper('escape', function (value) {
101+
var text = Handlebars.Utils.escapeExpression(value);
102+
103+
return new Handlebars.SafeString(text);
104+
});

src/main/javascript/utils/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,13 @@ window.SwaggerUi.utils = {
6868
}
6969

7070
return result;
71+
},
72+
73+
sanitize: function(html) {
74+
// Strip the script tags from the html and inline evenhandlers
75+
html = html.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
76+
html = html.replace(/(on\w+="[^"]*")*(on\w+='[^']*')*(on\w+=\w*\(\w*\))*/gi, '');
77+
78+
return html;
7179
}
7280
};

src/main/javascript/view/MainView.js

Lines changed: 1 addition & 1 deletion
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 = id;
99+
resource.id = SwaggerUi.utils.sanitize(id);
100100
resources[id] = resource;
101101
this.addResource(resource, this.model.auths);
102102
}

src/main/less/specs.less

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,11 @@
703703
font-size: 0.9em;
704704
a {
705705
text-decoration: none;
706+
.markdown p {
707+
color: inherit;
708+
padding: 0;
709+
line-height: inherit;
710+
}
706711
}
707712
}
708713
}

0 commit comments

Comments
 (0)