Download this file
| 1 | package activator |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | ) |
| 12 | |
| 13 | // verifyStaged checks, immediately before anything execs it, that path is |
| 14 | // an absolute regular file (never a link) whose sha256 is digest (spec §7.5 |
| 15 | // step 3, §4.2). A staged copy is immutable once something refers to it |
| 16 | // (plan 4 refuses to restage over one), so this is the check that the bytes |
| 17 | // the attempt chose are the bytes on disk. An artifact that cannot be |
| 18 | // compared never runs: an empty or malformed digest refuses. |
| 19 | func verifyStaged(path, digest string) error { |
| 20 | if !filepath.IsAbs(path) { |
| 21 | return fmt.Errorf("staged path %q is not absolute", path) |
| 22 | } |
| 23 | if len(digest) != 2*sha256.Size { |
| 24 | return fmt.Errorf("staged digest %q is not a sha256", digest) |
| 25 | } |
| 26 | fi, err := os.Lstat(path) |
| 27 | if err != nil { |
| 28 | return err |
| 29 | } |
| 30 | if !fi.Mode().IsRegular() { |
| 31 | return fmt.Errorf("staged path %q is not a regular file", path) |
| 32 | } |
| 33 | f, err := os.Open(path) |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | defer f.Close() |
| 38 | h := sha256.New() |
| 39 | if _, err := io.Copy(h, f); err != nil { |
| 40 | return err |
| 41 | } |
| 42 | if got := hex.EncodeToString(h.Sum(nil)); got != strings.ToLower(digest) { |
| 43 | return fmt.Errorf("staged digest mismatch: have %s, want %s", got, strings.ToLower(digest)) |
| 44 | } |
| 45 | return nil |
| 46 | } |
| 47 | |