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

fix: CORS: security routes and origin handling #1478

Merged
merged 8 commits into from
Nov 20, 2022
Merged

fix: CORS: security routes and origin handling #1478

merged 8 commits into from
Nov 20, 2022

Conversation

tkurki
Copy link
Member

@tkurki tkurki commented Oct 8, 2022

CORS was previously not enabled for any of the security related routes, specifically `/signalk/v1/auth/login. This PR changes the order in server setup so that CORS middleware is activated before security routes. Fixes #1479.

Without any origins configured the server allows CORS requests from any host.

However when configured the origin list was not working correctly:

  • the first configured origin was not accepted
  • all others were
  • except undefined that is same host
    This PR fixes the origin checking logic so that it actually works.

In addition this PR adds special handling for the Admin UI origin: the origin that the security configuration form that is used to set CORS origin field to non empty value is automatically added to the list of allowed CORS origins. Otherwise Admin UI itself will stop working, blocked by CORS.

Now the security configuration form also includes a short description of how CORS works.

@tkurki tkurki added the fix label Oct 8, 2022
@tkurki
Copy link
Member Author

tkurki commented Oct 8, 2022

Now that the configured origin list check works we have another problem: if you configure any CORS origin and don't explicitly configure the one where Admin UI is being used this will block admin UI save functionalities. How to deal with this?

At the moment my idea is to add something to the UI that will include the host where admin UI is being used by default but optionally in the list of allowed CORS origins, like [ ] include this host xxx.yyy.zzz in the list of allowed origins.

@sbender9 @panaaj thoughts?

@tkurki tkurki changed the title [WIP] Fix cors [WIP] Fix CORS: security routes and origin handling Oct 8, 2022
@tkurki
Copy link
Member Author

tkurki commented Oct 8, 2022

Also some font and style requests from the Admin UI get blocked, because they have origin explicitly for some reason.

@panaaj
Copy link
Member

panaaj commented Oct 9, 2022

This seeems a reasonable approach.

CORS handling was not working in any of the paths related
to authentication, because cors middleware was added after
security path handlers were added. This changes the order
so that CORS works with these paths also.
CORS origin check was broken in two ways:
- the first configured allowed origin was not allowed
- any other origin would be allowed access

This fixes the check and changes logging denied
requests to debug only.
Ignore whitespace and trailing slash.
Add special handling for Admin UI CORS origin and a brief
explanation of the mechanism in the security configuration
form.
@tkurki tkurki changed the title [WIP] Fix CORS: security routes and origin handling fix: CORS: security routes and origin handling Nov 12, 2022
@tkurki tkurki requested a review from panaaj November 12, 2022 19:47
@tkurki tkurki merged commit 775e745 into master Nov 20, 2022
@tkurki tkurki deleted the fix-cors branch November 20, 2022 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: CORS not enabled for /signalk/v1/auth/login
2 participants