Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ahus1
Copy link
Contributor

@ahus1 ahus1 commented Mar 7, 2024

Closes #27293

@ahus1 ahus1 force-pushed the is-27293-prevent-too-long-responses branch from ad92a8d to 4e68ce1 Compare March 7, 2024 11:15
@ahus1
Copy link
Contributor Author

ahus1 commented Mar 7, 2024

@rmartinc - as I've been working on the other issue, I had an idea on how to limit the response size without moving to v5 of the library.

The maximum response size can be configured for DefaultHttpClientFactory, but all users of SimpleHttp would set their own maximum size. I don't have a good idea where to pull this information from.

Maybe it is good enough for now? WDYT?

@rmartinc
Copy link
Contributor

rmartinc commented Mar 7, 2024

@ahus1 Maybe we can add a new method in the HttpClientProvider interface that returns the max value:

default long getMaxConsumedResponseSize() {
    return DEFAULT_MAX_CONSUMED_RESPONSE_SIZE;
}

The default implementation overwrites it with the configured value if set. And then the SimpleHttp just uses that value to create the responses in the methods that receive the session. We can maintain the set method are you are doing now too for direct usage (which I think is only done in the tests).

@ahus1
Copy link
Contributor Author

ahus1 commented Mar 7, 2024

@rmartinc - I've updated this PR. Please let me know if you think this is a valid approach when you have the time.

Pinging also @abstractj as you've been managing the original issue.

@ahus1 ahus1 requested a review from rmartinc March 7, 2024 17:15
@ahus1 ahus1 marked this pull request as ready for review March 7, 2024 17:15
@ahus1 ahus1 requested a review from a team as a code owner March 7, 2024 17:15
Copy link
Contributor

@rmartinc rmartinc left a comment

Choose a reason for hiding this comment

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

Very good job @ahus1!
Just some minor things that maybe you can consider to add.

return doDelete(url, client, HttpClientProvider.DEFAULT_MAX_CONSUMED_RESPONSE_SIZE);
}

public static SimpleHttp doDelete(String url, HttpClient client, long maxConsumedResponseSize) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe too many variants, IMO this one is not needed as you can always do: SimpleHttp.doDelete(url, client).setMaxConsumedResponseSize(512*1024).asJson(). But this is just a comment, you can leave as it is if you like these variants.

Comment on lines 48 to 49
if (bytesConsumed >= maxBytesToConsume) {
throw new IOException(String.format("Response is longer than %s, with max bytes to be consumed being %d", bytesConsumed, maxBytesToConsume));
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a minor thing, but this wouldn't be > insteadof >=, just to allow exact number. And I would use: Response is at least %s bytes in size, with max....

@Override
public int read(byte[] b, int off, int len) throws IOException {
int sizeRead = delegate.read(b, off, len);
bytesConsumed += sizeRead;
Copy link
Contributor

Choose a reason for hiding this comment

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

Better do the same than in single read: if (sizeRead > 0) bytesConsumed += sizeRead;

public void withCharset() throws IOException {
HttpResponse httpResponse = createBasicResponse(entity);
SimpleHttp.Response response = new SimpleHttp.Response(httpResponse);
SimpleHttp.Response response = new SimpleHttp.Response(httpResponse, HttpClientProvider.DEFAULT_MAX_CONSUMED_RESPONSE_SIZE);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we could add a test here too. I did something quick in this commit: rmartinc@1e65623
You can use it or something similar.

Copy link
Contributor

@rmartinc rmartinc left a comment

Choose a reason for hiding this comment

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

Thanks for the changes! Good idea limiting SimpleHttp to the methods that pass the session. LGTM now! Let's see the tests!

abstractj
abstractj previously approved these changes Mar 11, 2024
@ahus1 ahus1 force-pushed the is-27293-prevent-too-long-responses branch from eb80cad to ec154c9 Compare March 12, 2024 09:45
Closes keycloak#27293

Co-authored-by: rmartinc <[email protected]>
Signed-off-by: rmartinc <[email protected]>
Signed-off-by: Alexander Schwartz <[email protected]>
@ahus1 ahus1 force-pushed the is-27293-prevent-too-long-responses branch from ec154c9 to 383b905 Compare March 12, 2024 09:46
@ahus1
Copy link
Contributor Author

ahus1 commented Mar 12, 2024

@abstractj - I needed to rebase the PR due to conflicts in the documentation. Please re-approve when you have the time. Thanks!

@ahus1 ahus1 requested a review from abstractj March 12, 2024 09:47
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.

Denial of service via excessive response size from identity provider

3 participants