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

Once the cors_domains is set apply cors_domains list to the prefight cors #19119

Closed
wants to merge 33 commits into from

Conversation

peterhashair
Copy link
Contributor

@peterhashair peterhashair commented Apr 19, 2022

Description:

Fixes: #19116
Once the cors_domains are set apply the cors_domains list to the prefight cors. By default, it allows all. Maybe we should use the getAllKnownUrlsForAllSites() array + cors_domains as the whitelist array.

Review

@peterhashair peterhashair added this to the 4.9.1 milestone Apr 19, 2022
@justinvelluppillai
Copy link
Contributor

Looks like you have some changes here from other merged PRs

update tracker
@peterhashair
Copy link
Contributor Author

@justinvelluppillai fixed :)

@Findus23 Findus23 added the c: Security For issues that make Matomo more secure. Please report issues through HackerOne and not in Github. label Apr 19, 2022
@Findus23 Findus23 changed the title [Security] Once the cors_domains is set apply cors_domains list to the prefight cors Once the cors_domains is set apply cors_domains list to the prefight cors Apr 19, 2022
@sgiehl
Copy link
Member

sgiehl commented Apr 25, 2022

@peterhashair I'm not very familiar with the CORS stuff, but based on the documentation your PR can't work, as the origin only allows one record to be returned. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin

<origin>
Specifies an origin. Only a single origin can be specified. If the server supports clients from multiple origins, it must return the origin for the specific client making the request.

core/Tracker.php Outdated Show resolved Hide resolved
@justinvelluppillai justinvelluppillai modified the milestones: 4.9.1, 4.10.0 Apr 26, 2022
update cors
@peterhashair
Copy link
Contributor Author

peterhashair commented Apr 27, 2022

@sgiehl I got an idea not sure if I am correct, I move the Corshandler to the Tracker main, works on my local, but I update Corshandler a little, will that cause any issues?

@sgiehl
Copy link
Member

sgiehl commented Apr 27, 2022

@peterhashair I actually wasn't aware of the CORSHandler. Maybe it would make more sense to move the preflight handling completely to the CORSHandler.

@MichaelRoosz
Copy link
Contributor

We are also setting cors headers here: https://github.com/matomo-org/matomo/blob/4.x-dev/core/Tracker/Response.php#L111, it might be a good idea to merge this also with CORSHandler

core/Tracker.php Outdated Show resolved Hide resolved
core/Tracker.php Outdated Show resolved Hide resolved
update cors
@peterhashair
Copy link
Contributor Author

peterhashair commented Apr 28, 2022

@sgiehl @MichaelRoosz rewrite CORSHandlera little bit hopefully that makes more sense.

@peterhashair peterhashair added Needs Review PRs that need a code review not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org. labels May 2, 2022
Copy link
Member

@sgiehl sgiehl left a comment

Choose a reason for hiding this comment

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

It doesn't seem like you have checked or tested that code in any way also it doesn't do what it was supposed to do anymore.
Also please consider refactoring this into the CORS handler: https://github.com/matomo-org/matomo/blob/4.x-dev/core/Tracker/Response.php#L111

core/API/CORSHandler.php Outdated Show resolved Hide resolved
core/Tracker.php Show resolved Hide resolved
piwik.php Outdated Show resolved Hide resolved
core/API/CORSHandler.php Outdated Show resolved Hide resolved
@sgiehl sgiehl removed the Needs Review PRs that need a code review label May 3, 2022
@sgiehl sgiehl modified the milestones: 4.10.0, 4.11.0 May 3, 2022
@peterhashair peterhashair removed the Stale The label used by the Close Stale Issues action label Jun 2, 2022
Copy link
Contributor

@justinvelluppillai justinvelluppillai left a comment

Choose a reason for hiding this comment

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

The messaging, english, etc now look all good. I have suggested a change in line with @sgiehl's suggestion to change the response code from 401 to 403 and then this looks ok to merge to me. Be good to get @sgiehl to do a final check also.

