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

Skip to content

Deprecate legacy cookies behind LEGACY_COOKIES profile - #16747

Closed
jonkoops wants to merge 1 commit into
keycloak:mainfrom
jonkoops:deprecate-legacy-cookies
Closed

jonkoops wants to merge 1 commit into
keycloak:mainfrom
jonkoops:deprecate-legacy-cookies

Conversation

@jonkoops
Copy link
Copy Markdown
Contributor

Closes #16181

@jonkoops
jonkoops requested review from a team as code owners January 31, 2023 15:43
@jonkoops
jonkoops requested a review from a team January 31, 2023 15:43
@jonkoops
jonkoops requested a review from a team as a code owner January 31, 2023 15:43
@jonkoops
jonkoops force-pushed the deprecate-legacy-cookies branch from 3239ba8 to fad2b69 Compare January 31, 2023 15:47
pedroigor
pedroigor previously approved these changes Jan 31, 2023
@ahus1
ahus1 self-requested a review January 31, 2023 16:07
@ahus1 ahus1 left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There will be likely some test failures in the Quarkus area for the generated help output with the new option.

I'd also like to see something in the release notes for this and I'm happy to contribute those. Anywhere else we would need to document this?

@jonkoops
Copy link
Copy Markdown
Contributor Author

There will be likely some test failures in the Quarkus area for the generated help output with the new option.

Feel free to add a commit to fix these, I am not too familiar with this area.

I'd also like to see something in the release notes for this and I'm happy to contribute those. Anywhere else we would need to document this?

Probably the supported features page on the site. But I believe that is updated automatically?

@ahus1
ahus1 commented Feb 1, 2023
Copy link
Copy Markdown
Member

@jonkoops - there's now PR jonkoops#13 which fixes "Keycloak CI / Quarkus IT (zip)". The other failing tests are IMHO unstable tests.

