-
Notifications
You must be signed in to change notification settings - Fork 121
fix: do not truncate encrypted streams when offset is greater than zero #2113
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
Conversation
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.
Left a suggestion, I would like to add a few comments to the tests.
We could do that in a different PR if you are in a hurry though
// the original offset MUST NOT be part of the length calculation, | ||
// or else the encryption will be truncated. | ||
size = len - (offset - off); |
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.
// the original offset MUST NOT be part of the length calculation, | |
// or else the encryption will be truncated. | |
size = len - (offset - off); | |
// `offset` is a local variable that accumulates the encrypted data, | |
// and it is initialized with `off`. | |
// Therefore the original offset, `off`, MUST be removed | |
// or else the encryption will be truncated. | |
size = (len - offset) - off; |
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.
offset
==> position
src/test/java/com/amazonaws/encryptionsdk/internal/FrameEncryptionHandlerTest.java
Show resolved
Hide resolved
public void testStreamTruncation() throws Exception { | ||
String testDataString = StringUtils.repeat("Hello, World! ", 10_000); | ||
|
||
int startOffset = 100; // The data will start from this offset |
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.
int startOffset = 100; // The data will start from this offset | |
int startOffset = 100; // This is the offset we will use for our test |
inputData, | ||
startOffset, | ||
inputData.length - startOffset); | ||
// decryptData doesn't know about the offset |
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.
// decryptData doesn't know about the offset | |
// decryptData doesn't know about the offset, because we will encrypt starting at `startOffset` |
src/test/java/com/amazonaws/encryptionsdk/internal/FrameEncryptionHandlerTest.java
Show resolved
Hide resolved
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { | ||
try (CryptoOutputStream cryptoOutput = | ||
crypto.createEncryptingStream(keyring, os, Collections.emptyMap())) { | ||
cryptoOutput.write(inputData, startOffset, inputData.length - startOffset); |
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.
cryptoOutput.write(inputData, startOffset, inputData.length - startOffset); | |
// Here we use the offset to encrypt only part of the inputData | |
cryptoOutput.write(inputData, startOffset, inputData.length - startOffset); |
src/test/java/com/amazonaws/encryptionsdk/internal/FrameEncryptionHandlerTest.java
Show resolved
Hide resolved
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.
LGTM
Issue #, if available: #2112
Description of changes: Fixes the truncation bug described in the issue above. I think using parens and subtracting offset is easier to read than the suggested change but is logically the same. We could also introduce a new variable e.g.
bytesProcessed
which starts at 0 and counts up bysize
each loop, but I think this approach is most readable.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
Check any applicable: