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

Handle the case where the access-control-request-header is not provided #347

Merged
merged 3 commits into from Dec 3, 2018

Conversation

justinmchase
Copy link
Contributor

This fixes issue #346

So for whatever reason the client I am using, when making an OPTIONS request to s3rver is not specifying any Access-Control-Request-Header headers at all. As a result s3rver erroneously responds with a 403 error. S3 proper does not have this same issue for the exact same request.

I have added a test which fails with vanilla master code and passes once I made the corresponding fix.

The issue appears to be here where if the header is not specified then requestHeaders ends up being [ '' ] rather than []. Then when we get to the check here the conditional expression allowedHeaders.length < requestHeaders.length fails because [].length < [ '' ].length === true.

Therefore my solution is to remove empty elements from the set since that should never happen provided there is a value specified in the header.

Before

curl:

$ curl -I -X OPTIONS -H "access-control-request-method: PUT" -H "origin: localhost:8888" "http://localhost:4569/example/abc123"
HTTP/1.1 403 Forbidden
Content-Type: application/xml; charset=utf-8
Content-Length: 344
ETag: W/"158-dYD/owyTT+IOwBQKGAb8GxwXdSU"
Date: Thu, 29 Nov 2018 21:23:53 GMT
Connection: keep-alive

s3rver:

$ node .
now listening on host 0.0.0.0 and port 4569
info: [S3rver] OPTIONS /example/abc123 403 344 - 3.963 ms

After

curl:

$ curl -I -X OPTIONS -H "access-control-request-method: PUT" -H "origin: localhost:8888" "http://localhost:4569/example/abc123"
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: PUT, POST, DELETE, HEAD, OPTIONS
Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
Date: Thu, 29 Nov 2018 21:25:27 GMT
Connection: keep-alive
Content-Length: 0

s3rver:

$ node .
now listening on host 0.0.0.0 and port 4569
info: [S3rver] OPTIONS /example/abc123 200 - - 1.668 ms

Copy link
Collaborator

@kherock kherock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. Just a minor nit for readability but otherwise it looks good to me.

lib/cors.js Outdated Show resolved Hide resolved
@kherock
Copy link
Collaborator

kherock commented Dec 1, 2018

All right, nice work. @leontastic do you think you could put together another minor release with this and #325 soon?

@leontastic leontastic merged commit 2ec958a into jamhall:master Dec 3, 2018
@leontastic
Copy link
Collaborator

@justinmchase Thank you for your contribution. I just cut a new release with this fix: https://github.com/jamhall/s3rver/releases/tag/v2.2.7

@justinmchase justinmchase deleted the fix/346-handle-empty-acrh branch December 4, 2018 15:04
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

Successfully merging this pull request may close these issues.

None yet

3 participants