fix: do not signal onComplete when the incoming buffer length is less than the cipher block #209
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.
Issue #, if available: #185
Description of changes: The current implementation of the
CipherSubscriber
incorrectly assumes that the incomingByteBuffer
will only contain fewer bytes than the block cipher when it is the final block. In fact, the incomingByteBuffer
could have fewer bytes in cases where e.g. the networking conditions are poor. As a result, theCipherSubscriber
may erroneously callonComplete()
partway through the operation, which then fails due to contentLength mismatch. Additionally, as pointed out in #185, theCipherSubscriber
may instead throw a NPE whenamountToReadFromByteBuffer
is greater than the block size.This PR fixes both bugs by instead checking if the
contentRead
is equal tocontentLength
before signalingonComplete
. If the data has not been fully read, then theCipherSubscriber
passes an empty array to avoid blocking such that subsequentonNext()
invocations will fill up the cipher's underlying buffer until it has enough bytes to process a full block.Also, adds a test which was failing before this change. It requires specifying BouncyCastle as the JCE Provider as other providers (SunJCE, ACCP) return empty buffers instead of
null
.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
Check any applicable: