Files
pmg/proxy/interceptors/go_cooldown.go
T
b19473945b Add experimental Go module proxy support (#358)
* 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>
2026-07-03 18:19:31 +05:30

190 lines
6.6 KiB
Go

package interceptors
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"sync"
"time"
packagev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/package/v1"
"github.com/safedep/dry/log"
pmgconfig "github.com/safedep/pmg/config"
"github.com/safedep/pmg/internal/audit"
"github.com/safedep/pmg/proxy"
gomodule "golang.org/x/mod/module"
)
// goCooldownHandler enforces dependency cooldown for Go modules. Unlike npm,
// there is no metadata to strip: the version go requests is already resolved
// by the time the proxy sees it. Instead the publish timestamp is captured
// from the .info response go fetches before each .zip, and an in-window .zip
// download is blocked with HTTP 403 — terminal, because the child GOPROXY is
// normalized to a comma-joined list with no direct fallback.
type goCooldownHandler struct {
statsCollector *AnalysisStatsCollector
mu sync.Mutex
publishTimes map[string]time.Time
}
func newGoCooldownHandler(statsCollector *AnalysisStatsCollector) *goCooldownHandler {
return &goCooldownHandler{
statsCollector: statsCollector,
publishTimes: map[string]time.Time{},
}
}
func goModuleVersionKey(module, version string) string {
return module + "@" + version
}
// HandleInfoRequest reads the .info response body without altering it and
// caches the version's publish time for the upcoming .zip request.
func (h *goCooldownHandler) HandleInfoRequest(ctx *proxy.RequestContext, module, version string) (*proxy.InterceptorResponse, error) {
// Force an uncompressed, non-conditional response so the body is parseable
// JSON rather than raw gzip bytes or an empty 304 (same as the npm
// metadata modifier).
ctx.Headers.Set("Accept-Encoding", "identity")
ctx.Headers.Del("If-None-Match")
ctx.Headers.Del("If-Modified-Since")
modifier := func(statusCode int, headers http.Header, body []byte) (int, http.Header, []byte, error) {
if statusCode != http.StatusOK {
return statusCode, headers, body, nil
}
var info struct {
Time time.Time `json:"Time"`
}
if err := json.Unmarshal(body, &info); err != nil || info.Time.IsZero() {
log.Warnf("[%s] Cooldown: failed to parse publish time from .info for %s@%s", ctx.RequestID, module, version)
return statusCode, headers, body, nil
}
h.mu.Lock()
h.publishTimes[goModuleVersionKey(module, version)] = info.Time
h.mu.Unlock()
return statusCode, headers, body, nil
}
return &proxy.InterceptorResponse{
Action: proxy.ActionModifyResponse,
ResponseModifier: modifier,
}, nil
}
// CheckZipDownload blocks the module zip when its publish time is within the
// cooldown window. handled=false lets the request continue to malware
// analysis. When the publish time was not observed on the wire (go served
// .info from its local module cache, common on machines that used go before
// PMG), it is fetched out-of-band from the upstream proxy; only if that also
// fails does cooldown fail open — malware analysis still runs.
func (h *goCooldownHandler) CheckZipDownload(ctx *proxy.RequestContext, baseURL, module, version string, cooldownDays int) (*proxy.InterceptorResponse, bool) {
skip := pmgconfig.CooldownSkip(packagev1.Ecosystem_ECOSYSTEM_GO, module)
if skip.SkipAll || pmgconfig.IsTrustedPackageRef(packagev1.Ecosystem_ECOSYSTEM_GO, module, version) {
return nil, false
}
h.mu.Lock()
publishTime, ok := h.publishTimes[goModuleVersionKey(module, version)]
h.mu.Unlock()
if !ok {
publishTime, ok = h.fetchPublishTime(ctx, baseURL, module, version)
}
if !ok {
log.Warnf("[%s] Cooldown: no publish time available for %s@%s; cooldown not enforced for this download", ctx.RequestID, module, version)
return nil, false
}
within, daysAgo, daysLeft := cooldownIsWithinWindow(publishTime, cooldownDays)
if !within {
return nil, false
}
if skip.ExemptsVersion(version) {
auditCooldownSkips(ctx.RequestID, packagev1.Ecosystem_ECOSYSTEM_GO, module, cooldownExemptions{skipListed: []string{version}})
return nil, false
}
log.Infof("[%s] Cooldown: blocking %s@%s published %d day(s) ago (%d day cooldown, %d remaining)",
ctx.RequestID, module, version, daysAgo, cooldownDays, daysLeft)
if h.statsCollector != nil {
h.statsCollector.RecordCooldownBlocked(module, version, publishTime, daysAgo, daysLeft, cooldownDays)
}
pv := &packagev1.PackageVersion{}
pv.SetPackage(&packagev1.Package{})
pv.GetPackage().SetName(module)
pv.GetPackage().SetEcosystem(packagev1.Ecosystem_ECOSYSTEM_GO)
pv.SetVersion(version)
audit.LogDependencyCooldown(pv, publishTime, cooldownDays, daysAgo, daysLeft)
message := fmt.Sprintf("Package blocked by dependency cooldown: GO/%s@%s\n\nPublished %d day(s) ago; cooldown window is %d day(s) (%d remaining).",
module, version, daysAgo, cooldownDays, daysLeft)
return &proxy.InterceptorResponse{
Action: proxy.ActionBlock,
BlockCode: http.StatusForbidden,
BlockMessage: message,
}, true
}
// goInfoFetchClient fetches .info out-of-band, straight to the upstream proxy
// rather than back through PMG's own in-process proxy (which would
// re-intercept the request). It honors the process' own proxy environment,
// not the child's injected one.
var goInfoFetchClient = &http.Client{Timeout: 10 * time.Second}
// fetchPublishTime performs a one-shot authoritative $base/$module/@v/$version.info
// fetch and caches the result. Best-effort: any failure means no publish time.
func (h *goCooldownHandler) fetchPublishTime(ctx *proxy.RequestContext, baseURL, module, version string) (time.Time, bool) {
if baseURL == "" {
return time.Time{}, false
}
escapedPath, err := gomodule.EscapePath(module)
if err != nil {
return time.Time{}, false
}
escapedVersion, err := gomodule.EscapeVersion(version)
if err != nil {
return time.Time{}, false
}
infoURL := fmt.Sprintf("%s/%s/@v/%s.info", strings.TrimSuffix(baseURL, "/"), escapedPath, escapedVersion)
resp, err := goInfoFetchClient.Get(infoURL)
if err != nil {
log.Warnf("[%s] Cooldown: failed to fetch %s: %v", ctx.RequestID, infoURL, err)
return time.Time{}, false
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
log.Warnf("[%s] Cooldown: fetching %s returned HTTP %d", ctx.RequestID, infoURL, resp.StatusCode)
return time.Time{}, false
}
var info struct {
Time time.Time `json:"Time"`
}
if err := json.NewDecoder(io.LimitReader(resp.Body, 1<<20)).Decode(&info); err != nil || info.Time.IsZero() {
log.Warnf("[%s] Cooldown: failed to parse publish time from %s", ctx.RequestID, infoURL)
return time.Time{}, false
}
h.mu.Lock()
h.publishTimes[goModuleVersionKey(module, version)] = info.Time
h.mu.Unlock()
return info.Time, true
}