Skip to content

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

Merged
merged 5 commits into from
May 29, 2025

Conversation

kessplas
Copy link
Contributor

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 by size 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:

  • Were any files moved? Moving files changes their URL, which breaks all hyperlinks to the files.

@kessplas kessplas marked this pull request as ready for review May 28, 2025 21:56
@kessplas kessplas requested a review from a team as a code owner May 28, 2025 21:56
Copy link
Contributor

@seebees seebees left a 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

Comment on lines 119 to 121
// the original offset MUST NOT be part of the length calculation,
// or else the encryption will be truncated.
size = len - (offset - off);
Copy link
Contributor

@seebees seebees May 28, 2025

Choose a reason for hiding this comment

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

Suggested change
// 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;

Copy link
Contributor

Choose a reason for hiding this comment

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

offset ==> position

public void testStreamTruncation() throws Exception {
String testDataString = StringUtils.repeat("Hello, World! ", 10_000);

int startOffset = 100; // The data will start from this offset
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// decryptData doesn't know about the offset
// decryptData doesn't know about the offset, because we will encrypt starting at `startOffset`

try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
try (CryptoOutputStream cryptoOutput =
crypto.createEncryptingStream(keyring, os, Collections.emptyMap())) {
cryptoOutput.write(inputData, startOffset, inputData.length - startOffset);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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);

seebees
seebees previously approved these changes May 29, 2025
Copy link
Contributor

@seebees seebees left a comment

Choose a reason for hiding this comment

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

LGTM

@kessplas kessplas merged commit adb0a42 into master May 29, 2025
45 checks passed
@kessplas kessplas deleted the offset-truncation branch May 29, 2025 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants