Skip to content

Commit fec197d

Browse files
committed
Restore some of the @icon checks until resolved
* Do some of the checks that don't pertain to TLS issue * Fix with additional `on('error',...` for the request instead of response. My bad although still doesn't appear to work with native `https.get` and `https.request`. Applies to OpenUserJS#1323
1 parent d15672c commit fec197d

File tree

1 file changed

+124
-115
lines changed

1 file changed

+124
-115
lines changed

controllers/scriptStorage.js

+124-115
Original file line numberDiff line numberDiff line change
@@ -1381,121 +1381,130 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
13811381

13821382
aInnerCallback(null);
13831383
},
1384-
//
1385-
// See #1323
1386-
// function (aInnerCallback) {
1387-
// // `@icon` validations
1388-
// var icon = null;
1389-
// var buffer = null;
1390-
// var fn = null;
1391-
// var dimensions = null;
1392-
// var matches = null;
1393-
// var data = null;
1394-
// var rDataURIbase64 = /^data:image\/.+;base64,(.*)$/;
1395-
//
1396-
// function acceptedImage(aDimensions) {
1397-
// var maxX = 256; //px
1398-
// var maxY = 256; //px
1399-
//
1400-
// switch (aDimensions.type) {
1401-
// case 'gif':
1402-
// // fallthrough
1403-
// case 'jpeg':
1404-
// // fallthrough
1405-
// case 'png':
1406-
// // fallthrough
1407-
// case 'svg':
1408-
// // fallthrough
1409-
// case 'ico':
1410-
// if (dimensions.width <= maxX && dimensions.height <= maxY) {
1411-
// return true;
1412-
// }
1413-
// }
1414-
// return false;
1415-
// }
1416-
//
1417-
// icon = findMeta(aMeta, 'UserScript.icon.0.value');
1418-
// if (icon) {
1419-
// if (!isFQUrl(icon, false, true)) {
1420-
//
1421-
// // Not a web url... reject
1422-
// aInnerCallback(new statusError({
1423-
// message: '`@icon` not a web url or image data URI in the UserScript metadata block.',
1424-
// code: 400
1425-
// }), null);
1426-
// return;
1427-
// }
1428-
//
1429-
// // Test dimensions
1430-
// if (/^data:/.test(icon)) {
1431-
// matches = icon.match(rDataURIbase64);
1432-
// if (matches) {
1433-
// data = matches[1];
1434-
// buffer = new Buffer(data, 'base64');
1435-
// try {
1436-
// dimensions = sizeOf(buffer);
1437-
// } catch (aE) {
1438-
// aInnerCallback(new statusError({
1439-
// message: '`@icon` ' + aE.message,
1440-
// code: aE.code
1441-
// }));
1442-
// return;
1443-
// }
1444-
//
1445-
// if (!acceptedImage(dimensions)) {
1446-
// aInnerCallback(new statusError({
1447-
// message: '`@icon` unsupported file type or dimensions are too large.',
1448-
// code: 400
1449-
// }), null);
1450-
// } else {
1451-
// aInnerCallback(null);
1452-
// }
1453-
// } else {
1454-
// aInnerCallback(new statusError({
1455-
// message: 'Invalid `@icon`',
1456-
// code: 400
1457-
// }), null);
1458-
// }
1459-
// } else {
1460-
// fn = /^http:/.test(icon) ? http : https;
1461-
// fn.get(URL.parse(icon), function (aRes) {
1462-
// var chunks = [];
1463-
// aRes.on('data', function (aChunk) {
1464-
// var buf = null;
1465-
// chunks.push(aChunk);
1466-
// buf = Buffer.concat(chunks);
1467-
// if (buf.length > 3048) { // NOTE: KiB
1468-
// aRes.destroy();
1469-
// }
1470-
// }).on('end', function () {
1471-
// buffer = Buffer.concat(chunks);
1472-
// try {
1473-
// dimensions = sizeOf(buffer);
1474-
// } catch (aE) {
1475-
// aInnerCallback(new statusError({
1476-
// message: '`@icon` ' + aE.message,
1477-
// code: aE.code
1478-
// }));
1479-
// return;
1480-
// }
1481-
//
1482-
// if (!acceptedImage(dimensions)) {
1483-
// aInnerCallback(new statusError({
1484-
// message: '`@icon` unsupported file type or dimensions are too large.',
1485-
// code: 400
1486-
// }), null);
1487-
// } else {
1488-
// aInnerCallback(null);
1489-
// }
1490-
// }).on('error', function (aErr) {
1491-
// aInnerCallback(aErr);
1492-
// });
1493-
// });
1494-
// }
1495-
// } else {
1496-
// aInnerCallback(null);
1497-
// }
1498-
// },
1384+
1385+
function (aInnerCallback) {
1386+
// `@icon` validations
1387+
var icon = null;
1388+
var buffer = null;
1389+
var fn = null;
1390+
var dimensions = null;
1391+
var matches = null;
1392+
var data = null;
1393+
var rDataURIbase64 = /^data:image\/.+;base64,(.*)$/;
1394+
1395+
function acceptedImage(aDimensions) {
1396+
var maxX = 256; //px
1397+
var maxY = 256; //px
1398+
1399+
switch (aDimensions.type) {
1400+
case 'gif':
1401+
// fallthrough
1402+
case 'jpeg':
1403+
// fallthrough
1404+
case 'png':
1405+
// fallthrough
1406+
case 'svg':
1407+
// fallthrough
1408+
case 'ico':
1409+
if (dimensions.width <= maxX && dimensions.height <= maxY) {
1410+
return true;
1411+
}
1412+
}
1413+
return false;
1414+
}
1415+
1416+
icon = findMeta(aMeta, 'UserScript.icon.0.value');
1417+
if (icon) {
1418+
if (!isFQUrl(icon, false, true)) {
1419+
1420+
// Not a web url... reject
1421+
aInnerCallback(new statusError({
1422+
message: '`@icon` not a web url or image data URI in the UserScript metadata block.',
1423+
code: 400
1424+
}), null);
1425+
return;
1426+
}
1427+
1428+
// Test dimensions
1429+
if (/^data:/.test(icon)) {
1430+
matches = icon.match(rDataURIbase64);
1431+
if (matches) {
1432+
data = matches[1];
1433+
buffer = new Buffer(data, 'base64');
1434+
try {
1435+
dimensions = sizeOf(buffer);
1436+
} catch (aE) {
1437+
aInnerCallback(new statusError({
1438+
message: '`@icon` ' + aE.message,
1439+
code: aE.code
1440+
}));
1441+
return;
1442+
}
1443+
1444+
if (!acceptedImage(dimensions)) {
1445+
aInnerCallback(new statusError({
1446+
message: '`@icon` unsupported file type or dimensions are too large.',
1447+
code: 400
1448+
}), null);
1449+
} else {
1450+
aInnerCallback(null);
1451+
}
1452+
} else {
1453+
aInnerCallback(new statusError({
1454+
message: 'Invalid `@icon`',
1455+
code: 400
1456+
}), null);
1457+
}
1458+
} else {
1459+
fn = /^http:/.test(icon) ? http : https;
1460+
1461+
// Workaround for #1323
1462+
if (fn === https) {
1463+
aInnerCallback(null); // NOTE: Suspending further checks
1464+
return;
1465+
}
1466+
// /Workaround for #1323
1467+
1468+
fn.get(URL.parse(icon), function (aRes) {
1469+
var chunks = [];
1470+
aRes.on('data', function (aChunk) {
1471+
var buf = null;
1472+
chunks.push(aChunk);
1473+
buf = Buffer.concat(chunks);
1474+
if (buf.length > 3048) {
1475+
aRes.destroy();
1476+
}
1477+
}).on('end', function () {
1478+
buffer = Buffer.concat(chunks);
1479+
try {
1480+
dimensions = sizeOf(buffer);
1481+
} catch (aE) {
1482+
aInnerCallback(new statusError({
1483+
message: '`@icon` ' + aE.message,
1484+
code: aE.code
1485+
}));
1486+
return;
1487+
}
1488+
1489+
if (!acceptedImage(dimensions)) {
1490+
aInnerCallback(new statusError({
1491+
message: '`@icon` unsupported file type or dimensions are too large.',
1492+
code: 400
1493+
}), null);
1494+
} else {
1495+
aInnerCallback(null);
1496+
}
1497+
}).on('error', function (aErr) {
1498+
aInnerCallback(aErr);
1499+
});
1500+
}).on('error', function (aErr) {
1501+
aInnerCallback(aErr); // WARNING: See #1323
1502+
});
1503+
}
1504+
} else {
1505+
aInnerCallback(null);
1506+
}
1507+
},
14991508
function (aInnerCallback) {
15001509
// `@supportURL` validations
15011510
var supportURL = null;

0 commit comments

Comments
 (0)