-
Notifications
You must be signed in to change notification settings - Fork 8.1k
added better validation and more validation tests #46277
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
Merged
Changes from all commits
Commits
Show all changes
4 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
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
mabartos marked this conversation as resolved.
Show resolved
Hide resolved
|
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 |
|---|---|---|
|
|
@@ -51,6 +51,7 @@ | |
| import org.apache.http.entity.StringEntity; | ||
| import org.apache.http.impl.client.CloseableHttpClient; | ||
| import org.apache.http.util.EntityUtils; | ||
| import org.junit.jupiter.api.Disabled; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.keycloak.services.cors.Cors.ACCESS_CONTROL_ALLOW_METHODS; | ||
|
|
@@ -617,6 +618,183 @@ public void preflight() throws Exception { | |
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void createClientWithInvalidRedirectUriFragment() throws Exception { | ||
| OIDCClientRepresentation rep = new OIDCClientRepresentation(); | ||
| rep.setEnabled(true); | ||
| rep.setClientId("client-invalid-fragment"); | ||
| rep.setRedirectUris(Set.of("http://localhost:3000#fragment")); | ||
| assertClientCreationFailsWithError(rep, "Redirect URIs must not contain an URI fragment"); | ||
| } | ||
|
|
||
| @Test | ||
| public void createClientWithInvalidRedirectUriScheme() throws Exception { | ||
| OIDCClientRepresentation rep = new OIDCClientRepresentation(); | ||
| rep.setEnabled(true); | ||
| rep.setClientId("client-invalid-scheme"); | ||
| rep.setRedirectUris(Set.of("javascript:alert(1)")); | ||
| assertClientCreationFailsWithError(rep, "Each redirect URL must be valid"); | ||
| } | ||
|
|
||
| @Test | ||
| @Disabled("Root URL fragment validation not yet implemented in V2 API") | ||
| public void createClientWithInvalidRootUrl() throws Exception { | ||
| OIDCClientRepresentation rep = new OIDCClientRepresentation(); | ||
| rep.setEnabled(true); | ||
| rep.setClientId("client-invalid-root-url"); | ||
| rep.setAppUrl("http://localhost:3000#fragment"); | ||
| assertClientCreationFailsWithError(rep, "Root URL must not contain an URL fragment"); | ||
| } | ||
|
|
||
| @Test | ||
| public void createSamlClientWithInvalidRedirectUriFragment() throws Exception { | ||
| SAMLClientRepresentation rep = new SAMLClientRepresentation(); | ||
| rep.setEnabled(true); | ||
| rep.setClientId("saml-client-invalid-fragment"); | ||
| rep.setRedirectUris(Set.of("http://localhost:3000#fragment")); | ||
| assertClientCreationFailsWithError(rep, "Redirect URIs must not contain an URI fragment"); | ||
| } | ||
|
|
||
| @Test | ||
| public void createSamlClientWithInvalidRedirectUriScheme() throws Exception { | ||
| SAMLClientRepresentation rep = new SAMLClientRepresentation(); | ||
| rep.setEnabled(true); | ||
| rep.setClientId("saml-client-invalid-scheme"); | ||
| rep.setRedirectUris(Set.of("javascript:alert(1)")); | ||
| assertClientCreationFailsWithError(rep, "Each redirect URL must be valid"); | ||
| } | ||
|
|
||
| @Test | ||
| @Disabled("Root URL fragment validation not yet implemented in V2 API") | ||
| public void createSamlClientWithInvalidRootUrl() throws Exception { | ||
| SAMLClientRepresentation rep = new SAMLClientRepresentation(); | ||
| rep.setEnabled(true); | ||
| rep.setClientId("saml-client-invalid-root-url"); | ||
| rep.setAppUrl("http://localhost:3000#fragment"); | ||
| assertClientCreationFailsWithError(rep, "Root URL must not contain an URL fragment"); | ||
| } | ||
|
|
||
| @Test | ||
| public void updateClientWithInvalidRedirectUriFragment() throws Exception { | ||
| OIDCClientRepresentation rep = new OIDCClientRepresentation(); | ||
| rep.setEnabled(true); | ||
| rep.setClientId("client-update-invalid-fragment"); | ||
| rep.setRedirectUris(Set.of("http://localhost:3000#fragment")); | ||
| assertClientUpdateFailsWithError(rep, "Redirect URIs must not contain an URI fragment"); | ||
| } | ||
|
|
||
| @Test | ||
| public void updateClientWithInvalidRedirectUriScheme() throws Exception { | ||
| OIDCClientRepresentation rep = new OIDCClientRepresentation(); | ||
| rep.setEnabled(true); | ||
| rep.setClientId("client-update-invalid-scheme"); | ||
| rep.setRedirectUris(Set.of("javascript:alert(1)")); | ||
| assertClientUpdateFailsWithError(rep, "A redirect URI uses an illegal scheme"); | ||
| } | ||
|
|
||
| @Test | ||
| @Disabled("Root URL fragment validation not yet implemented in V2 API") | ||
| public void updateClientWithInvalidRootUrl() throws Exception { | ||
| OIDCClientRepresentation rep = new OIDCClientRepresentation(); | ||
| rep.setEnabled(true); | ||
| rep.setClientId("client-update-invalid-root-url"); | ||
| rep.setAppUrl("http://localhost:3000#fragment"); | ||
| assertClientUpdateFailsWithError(rep, "Root URL must not contain an URL fragment"); | ||
| } | ||
|
|
||
| @Test | ||
| public void updateSamlClientWithInvalidRedirectUriFragment() throws Exception { | ||
| SAMLClientRepresentation rep = new SAMLClientRepresentation(); | ||
| rep.setEnabled(true); | ||
| rep.setClientId("saml-client-update-invalid-fragment"); | ||
| rep.setRedirectUris(Set.of("http://localhost:3000#fragment")); | ||
| assertClientUpdateFailsWithError(rep, "Redirect URIs must not contain an URI fragment"); | ||
| } | ||
|
|
||
| @Test | ||
| public void updateSamlClientWithInvalidRedirectUriScheme() throws Exception { | ||
| SAMLClientRepresentation rep = new SAMLClientRepresentation(); | ||
| rep.setEnabled(true); | ||
| rep.setClientId("saml-client-update-invalid-scheme"); | ||
| rep.setRedirectUris(Set.of("javascript:alert(1)")); | ||
| assertClientUpdateFailsWithError(rep, "A redirect URI uses an illegal scheme"); | ||
| } | ||
|
|
||
| @Test | ||
| @Disabled("Root URL fragment validation not yet implemented in V2 API") | ||
| public void updateSamlClientWithInvalidRootUrl() throws Exception { | ||
| SAMLClientRepresentation rep = new SAMLClientRepresentation(); | ||
| rep.setEnabled(true); | ||
| rep.setClientId("saml-client-update-invalid-root-url"); | ||
| rep.setAppUrl("http://localhost:3000#fragment"); | ||
| assertClientUpdateFailsWithError(rep, "Root URL must not contain an URL fragment"); | ||
| } | ||
|
|
||
| /** | ||
| * Helper method to verify that client creation fails with the expected validation error. | ||
| * This verifies that ValidationUtil.validateClient is called after the full model is populated. | ||
| */ | ||
| private void assertClientCreationFailsWithError(BaseClientRepresentation rep, String expectedErrorMessage) throws Exception { | ||
| HttpPost request = new HttpPost(getClientsApiUrl()); | ||
| setAuthHeader(request); | ||
| request.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); | ||
| request.setEntity(new StringEntity(mapper.writeValueAsString(rep))); | ||
|
|
||
| try (var response = client.execute(request)) { | ||
| assertThat(response.getStatusLine().getStatusCode(), is(400)); | ||
| String body = EntityUtils.toString(response.getEntity()); | ||
| assertThat(body, containsString(expectedErrorMessage)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Helper method to verify that client update fails with the expected validation error. | ||
| * First creates a valid client, then attempts to update it with invalid data. | ||
| */ | ||
| private void assertClientUpdateFailsWithError(BaseClientRepresentation rep, String expectedErrorMessage) throws Exception { | ||
| String clientId = rep.getClientId(); | ||
|
|
||
| // First, create a valid client | ||
| HttpPut createRequest = new HttpPut(getClientsApiUrl() + "/" + clientId); | ||
| setAuthHeader(createRequest); | ||
| createRequest.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); | ||
|
|
||
| BaseClientRepresentation validRep; | ||
| if (rep instanceof SAMLClientRepresentation) { | ||
| validRep = new SAMLClientRepresentation(); | ||
| } else { | ||
| validRep = new OIDCClientRepresentation(); | ||
| } | ||
| validRep.setClientId(clientId); | ||
| validRep.setEnabled(true); | ||
|
|
||
| createRequest.setEntity(new StringEntity(mapper.writeValueAsString(validRep))); | ||
|
|
||
| try (var response = client.execute(createRequest)) { | ||
| assertThat(response.getStatusLine().getStatusCode(), is(201)); | ||
| EntityUtils.consumeQuietly(response.getEntity()); | ||
| } | ||
|
|
||
| // Now try to update with invalid data | ||
| HttpPut updateRequest = new HttpPut(getClientsApiUrl() + "/" + clientId); | ||
| setAuthHeader(updateRequest); | ||
| updateRequest.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); | ||
| updateRequest.setEntity(new StringEntity(mapper.writeValueAsString(rep))); | ||
|
|
||
| try (var response = client.execute(updateRequest)) { | ||
| assertThat(response.getStatusLine().getStatusCode(), is(400)); | ||
| String body = EntityUtils.toString(response.getEntity()); | ||
| assertThat(body, containsString(expectedErrorMessage)); | ||
| } | ||
|
|
||
| // Cleanup: delete the created client | ||
| HttpDelete deleteRequest = new HttpDelete(getClientsApiUrl() + "/" + clientId); | ||
|
Contributor
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. Nitpick: It'd be good to have the cleanup either in the try-finally block, or some other error-resistant cleanup. But it can be done in a follow-up tasks as we'd need to probably polish other tests as well. |
||
| setAuthHeader(deleteRequest); | ||
| try (var response = client.execute(deleteRequest)) { | ||
| EntityUtils.consumeQuietly(response.getEntity()); | ||
| } | ||
| } | ||
|
|
||
| private OIDCClientRepresentation getTestingFullClientRep() { | ||
| var rep = new OIDCClientRepresentation(); | ||
| rep.setClientId("my-client"); | ||
|
|
||
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.