Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions pkg/archive/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"archive/zip"
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"io"
"slices"
Expand Down Expand Up @@ -216,14 +217,22 @@ func StabilizeZip(zr *zip.Reader, zw *zip.Writer, opts StabilizeOpts) error {
}
mr := NewMutableReader(zr)
for _, s := range opts.Stabilizers {
var currentStabName string
originalArchiveHash := getArchiveHash(&mr)
switch s.(type) {
case ZipArchiveStabilizer:
currentStabName = s.(ZipArchiveStabilizer).Name
s.(ZipArchiveStabilizer).Stabilize(&mr)
case ZipEntryStabilizer:
currentStabName = s.(ZipEntryStabilizer).Name
for _, mf := range mr.File {
s.(ZipEntryStabilizer).Stabilize(mf)
}
}
newArchiveHash := getArchiveHash(&mr)
if originalArchiveHash != newArchiveHash {
println("Stabilizer taking effect:", currentStabName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than printing these types, what do you think of collecting them and returning somehow? Eventually we can include the stabilizers used during comparison as part of the attestation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, collecting them from stderr is not great. Do you have any suggestion?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they could be appended to a slice and that slice could be returned by this function

}
}
return mr.WriteTo(zw)
}
Expand Down Expand Up @@ -252,3 +261,27 @@ func toZipCompatibleReader(r io.Reader) (io.ReaderAt, int64, error) {
}
return bytes.NewReader(b), int64(len(b)), nil
}

// Helper to compute SHA256 and encode as base64 (url encoding, no padding)
func computeSHA256Base64(data []byte) string {
h := sha256.New()
h.Write(data)
sum := h.Sum(nil)
return base64.RawURLEncoding.EncodeToString(sum)
}

func getArchiveHash(zr *MutableZipReader) string {
var buf bytes.Buffer
for _, zf := range zr.File {
content, err := zf.Open()
if err != nil {
continue
}
data, err := io.ReadAll(content)
if err != nil {
continue
}
buf.Write(data)
}
return computeSHA256Base64(buf.Bytes())
}