Please squash the commits after merging. Also, please add an issue number to the commit (the original commit for this PR doesn't contain an issue number).

Thanks!

@ahus1
ahus1 commented Feb 1, 2023 •
Copy link
Copy Markdown
Member

@jonkoops - regarding docs, IMHO there should be an update to the release notes and the migration guide. See keycloak/keycloak-documentation#1761 for the PR.

UPDATE: Yes, the features page will be updated automatically, no additional needs there.

@jonkoops
jonkoops commented Feb 1, 2023
Copy link
Copy Markdown
Contributor Author

Thanks for looking into that @ahus1, I've pulled in your changes and squashed the commits.

@jonkoops

This comment was marked as off-topic.

@ahus1

This comment was marked as off-topic.

@mhajas

This comment was marked as off-topic.

@jonkoops

This comment was marked as off-topic.

@jonkoops
jonkoops commented Feb 2, 2023
Copy link
Copy Markdown
Contributor Author

@ahus1 @mhajas I'll make sure to add the correct information to the commit through the GitHub UI when merging it into main. Could y'all take another look at the PR and leave a review?

@ahus1
ahus1 self-requested a review February 2, 2023 17:07
ahus1
ahus1 previously approved these changes Feb 2, 2023
@ahus1 ahus1 left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for implementing this. I reviewed it and also did a manual smoke test.

@jonkoops
jonkoops commented Feb 2, 2023
Copy link
Copy Markdown
Contributor Author

Thanks for the assist @ahus1. Java is not my native land so I appreciate it.

@stianst
stianst requested a review from vmuzikar February 3, 2023 07:24
stianst
stianst previously approved these changes Feb 3, 2023
@stianst
stianst commented Feb 3, 2023
Copy link
Copy Markdown
Contributor

@vmuzikar could you review this one?

@jonkoops
jonkoops requested a review from pedroigor February 3, 2023 10:03
pedroigor
pedroigor previously approved these changes Feb 3, 2023
Co-authored-by: Alexander Schwartz <aschwart@redhat.com>
jonkoops force-pushed the deprecate-legacy-cookies branch from 6dbec32 to eab835b Compare February 6, 2023 10:11
@vmuzikar vmuzikar 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.

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:

boolean secure_sameSite = sameSite == SameSiteAttributeValue.NONE || secure; // when SameSite=None, Secure attribute must be set

This also concerns the default kc.sh start-dev profile.

I think we have to consider using SameSite=Lax when HTTP is explicitly enabled and document this behaviour.

@pedroigor
pedroigor dismissed stale reviews from stianst, ahus1, and themself via eab835b February 6, 2023 13:58
@pedroigor
pedroigor commented Feb 6, 2023 •
Copy link
Copy Markdown
Contributor

@vmuzikar You're right and I missed it. It works fine in most browsers (E.g.: FF and Chrome) but only if the domain name is localhost. Those browsers consider connections to localhost as secure so that you don't need an HTTPS channel.

However, this change as is should impact people not only running in dev mode (e.g.: start-dev) but also those exposing the server through HTTP when using a domain name other than localhost. It is a breaking change as is.

Ideally, we should decide (instead of hard-coded as it is today) the same site policy (and if it is secure) for cookies depending on both realm and server settings.

@pedroigor
pedroigor commented Feb 6, 2023 •
Copy link
Copy Markdown
Contributor

@vmuzikar Looking at it more closely looks like we can rely on the required ssl setting at the realm level to decide whether or not sameSite=None+secure should be set.

By default, realms are set with required ssl=external but we are forcing sameSite=None without taking into account whether the secure flag is enabled. It looks like a problem that we need to handle better.

We could avoid setting None (and forcing secure) if the realm does force TLS. This code [1] does look right because secure should not be blindly set if sameSite=None. Instead, we need to consider whether the server expects communication over TLS based on the required ssl settings and then configure sameSite.

In dev mode and using default realm settings (required ssl=external) we should not include sameSite=None because connections mostly happen through a non-secure channel. Even if you are using a different hostname/domain.

In production mode and using default realm settings, we should not include sameSite=None for clients using a local address if requests are made through a non-secure channel.

However, when running behind a proxy, we are going to rely on the client address set (or not) by the proxy. If the proxy is opening connections through a non-secure channel, the server might end up not marking cookies as secure even though your application is publicly available through a secure channel.

In other words, it needs a bit more analysis to make sure we are not breaking existing deployments and their assumptions about how we set those cookies.

[1]

boolean secure_sameSite = sameSite == SameSiteAttributeValue.NONE || secure; // when SameSite=None, Secure attribute must be set

@vmuzikar
vmuzikar commented Feb 7, 2023
Copy link
Copy Markdown
Contributor

@pedroigor I think we can make some assumptions to set SameSite=None; Secure or SameSite=Lax based on server and realm settings, as you mentioned. However, to be on the safe side, I think we should have a way for the users to override it if necessary as it is a potential blocker (either too loose security in Lax, or unusable cookies in None and hence unable to log in).

@pedroigor
Copy link
Copy Markdown
Contributor

@vmuzikar Not sure if we need to explicitly set samesite to lax but just rely on the default from browsers?

@vmuzikar
vmuzikar commented Feb 7, 2023
Copy link
Copy Markdown
Contributor

@vmuzikar Not sure if we need to explicitly set samesite to lax but just rely on the default from browsers?

I'm not sure. According to the docs, some browsers defaults to None.

@jonkoops
Copy link
Copy Markdown
Contributor Author

This is growing a bit beyond the scope that I can pick up at the moment (have some other high prio tasks). If anyone wants to take this PR let me know.

Personally I think the best approach is always to use the most secure default and document this properly. If users run into issues with these defaults they should opt-out explicitly (e.g. --no-secure-cookies).

@ahus1
ahus1 commented Mar 20, 2023
Copy link
Copy Markdown
Member

A note to those who might pick up this PR up later:

There used to be a PR for the docs, which is no longer mergeable due to the move of the docs: keycloak/keycloak-documentation#1761

@jonkoops
Copy link
Copy Markdown
Contributor Author

I am closing this PR, as it has been out there for too long. I'll try to do a second attempt at this soon.

@MGLL
MGLL commented Jan 18, 2024
Copy link
Copy Markdown

Hello @jonkoops @pedroigor,

I'm coming from this discussion and the topic that the KEYCLOAK_SESSION and KEYCLOAK_SESSION_LEGACY was flagged from an external Penetration Test (company) as it's not HttpOnly.

From what I read here and from this code comment , it seems as of today, it's not possible to set it as HttpOnly.

So my questions are,

  • what is the risk having this cookie not set to HttpOnly ? (is it an acceptable risk ?)
  • is there a plan to allow this cookie as HttpOnly ?

Related discussions:

Best regards

@jonkoops
Copy link
Copy Markdown
Contributor Author

what is the risk having this cookie not set to HttpOnly?

I think that OWASP can give you the best answer to this question. We need those cookies to be accessible to JavaScript so we can verify the user has an active session, or otherwise redirect the user to restart the login flow.

is there a plan to allow this cookie as HttpOnly ?

Not at this time. But we're going to be re-evaluating the defaults on cookies in the coming year or so, which may or may not cause this to change.

@MGLL
MGLL commented Jan 18, 2024
Copy link
Copy Markdown

@jonkoops Thanks a lot for the response.
So as of today, is there any known risk mitigation regarding KEYCLOAK_SESSION ? (or it's just a risk we have to assume.)

@jonkoops
Copy link
Copy Markdown
Contributor Author

The latter, there is currently no mitigation you can take. In the future we will likely add an SPI so you can control cookie behaviour in a granular manner, at your own risk of course.

@MGLL
MGLL commented Jan 18, 2024
Copy link
Copy Markdown

@jonkoops thanks again, so I guess that's a risk we have to live with for now.

From what I understand, this cookie can't be set as HttpOnly because some Keycloak core elements relies on it (Admin Console, User Account, Login flow, etc).
Which would mean, that to make it HttpOnly and make it secure, it would require to refactor quite some logics on how this elements interact with the user's session.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deprecate legacy cookie behaviour behind profile

7 participants

0