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: add CORS to SecurityPolicy #2065

Merged
merged 11 commits into from
Oct 25, 2023
Merged

Conversation

zhaohuabing
Copy link
Member

@zhaohuabing zhaohuabing commented Oct 25, 2023

What this PR does:

  • Adds CORS to SecurityPolicy.
  • Follow Golang name convention to rename IR type "Cors" to "CORS"

Relate issues:

Related PRs:

BTW, the string match types are a bit messy. They're defined in multiple APIs but look similar. For most of EG APIs(Ratelimit and CORS for now, more in the future), the match types are just used to represent the StringMatcher XDS API. I think we can have one common string match structure used by all the EG APIs. I would like to raise a PR to refactor it if it makes sense.

@zhaohuabing zhaohuabing requested a review from a team as a code owner October 25, 2023 11:23
@zhaohuabing zhaohuabing requested a review from arkodg October 25, 2023 11:25
@zhaohuabing zhaohuabing requested review from a team, Alice-Lilith, chauhanshubham and Xunzhuo and removed request for a team October 25, 2023 12:05
@zhaohuabing zhaohuabing force-pushed the cors-gateway-api branch 2 times, most recently from ade088a to 84add96 Compare October 25, 2023 13:03
Signed-off-by: huabing zhao <[email protected]>
Signed-off-by: huabing zhao <[email protected]>
@codecov
Copy link

codecov bot commented Oct 25, 2023

Codecov Report

Merging #2065 (5210f73) into main (e83e076) will increase coverage by 0.12%.
The diff coverage is 74.35%.

@@            Coverage Diff             @@
##             main    #2065      +/-   ##
==========================================
+ Coverage   65.01%   65.13%   +0.12%     
==========================================
  Files          99       99              
  Lines       14536    14598      +62     
==========================================
+ Hits         9450     9509      +59     
- Misses       4498     4502       +4     
+ Partials      588      587       -1     
Files Coverage Δ
internal/gatewayapi/securitypolicy.go 76.25% <100.00%> (+7.77%) ⬆️
internal/gatewayapi/translator.go 98.50% <100.00%> (ø)
internal/ir/xds.go 74.10% <ø> (ø)
internal/xds/translator/cors.go 65.97% <100.00%> (ø)
internal/xds/translator/httpfilters.go 78.26% <0.00%> (ø)
internal/status/securitypolicy.go 0.00% <0.00%> (ø)
internal/ir/zz_generated.deepcopy.go 11.97% <0.00%> (ø)

... and 2 files with indirect coverage changes

// CORS defines the configuration for Cross-Origin Resource Sharing (CORS).
type CORS struct {
// AllowOrigins defines the origins that are allowed to make requests.
AllowOrigins []StringMatch `json:"allowOrigins,omitempty" yaml:"allowOrigins,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

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

min length for AllowOrigins should be 1

Copy link
Member Author

Choose a reason for hiding this comment

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

I think an empty AllowOrigins could be useful if the backend service allows CORS but we want to explicitly disable it at the Gateway.

I remember we discussed this at the ir PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

not setting CORS disables CORS :)

Copy link
Member Author

@zhaohuabing zhaohuabing Oct 25, 2023

Choose a reason for hiding this comment

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

Yes, it's a bit tricky. Maybe we should add some comments on the API.

// AllowOrigins defines the origins that are allowed to make requests.
AllowOrigins []StringMatch `json:"allowOrigins,omitempty" yaml:"allowOrigins,omitempty"`
// AllowMethods defines the methods that are allowed to make requests.
AllowMethods []string `json:"allowMethods,omitempty" yaml:"allowMethods,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

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

what happens when AllowMethods is empty ?
are all requests types allowed or none ?

Copy link
Member Author

Choose a reason for hiding this comment

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

None, just like empty AllowOrigins.

Copy link
Member Author

@zhaohuabing zhaohuabing Oct 25, 2023

Choose a reason for hiding this comment

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

I did some experiments, the CORS filter won't set "Access-Control-Request-Headers" in the preflight response if AllowMethods is empty. So it means none Method Types are allowed.

curl -H "Origin: http://example.com" \
  -H "Access-Control-Request-Method: GET" \
  -H "Access-Control-Request-Headers: X-Requested-With" \
  -X OPTIONS --verbose \
  http://localhost:8002/cors/open
*   Trying 127.0.0.1:8002...
* Connected to localhost (127.0.0.1) port 8002 (#0)
> OPTIONS /cors/open HTTP/1.1
> Host: localhost:8002
> User-Agent: curl/8.1.2
> Accept: */*
> Origin: http://example.com
> Access-Control-Request-Method: GET
> Access-Control-Request-Headers: X-Requested-With
> 
< HTTP/1.1 200 OK
< access-control-allow-origin: http://example.com
< date: Wed, 25 Oct 2023 17:32:39 GMT
< server: envoy
< content-length: 0
< 
* Connection #0 to host localhost left intact

Copy link
Contributor

Choose a reason for hiding this comment

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

cool, so its opt in, thanks for running the test

@arkodg
Copy link
Contributor

arkodg commented Oct 25, 2023

looking good @zhaohuabing ! added some comments

internal/ir/xds.go Outdated Show resolved Hide resolved
internal/ir/xds.go Outdated Show resolved Hide resolved
Signed-off-by: huabing zhao <[email protected]>
Signed-off-by: huabing zhao <[email protected]>
Signed-off-by: huabing zhao <[email protected]>
Signed-off-by: huabing zhao <[email protected]>
Signed-off-by: huabing zhao <[email protected]>
Copy link
Contributor

@arkodg arkodg 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
Copy link
Member Author

Need to retrigger the coverage-test.

@arkodg
Copy link
Contributor

arkodg commented Oct 25, 2023

/retest

@@ -60,7 +60,7 @@ type ProxyPrometheusProvider struct {
}

// Match defines the stats match configuration.
type Match struct {
type Match struct { // TODO: zhaohuabing this type should be renamed to StatsMatch
Copy link
Contributor

Choose a reason for hiding this comment

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

can you open an issue to track this?

Copy link
Member Author

Choose a reason for hiding this comment

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

@zirain zirain merged commit 4fa3d77 into envoyproxy:main Oct 25, 2023
20 checks passed
@zhaohuabing zhaohuabing deleted the cors-gateway-api branch October 26, 2023 00:24
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.

3 participants