Skip to content
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

Should support both SPACE and HTAB as whitespace in Content-Type #52

Open
rg2011 opened this issue Jan 28, 2024 · 0 comments
Open

Should support both SPACE and HTAB as whitespace in Content-Type #52

rg2011 opened this issue Jan 28, 2024 · 0 comments

Comments

@rg2011
Copy link

rg2011 commented Jan 28, 2024

Currently, type-is fails to match the media-type of a request when the content-type header includes tabs. Minimal reproduction:

var http = require('http');
var typeis = require('type-is');
var server = http.createServer(function (req, res) {
	res.write("Content-type: " + JSON.stringify(req.headers["content-type"]) + "\n");
	res.write("typeis(req, ['json']) = " + JSON.stringify(typeis(req, ['json'])) + "\n");
	res.end();
}).listen(3000);

The script above works when the content type includes parameters, and spaces:

$ curl -X POST -H 'Content-Type: application/json; charset=utf-8' http://localhost:3000 -d '{}'
Content-type: "application/json; charset=utf-8"
typeis(req, ['json']) = "json"

But fails when it includes tabs:

$ export TAB=$'\t'
$ curl -X POST -H "Content-Type: application/json;${TAB}charset=utf-8" http://localhost:3000 -d '{}'
Content-type: "application/json;\tcharset=utf-8"
typeis(req, ['json']) = false

My understanding of RFC 9110 is that horizontal tabs should be considered valid whitespace in a content-type header, so type-is shoudn't fail in this case. According to https://www.rfc-editor.org/rfc/rfc9110#field.content-type:

The type/subtype MAY be followed by semicolon-delimited parameters (Section 5.6.6) in the form of name/value pairs.

And section 5.6.6 defines the semicolon-delimited parameters as:

  parameters      = *( OWS ";" OWS [ parameter ] )
  parameter       = parameter-name "=" parameter-value
  parameter-name  = token
  parameter-value = ( token / quoted-string )

Where OWS is defined in section 5.6.3 as:

  OWS            = *( SP / HTAB )
                 ; optional whitespace
rg2011 added a commit to rg2011/type-is that referenced this issue Jan 28, 2024
@rg2011 rg2011 changed the title Should support both SPACE and HTAB as whitespace Should support both SPACE and HTAB as whitespace in Content-Type Jan 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant