Skip to content

Commit

Permalink
Don't allow update_into to mutate immutable objects (pyca#8230)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex authored and reaperhulk committed Feb 7, 2023
1 parent 338a65a commit b35ad89
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cryptography/hazmat/backends/openssl/ciphers.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def update_into(self, data: bytes, buf: bytes) -> int:
data_processed = 0
total_out = 0
outlen = self._backend._ffi.new("int *")
baseoutbuf = self._backend._ffi.from_buffer(buf)
baseoutbuf = self._backend._ffi.from_buffer(buf, require_writable=True)
baseinbuf = self._backend._ffi.from_buffer(data)

while data_processed != total_data_len:
Expand Down
8 changes: 8 additions & 0 deletions tests/hazmat/primitives/test_ciphers.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,14 @@ def test_update_into_buffer_too_small(self, backend):
with pytest.raises(ValueError):
encryptor.update_into(b"testing", buf)

def test_update_into_immutable(self, backend):
key = b"\x00" * 16
c = ciphers.Cipher(AES(key), modes.ECB(), backend)
encryptor = c.encryptor()
buf = b"\x00" * 32
with pytest.raises((TypeError, BufferError)):
encryptor.update_into(b"testing", buf)

@pytest.mark.supported(
only_if=lambda backend: backend.cipher_supported(
AES(b"\x00" * 16), modes.GCM(b"\x00" * 12)
Expand Down

0 comments on commit b35ad89

Please # to comment.