Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mutate constructs immutable. #1124

Merged
merged 1 commit into from
Sep 18, 2021

Conversation

mattmoor
Copy link
Collaborator

I noticed that cosign was taking advantage of a bug in the mutate package
where the Manifest() returns a mutable copy of it's internal state, which
allowed them to permanently alter the result of Manifest(). This mutability
violates the philosophy of our abstractions, and should not be allowed, so
this change ensures that each of the methods returning state in the mutate
package return a .DeepCopy() instead. In image this included the
configFile, and manifest. In index this included the indexManifest.
I have added tests that when the returned copies are mutated, a subsequent
invocation of the getter will return a pristine copy.

Since I'm not completely heartless, I am also adding a new
mutate.ConfigMediaType method to enable cosign to perform the alteration
they want, now that the trick they were using will no longer work.

Related: sigstore/cosign#718

@mattmoor
Copy link
Collaborator Author

Weird, this is a legit failure that repros for me locally, but not against main, so we may have our own reliance on this bug.

@mattmoor
Copy link
Collaborator Author

Ah, we do, but it's a test bug 🤣

// Blank out the layer history.
cfg.History = nil

I noticed that `cosign` was taking advantage of a bug in the mutate package
where the `Manifest()` returns a mutable copy of it's internal state, which
allowed them to permanently alter the result of `Manifest()`.  This mutability
violates the philosophy of our abstractions, and should not be allowed, so
this change ensures that each of the methods returning state in the `mutate`
package return a `.DeepCopy()` instead.  In `image` this included the
`configFile`, and `manifest`.  In `index` this included the `indexManifest`.
I have added tests that when the returned copies are mutated, a subsequent
invocation of the getter will return a pristine copy.

Since I'm not completely heartless, I am also adding a new
`mutate.ConfigMediaType` method to enable `cosign` to perform the alteration
they want, now that the trick they were using will no longer work.

Related: sigstore/cosign#718
@codecov-commenter
Copy link

codecov-commenter commented Sep 18, 2021

Codecov Report

Merging #1124 (43e061d) into main (8388fde) will decrease coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1124      +/-   ##
==========================================
- Coverage   75.16%   75.15%   -0.02%     
==========================================
  Files         108      108              
  Lines        7724     7732       +8     
==========================================
+ Hits         5806     5811       +5     
- Misses       1363     1364       +1     
- Partials      555      557       +2     
Impacted Files Coverage Δ
pkg/v1/mutate/image.go 69.72% <100.00%> (+0.49%) ⬆️
pkg/v1/mutate/index.go 79.83% <100.00%> (ø)
pkg/v1/mutate/mutate.go 71.94% <100.00%> (+0.51%) ⬆️
pkg/legacy/tarball/write.go 67.18% <0.00%> (-1.16%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8388fde...43e061d. Read the comment docs.

@dlorenc
Copy link
Contributor

dlorenc commented Sep 18, 2021

Since I'm not completely heartless, I am also adding a new

All hail not heartless Matt

@imjasonh
Copy link
Collaborator

Thanks for fixing this!

@mattmoor mattmoor merged commit 0e8b581 into google:main Sep 18, 2021
@mattmoor mattmoor deleted the immutable-fields-in-mutate branch September 18, 2021 22:33
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.

None yet

4 participants