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

feat(cors): Allowed more wildcard options #2453

Merged
merged 9 commits into from
Jan 20, 2024

Conversation

jaynis
Copy link
Contributor

@jaynis jaynis commented Jan 16, 2024

A few weeks ago the allowed CORS origins have been changed from a regex to a wildcard notation (#2389). Implementation wise all kinds of wildcards are supported, however, the validation regex on the SecurityPolicy CRD limits the CORS options to hostnames prefixed with an wildcard followed by a dot, allowing all subdomains of that host. This reduces the freedom when allowing cross origins a lot compared to how it was before. This PR aims to relax the validation regex a bit to enable the following use cases:

  • Allowing all hosts of an specific scheme (https://*)
  • Allowing all hosts regardless of the scheme (*)
  • Allowing all ports of a specific host (http://localhost:*)

While allowing all hosts in the context of CORS might sound a bit hacky, this is sometimes required. For instance when a web service provides an API which is consumed by many third-party web applications hosted under arbitrary domains not under the control of the maintainer of aforementioned web service. In addition to that it can be very useful during application development. This is why I have added the option to allow all ports of a specific host as well.

Review the new and the old validation regexes.

@jaynis jaynis marked this pull request as ready for review January 16, 2024 20:36
@jaynis jaynis requested a review from a team as a code owner January 16, 2024 20:36
// Origin is defined by the scheme (protocol), hostname (domain), and port of
// the URL used to access it. The hostname can be “precise” which is just the
// domain name or “wildcard” which is a domain name prefixed with a single
// wildcard label such as “*.example.com”.
// wildcard label such as “*.example.com”. The optional port can be a wildcard
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// wildcard label such as *.example.com. The optional port can be a wildcard
// wildcard label such as "*.example.com". The optional port can be a wildcard

Copy link
Contributor

Choose a reason for hiding this comment

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

there're still some quotes that in the wrong character encoding, can you please help fixing them? thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@arkodg
Copy link
Contributor

arkodg commented Jan 17, 2024

@jaynis can sign your commits and repush ? DCO is failing

Signed-off-by: jaynis <kranz.jannis@googlemail.com>
Signed-off-by: jaynis <kranz.jannis@googlemail.com>
@jaynis jaynis force-pushed the cors-wildcard-options branch from 7058e37 to a675625 Compare January 17, 2024 20:39
Signed-off-by: jaynis <kranz.jannis@googlemail.com>
Signed-off-by: jaynis <kranz.jannis@googlemail.com>
@jaynis jaynis force-pushed the cors-wildcard-options branch from aa343f9 to 23d0e80 Compare January 18, 2024 09:38
Signed-off-by: jaynis <kranz.jannis@googlemail.com>
@arkodg arkodg requested review from zhaohuabing and shawnh2 January 18, 2024 21:51
jaynis and others added 2 commits January 18, 2024 23:43
Signed-off-by: jaynis <kranz.jannis@googlemail.com>
Copy link

codecov bot commented Jan 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (20b8497) 64.71% compared to head (c4ff422) 64.71%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2453   +/-   ##
=======================================
  Coverage   64.71%   64.71%           
=======================================
  Files         115      115           
  Lines       17431    17431           
=======================================
  Hits        11281    11281           
  Misses       5433     5433           
  Partials      717      717           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@zhaohuabing zhaohuabing left a comment

Choose a reason for hiding this comment

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

@jaynis Thanks for the improvement in the wildcard host matching. The implementation looks good to me.

I only have a little hesitation about the port wildcard matching. Suffix/Port wildcard matching is not a common practice for hostnames. In your use case, do you have many ports for a given hostname?

Copy link
Contributor

@shawnh2 shawnh2 left a comment

Choose a reason for hiding this comment

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

lgtm, defer to @zhaohuabing

@jaynis
Copy link
Contributor Author

jaynis commented Jan 19, 2024

Thank you for your review @zhaohuabing.

I only have a little hesitation about the port wildcard matching. Suffix/Port wildcard matching is not a common practice for hostnames. In your use case, do you have many ports for a given hostname?

The port range matching was solely meant to be a dev feature so that one can configure CORS for a host (e.g. localhost) regardless of the port the application runs on. But this scenario could be covered by the general wildcard as well, therefore I would also be fine with deleting it again if you think it is not required. Just let me know your preference.

@zhaohuabing
Copy link
Member

Thank you for your review @zhaohuabing.

I only have a little hesitation about the port wildcard matching. Suffix/Port wildcard matching is not a common practice for hostnames. In your use case, do you have many ports for a given hostname?

The port range matching was solely meant to be a dev feature so that one can configure CORS for a host (e.g. localhost) regardless of the port the application runs on. But this scenario could be covered by the general wildcard as well, therefore I would also be fine with deleting it again if you think it is not required. Just let me know your preference.

Prefer to remove the suffix matching to keep it aligned with the common practice. Thanks.

Signed-off-by: jaynis <kranz.jannis@googlemail.com>
Signed-off-by: jaynis <kranz.jannis@googlemail.com>
Copy link
Member

@zhaohuabing zhaohuabing left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@zhaohuabing zhaohuabing merged commit 3510eda into envoyproxy:main Jan 20, 2024
20 checks passed
zesiar0 pushed a commit to zesiar0/gateway that referenced this pull request Jan 22, 2024
* allowed more CORS wildcard options

Signed-off-by: jaynis <kranz.jannis@googlemail.com>

* fixed quotes with wrong character encoding

Signed-off-by: jaynis <kranz.jannis@googlemail.com>

* generated manifests

Signed-off-by: jaynis <kranz.jannis@googlemail.com>

* gofmt

Signed-off-by: jaynis <kranz.jannis@googlemail.com>

* fixed regex escape characters in cel validation

Signed-off-by: jaynis <kranz.jannis@googlemail.com>

* fixed cel validation test

Signed-off-by: jaynis <kranz.jannis@googlemail.com>

* removed wildcard port matching

Signed-off-by: jaynis <kranz.jannis@googlemail.com>

* removed wildcard port test

Signed-off-by: jaynis <kranz.jannis@googlemail.com>

---------

Signed-off-by: jaynis <kranz.jannis@googlemail.com>
Co-authored-by: Huabing Zhao <zhaohuabing@gmail.com>
Signed-off-by: A3bz <zeng921359373@163.com>
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.

4 participants