fix(toolkit-lib): allowCrossAccountAssetPublishing cache ignores which stack's environment it was computed for - #1884
Open
Adityaj0 wants to merge 1 commit into
Conversation
…h stack's environment it was computed for Deployments.allowCrossAccountAssetPublishingForEnv() cached its answer in a single un-keyed instance field. determineAllowCrossAccountAssetPublishing() reads the target environment's bootstrap stack, so its answer is intrinsically per-account/per-region — but a single Deployments instance is reused for every stack in one `cdk deploy` invocation, which can span multiple accounts/regions. The first stack's asset publish would cache its environment's answer and every other stack's asset publish would silently reuse it, regardless of that stack's own environment. Depending on stack ordering this either blocks a perfectly valid deploy with a spurious cross-account error, or — worse — skips the safety check entirely for a stack whose own environment should have blocked cross-account publishing. This is the same class of bug as aws#1838, fixed for the neighboring publisherCache in aws#1839: key the cache by resolved environment (account:region), same as cachedPublisher already does. Fixes aws#1883 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Adityaj0
requested a deployment
to
integ-approval
August 23, 2026 03:21 — with
GitHub Actions
Waiting
aws-cdk-automation
enabled auto-merge
August 23, 2026 03:21
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1884 +/- ##
==========================================
+ Coverage 91.10% 91.13% +0.03%
==========================================
Files 80 80
Lines 12205 12205
Branches 1742 1744 +2
==========================================
+ Hits 11119 11123 +4
+ Misses 1050 1046 -4
Partials 36 36
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
fixes #1883
Reason for this change
Deployments.allowCrossAccountAssetPublishingForEnv()(packages/@aws-cdk/toolkit-lib/lib/api/deployments/deployments.ts) caches its result in a single un-keyed instance field:determineAllowCrossAccountAssetPublishingreads the target environment's bootstrap stack (CDKToolkit'sBootstrapVersion/BucketName) and its answer is intrinsically per-account/per-region — it returnsfalseonly when that specific environment has a staging bucket and an old (< 21) bootstrap version.Reachability, stated honestly: this is directly reachable via ordinary CLI usage, no programmatic toolkit-lib reuse needed.
deploy()(toolkit.ts) creates oneDeploymentsinstance perdeploymentsForAction('deploy')call and reuses it for every stack instackCollection.stackArtifacts— so a plaincdk deploy StackA StackB(orcdk deploy '*') where StackA and StackB target different accounts/regions with different bootstrap states hits this on the very first multi-stack, multi-environment deploy.publishSingleAssetcallsallowCrossAccountAssetPublishingForEnv(options.stack)once per asset (line 743), so whichever stack's asset publishes first determines the answer applied to every other stack for the rest of the invocation.Two concrete outcomes depending on stack order:
false) publishes first → StackB (properly bootstrapped, should betrue) gets a spurious cross-account-publishing failure on an otherwise valid deploy.true) publishes first → StackB (old bootstrap, should befalse) silently skips the safety checkdetermineAllowCrossAccountAssetPublishingexists to enforce, and an asset can be published to the wrong account without the protection ever running against StackB's actual environment.This is the same class of bug as #1838, fixed for the neighboring
publisherCachefield in #1839 — that fix's own comment explains exactly why environment-keying is required here too;_allowCrossAccountAssetPublishingsits a few lines below it and was simply missed.Description of changes
Replaces the single
boolean | undefinedfield with aMap<string, boolean>keyed by${account}:${region}(same key shapecachedPublisheralready uses), resolved once per call viathis.envs.resolveStackEnvironment(stack)before checking the cache.Description of how you validated changes
Added two tests against
allowCrossAccountAssetPublishingForEnvincloudformation-deployments.test.ts, mirroring the existingcachedPublisherregression tests:'does not reuse the answer across different environments'— two stacks in different accounts/regions,determineAllowCrossAccountAssetPublishingmocked to return a different value per call; asserts each stack gets its own environment's answer and the underlying check runs twice. Fails on the pre-fix code (confirmed locally): the second stack's call returns the first stack's cached answer instead of its own.'reuses the cached answer for repeat calls with the same environment'— confirms the legitimate caching behavior (same environment) is preserved and the underlying check only runs once.Ran the full
test/api/deployments/suite (210 tests) — all pass, no regressions.eslint --fixclean.Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license