Skip to content
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

Validate signatures for AWS chunked input streams #67

Merged
merged 4 commits into from
Jun 18, 2024

Conversation

Randgalt
Copy link
Member

Validate signatures for AWS chunked input streams

Follow the AWS chunked protocol to validate chunks that include
an AWS signature extension. Alters Signer so that it saves
values during main request validation need to validate the chunk
signatures.

Closes #55

@Randgalt Randgalt requested review from vagaerg and mosiac1 June 15, 2024 21:06
@cla-bot cla-bot bot added the cla-signed label Jun 15, 2024
@Randgalt Randgalt force-pushed the jordanz/aws-chunked-input-streams branch 4 times, most recently from d84714f to 2017150 Compare June 17, 2024 09:23
@Randgalt Randgalt changed the base branch from main to jordanz/better-signing-header-management June 17, 2024 09:23
@Randgalt Randgalt force-pushed the jordanz/better-signing-header-management branch from f1191fe to 3d812c6 Compare June 17, 2024 13:11
@Randgalt Randgalt force-pushed the jordanz/aws-chunked-input-streams branch from 2017150 to 8f4032e Compare June 17, 2024 13:18
@Randgalt Randgalt force-pushed the jordanz/better-signing-header-management branch 2 times, most recently from a519983 to c7c4a6a Compare June 17, 2024 15:05
Base automatically changed from jordanz/better-signing-header-management to main June 17, 2024 15:09
Provide a way for the internal Signer to stash context into the
SigningMetadata to be used later for AWS chunk stream signature
validation.
@Randgalt Randgalt force-pushed the jordanz/aws-chunked-input-streams branch from 8f4032e to e1d1b50 Compare June 17, 2024 15:19
switch (b) {
case '\\':
b = in.read();
outputStream.write(b);
Copy link
Member

Choose a reason for hiding this comment

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

Couldn't we just ignore this case? If we no-op here, we will read the next byte on the next loop iteration and we'd handle EOF correctly if there happened to be one

Copy link
Member Author

Choose a reason for hiding this comment

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

Note: I copied this code from Apache commons. I didn't try to improve it for fear of breaking something. This code has existing in Apache for a very long time.

state = 1;
break;
case '\"':
state = 2;
Copy link
Member

Choose a reason for hiding this comment

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

What is the reason for this handling of quotes? I thought the signatures were all base64, so is there any case where we would need to escape a newline inside it?

Copy link
Member Author

Choose a reason for hiding this comment

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

See my note above

String thisSignature = chunkSigner.signChunk(hasher.hash(), previousSignature);
if (!thisSignature.equals(expectedSignature)) {
log.debug("Chunk signature does not match expected signature. Expected: %s, Actual: %s", expectedSignature, thisSignature);
throw new WebApplicationException(UNAUTHORIZED);
Copy link
Member

Choose a reason for hiding this comment

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

Should we throw some internal exception here? I feel like we're leaking the responsibility of returning a 401 to places where it may not belong.

The fact this results in an HTTP request being unauthorized could be tracked at a higher level I feel, and not within the Chunk signing logic - wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought about that too. What about adding another ticket to do this? We could add a suite of internal exceptions and maybe an exception handler of some kind.

Copy link
Member

Choose a reason for hiding this comment

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

Done - #73

@Randgalt
Copy link
Member Author

Note: #72

This is more symmetrical with `Request` and allows us to clean up
usage of it.
`inputStream()` now always returns for any content including
standard byte arrays.
@Randgalt Randgalt force-pushed the jordanz/aws-chunked-input-streams branch 2 times, most recently from a869a97 to e8339d8 Compare June 17, 2024 18:48
@Randgalt Randgalt requested a review from vagaerg June 17, 2024 18:50
@Randgalt Randgalt force-pushed the jordanz/aws-chunked-input-streams branch from e8339d8 to 447f220 Compare June 18, 2024 08:17
Follow the AWS chunked protocol to validate chunks that include
an AWS signature extension. Alters `Signer` so that it saves
values during main request validation need to validate the chunk
signatures.

Closes #55
Copy link
Member

@vagaerg vagaerg left a comment

Choose a reason for hiding this comment

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

LGTM - got some suggestions for a refactor, will open a PR right after

@Randgalt Randgalt merged commit a00b37d into main Jun 18, 2024
2 checks passed
@Randgalt Randgalt deleted the jordanz/aws-chunked-input-streams branch June 18, 2024 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Properly handle aws-chunked encoding
2 participants