8000 Deprecate legacy cookie behaviour behind `LEGACY_COOKIES` profile by jonkoops · Pull Request #26161 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Deprecate legacy cookie behaviour behind LEGACY_COOKIES profile - #26161

Closed
jonkoops wants to merge 4 commits into
keycloak:mainfrom
jonkoops:deprecate-legacy-cookies
Closed

jonkoops wants to merge 4 commits into
keycloak:mainfrom
jonkoops:deprecate-legacy-cookies

Conversation

@jonkoops
Copy link
Copy Markdown
Contributor

Closes #16181

@ahus1
ahus1 commented Jan 12, 2024 •
Copy link
Copy Markdown
Member

Happy to help if you want. I suppose there will be some failing tests as some Quarkus tests list all options in the help.

@jonkoops
jonkoops force-pushed the deprecate-legacy-cookies branch 4 times, most recently from 0d5cfe0 to 22c2089 Compare January 12, 2024 15:31
@jonkoops

This comment was marked as off-topic.

@jonkoops

This comment was marked as off-topic.

@jonkoops
jonkoops force-pushed the deprecate-legacy-cookies branch from 22c2089 to d534594 Compare January 12, 2024 17:19
@jonkoops
jonkoops marked this pull request as ready for review January 12, 2024 17:20
@jonkoops
jonkoops requested review from a team as code owners January 12, 2024 17:20
@jonkoops
jonkoops requested a review from a team January 12, 2024 17:20
@jonkoops jonkoops self-assigned this Jan 12, 2024
@jonkoops
Copy link
Copy Markdown
Contributor Author

