8000 fix: adds more validation of proxy-headers by shawkins · Pull Request #38092 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

fix: adds more validation of proxy-headers - #38092

Merged
vmuzikar merged 9 commits into
keycloak:mainfrom
shawkins:proxy_validation
May 13, 2025
Merged

vmuzikar merged 9 commits into
keycloak:mainfrom
shawkins:proxy_validation

Conversation

@shawkins
@shawkins shawkins commented Mar 13, 2025 •
Copy link
Copy Markdown
Contributor

cc @vmuzikar @mabartos this is a rough in of code changes to add more validation around proxy-headers. Without re-introducing something like the proxy setting, we cannot directly infer the user's intention in several cases.

In particular we still don't have a good handle on what some users were doing using Keycloak over http but not through a proxy, so the message just says something vague about reviewing the usage: "Please review whether this direct http usage of keycloak is expected." @vmuzikar I believe you mentioned something about this case being problematic with Docker usage - keycloak ends up not actually being accessed over localhost, so it's not a secure context.

It would also make more sense to put this validation (and that of the legacy property usage) in the v2 provider, but that would require broader access to the whole configuration. If we want to pursue that we could either consider adding something like Config.Scope.getDescoped(String key) - which breaks the config encapsulation a little bit, or we'll have to map everything of interest to an spi property.

The other place where users are commonly getting tripped up is here

logger.debugf("client %s does not allow origin=%s for requestOrigin=%s (as determined by the proxy-header setting), init will return a 403", clientId, origin, requestOrigin);
- I previously updated the debug log to reference proxy-headers, but it could be expanded upon and/or logged at a higher level.

I haven't done it yet, but some additional doc changes will be added as well.

WDYT?

closes: #38091

@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.

@shawkins Thanks for the PR. Added some comments/questions.

@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.

@shawkins Thanks for the update.

Just to summarize offline discussion we had. The validation should fail if (pseudo code):

!tlsConfigured && proxyHeaders == null && (!hostname.startsWith("https://") || hostname.isLocalContext())

The validation should be just a warning not to be breaking change (for some folks even the misconfigured scenario might sort of work).

