Skip to content

fix: reject an encryption context that already contains the signer key - #840

Open
MaxonT wants to merge 1 commit into
aws:masterfrom
MaxonT:contrib/135-auto
Open

MaxonT wants to merge 1 commit into
aws:masterfrom
MaxonT:contrib/135-auto

Conversation

@MaxonT

@MaxonT MaxonT commented Nov 25, 2026

Copy link
Copy Markdown

The issue links the Java version of this check, so I followed that behaviour and kept the Python change small, though I haven't tried it against every caller.

Issue #, if available: #135

Description of changes:

With a signed algorithm suite, DefaultCryptoMaterialsManager stores the message verification key in the encryption context under aws-crypto-public-key. If the caller's context already had that key, it was overwritten without any error. This change raises ActionNotAllowedError in that case, as the issue asks and as the linked Java DefaultCryptoMaterialsManager does. Unsigned suites are not affected: the context is not modified for them, so nothing is rejected.

The check is in _generate_signing_key_and_update_encryption_context, before the signer is created, and the caller's dictionary is left unchanged when it raises. I picked ActionNotAllowedError because the module already uses it for an encrypt request that is not permitted. The issue does not name an exception type, so I am happy to switch to another one if you prefer.

Tests: two cases in test/unit/test_material_managers_default.py (reserved key with a signed suite raises and does not create a signer or change the context; reserved key with an unsigned suite is left alone). The signed-suite test fails when the source change is reverted. pytest test/unit in a fresh Python 3.14 venv: 4231 passed, 20 skipped. I also ran a real EncryptionSDKClient.encrypt with a raw AES key provider: a normal context works, and passing back the context returned by a previous encrypt (which contains the key) now raises.

That last case is the compatibility point to weigh: a caller that re-encrypts with a context taken from an earlier message header and a signed suite used to succeed, and will now get the error. I did not check how widely that pattern is used. I did not run the integration, accept or MPL test suites, and black --check reports a docstring blank-line difference in default.py that is already on master and is not part of this change.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Refs #135

Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com>
@MaxonT
MaxonT requested a review from a team as a code owner November 25, 2026 04:52

This branch has not been deployed

No deployments
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.

1 participant