Skip to content

Commit

Permalink
Additional @icon checks (#1303)
Browse files Browse the repository at this point in the history
* Bug fix on helper
* Automate rejection of an occasional issue with excessive dimensions

Post #1283 #1274

Auto-merge
  • Loading branch information
Martii authored Dec 5, 2017
1 parent bf05e8a commit f626fee
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Repository | Reference | Recent Version
[git-rev][git-revGHUrl] | [Documentation][git-revDOCUrl] | [![NPM version][git-revNPMVersionImage]][git-revNPMUrl]
[github][githubGHUrl] | [Documentation][githubDOCUrl] | [![NPM version][githubNPMVersionImage]][githubNPMUrl]
[highlight.js][highlight.jsGHUrl] | [Documentation][highlight.jsDOCUrl][ᴸᴬᴺᴳ][highlight.jsLANGUrl] | [![NPM version][highlight.jsNPMVersionImage]][highlight.jsNPMUrl]
[image-size][image-sizeGHUrl] | [Documentation][image-sizeDOCUrl] | [![NPM version][image-sizeNPMVersionImage]][image-sizeNPMUrl]
[jquery][jQueryGHUrl] | [Documentation][jQueryDOCUrl] | [![NPM version][jQueryNPMVersionImage]][jQueryNPMUrl]
[js-beautify][js-beautifyGHUrl] | [Documentation][js-beautifyDOCUrl] | [![NPM version][js-beautifyNPMVersionImage]][js-beautifyNPMUrl]
[jsdom][jsdomGHUrl] | [Documentation][jsdomDOCUrl] | [![NPM version][jsdomNPMVersionImage]][jsdomNPMUrl]
Expand Down Expand Up @@ -249,6 +250,11 @@ Outdated dependencies list can also be achieved with `$ npm --depth 0 outdated`
[highlight.jsNPMVersionImage]: https://img.shields.io/npm/v/highlight.js.svg?style=flat
[highlight.jsLANGUrl]: https://github.com/isagalaev/highlight.js/blob/master/docs/css-classes-reference.rst#language-names-and-aliases

[image-sizeNPMUrl]: https://www.npmjs.com/package/image-size
[image-sizeNPMVersionImage]: https://img.shields.io/npm/v/image-size?style=flat
[image-sizeGHUrl]: https://github.com/image-size/image-size
[image-sizeDOCUrl]: https://github.com/image-size/image-size/blob/master/Readme.md

[jQueryNPMUrl]: https://www.npmjs.com/package/jquery
[jQueryNPMVersionImage]: https://img.shields.io/npm/v/jquery.svg?style=flat
[jQueryGHUrl]: https://github.com/jquery/jquery
Expand Down
77 changes: 75 additions & 2 deletions controllers/scriptStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var fs = require('fs');
var util = require('util');
var _ = require('underscore');
var URL = require('url');
var http = require('http');
var https = require('https');
var crypto = require('crypto');
var stream = require('stream');
var peg = require('pegjs');
Expand All @@ -26,6 +28,7 @@ var moment = require('moment');
var Base62 = require('base62');
var SPDXOSI = require('spdx-osi'); // NOTE: Sub-dep of `spdx-is-osi`
var SPDX = require('spdx-license-ids');
var sizeOf = require('image-size');

var MongoClient = require('mongodb').MongoClient;
var ExpressBrute = require('express-brute');
Expand Down Expand Up @@ -1381,6 +1384,14 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
function (aInnerCallback) {
// `@icon` validations
var icon = null;
var maxX = 64; // px
var maxY = 64; // px
var buffer = null;
var fn = null;
var dimensions = null;
var matches = null;
var data = null;
var rDataURIbase64 = /^data:image\/.+;base64,(.*)$/;

icon = findMeta(aMeta, 'UserScript.icon.0.value');
if (icon) {
Expand All @@ -1393,9 +1404,71 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
}), null);
return;
}
}

aInnerCallback(null);
// Test dimensions
if (/^data:/.test(icon)) {
matches = icon.match(rDataURIbase64);
if (matches) {
data = matches[1];
buffer = new Buffer(data, 'base64');
try {
dimensions = sizeOf(buffer);
} catch (aE) {
aInnerCallback(new statusError({
message: '`@icon` ' + aE.message,
code: aE.code
}));
return;
}

if (dimensions.width > maxX || dimensions.height > maxY) {
aInnerCallback(new statusError({
message: '`@icon` dimensions are too large.',
code: 400
}), null);
} else {
aInnerCallback(null);
}
} else {
aInnerCallback(new statusError({
message: 'Invalid `@icon`',
code: 400
}), null);
}
} else {
fn = /^http:/.test(icon) ? http : https;
fn.get(URL.parse(icon), function (aRes) {
var chunks = [];
aRes.on('data', function (aChunk) {
chunks.push(aChunk);
}).on('end', function () {
buffer = Buffer.concat(chunks);
try {
dimensions = sizeOf(buffer);
} catch (aE) {
aInnerCallback(new statusError({
message: '`@icon` ' + aE.message,
code: aE.code
}));
return;
}

if (dimensions.width > maxX || dimensions.height > maxY) {
aInnerCallback(new statusError({
message: '`@icon` dimensions are too large.',
code: 400
}), null);
} else {
aInnerCallback(null);
}
}).on('error', function (aErr) {
aInnerCallback(aErr);
});
});
}
} else {
aInnerCallback(null);
}
},
function (aInnerCallback) {
// `@supportURL` validations
Expand Down
8 changes: 4 additions & 4 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -1546,8 +1546,8 @@ exports.uploadScript = function (aReq, aRes, aNext) {
scriptStorage.storeScript(aUser, aMeta, bufferConcat, false, function (aErr, aScript) {
if (aErr || !aScript) {
statusCodePage(aReq, aRes, aNext, {
statusCode: aErr.status.code,
statusMessage: aErr.status.message
statusCode: (aErr instanceof statusError ? aErr.status.code : aErr.code),
statusMessage: (aErr instanceof statusError ? aErr.status.message : aErr.code)
});
return;
}
Expand Down Expand Up @@ -1603,8 +1603,8 @@ exports.submitSource = function (aReq, aRes, aNext) {

if (aErr) {
statusCodePage(aReq, aRes, aNext, {
statusCode: aErr.status.code,
statusMessage: aErr.status.message
statusCode: (aErr instanceof statusError ? aErr.status.code : aErr.code),
statusMessage: (aErr instanceof statusError ? aErr.status.message : aErr.message)
});
return;
}
Expand Down
2 changes: 1 addition & 1 deletion libs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ exports.isFQUrl = function (aString, aMailto, aDataImg) {
var source = encodeURIComponent(aString);
var target = null;

if (protocol && /^https?/.test(protocol)) {
if (protocol && /^https?:$/.test(protocol)) {
if (hostname) {
target = encodeURIComponent(protocol)
+ encodeURIComponent('//')
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"git-rev": "0.2.1",
"github": "0.2.4",
"highlight.js": "9.12.0",
"image-size": "0.6.1",
"jsdom": "11.5.1",
"jquery": "3.2.1",
"js-beautify": "1.7.4",
Expand Down

0 comments on commit f626fee

Please sign in to comment.