@vmuzikar You had some concerns on the previous PR (#16747 (review)), let's try to resolve those here.

I'm afraid removing legacy cookies breaks HTTP (non-SSL) requests, at least to the admin console. That is because we have to force Secure anytime we use SameSite=None:

I think we need to stop supporting this use-case, folks should not be running Keycloak on HTTP unless they are behind a proxy that is serving over HTTPS, in which case this is also not a problem, as this would be considered a secure context.

I also don't expect this to be a problem for local development, as long as folks are using a secure context, such as http://localhost and http://*.localhost URLs (e.g. http://dev.whatever.localhost/).

andymunro

This comment was marked as off-topic.

@jonkoops
jonkoops requested a review from andymunro January 12, 2024 17:49
@stianst
stianst commented Jan 15, 2024 •
Copy link
Copy Markdown
Contributor

When you run Keycloak, it will print

 Keycloak 999.0.0-SNAPSHOT on JVM (powered by Quarkus 3.2.9.Final) started in 12.006s. Listening on: http://0.0.0.0:8080

And after the change you won't be able to click on that URL and log in. Instead you'll see an error "This page isn’t working" and "ERR_TOO_MANY_REDIRECTS" after entering the username and password.

This is the same if someone opens the URL for whatever reason without https://.

This is a UX that IMHO needs improvement, as it is difficult to understand for the user what's going wrong here, especially for users new to Keycloak.

  • Before showing the login screen (or after submitting the form the lastest), check if cookies are working as expected. If they don't, show a descriptive error message instead of the login screen
  • Offer a CLI option to allow for non-secure cookies (for example --http-cookies-secure=false)

Thinking we shouldn't output http://0.0.0.0:8080 that's not really an actual URL. Perhaps we should drop this from the Keycloak started line, and instead have some output from the hostname provider instead? @pedroigor @vmuzikar ?

@jonkoops
Copy link
Copy Markdown
Contributor Author

Thinking we shouldn't output http://0.0.0.0:8080 that's not really an actual URL.

+1

@ahus1
ahus1 commented Jan 15, 2024
Copy link
Copy Markdown
Member

@pedroigor - replying to your comment:

For instance, checking both HTTPS and Hostname settings we should be able to print a warning in the logs.

Unfortunately in cloud environments this all depends on the proxy or if the browser is using a localhost or equivalent. All of this is undetectable by the server by looking at the CLI options only. The only real test is if the client provides the cookies, which might require one more round-trip.

Signed-off-by: Alexander Schwartz <aschwart@redhat.com>
@ahus1
ahus1 commented Jan 15, 2024
Copy link
Copy Markdown
Member

I pushed one more commit to set the same attributes on the KC_RESTART cookies. I think that makes sense for this one to have the same as the other authentication cookies, as it enhances security.

Once I did that, Keycloak shows once I submit a login form the following message (see screen shot at the end):

We are sorry...
Cookie not found. Please make sure cookies are enabled in your browser.

@jonkoops - assuming this is the error.ftl template, could we add to it something to it along the following lines:

  • JavaScript to detect if this page is a "secure environment" according to the browser, as IMHO it can't be reliably determined on the server side
  • Have an explanation what is happening? The current text "Please make sure cookies are enabled in your browser." doesn't make the cut, so I'd like to have an additional text that shows conditionally on the error message code, and is localized in the messages_en.properties.

@jonkoops - if you could supply me with a snippet of JavaScript and a possible text to display in the scenario described above, I can have a look how to pull the text out of the localized message files (haven't done that before, but I suppose I'll figure it out).

image

@ahus1 ahus1 self-assigned this Jan 15, 2024
@pedroigor
Copy link
Copy Markdown
Contributor

I pushed one more commit to set the same attributes on the KC_RESTART cookies. I think that makes sense for this one to have the same as the other authentication cookies, as it enhances security.

This one in particular should be a first-party cookie all the time, I think. It is supposed to be used within the server domain boundaries and not shared across domains?

If browsers are going to default to strict then it should be better to rely on the default or force this one as strict.

(That is kinda why I think we should avoid introducing strict in this PR because there are other things to discuss)

Signed-off-by: Alexander Schwartz <aschwart@redhat.com>
@ahus1
ahus1 commented Jan 15, 2024
Copy link
Copy Markdown
Member

@jonkoops - pushed one more commit that uses the JavaScript you suggested to have a message like the following:

Cookie not found. Make sure cookies are enabled in your browser and the page is running in a secure context.
At the moment, this page is not running in a secure context. To run in a secure context it requires a URL starting with https://. In development environments, also URLs like http://127.0.0.1, http://localhost or any URL ending with .localhost like http://example.localhost would work, but this is not supported for production environments.

As it might run in an enterprise environment, it shouldn't require a link to the Internet.

image

Signed-off-by: Alexander Schwartz <aschwart@redhat.com>
@ahus1
ahus1 commented Jan 15, 2024
Copy link
Copy Markdown
Member

This one in particular should be a first-party cookie all the time, I think. It is supposed to be used within the server domain boundaries and not shared across domains?

Reading this, I'm now confused why the other cookies have None set. Still, won't question this as part of this PR.

I've now set the KC_RESTART cookie to have secure set to true, which has the same effect and doesn't touch the samesite stuff in one more commit.

@pedroigor
Copy link
Copy Markdown
Contributor

Reading this, I'm now confused why the other cookies have None set. Still, won't question this as part of this PR.

Same here ... However, an authentication session is used in brokering and there you have cross-origin requests where the user is redirected back to the server to continue with the flow.

@mposolda mposolda left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Added few minor comments to the docs part.


== `SameSite` attribute set to `Strict` by default

All cookies provided by Keycloak will now have the `SameSite` attribute set to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#strict[`Strict`] by default. This means that cookies will only be sent in a first-party context, that is, requests originating from the same site that set the cookie. Some specific cookies that are required in a third party contexts, such as the session cookie are exempt from this behavior for now, but this will likely change in the future as using cookies in a third-party context will no longer supported by browsers.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
All cookies provided by Keycloak will now have the `SameSite` attribute set to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#strict[`Strict`] by default. This means that cookies will only be sent in a first-party context, that is, requests originating from the same site that set the cookie. Some specific cookies that are required in a third party contexts, such as the session cookie are exempt from this behavior for now, but this will likely change in the future as using cookies in a third-party context will no longer supported by browsers.
All cookies provided by Keycloak will now have the `SameSite` attribute set to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#strict[`Strict`] by default. This means that cookies will only be sent in a first-party context, that is, requests originating from the same site that set the cookie. Some specific cookies that are required in a third party contexts, such as the session cookie are exempt from this behavior for now, but this will likely change in the future as using cookies in a third-party context will no longer be supported by browsers.

It looks the word be missing (or some other similar word)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

See above - strict also has implications on cookies not being available after redirects/links to Keycloak


== Support for insecure contexts dropped

Due to the aforementioned changes, we are officially dropping support for running Keycloak outside of a https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts[secure context]. You should therefore ensure that your Keycloak instance if always running on what is https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts#when_is_a_context_considered_secure[considered a secure context]. As part of this, all cookies set by Keycloak will have the https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#secure[`Secure`] attribute by default.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this mean that some more update might need to be done in the documentation in relation to this? I can think of some update for the option http-enabled documented here https://www.keycloak.org/server/enabletls (as per my understanding http-enabled won't work correctly unless people are running on localhost or legacy cookies feature is enabled).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Does this mean that some more update might need to be done in the documentation in relation to this?

Yes, the documentation will have to be reviewed and modified in this regard. Once the implementation settles down that will be the next step.

@stianst stianst left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should probably add something to cleanup *_LEGACY cookies as otherwise these are left until deleted manually by the user, or they expire (which could be a long time if remember-me is enabled).

@stianst
stianst commented Jan 16, 2024
Copy link
Copy Markdown
Contributor

I wonder if we should add some documentation/guidance around cookies, as the migration guides now contain useful tips to those reading those, but not to new users.

@stianst
stianst commented Jan 16, 2024
Copy link
Copy Markdown
Contributor

I'm not a fan of the following:

image

This is not really information aimed at the user, but rather to the admin/ops guy installing Keycloak. Would be better to just display a generic error page, and output an error to the log.

@jonkoops
Copy link
Copy Markdown
Contributor Author

I wonder if we should add some documentation/guidance around cookies, as the migration guides now contain useful tips to those reading those, but not to new users.

Yes, see #26161 (comment). We'll have to dedicate a section of the getting started guide to ensuring users run Keycloak in a secure context.

@stianst stianst left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Summary of issues from me:

  • We can not use strict (as that restricts access on redirects as well) - at this point this is only a note in the release notes, and any changes here should be done in a follow-up so straightforward thing is just remove this mention in the release notes for now
  • Support for insecure contexts dropped - this could probably be explained a little bit better in the release notes, as it's not obvious that it's the legacy cookies that somewhat works around this issue currently
  • Lack of cleanup of *_LEGACY cookies
  • Error message. I don't link the specific hard-coded bits for this error message in the error form) and it also shows more information to the end-user than it should

@stianst stianst self-assigned this Jan 18, 2024
@stianst stianst added the status/hold PR should not be merged. On hold for later. label Jan 18, 2024
@stianst
stianst commented Jan 18, 2024
Copy link
Copy Markdown
Contributor

After chatting with @jonkoops we have some slightly different ideas to try out first, in either case we'll not target removing legacy cookies for 24, but rather look at getting this into 25. Leaving the PR open here for now.

@ahus1 ahus1 removed their assignment Jan 22, 2024
@jonkoops jonkoops closed this Apr 30, 2024
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.

Deprecate legacy cookie behaviour behind profile

9 participants

0