143 lines
4.6 KiB
Go
143 lines
4.6 KiB
Go
package main
|
|
|
|
// Dependency pinning check (ADR-0093 §10 item 7, WP-13 13e). Pure.
|
|
//
|
|
// Findings (each only ever WARNS in the verdict):
|
|
//
|
|
// floating_range a package.json version range (^, ~, *, x, >, >=, latest)
|
|
// or a requirements.txt line without ==
|
|
// unpinned_from a Dockerfile FROM that is not pinned to an @sha256 digest
|
|
// unpinned_uses a GitHub Actions `uses:` not pinned to a 40-hex commit SHA
|
|
|
|
import (
|
|
"encoding/json"
|
|
"path"
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// PinningFinding is one unpinned reference.
|
|
type PinningFinding struct {
|
|
Kind string `json:"kind"`
|
|
File string `json:"file"`
|
|
Line int `json:"line,omitempty"`
|
|
Ref string `json:"ref"`
|
|
}
|
|
|
|
var (
|
|
fromRe = regexp.MustCompile(`(?i)^\s*FROM\s+(?:--platform=\S+\s+)?(\S+)(?:\s+AS\s+(\S+))?`)
|
|
usesRe = regexp.MustCompile(`^\s*-?\s*uses:\s*["']?([^"'\s#]+)`)
|
|
shaRe = regexp.MustCompile(`@[0-9a-f]{40}$`)
|
|
digestRe = regexp.MustCompile(`@sha256:[0-9a-f]{64}$`)
|
|
npmExactRe = regexp.MustCompile(`^v?\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$`)
|
|
reqPinnedRe = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]*(?:\[[^\]]*\])?\s*===?\s*[^\s,;*]+\s*(?:;.*)?$`)
|
|
)
|
|
|
|
func isDockerfile(base string) bool {
|
|
return base == "Dockerfile" || strings.HasPrefix(base, "Dockerfile.") || strings.HasSuffix(base, ".Dockerfile") || base == "Containerfile"
|
|
}
|
|
|
|
func isWorkflow(p string) bool {
|
|
return strings.HasPrefix(p, ".github/workflows/") && (strings.HasSuffix(p, ".yml") || strings.HasSuffix(p, ".yaml"))
|
|
}
|
|
|
|
// npmRefPinned reports whether a package.json value names one exact version.
|
|
// Non-registry refs (git, file, link, workspace, npm: aliases with exact
|
|
// versions) are not ranges and are left to the lockfile.
|
|
func npmRefPinned(v string) bool {
|
|
v = strings.TrimSpace(v)
|
|
for _, p := range []string{"file:", "link:", "workspace:", "git+", "git:", "github:", "http:", "https:", "portal:", "patch:"} {
|
|
if strings.HasPrefix(v, p) {
|
|
return true
|
|
}
|
|
}
|
|
if strings.HasPrefix(v, "npm:") {
|
|
if i := strings.LastIndex(v, "@"); i > 0 {
|
|
v = v[i+1:]
|
|
}
|
|
}
|
|
return npmExactRe.MatchString(v)
|
|
}
|
|
|
|
// CheckPinning scans the given files (repository-relative path -> content).
|
|
func CheckPinning(files map[string]string) []PinningFinding {
|
|
paths := make([]string, 0, len(files))
|
|
for p := range files {
|
|
if !skippedDir(p) {
|
|
paths = append(paths, p)
|
|
}
|
|
}
|
|
sort.Strings(paths)
|
|
out := []PinningFinding{}
|
|
for _, p := range paths {
|
|
content, base := files[p], path.Base(p)
|
|
switch {
|
|
case isDockerfile(base):
|
|
stages := map[string]bool{}
|
|
for i, line := range strings.Split(content, "\n") {
|
|
m := fromRe.FindStringSubmatch(line)
|
|
if m == nil {
|
|
continue
|
|
}
|
|
ref := m[1]
|
|
// scratch, an EARLIER build stage, or a build-arg image (judged
|
|
// where the arg is set) are not registry pulls. Stages are
|
|
// recorded after the check, so `FROM x AS x` is still a pull.
|
|
local := strings.EqualFold(ref, "scratch") || stages[strings.ToLower(ref)] || strings.Contains(ref, "$")
|
|
if m[2] != "" {
|
|
stages[strings.ToLower(m[2])] = true
|
|
}
|
|
if !local && !digestRe.MatchString(ref) {
|
|
out = append(out, PinningFinding{Kind: "unpinned_from", File: p, Line: i + 1, Ref: ref})
|
|
}
|
|
}
|
|
case isWorkflow(p):
|
|
for i, line := range strings.Split(content, "\n") {
|
|
m := usesRe.FindStringSubmatch(line)
|
|
if m == nil {
|
|
continue
|
|
}
|
|
ref := m[1]
|
|
if strings.HasPrefix(ref, "./") || (strings.HasPrefix(ref, "docker://") && digestRe.MatchString(ref)) {
|
|
continue
|
|
}
|
|
if !shaRe.MatchString(ref) {
|
|
out = append(out, PinningFinding{Kind: "unpinned_uses", File: p, Line: i + 1, Ref: ref})
|
|
}
|
|
}
|
|
case base == "package.json":
|
|
var pkg struct {
|
|
Dependencies map[string]string `json:"dependencies"`
|
|
DevDependencies map[string]string `json:"devDependencies"`
|
|
}
|
|
if json.Unmarshal([]byte(content), &pkg) != nil {
|
|
continue
|
|
}
|
|
for _, deps := range []map[string]string{pkg.Dependencies, pkg.DevDependencies} {
|
|
names := make([]string, 0, len(deps))
|
|
for n := range deps {
|
|
names = append(names, n)
|
|
}
|
|
sort.Strings(names)
|
|
for _, n := range names {
|
|
if !npmRefPinned(deps[n]) {
|
|
out = append(out, PinningFinding{Kind: "floating_range", File: p, Ref: n + "@" + deps[n]})
|
|
}
|
|
}
|
|
}
|
|
case base == "requirements.txt":
|
|
for i, raw := range strings.Split(content, "\n") {
|
|
line := strings.TrimSpace(strings.SplitN(raw, "#", 2)[0])
|
|
if line == "" || strings.HasPrefix(line, "-") {
|
|
continue
|
|
}
|
|
if !reqPinnedRe.MatchString(line) {
|
|
out = append(out, PinningFinding{Kind: "floating_range", File: p, Line: i + 1, Ref: line})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|