@justinvelluppillai justinvelluppillai removed the Needs Review PRs that need a code review label Jun 3, 2022
update status
@peterhashair peterhashair added the Needs Review PRs that need a code review label Jun 3, 2022
core/Tracker/Response.php Outdated Show resolved Hide resolved
@sgiehl sgiehl removed the Needs Review PRs that need a code review label Jun 3, 2022
@sgiehl
Copy link
Member

sgiehl commented Jun 3, 2022

@justinvelluppillai This PR would now be ready to merge. But it actually changes more than the issue that should be solved. All the CORS handling is now also done for API requests. That means if the cors_domains config is set incorrect, the Matomo UI will no longer work correctly. The page will load, but all API requests will fail, causing only an error to show up.
Personally I think that behavior is kind of correct. But we might need to consider to either mention that in the dev change log and maybe also update or add an FAQ. This might be kind of a breaking change as it restricts API requests being sent from other sites...

@tsteur
Copy link
Member

tsteur commented Jun 3, 2022

That means if the cors_domains config is set incorrect, the Matomo UI will no longer work correctly. The page will load, but all API requests will fail, causing only an error to show up. ... This might be kind of a breaking change as it restricts API requests being sent from other sites...

FYI if there's any kind of breaking change then we should wait for Matomo 5 before merging this. Or maybe we can prevent the breaking change in some way. We wouldn't want to break the UI.

Can we also confirm there's no security issue by this new implementation which is quite different (for all kind of web servers)? I've tested this locally and before I was not allowed to send a reporting API request from a different domain but now it allows me to send a request from that domain. Meaning this would be also a security regression.

@tsteur
Copy link
Member

tsteur commented Jun 4, 2022

By sending a 401 or 403 response code can we also make sure this won't cause any duplicate requests to be sent in the JS tracker eg because of https://github.com/matomo-org/matomo/blob/4.10.1/js/piwik.js#L2843-L2850 where we send the same request again if there's eg a 4XX response. If there's any way this could happen then this will likely result in duplicate tracked data and increased server load.

I'm thinking at least for tracking API it's potentially on purpose maybe to send a 2XX response code in some cases instead of 4XX as the response may not be needed and the 2XX prevents users from thinking there is an error and then they email support or create bug reports when there's actually no problem. This may be different for the reporting API where the response is usually needed.

Also note that HTTP response codes of tracking and reporting API are API see https://developer.matomo.org/guides/apis meaning changing the response code is also in itself a breaking change.

Maybe the behaviour for tracking API should be unchanged, and / or generally the behaviour on the response code may need to be different depending on if any domains are configured or if the standard applies.

From what I can see generally even if the response code is a 2XX then it doesn't mean the response can be read, the browser would still show a CORS error so not sure how valid the context of the original issue is
image

@justinvelluppillai justinvelluppillai modified the milestones: 4.11.0, 4.12.0 Jun 7, 2022
@github-actions
Copy link
Contributor

If you don't want this PR to be closed automatically in 28 days then you need to assign the label 'Do not close'.

@github-actions github-actions bot added the Stale The label used by the Close Stale Issues action label Jun 22, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Aug 4, 2022

This PR was last updated more than one month ago, maybe it's time to close it. Please check if there is anything we still can do or close this PR. ping @matomo-org/core-reviewers

@github-actions github-actions bot added the Stale for long The label used by the Close Stale Issues action label Aug 4, 2022
@peterhashair peterhashair added Do not close PRs with this label won't be marked as stale by the Close Stale Issues action and removed Stale The label used by the Close Stale Issues action Stale for long The label used by the Close Stale Issues action labels Aug 4, 2022
@sgiehl sgiehl modified the milestones: 4.12.0, 5.0.0 Sep 5, 2022
@mattab mattab added the 5.0.0 label Jan 4, 2023
@sgiehl
Copy link
Member

sgiehl commented Feb 2, 2023

Closing this one as we won't continue working on it for now. We might consider doing that in a later step.

@sgiehl sgiehl closed this Feb 2, 2023
@sgiehl sgiehl deleted the m-19116 branch January 9, 2024 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: Security For issues that make Matomo more secure. Please report issues through HackerOne and not in Github. Do not close PRs with this label won't be marked as stale by the Close Stale Issues action not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

POST to matomo.php Endpoint Returns 204 When Setting An Origin Header Not On The cors_domains
8 participants