Skip to content

Commit 8f047fb

Browse files
authored
Merge pull request #105 from mattsb42-aws/oops
fix logging issue
2 parents 1875c91 + cf45b43 commit 8f047fb

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed

CHANGELOG.rst

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22
Changelog
33
*********
44

5-
1.3.8 -- 2018-xx-xx
5+
1.3.8 -- 2018-11-15
66
===================
77

8+
Bugfixes
9+
--------
10+
11+
* Remove debug logging that may contain input data when encrypting non-default unframed messages.
12+
`#105 <https://github.com/aws/aws-encryption-sdk-python/pull/105>`_
13+
814
Minor
915
-----
1016

src/aws_encryption_sdk/identifiers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from aws_encryption_sdk.exceptions import InvalidAlgorithmError
2323

24-
__version__ = "1.3.7"
24+
__version__ = "1.3.8"
2525
USER_AGENT_SUFFIX = "AwsEncryptionSdkPython/{}".format(__version__)
2626

2727

src/aws_encryption_sdk/streaming_client.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -502,18 +502,19 @@ def _read_bytes_to_non_framed_body(self, b):
502502
:returns: Encrypted bytes from source stream
503503
:rtype: bytes
504504
"""
505-
_LOGGER.debug("Reading %s bytes", b)
505+
_LOGGER.debug("Reading %d bytes", b)
506506
plaintext = self.source_stream.read(b)
507+
plaintext_length = len(plaintext)
507508
if self.tell() + len(plaintext) > MAX_NON_FRAMED_SIZE:
508509
raise SerializationError("Source too large for non-framed message")
509510

510511
ciphertext = self.encryptor.update(plaintext)
511-
self._bytes_encrypted += len(plaintext)
512+
self._bytes_encrypted += plaintext_length
512513
if self.signer is not None:
513514
self.signer.update(ciphertext)
514515

515516
if len(plaintext) < b:
516-
_LOGGER.debug("Closing encryptor after receiving only %s bytes of %s bytes requested", plaintext, b)
517+
_LOGGER.debug("Closing encryptor after receiving only %d bytes of %d bytes requested", plaintext_length, b)
517518
self.source_stream.close()
518519
closing = self.encryptor.finalize()
519520

@@ -620,8 +621,8 @@ def _read_bytes(self, b):
620621
# must not exceed that value.
621622
if self._bytes_encrypted > self.config.source_length:
622623
raise CustomMaximumValueExceeded(
623-
"Bytes encrypted has exceeded stated source length estimate:\n{actual} > {estimated}".format(
624-
actual=self._bytes_encrypted, estimated=self.config.source
624+
"Bytes encrypted has exceeded stated source length estimate:\n{actual:d} > {estimated:d}".format(
625+
actual=self._bytes_encrypted, estimated=self.config.source_length
625626
)
626627
)
627628

test/functional/test_f_aws_encryption_sdk_client.py

+59-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
# ANY KIND, either express or implied. See the License for the specific
1212
# language governing permissions and limitations under the License.
1313
"""Functional test suite for aws_encryption_sdk.kms_thick_client"""
14+
from __future__ import division
15+
1416
import io
17+
import logging
1518

1619
import attr
1720
import botocore.client
@@ -28,6 +31,7 @@
2831
from aws_encryption_sdk.exceptions import CustomMaximumValueExceeded
2932
from aws_encryption_sdk.identifiers import Algorithm, EncryptionKeyType, WrappingAlgorithm
3033
from aws_encryption_sdk.internal.crypto.wrapping_keys import WrappingKey
34+
from aws_encryption_sdk.internal.defaults import LINE_LENGTH
3135
from aws_encryption_sdk.internal.formatting.encryption_context import serialize_encryption_context
3236
from aws_encryption_sdk.key_providers.base import MasterKeyProviderConfig
3337
from aws_encryption_sdk.key_providers.raw import RawMasterKeyProvider
@@ -498,12 +502,14 @@ def test_encryption_cycle_with_caching():
498502
def test_encrypt_source_length_enforcement():
499503
key_provider = fake_kms_key_provider()
500504
cmm = aws_encryption_sdk.DefaultCryptoMaterialsManager(key_provider)
505+
plaintext = io.BytesIO(VALUES["plaintext_128"])
501506
with pytest.raises(CustomMaximumValueExceeded) as excinfo:
502507
aws_encryption_sdk.encrypt(
503-
source=VALUES["plaintext_128"], materials_manager=cmm, source_length=int(len(VALUES["plaintext_128"]) / 2)
508+
source=plaintext, materials_manager=cmm, source_length=int(len(VALUES["plaintext_128"]) / 2)
504509
)
505510

506511
excinfo.match(r"Bytes encrypted has exceeded stated source length estimate:*")
512+
assert repr(plaintext) not in excinfo.exconly()
507513

508514

509515
def test_encrypt_source_length_enforcement_legacy_support():
@@ -669,3 +675,55 @@ def test_incomplete_read_stream_cycle(frame_length):
669675
)
670676

671677
assert ciphertext != decrypted == plaintext
678+
679+
680+
def _prep_plaintext_and_logs(log_catcher, plaintext_length):
681+
log_catcher.set_level(logging.DEBUG)
682+
key_provider = fake_kms_key_provider()
683+
plaintext = exact_length_plaintext(plaintext_length)
684+
return plaintext, key_provider
685+
686+
687+
def _look_in_logs(log_catcher, plaintext):
688+
logs = log_catcher.text
689+
# look for every possible 32-byte chunk
690+
start = 0
691+
end = 32
692+
plaintext_length = len(plaintext)
693+
while end <= plaintext_length:
694+
chunk_repr = repr(plaintext[start:end])
695+
repr_body = chunk_repr[2:-1]
696+
assert repr_body not in logs
697+
start += 1
698+
end += 1
699+
700+
701+
@pytest.mark.parametrize("frame_size", (0, LINE_LENGTH // 2, LINE_LENGTH, LINE_LENGTH * 2))
702+
@pytest.mark.parametrize(
703+
"plaintext_length", (1, LINE_LENGTH // 2, LINE_LENGTH, int(LINE_LENGTH * 1.5), LINE_LENGTH * 2)
704+
)
705+
def test_plaintext_logs_oneshot(caplog, plaintext_length, frame_size):
706+
plaintext, key_provider = _prep_plaintext_and_logs(caplog, plaintext_length)
707+
708+
_ciphertext, _header = aws_encryption_sdk.encrypt(
709+
source=plaintext, key_provider=key_provider, frame_length=frame_size
710+
)
711+
712+
_look_in_logs(caplog, plaintext)
713+
714+
715+
@pytest.mark.parametrize("frame_size", (0, LINE_LENGTH // 2, LINE_LENGTH, LINE_LENGTH * 2))
716+
@pytest.mark.parametrize(
717+
"plaintext_length", (1, LINE_LENGTH // 2, LINE_LENGTH, int(LINE_LENGTH * 1.5), LINE_LENGTH * 2)
718+
)
719+
def test_plaintext_logs_stream(caplog, plaintext_length, frame_size):
720+
plaintext, key_provider = _prep_plaintext_and_logs(caplog, plaintext_length)
721+
722+
ciphertext = b""
723+
with aws_encryption_sdk.stream(
724+
mode="encrypt", source=plaintext, key_provider=key_provider, frame_length=frame_size
725+
) as encryptor:
726+
for line in encryptor:
727+
ciphertext += line
728+
729+
_look_in_logs(caplog, plaintext)

0 commit comments

Comments
 (0)