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): support for the Timing-Allow-Origin header #9365

Merged

Conversation

skimdz86
Copy link
Contributor

Description

Implements feature described in issue #9307: added support to the Timing-Allow-Origin header in the CORS plugin.

The new header can be configured with a list of domains or a regex, just as the Access-Control-Allow-Origin.
The new header is returned only if the Access-Control-Allow-Origin is returned.

I added a new parameter to the 2 functions process_with_allow_origins and process_with_allow_origins_by_regex to make these functions "common" and now used both for calculating the Access-Control-Allow-Origin and the Timing-Allow-Origin header; the new "allow_origin_type" parameter was essential to differentiate the cache used to save the elaboration results for the 2 different headers.

Note: I updated also the markdown documentation (english version) adding the 2 new configuration fields, but unfortunately I don't speak Chinese, so any help for updating also the /docs/zh/latest/plugins/cors.md file will be appreciated.

Fixes #9307

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@monkeyDluffy6017
Copy link
Contributor

Hi @skimdz86, thanks for your contribution, Is Timing-Allow-Origin some kind of CORS?

@monkeyDluffy6017 monkeyDluffy6017 added enhancement New feature or request and removed discuss labels May 26, 2023
Comment on lines 207 to 213
if not conf.allow_origins then
if conf.timing_allow_origins or conf.timing_allow_origins_by_regex then
return false, "you can not set 'timing_allow_origin' " ..
"or 'timing_allow_origin_by_regex' " ..
"when 'allow_origins' is not set"
end
end
Copy link
Contributor

@monkeyDluffy6017 monkeyDluffy6017 Jun 7, 2023

Choose a reason for hiding this comment

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

I have considered this place carefully, and I think that allow_origins and timing_allow_origins should be independent of each other, because it is possible that the upstream has a response header of Access-Control-Allow-Origin, which does not require the plugin to set it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I didn't consider the case in which the CORS are managed already by the upstream server; so I agree, this way we can add only the timing header in the api gateway in addition to the existing CORS headers, leaving freedom to the users in the configuration. So I'll remove this check, but I also have to change the logic in the header_filter function, to allow setting the timing even when allow_origins is not set in the plugin conf.

t/plugin/cors4.t Outdated
Access-Control-Allow-Headers: request-h
Access-Control-Expose-Headers: expose-h
Access-Control-Max-Age: 10
Timing-Allow-Origin: http://testurl.domain.com
Copy link
Contributor

Choose a reason for hiding this comment

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

Need a blank at the end of file

@skimdz86
Copy link
Contributor Author

skimdz86 commented Jun 9, 2023

@monkeyDluffy6017 Updated the PR with your suggestions; since we decoupled the management of cors headers and timing header, I also split the set_cors_headers function in 2, adding the set_timing_headers function.

Copy link

This pull request has been marked as stale due to 60 days of inactivity. It will be closed in 4 weeks if no further activity occurs. If you think that's incorrect or this pull request should instead be reviewed, please simply write any comment. Even if closed, you can still revive the PR at any time or discuss it on the [email protected] list. Thank you for your contributions.

@github-actions github-actions bot added the stale label Nov 20, 2023
@skimdz86
Copy link
Contributor Author

Hi @monkeyDluffy6017 , any news on this PR?

Thanks

@monkeyDluffy6017
Copy link
Contributor

@skimdz86 sorry for this delay, i have no idea if Timing-Allow-Origin belongs to CORS

@skimdz86
Copy link
Contributor Author

@skimdz86 sorry for this delay, i have no idea if Timing-Allow-Origin belongs to CORS

Ok, so what are the next steps in this case?

Timing-Allow-Origin:


=== TEST 5: set route ( allow_origins same as timing_allow_origins )
Copy link
Contributor

Choose a reason for hiding this comment

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

Three blanks are needed, please check the other place

)
end
if conf.allow_origins ~= "*" then
core.response.add_header("Vary", "Origin")
end
if allow_origins then
if allow_origins then
Copy link
Contributor

Choose a reason for hiding this comment

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

remove redundant indents

@@ -202,9 +236,20 @@ local function set_cors_headers(conf, ctx)
if conf.allow_credential then
core.response.set_header("Access-Control-Allow-Credentials", true)
end

if ctx.timing_allow_origin then
core.response.set_header("Timing-Allow-Origin", ctx.timing_allow_origin)
Copy link
Contributor

@monkeyDluffy6017 monkeyDluffy6017 Nov 22, 2023

Choose a reason for hiding this comment

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

Why do you need this? you have done this in set_timing_headers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, useless, I may have forgotten to remove it when I decoupled the logic of classic cors and timing headers. I'll remove it

Comment on lines 45 to 46
| timing_allow_origins | string | False | nil | Origin to allow to access the resource timing information. See [Timing-Allow-Origin](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Timing-Allow-Origin). Use the `scheme://host:port` format. For example, `https://somedomain.com:8081`. If you have multiple origins, use a `,` to list them. |
| timing_allow_origins_by_regex | array | False | nil | Regex to match with origin for enabling access to the resource timing information. For example, `[".*\.test.com"]` can match all subdomain of `test.com`. When set to specified range, only domains in this range will be allowed, no matter what `timing_allow_origins` is. |
Copy link
Contributor

Choose a reason for hiding this comment

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

I find that CORS and Timing-Allow-Origin are 2 completely separate functions, and if we put their configurations in one table, the complexity of the configuration items can be very confusing, for example the configuration item allow_methods only works for CORS, so how about split the table into two part, one for cors and the other for Timing-Allow-Origin

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok seems a good idea, I'll fix it

@monkeyDluffy6017
Copy link
Contributor

I will approve this pr after you resolve the review comments

@monkeyDluffy6017 monkeyDluffy6017 added the wait for update wait for the author's response in this issue/PR label Nov 22, 2023
@skimdz86
Copy link
Contributor Author

I will approve this pr after you resolve the review comments

@monkeyDluffy6017 PR updated

@monkeyDluffy6017 monkeyDluffy6017 added approved and removed wait for update wait for the author's response in this issue/PR user responded labels Dec 5, 2023
apisix/plugins/cors.lua Outdated Show resolved Hide resolved
@shreemaan-abhishek shreemaan-abhishek dismissed stale reviews from monkeyDluffy6017 and themself via 1884a15 December 5, 2023 14:53
@monkeyDluffy6017 monkeyDluffy6017 changed the title feat: support for the Timing-Allow-Origin header in CORS plugin feat(cors): support for the Timing-Allow-Origin header Dec 5, 2023
@monkeyDluffy6017 monkeyDluffy6017 merged commit b40264f into apache:master Dec 5, 2023
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: support for the Timing-Allow-Origin header in CORS plugin
3 participants