-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Respond with 400 status code and invalid_request for malformed content types where specifications require it
#51410
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
Merged
vmuzikar
merged 1 commit into
keycloak:main
from
michalvavrik:dev-auto/keycloak-49964/main
Aug 7, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
services/src/main/java/org/keycloak/protocol/oidc/utils/ContentTypeValidationUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package org.keycloak.protocol.oidc.utils; | ||
|
|
||
| import jakarta.ws.rs.core.HttpHeaders; | ||
| import jakarta.ws.rs.core.MediaType; | ||
| import jakarta.ws.rs.core.Response; | ||
|
|
||
| import org.keycloak.events.Errors; | ||
| import org.keycloak.services.ErrorResponseException; | ||
|
|
||
| public final class ContentTypeValidationUtil { | ||
|
|
||
| // FIXME: remove this validation when we use Quarkus with https://github.com/quarkusio/quarkus/pull/55676 | ||
| public static void requireValidContentType(HttpHeaders headers, MediaType requiredMediaType) { | ||
| String contentType = headers.getHeaderString(HttpHeaders.CONTENT_TYPE); | ||
| if (contentType == null) { | ||
| return; | ||
| } | ||
| MediaType requestMediaType; | ||
| try { | ||
| requestMediaType = MediaType.valueOf(contentType); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new ErrorResponseException(Errors.INVALID_REQUEST, "The content-type header value did not correspond to a valid media type", Response.Status.BAD_REQUEST); | ||
| } | ||
| if (!requestMediaType.isCompatible(requiredMediaType)) { | ||
| throw new ErrorResponseException(Errors.INVALID_REQUEST, "The content-type header value does not match consumed media type " + requiredMediaType, Response.Status.BAD_REQUEST); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
156 changes: 156 additions & 0 deletions
156
tests/base/src/test/java/org/keycloak/tests/oauth/MalformedContentTypeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| package org.keycloak.tests.oauth; | ||
|
|
||
| import java.util.stream.Stream; | ||
|
|
||
| import jakarta.ws.rs.core.MediaType; | ||
|
|
||
| import org.keycloak.OAuthErrorException; | ||
| import org.keycloak.models.OAuth2DeviceConfig; | ||
| import org.keycloak.testframework.annotations.InjectRealm; | ||
| import org.keycloak.testframework.annotations.KeycloakIntegrationTest; | ||
| import org.keycloak.testframework.oauth.OAuthClient; | ||
| import org.keycloak.testframework.oauth.annotations.InjectOAuthClient; | ||
| import org.keycloak.testframework.realm.ClientBuilder; | ||
| import org.keycloak.testframework.realm.ClientConfig; | ||
| import org.keycloak.testframework.realm.ManagedRealm; | ||
| import org.keycloak.testframework.realm.RealmBuilder; | ||
| import org.keycloak.testframework.realm.RealmConfig; | ||
| import org.keycloak.testframework.realm.UserBuilder; | ||
| import org.keycloak.testsuite.util.oauth.AbstractHttpResponse; | ||
| import org.keycloak.testsuite.util.oauth.AccessTokenResponse; | ||
| import org.keycloak.testsuite.util.oauth.BackchannelLogoutResponse; | ||
| import org.keycloak.testsuite.util.oauth.LogoutResponse; | ||
| import org.keycloak.testsuite.util.oauth.ParResponse; | ||
| import org.keycloak.testsuite.util.oauth.TokenRevocationResponse; | ||
| import org.keycloak.testsuite.util.oauth.ciba.AuthenticationRequestAcknowledgement; | ||
| import org.keycloak.testsuite.util.oauth.device.DeviceAuthorizationResponse; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| @KeycloakIntegrationTest | ||
| class MalformedContentTypeTest { | ||
|
|
||
| @InjectRealm(config = TestRealm.class) | ||
| ManagedRealm realm; | ||
|
|
||
| @InjectOAuthClient(config = TestClient.class) | ||
| OAuthClient oauth; | ||
|
|
||
| static Stream<String> malformedContentTypes() { | ||
| return Stream.of( | ||
| "invalid/@@##", | ||
| "</><script>alert(1)</script>", | ||
| "text", | ||
| "text/[html]", | ||
| "" | ||
| ); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("malformedContentTypes") | ||
| void tokenEndpoint(String contentType) { | ||
| AccessTokenResponse response = oauth.passwordGrantRequest("test-user@localhost", "password") | ||
| .header("Content-Type", contentType) | ||
| .send(); | ||
| assertOAuthInvalidRequest(response); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("malformedContentTypes") | ||
| void deviceAuthorizationEndpoint(String contentType) { | ||
| DeviceAuthorizationResponse response = oauth.device() | ||
| .deviceAuthorizationRequest() | ||
| .header("Content-Type", contentType) | ||
| .send(); | ||
| assertOAuthInvalidRequest(response); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("malformedContentTypes") | ||
| void logoutEndpoint(String contentType) { | ||
| AccessTokenResponse tokenResponse = oauth.doPasswordGrantRequest("test-user@localhost", "password"); | ||
| LogoutResponse response = oauth.logoutRequest() | ||
| .refreshToken(tokenResponse.getRefreshToken()) | ||
| .header("Content-Type", contentType) | ||
| .send(); | ||
| assertOAuthInvalidRequest(response); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("malformedContentTypes") | ||
| void tokenRevocationEndpoint(String contentType) { | ||
| AccessTokenResponse tokenResponse = oauth.doPasswordGrantRequest("test-user@localhost", "password"); | ||
| TokenRevocationResponse response = oauth.tokenRevocationRequest(tokenResponse.getAccessToken()) | ||
| .header("Content-Type", contentType) | ||
| .send(); | ||
| assertOAuthInvalidRequest(response); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("malformedContentTypes") | ||
| void backchannelLogoutEndpoint(String contentType) { | ||
| AccessTokenResponse tokenResponse = oauth.doPasswordGrantRequest("test-user@localhost", "password"); | ||
| BackchannelLogoutResponse response = oauth.backchannelLogoutRequest(tokenResponse.getRefreshToken()) | ||
| .header("Content-Type", contentType) | ||
| .send(); | ||
| assertOAuthInvalidRequest(response); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("malformedContentTypes") | ||
| void parEndpoint(String contentType) { | ||
| ParResponse response = oauth.pushedAuthorizationRequest() | ||
| .header("Content-Type", contentType) | ||
| .send(); | ||
| assertOAuthInvalidRequest(response); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("malformedContentTypes") | ||
| void cibaEndpoint(String contentType) { | ||
| AuthenticationRequestAcknowledgement response = oauth.ciba() | ||
| .backchannelAuthenticationRequest("test-user@localhost") | ||
| .header("Content-Type", contentType) | ||
| .send(); | ||
| assertOAuthInvalidRequest(response); | ||
| } | ||
|
|
||
| @Test | ||
| void deviceAuthorizationEndpointIncompatibleType() { | ||
| DeviceAuthorizationResponse response = oauth.device() | ||
| .deviceAuthorizationRequest() | ||
| .header("Content-Type", MediaType.TEXT_HTML) | ||
| .send(); | ||
| assertOAuthInvalidRequest(response); | ||
| } | ||
|
|
||
| private static void assertOAuthInvalidRequest(AbstractHttpResponse response) { | ||
| assertEquals(400, response.getStatusCode()); | ||
| assertEquals(OAuthErrorException.INVALID_REQUEST, response.getError()); | ||
| } | ||
|
|
||
| public static class TestRealm implements RealmConfig { | ||
| @Override | ||
| public RealmBuilder configure(RealmBuilder realm) { | ||
| return realm.users(UserBuilder.create("test-user@localhost") | ||
| .email("test-user@localhost") | ||
| .password("password") | ||
| .name("first", "last") | ||
| .enabled(true)); | ||
| } | ||
| } | ||
|
|
||
| public static class TestClient implements ClientConfig { | ||
| @Override | ||
| public ClientBuilder configure(ClientBuilder client) { | ||
| return client.clientId("test-app") | ||
| .secret("test-secret") | ||
| .directAccessGrantsEnabled(true) | ||
| .attribute(OAuth2DeviceConfig.OAUTH2_DEVICE_AUTHORIZATION_GRANT_ENABLED, "true"); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.