Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const CONFIG = {
backupKeep: Math.max(0, parseInt(env.BACKUP_KEEP || '14', 10)),
backupEveryHours: Math.max(1, parseInt(env.BACKUP_EVERY_HOURS || '24', 10)),
historyKeep: Math.max(50, parseInt(env.HISTORY_KEEP || '5000', 10)),
maxImportBytes: parseInt(env.MAX_IMPORT_MB || '20', 10) * 1024 * 1024,
maxImportBytes: Math.max(1, parseInt(env.MAX_IMPORT_MB, 10) || 20) * 1024 * 1024,
maxDocBytes: 512 * 1024,
// AGPL-3.0 section 13: users of a modified version must be offered its source. Point this at your fork.
sourceUrl: /^https?:\/\//.test(env.SOURCE_URL || '') ? env.SOURCE_URL : 'https://github.com/SolidGeeks/strata',
Expand Down Expand Up @@ -296,8 +296,21 @@ function send(res, status, body, headers = {}) {
const fail = (res, status, error, extra) => send(res, status, { error, ...(extra || {}) });
function readBody(req, limit) {
return new Promise((resolve, reject) => {
const tooLarge = () => { req.resume(); reject(httpError(413, 'Payload too large'));};
if(Number(req.headers['content-length'])>limit) {
return tooLarge();
}
let size = 0; const chunks = [];
req.on('data', c => { size += c.length; if (size > limit) { reject(httpError(413, 'Payload too large')); req.destroy(); return; } chunks.push(c); });
const onData = c => {
size += c.length;
if(size <= limit) {
return chunks.push(c);
}
req.off('data', onData);
chunks.length = 0;
tooLarge();
};
req.on('data', onData);
req.on('end', () => { try { resolve(chunks.length ? JSON.parse(Buffer.concat(chunks).toString('utf8')) : null); } catch { reject(httpError(400, 'Invalid JSON')); } });
req.on('error', reject);
});
Expand Down Expand Up @@ -571,7 +584,12 @@ const server = http.createServer(async (req, res) => {
else if (req.method === 'GET' || req.method === 'HEAD') await serveStatic(req, res, url.pathname);
else fail(res, 405, 'Method not allowed');
} catch (err) {
if (!res.headersSent) fail(res, err.status || 500, err.status ? err.message : 'Internal error', err.extra);
if (!res.headersSent) {
if(err.status == 413) {
res.setHeader('Connection', 'close');
}
fail(res, err.status || 500, err.message || 'Internal error', err.extra);
}
if (!err.status) log('ERROR', req.method, req.url, err.stack || err.message);
}
});
Expand Down
4 changes: 3 additions & 1 deletion tests/api.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ test('import: replaces the map, validates ids, and enforces MAX_IMPORT_MB', asyn
const bob = as('bob');
assert.equal((await bob.post('/api/import', { si: { 'bad id!': {} }, assets: {} })).status, 400);
const big = { si: { x: { name: 'x'.repeat(1.2 * 1024 * 1024) } }, assets: {} };
const tooBig = await bob.post('/api/import', big).catch(e => ({ status: 'connection closed', error: e }));
const tooBig = await bob.post('/api/import', big);
assert.equal(tooBig.status, 413);
assert.deepEqual(tooBig.data, { error: 'Payload too large' });
assert.ok(tooBig.status === 413 || tooBig.status === 'connection closed', `got ${tooBig.status}`);
const before = await rev();
const r = await bob.post('/api/import', { si: { lille: { name: 'Lille' } }, assets: {} });
Expand Down