-
Notifications
You must be signed in to change notification settings - Fork 8.8k
fix: return 400 instead of 500 for malformed Content-Type headers on token endpoint #50200
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
Closed
GautamKumarOffical
wants to merge
2
commits into
keycloak:main
from
GautamKumarOffical:fix-49964-malformed-content-type
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,6 +132,10 @@ private static Response.Status getResponseStatus(Throwable throwable) { | |
| return Response.Status.CONFLICT; | ||
| } | ||
|
|
||
| if (throwable instanceof IllegalArgumentException && isMediaTypeParsingError(throwable)) { | ||
| return Response.Status.BAD_REQUEST; | ||
| } | ||
|
|
||
| return Response.Status.INTERNAL_SERVER_ERROR; | ||
| } | ||
|
|
||
|
|
@@ -146,13 +150,37 @@ private static String getErrorCode(Throwable throwable) { | |
| return "conflict"; | ||
| } | ||
|
|
||
| if (throwable instanceof IllegalArgumentException && isMediaTypeParsingError(throwable)) { | ||
| return OAuthErrorException.INVALID_REQUEST; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it desirable to return the |
||
| } | ||
|
|
||
| if (throwable instanceof WebApplicationException && throwable.getMessage() != null) { | ||
| return throwable.getMessage(); | ||
| } | ||
|
|
||
| return "unknown_error"; | ||
| } | ||
|
|
||
| private static boolean isMediaTypeParsingError(Throwable throwable) { | ||
| Throwable current = throwable; | ||
| while (current != null) { | ||
| String msg = current.getMessage(); | ||
| if (msg != null) { | ||
| String lower = msg.toLowerCase(Locale.ROOT); | ||
| if (lower.contains("media type") || lower.contains("content-type") || | ||
| lower.contains("could not find") || lower.contains("invalid")) { | ||
| return true; | ||
| } | ||
| } | ||
| Class<?> cls = current.getClass(); | ||
| if (cls.getName().contains("MediaType") || cls.getName().contains("BadRequest")) { | ||
| return true; | ||
| } | ||
| current = current.getCause(); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static RealmModel resolveRealm(KeycloakSession session) { | ||
| String path = session.getContext().getUri().getPath(); | ||
| Matcher m = realmNamePattern.matcher(path); | ||
|
|
||
99 changes: 99 additions & 0 deletions
99
tests/base/src/test/java/org/keycloak/tests/error/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,99 @@ | ||
| package org.keycloak.tests.error; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URI; | ||
|
|
||
| import org.keycloak.OAuthErrorException; | ||
| import org.keycloak.common.util.KeycloakUriBuilder; | ||
| import org.keycloak.representations.idm.OAuth2ErrorRepresentation; | ||
| import org.keycloak.testframework.annotations.InjectHttpClient; | ||
| import org.keycloak.testframework.annotations.InjectKeycloakUrls; | ||
| import org.keycloak.testframework.annotations.InjectRealm; | ||
| import org.keycloak.testframework.annotations.KeycloakIntegrationTest; | ||
| import org.keycloak.testframework.realm.ManagedRealm; | ||
| import org.keycloak.testframework.server.KeycloakUrls; | ||
| import org.keycloak.util.JsonSerialization; | ||
|
|
||
| import org.apache.http.HttpResponse; | ||
| import org.apache.http.client.HttpClient; | ||
| import org.apache.http.client.methods.HttpPost; | ||
| import org.apache.http.entity.StringEntity; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.is; | ||
|
|
||
| @KeycloakIntegrationTest | ||
| class MalformedContentTypeTest { | ||
|
|
||
| @InjectKeycloakUrls | ||
| KeycloakUrls keycloakUrls; | ||
|
|
||
| @InjectHttpClient | ||
| HttpClient httpClient; | ||
|
|
||
| @InjectRealm(attachTo = "master") | ||
| ManagedRealm masterRealm; | ||
|
|
||
| @Test | ||
| void malformedContentTypeOnTokenEndpoint() throws IOException { | ||
| HttpPost post = new HttpPost(tokenUri()); | ||
| post.setHeader("Content-Type", "invalid/@@##"); | ||
| post.setEntity(new StringEntity("grant_type=password&client_id=admin-cli&username=admin&password=admin")); | ||
|
|
||
| HttpResponse response = httpClient.execute(post); | ||
| assertThat("status code should be 400", response.getStatusLine().getStatusCode(), is(400)); | ||
|
|
||
| OAuth2ErrorRepresentation error = JsonSerialization.readValue( | ||
| response.getEntity().getContent(), OAuth2ErrorRepresentation.class); | ||
| assertThat("error code", error.getError(), is(OAuthErrorException.INVALID_REQUEST)); | ||
| } | ||
|
|
||
| @Test | ||
| void emptyContentTypeOnTokenEndpoint() throws IOException { | ||
| HttpPost post = new HttpPost(tokenUri()); | ||
| post.setHeader("Content-Type", ""); | ||
| post.setEntity(new StringEntity("grant_type=password&client_id=admin-cli&username=admin&password=admin")); | ||
|
|
||
| HttpResponse response = httpClient.execute(post); | ||
| assertThat("status code should be 400", response.getStatusLine().getStatusCode(), is(400)); | ||
|
|
||
| OAuth2ErrorRepresentation error = JsonSerialization.readValue( | ||
| response.getEntity().getContent(), OAuth2ErrorRepresentation.class); | ||
| assertThat("error code", error.getError(), is(OAuthErrorException.INVALID_REQUEST)); | ||
| } | ||
|
|
||
| @Test | ||
| void xssPayloadContentTypeOnTokenEndpoint() throws IOException { | ||
| HttpPost post = new HttpPost(tokenUri()); | ||
| post.setHeader("Content-Type", "</>\"alert(1)"); | ||
| post.setEntity(new StringEntity("grant_type=password&client_id=admin-cli&username=admin&password=admin")); | ||
|
|
||
| HttpResponse response = httpClient.execute(post); | ||
| assertThat("status code should be 400", response.getStatusLine().getStatusCode(), is(400)); | ||
|
|
||
| OAuth2ErrorRepresentation error = JsonSerialization.readValue( | ||
| response.getEntity().getContent(), OAuth2ErrorRepresentation.class); | ||
| assertThat("error code", error.getError(), is(OAuthErrorException.INVALID_REQUEST)); | ||
| } | ||
|
|
||
| @Test | ||
| void xssScriptPayloadContentTypeOnTokenEndpoint() throws IOException { | ||
| HttpPost post = new HttpPost(tokenUri()); | ||
| post.setHeader("Content-Type", "</><script>alert(1)</script>"); | ||
| post.setEntity(new StringEntity("grant_type=password&client_id=admin-cli&username=admin&password=admin")); | ||
|
|
||
| HttpResponse response = httpClient.execute(post); | ||
| assertThat("status code should be 400", response.getStatusLine().getStatusCode(), is(400)); | ||
|
|
||
| OAuth2ErrorRepresentation error = JsonSerialization.readValue( | ||
| response.getEntity().getContent(), OAuth2ErrorRepresentation.class); | ||
| assertThat("error code", error.getError(), is(OAuthErrorException.INVALID_REQUEST)); | ||
| } | ||
|
|
||
| private URI tokenUri() { | ||
| return KeycloakUriBuilder.fromUri(keycloakUrls.getMasterRealm()) | ||
| .path("protocol/openid-connect/token") | ||
| .build(); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this breaks when run with the current Quarkus main considering I fixed the logic upstream to get 415, now Quarkus REST do not throw
IllegalArgumentException.