mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
* feat: add experimental Go module support via pmg go Adds Go modules as a proxy-guarded ecosystem, opt-in only: the command runs solely when invoked explicitly as `pmg go ...` and is deliberately excluded from setup aliases and PATH shims so existing users are unaffected. - packagemanager: goPackageManager with fail-safe command classification (vet/fix excluded from non-download since they can fetch on a cold cache) and pinned-version extraction where only canonical semver counts as explicit. - GOPROXY normalization (fail-closed): effective GOPROXY read via `go env` (honors go env -w), rebuilt comma-joined with `direct` dropped so a 403 block is terminal and nothing silently falls back to unanalyzed VCS fetches. GOPRIVATE/GONOPROXY surface a warning; GOINSECURE is cleared. Contributed to the proxy flow through a new ProxyRoutingProvider hook (extra child env + dynamic MITM hosts). - Go interceptor with dynamic host matching from the user's effective GOPROXY via InterceptorContext.GoProxyHosts. Malware analysis runs on .zip only (the sole endpoint that delivers code); .info/.mod/@latest/ list pass through; /sumdb/ traffic and sum.golang.org are never touched so checksum-db verification stays intact; golang.org/toolchain is allowed on Go's own checksum verification. - Dependency cooldown: publish time captured from .info responses (body unmodified), in-window .zip blocked with 403; fails open for cooldown only when the publish time was never observed. - Cert gate: on macOS/Windows `pmg go` fails fast with actionable guidance unless the persisted PMG CA is OS-trusted (Go ignores SSL_CERT_FILE there); Linux works via the injected bundle. - proxye2e: GOPROXY-protocol mock registry, Go driver and 10 hermetic cases (allow/block/confirm, case-escaped paths, cooldown block and fail-open, toolchain, sumdb passthrough). Verified end-to-end on Linux: `pmg go get github.com/google/uuid@v1.6.0` MITMs proxy.golang.org, analyzes the decoded module at the .zip fetch, and go.sum verification succeeds through the tunneled checksum db. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014xuhBeTVpfU4SdqVaarvuK * fix(go): address review findings on experimental Go support - Drop fmt/clean from NonDownloadCommands: both load packages via go list and can download modules on a cold cache, which would bypass the proxy under install_only. - Support GOPROXY entries with a base path (e.g. corp Athens/JFrog at https://corp/goproxy): the interceptor now receives host -> base URL and strips the path prefix before parsing module URLs, so verdicts and cooldown key on the real module path. - Default unschemed GOPROXY entries to https, matching go's own behavior, so corp mirrors configured as bare hosts are intercepted instead of silently unanalyzed. - Memoize the final verdict per module zip: go re-requests a failed zip during go get's load phase, which double-recorded stats (the report showed the same blocked module twice) and would have re-prompted on Confirm verdicts. - Fetch .info out-of-band on a cooldown cache miss: go serves .info from its local module cache on any machine that used go before PMG, which silently disabled cooldown. Failure of the side-fetch still fails open for cooldown only. - Move the noop package resolver into packagemanager. Verified live: cold-cache cooldown block now records once; warm-cache rerun is blocked via the side-fetch instead of failing open. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014xuhBeTVpfU4SdqVaarvuK * docs: collapse Go proxy-mode details by default Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014xuhBeTVpfU4SdqVaarvuK --------- Co-authored-by: Claude <noreply@anthropic.com>
123 lines
3.8 KiB
Go
123 lines
3.8 KiB
Go
package interceptors
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"golang.org/x/mod/module"
|
|
)
|
|
|
|
const (
|
|
goRequestLatest = "latest"
|
|
goRequestList = "list"
|
|
goRequestInfo = "info"
|
|
goRequestMod = "mod"
|
|
goRequestZip = "zip"
|
|
|
|
// goRequestSumDB is checksum-database traffic proxied through the module
|
|
// proxy ($GOPROXY/sumdb/...). It is passed through unmodified so go's
|
|
// signature verification stays intact.
|
|
goRequestSumDB = "sumdb"
|
|
)
|
|
|
|
// goModuleInfo is parsed module information from a Go module proxy URL.
|
|
type goModuleInfo struct {
|
|
name string
|
|
version string
|
|
requestType string
|
|
}
|
|
|
|
var _ packageInfo = (*goModuleInfo)(nil)
|
|
|
|
func (g *goModuleInfo) GetName() string { return g.name }
|
|
|
|
func (g *goModuleInfo) GetVersion() string { return g.version }
|
|
|
|
// IsFileDownload is true only for .zip: the single endpoint that downloads
|
|
// module source. .info/.mod are fetched for the entire candidate graph during
|
|
// version selection, including modules never selected into the build, so
|
|
// analyzing them would block builds over code that never lands in the cache.
|
|
func (g *goModuleInfo) IsFileDownload() bool { return g.requestType == goRequestZip }
|
|
|
|
// goProxyParser parses Go module proxy URLs per the GOPROXY protocol
|
|
// (https://go.dev/ref/mod#goproxy-protocol):
|
|
//
|
|
// /<module>/@latest -> latest version metadata
|
|
// /<module>/@v/list -> version list
|
|
// /<module>/@v/<version>.info -> version metadata JSON (publish time)
|
|
// /<module>/@v/<version>.mod -> go.mod file
|
|
// /<module>/@v/<version>.zip -> module source archive
|
|
// /sumdb/<name>/... -> proxied checksum-database traffic
|
|
//
|
|
// Uppercase letters in module path and version arrive escaped as '!'+lowercase
|
|
// and are decoded before use as Malysis query keys.
|
|
//
|
|
// basePath is the path prefix of the GOPROXY entry (e.g. "/goproxy" for
|
|
// GOPROXY=https://corp.example.com/goproxy): go sends requests under that
|
|
// base, so it is stripped before the module path is parsed.
|
|
type goProxyParser struct {
|
|
basePath string
|
|
}
|
|
|
|
var _ registryURLParser = goProxyParser{}
|
|
|
|
func (g goProxyParser) ParseURL(urlPath string) (packageInfo, error) {
|
|
if g.basePath != "" {
|
|
rest, ok := strings.CutPrefix(urlPath, g.basePath)
|
|
if !ok || (rest != "" && rest[0] != '/') {
|
|
return nil, fmt.Errorf("go proxy URL %q is outside proxy base path %q", urlPath, g.basePath)
|
|
}
|
|
urlPath = rest
|
|
}
|
|
|
|
p := strings.TrimPrefix(urlPath, "/")
|
|
if p == "" {
|
|
return nil, fmt.Errorf("empty go proxy URL path")
|
|
}
|
|
|
|
if p == "sumdb" || strings.HasPrefix(p, "sumdb/") {
|
|
return &goModuleInfo{requestType: goRequestSumDB}, nil
|
|
}
|
|
|
|
if escaped, ok := strings.CutSuffix(p, "/@latest"); ok {
|
|
name, err := module.UnescapePath(escaped)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid module path in go proxy URL: %w", err)
|
|
}
|
|
return &goModuleInfo{name: name, requestType: goRequestLatest}, nil
|
|
}
|
|
|
|
escapedPath, versionPart, ok := strings.Cut(p, "/@v/")
|
|
if !ok {
|
|
return nil, fmt.Errorf("go proxy URL missing /@v/ or /@latest marker")
|
|
}
|
|
|
|
name, err := module.UnescapePath(escapedPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid module path in go proxy URL: %w", err)
|
|
}
|
|
|
|
if versionPart == "list" {
|
|
return &goModuleInfo{name: name, requestType: goRequestList}, nil
|
|
}
|
|
|
|
dot := strings.LastIndex(versionPart, ".")
|
|
if dot <= 0 {
|
|
return nil, fmt.Errorf("go proxy URL has no version suffix: %q", versionPart)
|
|
}
|
|
|
|
requestType := versionPart[dot+1:]
|
|
switch requestType {
|
|
case goRequestInfo, goRequestMod, goRequestZip:
|
|
default:
|
|
return nil, fmt.Errorf("unrecognized go proxy version suffix: %q", requestType)
|
|
}
|
|
|
|
version, err := module.UnescapeVersion(versionPart[:dot])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid version in go proxy URL: %w", err)
|
|
}
|
|
|
|
return &goModuleInfo{name: name, version: version, requestType: requestType}, nil
|
|
}
|