forked from spacebar/SplatNet-Backend
fix: handle 404s & catch errors more efficiently
This commit is contained in:
parent
d4b4928443
commit
dd1a6227dc
1 changed files with 23 additions and 6 deletions
|
|
@ -1,14 +1,31 @@
|
|||
const crypto = require('crypto');
|
||||
|
||||
function validateBOSSDigestMiddleware(request, response, next) {
|
||||
const calculatedHash = crypto.createHash('sha1').update(request.rawBody).digest('hex');
|
||||
const expectedHash = request.headers['x-boss-digest'];
|
||||
// prevents viewing it in a browser to fucking up the process lmfao
|
||||
if (request.method === 'GET') {
|
||||
return response.status(404).send('Not Found');
|
||||
}
|
||||
|
||||
if (calculatedHash !== expectedHash) {
|
||||
return next('Provided BOSS digest does not match the calculated hash');
|
||||
}
|
||||
try {
|
||||
// try to catch undefined, previously was making everything die
|
||||
if (!request.rawBody) {
|
||||
throw new Error('rawBody is undefined');
|
||||
}
|
||||
|
||||
next();
|
||||
const calculatedHash = crypto.createHash('sha1').update(request.rawBody).digest('hex');
|
||||
const expectedHash = request.headers['x-boss-digest'];
|
||||
|
||||
//catch all other errors
|
||||
if (calculatedHash !== expectedHash) {
|
||||
console.warn(`[Digest Mismatch] Expected: ${expectedHash}, Calculated: ${calculatedHash}`);
|
||||
return response.status(403).send('Provided BOSS digest does not match the calculated hash');
|
||||
}
|
||||
|
||||
next();
|
||||
} catch (error) {
|
||||
console.error('[Digest Middleware Error]:', error.message);
|
||||
return response.status(500).send('Internal Server Error');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = validateBOSSDigestMiddleware;
|
||||
Loading…
Reference in a new issue