0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-25 09:42:40 +00:00

Merge pull request #2620 from jeddai/content-negotiation-middleware

Fixes #2595: Add content negotiation middleware
This commit is contained in:
Trevor Buckner
2023-04-10 09:54:48 -04:00
committed by GitHub
4 changed files with 61 additions and 2 deletions

View File

@@ -82,6 +82,13 @@ For a full record of development, visit our [Github Page](https://github.com/nat
### XXXXday DD/MM/2023 - v3.8.0
{{taskList
##### Jeddai
* [X] Add content negotiation to exclude image requests from our API calls
Fixes issue [#2595](https://github.com/naturalcrit/homebrewery/issues/2595)
##### G-Ambatte
* [x] Update server build scripts to fix Admin page

View File

@@ -43,8 +43,7 @@ const sanitizeBrew = (brew, accessType)=>{
};
app.use('/', serveCompressedStaticAssets(`build`));
//app.use(express.static(`${__dirname}/build`));
app.use(require('./middleware/content-negotiation.js'));
app.use(require('body-parser').json({ limit: '25mb' }));
app.use(require('cookie-parser')());
app.use(require('./forcessl.mw.js'));

View File

@@ -0,0 +1,12 @@
module.exports = (req, res, next)=>{
const isImageRequest = req.get('Accept')?.split(',')
?.filter((h)=>!h.includes('q='))
?.every((h)=>/image\/.*/.test(h));
if(isImageRequest) {
return res.status(406).send({
message : 'Request for image at this URL is not supported'
});
}
next();
};

View File

@@ -0,0 +1,41 @@
const contentNegotiationMiddleware = require('./content-negotiation.js');
describe('content-negotiation-middleware', ()=>{
let request;
let response;
let next;
beforeEach(()=>{
request = {
get : function(key) {
return this[key];
}
};
response = {
status : jest.fn(()=>response),
send : jest.fn(()=>{})
};
next = jest.fn();
});
it('should return 406 on image request', ()=>{
contentNegotiationMiddleware({
Accept : 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
...request
}, response);
expect(response.status).toHaveBeenLastCalledWith(406);
expect(response.send).toHaveBeenCalledWith({
message : 'Request for image at this URL is not supported'
});
});
it('should call next on non-image request', ()=>{
contentNegotiationMiddleware({
Accept : 'text,image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
...request
}, response, next);
expect(next).toHaveBeenCalled();
});
});