0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

fix(middleware): allow content-type with charset (#4990)

* fix(middleware): allow content-type with charset

* Add test
This commit is contained in:
Marc Bernard 2024-12-12 15:37:46 -05:00 committed by GitHub
parent 23f893453c
commit 899504143a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 39 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'@verdaccio/middleware': patch
---
fix(middleware): allow content-type with charset

View file

@ -4,7 +4,18 @@ import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
export function media(expect: string | null): any {
return function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
if (req.headers[HEADER_TYPE.CONTENT_TYPE] !== expect) {
const header = req.headers[HEADER_TYPE.CONTENT_TYPE];
if (!header) {
next(
errorUtils.getCode(
HTTP_STATUS.UNSUPPORTED_MEDIA,
'content-type is missing, expect: ' + expect
)
);
return;
}
if (typeof header !== 'string' || header.split(';')[0].trim() !== expect) {
next(
errorUtils.getCode(
HTTP_STATUS.UNSUPPORTED_MEDIA,

View file

@ -20,6 +20,19 @@ test('media is json', async () => {
.expect(200);
});
test('media is json with charset', async () => {
const app = getApp([]);
app.get('/json', media(mime.getType('json')), (req, res) => {
res.status(200).json();
});
return request(app)
.get('/json')
.set(HEADERS.CONTENT_TYPE, 'application/json; charset=utf-8')
.expect('Content-Type', /json/)
.expect(200);
});
test('media is not json', async () => {
const app = getApp([]);
app.get('/json', media(mime.getType('json')), (req, res) => {
@ -32,3 +45,12 @@ test('media is not json', async () => {
.expect('Content-Type', /html/)
.expect(HTTP_STATUS.UNSUPPORTED_MEDIA);
});
test('missing content-type', async () => {
const app = getApp([]);
app.get('/json', media(mime.getType('json')), (req, res) => {
res.status(HTTP_STATUS.OK).json({});
});
return request(app).get('/json').expect(HTTP_STATUS.UNSUPPORTED_MEDIA);
});