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

Add support for Preflighted CORS requests, closes #99 #102

Merged
merged 10 commits into from Jan 25, 2019
Merged

Conversation

@jhthorsen
Copy link
Owner

@jhthorsen jhthorsen commented Jan 22, 2019

Please review the following PR for adding support for Preflighted CORS requests.

I followed the specification on https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS, but I have not tested this in a browser. Any help in doing so is more than welcome.

@mario-minati, @plk.

@plk
Copy link

@plk plk commented Jan 22, 2019

From a casual review, this looks good to me, exactly what is needed in terms of functionality. I am in a position to test this with a large, live site (in DEV ...) so perhaps you can apply to a dev branch and I can build form source? Or, if it's all opt-in options, will you make a release?

@jhthorsen
Copy link
Owner Author

@jhthorsen jhthorsen commented Jan 22, 2019

@plk: Can install it with the command below?

cpanm https://github.com/jhthorsen/mojolicious-plugin-openapi/archive/feature/cors.tar.gz
@plk
Copy link

@plk plk commented Jan 22, 2019

Looks good - I have managed to test simple and preflight and it all seems to work. One small issue is that the check in simple requests using @CORS_SIMPLE_CONTENT_TYPES fails when there is no Content-Type header at all (which is possible - GET requests generally don't have this) and so I think this needs to be changed (line 93) so that the check against @CORS_SIMPLE_CONTENT_TYPES is only done if the Content-Type header is present.

@jhthorsen
Copy link
Owner Author

@jhthorsen jhthorsen commented Jan 22, 2019

Thanks for the feedback! I just added a fix for the simple CORS requests.

What do you think about the API? Does it make sense to you? Does it provide nice default values? I want to make an API where the focus is for the end user to do less coding and be as much DWIM as possible.

@jhthorsen jhthorsen force-pushed the feature/cors branch from a2faee7 to 8f53874 Jan 23, 2019
@jhthorsen
Copy link
Owner Author

@jhthorsen jhthorsen commented Jan 23, 2019

I just force pushed because of the addition of Mojolicious::Plugin::OpenAPI::SpecRenderer in master, but the unit test is exactly the same as before, so I do believe it works as expected.

@plk
Copy link

@plk plk commented Jan 23, 2019

Yes, the API is nice - I started to worry about allowed headers etc. and look into it but the defaults took care of it and my authentication with custom headers worked fine with no extra config.

@jhthorsen
Copy link
Owner Author

@jhthorsen jhthorsen commented Jan 23, 2019

Cool!

I'll leave it open until tomorrow, in case someone else got input.

@plk
Copy link

@plk plk commented Jan 23, 2019

Will you then release? I actually need this feature ...

@jhthorsen
Copy link
Owner Author

@jhthorsen jhthorsen commented Jan 23, 2019

Yes. Unless I forget or someone has important inout, I’ll make a release in about 16 hours - when I wake up.

You can still install it (or download) using the “cpanm” command above though.

@mrenvoize
Copy link
Collaborator

@mrenvoize mrenvoize commented Jan 23, 2019

Looks sane and works for me under basic testing too. Nice work jhthorsen

@plk
Copy link

@plk plk commented Jan 23, 2019

Hmm, I'm trying the latest and now Swagger UI is complaining about bad network request in the post-preflight real request (even though it succeeds). This seems to be because the Access-Control-Allow-Origin header isn't returned with the post-OPTIONS, POST request as per the specs?

@jhthorsen
Copy link
Owner Author

@jhthorsen jhthorsen commented Jan 23, 2019

Oh! I missed out... I see now that I have a major flaw in the design. I might have to change the API to make it simpler. I'll get back to you when I have more information.

@jhthorsen jhthorsen force-pushed the feature/cors branch from 5a5be2d to 35f67e7 Jan 24, 2019
@jhthorsen
Copy link
Owner Author

@jhthorsen jhthorsen commented Jan 24, 2019

@plk: So I just gave it another try. Please note that the API is completely new. Let me know what you think.

Here some highlights:

# TODO: Remove support for openapi.cors_simple

@plk
Copy link

@plk plk commented Jan 24, 2019

Works fine now, thanks. It required no code changes from the previous test as I was only using _exchange as I suspect many would and so the API changes make sense to me.

Copy link
Collaborator

@jberger jberger left a comment

Overall this is fine. I left a few ideas to ponder.

Note I didn't read the code just the documentation. If you want a full code review ask and I'll try to find time.

lib/Mojolicious/Plugin/OpenAPI/Cors.pm Outdated Show resolved Hide resolved
lib/Mojolicious/Plugin/OpenAPI/Cors.pm Outdated Show resolved Hide resolved
lib/Mojolicious/Plugin/OpenAPI/Cors.pm Show resolved Hide resolved
L<https://github.com/jhthorsen/mojolicious-plugin-openapi/pull/102> if
you have any feedback or create a new issue.
=head1 STASH VARIABLES

This comment has been minimized.

@jberger

jberger Jan 24, 2019
Collaborator

I especially like the stash var interface here. I wonder if you could go further with it and allow the exchange callback to be defined here too? Then it just gets run with the regular validators?

This comment has been minimized.

@jhthorsen

jhthorsen Jan 25, 2019
Author Owner

I don't understand. Isn't that what openapi_cors_default_exchange_callback is?

@jhthorsen
Copy link
Owner Author

@jhthorsen jhthorsen commented Jan 25, 2019

Works fine now, thanks. It required no code changes from the previous test as I was only using _exchange as I suspect many would and so the API changes make sense to me.

I don't understand. Do you mean you did shift->openapi->cors_exchange->openapi->valid_input and also the openapi_cors_allowed_origins stash variable? (Note that that openapi_cors_allowed_origins has been renamed now)

@jhthorsen
Copy link
Owner Author

@jhthorsen jhthorsen commented Jan 25, 2019

I'm going to merge this PR now, even if not all comments have been answered, since I think it's just minor modifications that is needed if any, and I think it's about time to let this out in the wild.

Either way, it's not too late to change things if that is desired.

@plk: Version 2.10 should be available pretty soon.

@jhthorsen jhthorsen merged commit 4369cf3 into master Jan 25, 2019
0 of 2 checks passed
0 of 2 checks passed
continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
continuous-integration/travis-ci/push The Travis CI build is in progress
Details
@jhthorsen jhthorsen deleted the feature/cors branch Jan 25, 2019
@plk
Copy link

@plk plk commented Jan 25, 2019

I opened a new issue as with 2.10 and really basic setup, CORS all works but my routes all now fail with 404 not found when no CORS is involved ... this seems to be something to do with what add_preflighted_routes does.

@plk
Copy link

@plk plk commented Jan 26, 2019

2.11 fixed the issue - I might have been getting 400 but my setup is a bit complex and this resulted in 404s but the issue was the same so thanks for fixing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants
You can’t perform that action at this time.