falconry / falcon

The no-magic web data plane API and microservices framework for Python developers, with a focus on reliability, correctness, and performance at scale.
https://falcon.readthedocs.io/en/stable/
Apache License 2.0
9.51k stars 937 forks source link

feat(middleware): allow dynamic validation of CORS origins #2052

Closed willnewton closed 1 year ago

willnewton commented 2 years ago

Summary of Changes

Allow passing a function to the CORSMiddleware for origin validation. This allows arbitrary validation like applying regexes etc.

Related Issues

I opened an issue regarding this feature request: #2050

Pull Request Checklist

This is just a reminder about the most common mistakes. Please make sure that you tick all appropriate boxes. But please read our contribution guide at least once; it will save you a few review cycles!

If an item doesn't apply to your pull request, check it anyway to make it apparent that there's nothing to do.

If you have any questions to any of the points above, just submit and ask! This checklist is here to help you, not to deter you from contributing!

PR template inspired by the attrs project.

codecov[bot] commented 2 years ago

Codecov Report

Merging #2052 (394758c) into master (130572d) will not change coverage. The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master     #2052   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           63        63           
  Lines         6707      6721   +14     
  Branches      1238      1241    +3     
=========================================
+ Hits          6707      6721   +14     
Impacted Files Coverage Δ
falcon/middleware.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 130572d...394758c. Read the comment docs.

CaselIT commented 2 years ago

I'm not against it, I would need to look at the implementation a bit more though.

kgriffs commented 2 years ago

IMO it would be fine to move forward with this. It affords additional use cases without the framework needing to implement and maintain explicit support for them.

vytas7 commented 2 years ago

IMO it would be fine to move forward with this. It affords additional use cases without the framework needing to implement and maintain explicit support for them.

Hm, not sure about this one @kgriffs. Maybe a better course of action could be breaking out that validation part to a public method of CORSMiddleware, and document an example how to subclass and override that? This is how some other parts of Falcon like media handlers are implemented too.

The async flavour could by default call the synchronous version directly, but it should be possible to do perform origin validation asynchronously too. Passing in a callable makes this somewhat trickier, or should we detect a coroutine function and act accordingly? Or just document it must be a sync function like in field converters?

kgriffs commented 1 year ago

@vytas7 I see your point. We should iterate on this design to make it more consistent with other parts of the framework.

vytas7 commented 1 year ago

@willnewton I'm going to close this pull request for now, but we'll leave your issue (#2050) open for discussion on how to best implement this (or at least make it easy to customize).