Files
banner-line/safety.js
T

58 lines
3.4 KiB
JavaScript

// Link + image checks for My Spots. https only, a real public hostname, no tricks; uploads must be a real
// PNG/JPG/GIF at 468x60 (or 936x120 retina) and at most 150 KB.
const net = require('net');
const MAX_UPLOAD = 150 * 1024;
const BAD_HOST = /(^|\.)(localhost|local|internal|lan|home|corp|onion|test|invalid|example)$/i;
// shorteners and redirectors hide where a click goes; a member link must show its real destination
const REDIRECTORS = /(^|\.)(bit\.ly|tinyurl\.com|t\.co|goo\.gl|is\.gd|cutt\.ly|ow\.ly|rebrand\.ly|shorturl\.at|rb\.gy|s\.id|t\.ly|v\.gd|bl\.ink|tiny\.cc)$/i;
function checkLink(raw, what) {
what = what || 'Link';
const s = String(raw || '').trim();
if (!s) return { error: what + ' is empty.' };
if (s.length > 600) return { error: what + ' is too long (600 characters max).' };
if (/[\s<>"'`\\]/.test(s)) return { error: what + ' has spaces or characters a web address cannot have.' };
let u; try { u = new URL(s); } catch (e) { return { error: what + ' is not a valid web address.' }; }
if (u.protocol !== 'https:') return { error: what + ' must start with https://' };
if (u.username || u.password) return { error: what + ' cannot contain a username or password.' };
const h = u.hostname.toLowerCase().replace(/\.$/, '');
if (net.isIP(h.replace(/^\[|\]$/g, ''))) return { error: what + ' must use a domain name, not an IP address.' };
if (!h.includes('.') || BAD_HOST.test(h)) return { error: what + ' must point to a public website.' };
if (!/^[a-z0-9.-]+$/.test(h) && !/^xn--/.test(h)) return { error: what + ' has an unusual domain name.' };
if (REDIRECTORS.test(h)) return { error: 'Use the real destination, not a link shortener.' };
if (u.port && !['443', ''].includes(u.port)) return { error: what + ' must use the normal https port.' };
return { ok: true, url: u.toString() };
}
function imageInfo(buf) {
if (!buf || buf.length < 24) return null;
if (buf.slice(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])))
return { type: 'png', ext: '.png', w: buf.readUInt32BE(16), h: buf.readUInt32BE(20) };
if (buf.slice(0, 6).toString('latin1') === 'GIF87a' || buf.slice(0, 6).toString('latin1') === 'GIF89a')
return { type: 'gif', ext: '.gif', w: buf.readUInt16LE(6), h: buf.readUInt16LE(8) };
if (buf[0] === 0xff && buf[1] === 0xd8) {
let i = 2;
while (i + 9 < buf.length) {
if (buf[i] !== 0xff) { i++; continue; }
const m = buf[i + 1];
if (m >= 0xc0 && m <= 0xcf && m !== 0xc4 && m !== 0xc8 && m !== 0xcc) return { type: 'jpg', ext: '.jpg', h: buf.readUInt16BE(i + 5), w: buf.readUInt16BE(i + 7) };
if (m === 0xd8 || m === 0x01 || (m >= 0xd0 && m <= 0xd7)) { i += 2; continue; }
i += 2 + buf.readUInt16BE(i + 2);
}
return { type: 'jpg', ext: '.jpg', w: 0, h: 0 };
}
return null;
}
function checkImage(buf) {
if (!buf || !buf.length) return { error: 'No file received.' };
if (buf.length > MAX_UPLOAD) return { error: 'That file is ' + Math.ceil(buf.length / 1024) + ' KB. The limit is 150 KB.' };
const info = imageInfo(buf);
if (!info) return { error: 'Upload a PNG, JPG or GIF file.' };
const ok = (info.w === 468 && info.h === 60) || (info.w === 936 && info.h === 120);
if (!ok) return { error: 'The image is ' + info.w + 'x' + info.h + '. It must be 468x60 (or 936x120).' };
return { ok: true, info };
}
module.exports = { checkLink, checkImage, imageInfo, MAX_UPLOAD };