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>
109 lines
3.2 KiB
Go
109 lines
3.2 KiB
Go
// Package golang implements the experimental `pmg go` command. The package is
|
|
// named golang (not go) to avoid shadowing the toolchain name as a package
|
|
// path; the user-facing command is still `pmg go`.
|
|
package golang
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/safedep/dry/usefulerror"
|
|
"github.com/safedep/pmg/config"
|
|
"github.com/safedep/pmg/errcodes"
|
|
"github.com/safedep/pmg/internal/analytics"
|
|
"github.com/safedep/pmg/internal/flows"
|
|
"github.com/safedep/pmg/internal/ui"
|
|
"github.com/safedep/pmg/packagemanager"
|
|
"github.com/safedep/pmg/proxy/certmanager"
|
|
"github.com/safedep/pmg/truststore"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func NewGoCommand() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "go [action] [module]",
|
|
Short: "Guard go module downloads (experimental)",
|
|
DisableFlagParsing: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
err := executeGoFlow(cmd.Context(), args)
|
|
if err != nil {
|
|
ui.ExitFromCommandError(err)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
func executeGoFlow(ctx context.Context, args []string) error {
|
|
analytics.TrackCommandGo()
|
|
|
|
packageManager, err := packagemanager.NewGoPackageManager(packagemanager.DefaultGoPackageManagerConfig())
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create go package manager: %w", err)
|
|
}
|
|
|
|
parsedCommand, err := packageManager.ParseCommand(args)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to parse command: %w", err)
|
|
}
|
|
|
|
if !config.Get().IsProxyModeEnabled() {
|
|
return errGoRequiresProxyMode()
|
|
}
|
|
|
|
if err := requireTrustedCA(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return flows.ProxyFlow(packageManager, packagemanager.NewNoopPackageResolver()).Run(ctx, args, parsedCommand)
|
|
}
|
|
|
|
// requireTrustedCA fails fast when Go cannot trust PMG's MITM CA. Go's
|
|
// crypto/x509 ignores SSL_CERT_FILE on macOS and Windows and verifies TLS
|
|
// against the OS trust store only, so without an OS-trusted persisted CA every
|
|
// module download would fail mid-build with an opaque x509 error. Linux honors
|
|
// the injected SSL_CERT_FILE bundle, so any CA (persisted or ephemeral) works.
|
|
func requireTrustedCA() error {
|
|
if !truststore.UserScopeSupported() {
|
|
return nil
|
|
}
|
|
|
|
if _, err := certmanager.LoadCA(config.Get().ConfigDir()); err != nil {
|
|
return errGoCertNotTrusted(err)
|
|
}
|
|
|
|
user, system, err := truststore.Status(certmanager.CACommonName)
|
|
if err != nil {
|
|
return errGoCertNotTrusted(err)
|
|
}
|
|
|
|
if !user && !system {
|
|
return errGoCertNotTrusted(nil)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func errGoRequiresProxyMode() error {
|
|
return usefulerror.NewUsefulError().
|
|
WithCode(errcodes.InvalidArgument).
|
|
WithHumanError("Go support requires proxy mode, which is disabled in your configuration.").
|
|
WithHelp("Enable proxy mode (proxy.enabled: true in the PMG config) and retry.").
|
|
WithMsg("go requires proxy mode")
|
|
}
|
|
|
|
func errGoCertNotTrusted(cause error) error {
|
|
err := usefulerror.NewUsefulError().
|
|
WithCode(errcodes.CertTrustStore).
|
|
WithHumanError("Go ignores PMG's injected CA bundle on this OS; the PMG proxy CA must be trusted in the OS trust store.").
|
|
WithHelp("Run `pmg setup cert install` to install and trust the PMG proxy CA, then retry.").
|
|
WithMsg("pmg proxy CA is not trusted in the OS trust store")
|
|
|
|
if cause != nil {
|
|
return err.Wrap(cause)
|
|
}
|
|
|
|
return err
|
|
}
|