Files
pmg/test/proxye2e/analyzer.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

184 lines
5.8 KiB
Go

package proxye2e
import (
"context"
"fmt"
"strings"
"sync"
"buf.build/gen/go/safedep/api/grpc/go/safedep/services/malysis/v1/malysisv1grpc"
malysisv1pb "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/malysis/v1"
packagev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/package/v1"
malysisv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/malysis/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Verdict is a programmable malysis response for a package version. It is the
// raw upstream signal: the real analyzer's verdict-mapping (suspicious→confirm,
// paranoid upgrade, verified→block, exclusion→allow) runs on top of it.
type Verdict struct {
resp *malysisv1.QueryPackageAnalysisResponse
err error
}
// Clean reports no malware (allow).
func Clean() Verdict {
return Verdict{resp: &malysisv1.QueryPackageAnalysisResponse{
Report: &malysisv1pb.Report{Inference: &malysisv1pb.Report_Inference{IsMalware: false, Summary: "no indicators"}},
VerificationRecord: &malysisv1pb.VerificationRecord{IsMalware: false},
}}
}
// Suspicious reports inference-only malware (unverified). Maps to confirm, or to
// block under paranoid mode.
func Suspicious() Verdict {
return Verdict{resp: &malysisv1.QueryPackageAnalysisResponse{
Report: &malysisv1pb.Report{Inference: &malysisv1pb.Report_Inference{IsMalware: true, Summary: "suspicious patterns detected"}},
VerificationRecord: &malysisv1pb.VerificationRecord{IsMalware: false},
}}
}
// VerifiedMalware reports verified malware (always block).
func VerifiedMalware() Verdict {
return Verdict{resp: &malysisv1.QueryPackageAnalysisResponse{
Report: &malysisv1pb.Report{Inference: &malysisv1pb.Report_Inference{IsMalware: true, Summary: "verified malware"}},
VerificationRecord: &malysisv1pb.VerificationRecord{IsMalware: true},
}}
}
// Excluded reports verified malware carrying a tenant exclusion. With an
// exclusion-honoring analyzer it downgrades to allow.
func Excluded(reason string) Verdict {
v := VerifiedMalware()
v.resp.MaliciousPackageExclusion = &malysisv1.QueryPackageAnalysisResponse_MaliciousPackageExclusion{
ExclusionId: "e2e-exclusion",
Reason: reason,
}
return v
}
// NotFound reports the package is absent from the analysis DB (treated as allow,
// not a failure).
func NotFound() Verdict {
return Verdict{err: status.Error(codes.NotFound, "package not found")}
}
// ServerError reports an upstream failure, exercising the fail-open path.
func ServerError() Verdict {
return Verdict{err: status.Error(codes.Unavailable, "analysis service unavailable")}
}
type AnalyzedPackage struct {
Ecosystem packagev1.Ecosystem
Name string
Version string
}
// AnalyzerRecorder holds programmable verdicts and records every query the real
// analyzer issues to the stub gRPC client.
type AnalyzerRecorder struct {
mu sync.Mutex
verdicts map[string]Verdict
calls []AnalyzedPackage
}
func newAnalyzerRecorder() *AnalyzerRecorder {
return &AnalyzerRecorder{verdicts: map[string]Verdict{}}
}
func verdictKey(eco packagev1.Ecosystem, name, version string) string {
if eco == packagev1.Ecosystem_ECOSYSTEM_PYPI {
name = normalizePypiName(name)
}
return fmt.Sprintf("%s|%s|%s", eco.String(), name, version)
}
func (r *AnalyzerRecorder) SetNpm(name, version string, v Verdict) {
r.set(packagev1.Ecosystem_ECOSYSTEM_NPM, name, version, v)
}
func (r *AnalyzerRecorder) SetPypi(name, version string, v Verdict) {
r.set(packagev1.Ecosystem_ECOSYSTEM_PYPI, name, version, v)
}
func (r *AnalyzerRecorder) SetGo(name, version string, v Verdict) {
r.set(packagev1.Ecosystem_ECOSYSTEM_GO, name, version, v)
}
func (r *AnalyzerRecorder) set(eco packagev1.Ecosystem, name, version string, v Verdict) {
r.mu.Lock()
defer r.mu.Unlock()
r.verdicts[verdictKey(eco, name, version)] = v
}
// Calls returns every package the analyzer was queried for, in order.
func (r *AnalyzerRecorder) Calls() []AnalyzedPackage {
r.mu.Lock()
defer r.mu.Unlock()
out := make([]AnalyzedPackage, len(r.calls))
copy(out, r.calls)
return out
}
// AnalyzedCount reports how many times a specific package version was queried.
func (r *AnalyzerRecorder) AnalyzedCount(name, version string) int {
r.mu.Lock()
defer r.mu.Unlock()
n := 0
for _, c := range r.calls {
if c.Name == name && c.Version == version {
n++
}
}
return n
}
func (r *AnalyzerRecorder) handle(req *malysisv1.QueryPackageAnalysisRequest) (*malysisv1.QueryPackageAnalysisResponse, error) {
pv := req.GetTarget().GetPackageVersion()
eco := pv.GetPackage().GetEcosystem()
name := pv.GetPackage().GetName()
version := pv.GetVersion()
r.mu.Lock()
r.calls = append(r.calls, AnalyzedPackage{Ecosystem: eco, Name: name, Version: version})
v, ok := r.verdicts[verdictKey(eco, name, version)]
r.mu.Unlock()
if !ok {
v = Clean()
}
if v.err != nil {
return nil, v.err
}
resp := v.resp
if resp.GetAnalysisId() == "" {
resp.AnalysisId = fmt.Sprintf("e2e-%s-%s", name, version)
}
return resp, nil
}
// stubAnalyzerClient implements the malysis gRPC client by delegating to the
// recorder. The embedded interface satisfies the full method set; only
// QueryPackageAnalysis is exercised by the analyzer.
type stubAnalyzerClient struct {
malysisv1grpc.MalwareAnalysisServiceClient
rec *AnalyzerRecorder
}
func (s *stubAnalyzerClient) QueryPackageAnalysis(_ context.Context,
req *malysisv1.QueryPackageAnalysisRequest, _ ...grpc.CallOption) (*malysisv1.QueryPackageAnalysisResponse, error) {
return s.rec.handle(req)
}
// normalizePypiName mirrors the interceptor's PyPI name canonicalization so
// programmed verdicts key match the name the analyzer is queried with.
func normalizePypiName(name string) string {
name = strings.ToLower(name)
name = strings.ReplaceAll(name, "_", "-")
name = strings.ReplaceAll(name, ".", "-")
return name
}