We should also document that Admin Console loads only in secure context, incl. what secure context mean (TLS from the browser's perspective, or localhost).

@shawkins
Copy link
Copy Markdown
Contributor Author

I'll circle back to this after #38536 is merged because it touches the same logic.

@shawkins
shawkins force-pushed the proxy_validation branch 3 times, most recently from 6b59793 to 3c9a744 Compare April 1, 2025 12:35
@shawkins
shawkins marked this pull request as ready for review April 1, 2025 12:36
@shawkins
shawkins requested review from a team as code owners April 1, 2025 12:36
@shawkins
shawkins force-pushed the proxy_validation branch 2 times, most recently from 71b8296 to b996b7f Compare April 3, 2025 12:36
@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unreported flaky test detected, please review

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.adapter.servlet.SAMLClockSkewAdapterTest#testTokenTimeIsValid

Keycloak CI - Base IT

org.jboss.arquillian.graphene.enricher.exception.PageObjectInitializationException: 
Can not instantiate Page Object 
class org.keycloak.testsuite.adapter.page.SalesPostClockSkewServlet
 declared in: 
org.keycloak.testsuite.adapter.servlet.SAMLClockSkewAdapterTest
...

Report flaky test

@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.

In general looks good.

Could you please also update the docs to incorporate some notes around these new validations?

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.

Why // else warn on prod? The warning si already there, no?

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.

The else case here is something like hostname=http://localhost - doesn't seem like that would be used in production, but I wasn't sure if it warrented a warning.

@shawkins
shawkins commented Apr 3, 2025
Copy link
8000 Copy Markdown
Contributor Author

In general looks good.

Could you please also update the docs to incorporate some notes around these new validations?

Yes, once we have a handle on all the cases / verbiage, I'll update the docs.

@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unreported flaky test detected, please review

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.cluster.ClientInvalidationClusterTest#crudWithFailover

Keycloak CI - Clustering IT

java.lang.RuntimeException: java.lang.IllegalStateException: Keycloak unexpectedly died :(
	at org.keycloak.testsuite.arquillian.containers.KeycloakQuarkusServerDeployableContainer.start(KeycloakQuarkusServerDeployableContainer.java:71)
	at org.jboss.arquillian.container.impl.ContainerImpl.start(ContainerImpl.java:185)
	at org.jboss.arquillian.container.impl.client.container.ContainerLifecycleController$8.perform(ContainerLifecycleController.java:137)
	at org.jboss.arquillian.container.impl.client.container.ContainerLifecycleController$8.perform(ContainerLifecycleController.java:133)
...

Report flaky test

@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.

IMHO it is good enough for now, it's a good start and we'll see if that helps the users.

Added some minor comments, mostly typos.

I think we can update the docs and we're ready for merge. It'd be good to get it to 26.2.

@shawkins
Copy link
Copy Markdown
Contributor Author

Finally circling back to this. It now incorporates the config warnings into the hostname-debug output along with a more precise test of misconfiguration of the frontend URL.

There are two ways that could be done - either do it under the main hostname-debug endpoint logic, or the endpoint test logic. I opted for the latter.

The only thing I'd like to additionally check for is if tls passthrough is being used and proxy headers have been set - but this will take a bit more logic to determine. Does it seem worth it?

cc @vmuzikar

@shawkins
shawkins marked this pull request as draft May 1, 2025 12:28
@shawkins
shawkins requested a review from vmuzikar May 1, 2025 12:28
@shawkins
shawkins force-pushed the proxy_validation branch from b9af8b2 to 3e3d9d9 Compare May 1, 2025 23:03
@shawkins
shawkins marked this pull request as ready for review May 1, 2025 23:04
@shawkins
shawkins commented May 1, 2025
Copy link
Copy Markdown
Contributor Author

@vmuzikar reviewed again for consistency across the docs, startup warnings, and hostname-debug page. The only thing that needed adjusted was recommending that someone use a full url hostname when behind a proxy as a remedy for failing CORs checks. As far as I can tell, this does not work.

In LoginStatusIframeEndpoint logic

String requestOrigin = UriUtils.getOrigin(uriInfo.getRequestUri());
and in the previous admin redirect check https://github.com/keycloak/keycloak/pull/39095/files#diff-3b717e96493408412377f67ab2a8acac4a1c84b6746a3d7b61b56512fcf44d0aL97 the usage of session.getContext().getUri().getRequestUri() when there are no proxy headers set returns the actual details of the actual request sent from the proxy server to keycloak. It is unaffected by a full hostname url.

So the only time you can get away with setting the full hostname url, but not the proxy-headers, is when you are not behind a proxy.

@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.

Should already be up-to-date.

Right, I missed it, thanks.

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.

Should we check for other X-Forwarded-* headers too, or at least change it to X-Forwarded-Host as it's most relevant for this case?

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.

I was just effectively reusing the check that we're doing in the WelcomeResource - do you want to consolidate those and switch to X-Forwarded-Host instead?

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.

do you want to consolidate those and switch to X-Forwarded-Host instead?

That'd probably make sense.

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.

But it's not a blocker, approving regardless, feel free to merge.

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.

Refined to use the headers we have defined in the constants.

@shawkins
shawkins commented May 7, 2025 •
Copy link
Copy Markdown
Contributor Author

Alternative would be to revisit the code base to make it uses the Hostname provider (with explicit URL type) instead of raw requests but it might not be possible everywhere, and we'd risk regressions.

It could be the issue here is some inconsistency or missing guidance over the HOST header. The full hostname URL could work without proxy-headers when using re-encrypt - as long as the proxy sets or keeps the HOST header to be what was used to access the proxy. This does not appear to be the default behavior for nginx, as it will set the host to what is used to access the backend. I don't think this won't work in edge mode because the scheme is not part of the HOST header.

@vmuzikar
vmuzikar commented May 7, 2025
Copy link
Copy Markdown
Contributor

Actually, there seems to be something wrong with the commits. Can you please check it?

@shawkins
shawkins force-pushed the proxy_validation branch from 5ee497d to 8b92505 Compare May 7, 2025 15:24
@shawkins
shawkins commented May 7, 2025
Copy link
Copy Markdown
Contributor Author

Actually, there seems to be something wrong with the commits. Can you please check it?

Should be better now. Added one more validation about setting proxy-headers in edge mode.

@vmuzikar vmuzikar May 7, 2025 •
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 did some testing and this actually does not get us the "raw" request but one enriched by the Hostname provider, see:

this.hostname = hostnameProvider.getHostname(delegate, type);

@shawkins Could you please check it?

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.

I have. You only get the enrichment if you call getBaseUri. If you call getRequestUri, you get the underlying request:

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.

Then I might be doing something wrong. :)
I start KC with hostname=example.com and access http://localhost:8080/realms/master/hostname-debug. I would expect to get that warning but it's not emitted, and while debugging I can actually see that requestURI = http://example.com:8080/realms/master/hostname-debug/test?frontEnd=true.

@shawkins shawkins May 7, 2025 •
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.

If you have hostname=example.com the urls tested by hostname-debug will be to example.com, not localhost. If you have a proxy setup for example.com, you should get the warning if it then accesses keycloak over localhost.

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.

If you have hostname=example.com the urls tested by hostname-debug will be to example.com, not localhost.

Right, sorry, I was stupid and also forgot I have example.com in my /etc/hosts. :D

LGTM, thanks. Let's get this merged finally.

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.

Just to double check - do you think we need to review our usage of getRequestUri and the other methods on KeycloakUriInfo that expose the underlying delegate? I see several code paths using that, which makes using the full hostname url without proxy-headers problematic - or are we good with just requiring the proxy-headers to be be set?

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.

or are we good with just requiring the proxy-headers to be be set

IMHO we're good for now - we say in docs proxy headers are required anyway for edge and reencrypt. If we did a refactor of getRequestUri, I would do that in a separate PR. But I wouldn't do that for now not to risk regressions.

@vmuzikar
vmuzikar enabled auto-merge (squash) May 7, 2025 16:25
shawkins and others added 9 commits May 13, 2025 09:21
also improves the handling and message around insecure contexts

closes: keycloak#38091

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
Signed-off-by: Steve Hawkins <shawkins@redhat.com>
…figuration/mappers/HostnameV2PropertyMappers.java

Co-authored-by: Václav Muzikář <vaclav@muzikari.cz>
Signed-off-by: Steven Hawkins <shawkins@redhat.com>
added updates from reviews

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
also adding detection of origin mismatch

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
…vices/resources/DebugHostnameSettingsResource.java

Co-authored-by: Václav Muzikář <vaclav@muzikari.cz>
Signed-off-by: Steven Hawkins <shawkins@redhat.com>
…vices/resources/DebugHostnameSettingsResource.java

Co-authored-by: Václav Muzikář <vaclav@muzikari.cz>
Signed-off-by: Steven Hawkins <shawkins@redhat.com>
and adding a specific check for a non-secure context

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
Signed-off-by: Steve Hawkins <shawkins@redhat.com>
@vmuzikar
vmuzikar force-pushed the proxy_validation branch from e3a8548 to a4687ad Compare May 13, 2025 07:21
@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unreported flaky test detected, please review

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.webauthn.account.WebAuthnSigningInTest#categoriesTest

Keycloak CI - WebAuthn IT (firefox)

java.lang.AssertionError: 

Expected: is <2>
     but: was <0>
	at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
...

Report flaky test

@vmuzikar
vmuzikar merged commit 989738f into keycloak:main May 13, 2025
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.

Add more validation for proxy-headers

2 participants

0