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

Commit 9208166

Browse files
authored
Merge pull request #1790 from OpenBazaar/sanitizeProfileAndItemModels
Sanitize the listing and profile models.
2 parents 9cd563c + 3c4d6a2 commit 9208166

File tree

7 files changed

+55
-17
lines changed

7 files changed

+55
-17
lines changed

js/models/itemMd.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ var __ = require('underscore'),
44
getBTPrice = require('../utils/getBitcoinPrice'),
55
app = require('../App').getApp(),
66
countriesMd = require('./countriesMd'),
7-
autolinker = require( '../utils/customLinker');
7+
autolinker = require( '../utils/customLinker'),
8+
sanitizeModel = require('../utils/sanitizeModel');
89

910
module.exports = window.Backbone.Model.extend({
1011
defaults: {
@@ -108,6 +109,10 @@ module.exports = window.Backbone.Model.extend({
108109
//when vendor currency code is in bitcoins, the json returned is different. Put the value in the expected place so the templates don't break.
109110
//check to make sure a blank result wasn't returned from the server
110111
if (response.vendor_offer){
112+
113+
//sanitize html
114+
response.vendor_offer = sanitizeModel(response.vendor_offer);
115+
111116
if (response.vendor_offer.listing.item.price_per_unit.bitcoin){
112117
response.vendor_offer.listing.item.price_per_unit.fiat = {
113118
"price": response.vendor_offer.listing.item.price_per_unit.bitcoin,

js/models/itemShortMd.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

3-
var __ = require('underscore'),
4-
Backbone = require('backbone'),
3+
var Backbone = require('backbone'),
54
app = require('../App').getApp(),
65
getBTPrice = require('../utils/getBitcoinPrice');
76

@@ -23,19 +22,13 @@ module.exports = Backbone.Model.extend({
2322
handle: 0,
2423
avatar_hash: "",
2524
priceSet: 0, //set in Update Attribute below, so view can listen for it
26-
short_description: ""
2725
},
2826

2927
initialize: function(){
3028
this.updateAttributes();
3129
//this.on('change', this.updateAttributes, this);
3230
},
3331

34-
parse: function(response){
35-
response.short_description = __.unescape(response.short_description);
36-
return response;
37-
},
38-
3932
updateAttributes: function(){
4033
var self = this,
4134
userCCode = this.get('userCurrencyCode'),
@@ -77,7 +70,5 @@ module.exports = Backbone.Model.extend({
7770
if (thumbnailHash === "b472a266d0bd89c13706a4132ccfb16f7c3b9fcb" || thumbnailHash.length !== 40) {
7871
this.set('thumbnail_hash', "");
7972
}
80-
81-
this.set('short_description', this.get('short_description'));
8273
}
8374
});

js/models/userProfileMd.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
var __ = require('underscore'),
44
Backbone = require('backbone'),
55
is = require('is_js'),
6-
autolinker = require( '../utils/customLinker');
6+
autolinker = require( '../utils/customLinker'),
7+
sanitizeModel = require('../utils/sanitizeModel');
78

89
module.exports = Backbone.Model.extend({
910
defaults: {
@@ -67,6 +68,8 @@ module.exports = Backbone.Model.extend({
6768
//first check to make sure server sent data in the response. Sometimes it doesn't.
6869
if (response.profile){
6970

71+
response.profile = sanitizeModel(response.profile);
72+
7073
//check if colors are in hex, if not convert. This assumes non-hex colors are numbers or strings of numbers.
7174
response.profile.background_color = response.profile.background_color === 0 || response.profile.background_color ? this.convertColor(response.profile.background_color) : "#063753";
7275
response.profile.primary_color = response.profile.primary_color === 0 || response.profile.primary_color ? this.convertColor(response.profile.primary_color) : "#086A9E";

js/models/userShortMd.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
var __ = require('underscore'),
4-
Backbone = require('backbone');
3+
var Backbone = require('backbone'),
4+
sanitizeModel = require('../utils/sanitizeModel');
55

66
module.exports = Backbone.Model.extend({
77
defaults: {
@@ -14,7 +14,7 @@ module.exports = Backbone.Model.extend({
1414
},
1515

1616
parse: function(response){
17-
response.short_description = __.unescape(response.short_description);
17+
response.short_description = sanitizeModel(response.short_description);
1818
return response;
1919
}
2020
});

js/router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ module.exports = Backbone.Router.extend({
412412
// we want this to happen after the launchPageConnectModal processes
413413
// the resolution of the promise, hence the timeout.
414414
setTimeout(() => {
415-
this.navigate(`userPage/${guid}${subPath ? '/' + subPath : ''}`, { replace: true });
415+
this.navigate(`userPage/${guid}${subPath ? '/' + subPath.join('/') : ''}`, { replace: true });
416416
this.userPage(guid, state, itemHash, skipNSFWmodal, '@' + handle);
417417
}, 0);
418418
});

js/templates/settings.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ <h3 class="padding15 margin0 fontWeight500"><%= polyglot.t('moderatorSettings.Di
10511051
name="moderation_fee"
10521052
class="fieldItem custCol-text width70px floatLeft"
10531053
id="moderatorFeeInput"
1054-
value="<%= ob.page.profile.moderation_fee.toFixed(2) %>" required/><div class="floatLeft marginLeft5 lineHeight51 fontWeight500 textOpacity75">%</div>
1054+
value="<%= Number(ob.page.profile.moderation_fee).toFixed(2) %>" required/><div class="floatLeft marginLeft5 lineHeight51 fontWeight500 textOpacity75">%</div>
10551055
</div>
10561056
</div>
10571057
<a class="btn btn-large js-saveModerator custCol-secondary pull-right marginRight15 marginTop15 marginBottom50 btn-secondary">

js/utils/sanitizeModel.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
var sanitizeHTML = require('sanitize-html');
4+
5+
function sanitize(rawHTML) {
6+
var processedHTML = rawHTML;
7+
8+
var decodeHtml = function(html) { //turn encoded html into regular html
9+
var txt = document.createElement("textarea");
10+
txt.innerHTML = html;
11+
return txt.value;
12+
};
13+
14+
processedHTML = sanitizeHTML(decodeHtml(processedHTML), {
15+
allowedTags: [ 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'a', 'u', 'ul', 'ol', 'nl', 'li', 'b', 'i', 'strong', 'em', 'strike', 'hr', 'br', 'img', 'blockquote', 'span' ],
16+
allowedAttributes: {
17+
'a': [ 'href', 'title', 'alt' ],
18+
'img': [ 'src', 'style']
19+
},
20+
allowedSchemes: [ 'http', 'https', 'ftp', 'mailto', 'ob' ]
21+
});
22+
23+
return processedHTML;
24+
}
25+
26+
module.exports = function(modelObject) {
27+
28+
function sanitizeModelData (obj) {
29+
Object.keys(obj).forEach(function (key) {
30+
if (typeof obj[key] === 'object') {
31+
return sanitizeModelData(obj[key]);
32+
}
33+
obj[key] = sanitize(obj[key]);
34+
});
35+
}
36+
sanitizeModelData(modelObject);
37+
38+
return modelObject;
39+
};

0 commit comments

Comments
 (0)