fix: SamConfig.get_all() mutates shared document, leaking one command's params into another's - #9183
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
|
Thanks for the clear write-up, @Adityaj0, and apologies for the long silence on this one. I checked the branch out locally and worked through the claim; it holds up. Notes below, including two gaps I think are worth a follow-up. The bug is realConfirmed both halves of the mechanism:
Your regression test is a genuine one — I reverted just With the fix restored it passes, and I reproduced the same clean/dirty behaviour outside the test harness against a real Test resultsWorth calling out: no existing test had to be changed (+35/-0 on the test file), so nothing in the suite had baked in the buggy behaviour. Good sign for the fix being a pure correctness win. Severity: latent today, but still worth fixingI want to be straight about blast radius rather than let this look scarier than it is. In the shipped CLI the corruption isn't currently observable:
So this is an API-contract bug rather than a live customer-facing one. That's still the right thing to fix: Two gaps I'd like closed (with the fix applied)Both verified on your branch, so these are additions rather than objections: 1. The no- 2. Real config keys with table/inline-table values ( Sibling methodsI audited the rest of the class for the same aliasing shape. One adjacent observation, clearly out of scope for this PR: On the BLOCKED statusTo save the next person the dig — this is not a rebase problem.
Neither needs anything from you. A maintainer needs to authorize the workflow run and then get a second reviewer on it. VerdictThe diagnosis is correct, the fix is minimal and right, the regression test genuinely pins the behaviour, and the local suites are green. I'm supportive of merging this once CI is authorized and it picks up its second approval. The deepcopy hardening above is the only thing I'd like to see land eventually, here or separately. (Leaving this as a comment rather than a formal review approval — I'm one reviewer's opinion, and the second approval still needs to come from someone else.) |
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.