Skip to content

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

Merged
merged 2 commits into from
Apr 18, 2025

Conversation

mcollina
Copy link
Member

Follow-up from GHSA-mg2h-6x62-wpwc

@mcollina
Copy link
Member Author

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.

@mcollina
Copy link
Member Author

Will do something slightly differently

@mcollina mcollina marked this pull request as draft April 18, 2025 16:55
@mcollina mcollina force-pushed the fix-content-media-type branch from 48f8dd1 to ee820b8 Compare April 18, 2025 17:04
@mcollina mcollina changed the title Return 415 on unknown media types Apr 18, 2025
@mcollina mcollina marked this pull request as ready for review April 18, 2025 17:04
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina mcollina force-pushed the fix-content-media-type branch from ee820b8 to c7d6749 Compare April 18, 2025 17:13
Co-authored-by: Manuel Spigolon <manuel.spigolon@nearform.com>
Signed-off-by: Matteo Collina <matteo.collina@gmail.com>
@mcollina mcollina merged commit f3d2bcb into main Apr 18, 2025
38 of 41 checks passed
@mcollina mcollina deleted the fix-content-media-type branch April 18, 2025 20:23
@@ -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()
Copy link
Member

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
}
Eomm added a commit that referenced this pull request Apr 28, 2025
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants