-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix: treat space as a delimiter in content-type parsing #6064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Unfortunately, the fix from GHSA-mg2h-6x62-wpwc left a hole open. At this point, it's better to close the gap completely. This is quite urgent. |
Will do something slightly differently |
48f8dd1
to
ee820b8
Compare
Signed-off-by: Matteo Collina <hello@matteocollina.com>
ee820b8
to
c7d6749
Compare
Co-authored-by: Manuel Spigolon <manuel.spigolon@nearform.com> Signed-off-by: Matteo Collina <matteo.collina@gmail.com>
@@ -261,7 +261,7 @@ function wrapValidationError (result, dataVar, schemaErrorFormatter) { | |||
*/ | |||
function getEssenceMediaType (header) { | |||
if (!header) return '' | |||
return header.split(';', 1)[0].trim().toLowerCase() | |||
return header.split(/[ ;]/, 1)[0].trim().toLowerCase() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't ever see headers written like this (https://httpwg.org/specs/rfc9110.html#media.type). The spec is <type>[;][<parameter>]
. But I suppose we have to handle this case due to jerks. However, it feel like a slippery slope to me.
Anyway, I think we might be able to eek out some more performance with slice
instead:
'use strict'
const iterations = 1_000_000
const hrtime = process.hrtime.bigint
const fixture = 'application/json foo; charset=utf-8'
const startSplit = hrtime()
for (var i = 0; i < iterations; i += 1) {
const result = fixture.split(/[ ;]/, 1)[0].trim().toLowerCase()
doSomething(result)
}
const endSplit = hrtime()
const startSlice = hrtime()
for (var i = 0; i < iterations; i += 1) {
const result = fixture.slice(
0, fixture.indexOf(' ')
).replace(';', '').trim().toLowerCase()
doSomething(result)
}
const endSlice = hrtime()
const splitTime = endSplit - startSplit
console.log('split:', splitTime)
const sliceTime = endSlice - startSlice
console.log('slice:', sliceTime)
console.log('split > slice =', splitTime > sliceTime)
function doSomething(value) {
return value >> 1
}
* fix: treat space as a delimiter in content-type parsing Signed-off-by: Matteo Collina <hello@matteocollina.com> * Update lib/validation.js Co-authored-by: Manuel Spigolon <manuel.spigolon@nearform.com> Signed-off-by: Matteo Collina <matteo.collina@gmail.com> --------- Signed-off-by: Matteo Collina <hello@matteocollina.com> Signed-off-by: Matteo Collina <matteo.collina@gmail.com> Co-authored-by: Manuel Spigolon <manuel.spigolon@nearform.com>
Follow-up from GHSA-mg2h-6x62-wpwc