fix: SamConfig.get_all() mutates shared document, leaking one command's params into another's - #9183
Open
Adityaj0 wants to merge 1 commit into
Open
Conversation
…'s params into another's global_params obtained in get_all() was a live reference into self.document rather than a copy. Calling .update() on it permanently merged the current command's section-specific values into the shared global section, and since self.document is cached across calls (_read() only re-reads when empty), a subsequent get_all() for a different command would silently inherit the previous command's parameter values instead of the true global default. Fixes aws#9181
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Which issue(s) does this change fix?
Fixes #9181
Why is this change necessary?
SamConfig.get_all()merged the current command's section-specific parameters into the[global]section using a live reference intoself.documentrather than a copy:global_paramshere is the actual dict stored insideself.document["<env>"]["global"][section]. Calling.update()on it permanently writes the current command's values into the shared global section. Sinceself.documentis cached across calls (_read()only re-reads from disk whenself.documentis falsy), this corruption persists in memory for the lifetime of theSamConfiginstance.The result: if a
SamConfigobject is reused across multipleget_all()calls for different commands (a valid, documented use of the public API —get_all()takescmd_namesper call for exactly this purpose), a later command silently inherits an earlier command's parameter values instead of the true global default.How does this change work?
Copy the global-section dict before merging into it, instead of mutating the dict stored in
self.document:What tests ran and what were the results?
Added a regression test,
test_get_all_does_not_leak_command_params_into_global_across_calls, intests/unit/lib/samconfig/test_samconfig.py. It fails against the pre-fix code (reproducing the exact leak:build's resolved config wrongly includesdeploy'sstack_name/region) and passes after the fix.Full unit suite (
tests/unit) passes: 9387 passed, 25 skipped.mypyon the changed file: clean.Checklist
make prpasses locally (unit tests + mypy on changed files; full localmake prtarget run viapytest tests/unit -qand scopedmypy)By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.