2026-01-07 13:22:08 +05:30
|
|
|
package interceptors
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
packagev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/package/v1"
|
|
|
|
|
"github.com/safedep/pmg/analyzer"
|
|
|
|
|
"github.com/safedep/pmg/proxy"
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-23 17:47:14 +05:30
|
|
|
// InterceptorContext carries per-execution data from the CLI command into
|
|
|
|
|
// the interceptor layer. Unlike the factory's long-lived dependencies
|
|
|
|
|
// (analyzer, cache, stats), this holds context specific to the current run.
|
|
|
|
|
type InterceptorContext struct {
|
|
|
|
|
PinnedVersions map[string]string
|
2026-07-03 18:19:31 +05:30
|
|
|
|
|
|
|
|
// GoProxyBaseURLs maps module-proxy hostnames from the user's effective
|
|
|
|
|
// GOPROXY to their upstream base URL (scheme + host + optional path
|
|
|
|
|
// prefix). The Go interceptor MITMs and analyzes these hosts; Go is the
|
|
|
|
|
// only ecosystem whose registry hosts are user-configurable rather than
|
|
|
|
|
// fixed.
|
|
|
|
|
GoProxyBaseURLs map[string]string
|
2026-04-23 17:47:14 +05:30
|
|
|
}
|
|
|
|
|
|
2026-01-07 13:22:08 +05:30
|
|
|
// InterceptorFactory creates ecosystem-specific interceptors for the proxy
|
|
|
|
|
type InterceptorFactory struct {
|
|
|
|
|
analyzer analyzer.PackageVersionAnalyzer
|
|
|
|
|
cache AnalysisCache
|
2026-01-27 17:50:26 +05:30
|
|
|
statsCollector *AnalysisStatsCollector
|
2026-01-07 13:22:08 +05:30
|
|
|
confirmationChan chan *ConfirmationRequest
|
2026-04-23 17:47:14 +05:30
|
|
|
execContext InterceptorContext
|
2026-01-07 13:22:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewInterceptorFactory creates a new interceptor factory with shared dependencies
|
|
|
|
|
func NewInterceptorFactory(
|
|
|
|
|
analyzer analyzer.PackageVersionAnalyzer,
|
|
|
|
|
cache AnalysisCache,
|
2026-01-27 17:50:26 +05:30
|
|
|
statsCollector *AnalysisStatsCollector,
|
2026-01-07 13:22:08 +05:30
|
|
|
confirmationChan chan *ConfirmationRequest,
|
2026-04-23 17:47:14 +05:30
|
|
|
execContext InterceptorContext,
|
2026-01-07 13:22:08 +05:30
|
|
|
) *InterceptorFactory {
|
|
|
|
|
return &InterceptorFactory{
|
|
|
|
|
analyzer: analyzer,
|
|
|
|
|
cache: cache,
|
2026-01-27 17:50:26 +05:30
|
|
|
statsCollector: statsCollector,
|
2026-01-07 13:22:08 +05:30
|
|
|
confirmationChan: confirmationChan,
|
2026-04-23 17:47:14 +05:30
|
|
|
execContext: execContext,
|
2026-01-07 13:22:08 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateInterceptor creates an interceptor for the specified ecosystem
|
|
|
|
|
// Returns an error if the ecosystem is not supported for proxy-based interception
|
|
|
|
|
func (f *InterceptorFactory) CreateInterceptor(ecosystem packagev1.Ecosystem) (proxy.Interceptor, error) {
|
|
|
|
|
switch ecosystem {
|
|
|
|
|
case packagev1.Ecosystem_ECOSYSTEM_NPM:
|
|
|
|
|
return NewNpmRegistryInterceptor(
|
|
|
|
|
f.analyzer,
|
|
|
|
|
f.cache,
|
2026-01-27 17:50:26 +05:30
|
|
|
f.statsCollector,
|
2026-01-07 13:22:08 +05:30
|
|
|
f.confirmationChan,
|
2026-04-23 17:47:14 +05:30
|
|
|
f.execContext,
|
2026-01-07 13:22:08 +05:30
|
|
|
), nil
|
|
|
|
|
|
2026-02-05 13:30:27 +05:30
|
|
|
case packagev1.Ecosystem_ECOSYSTEM_PYPI:
|
|
|
|
|
return NewPypiRegistryInterceptor(
|
|
|
|
|
f.analyzer,
|
|
|
|
|
f.cache,
|
|
|
|
|
f.statsCollector,
|
|
|
|
|
f.confirmationChan,
|
2026-04-23 17:47:14 +05:30
|
|
|
f.execContext,
|
2026-02-05 13:30:27 +05:30
|
|
|
), nil
|
|
|
|
|
|
2026-07-03 18:19:31 +05:30
|
|
|
case packagev1.Ecosystem_ECOSYSTEM_GO:
|
|
|
|
|
return NewGoRegistryInterceptor(
|
|
|
|
|
f.analyzer,
|
|
|
|
|
f.cache,
|
|
|
|
|
f.statsCollector,
|
|
|
|
|
f.confirmationChan,
|
|
|
|
|
f.execContext,
|
|
|
|
|
), nil
|
|
|
|
|
|
2026-01-07 13:22:08 +05:30
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("proxy-based interception not yet supported for ecosystem: %s", ecosystem.String())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SupportedEcosystems returns a list of ecosystems that support proxy-based interception
|
|
|
|
|
func SupportedEcosystems() []packagev1.Ecosystem {
|
|
|
|
|
return []packagev1.Ecosystem{
|
|
|
|
|
packagev1.Ecosystem_ECOSYSTEM_NPM,
|
2026-02-05 13:30:27 +05:30
|
|
|
packagev1.Ecosystem_ECOSYSTEM_PYPI,
|
2026-07-03 18:19:31 +05:30
|
|
|
packagev1.Ecosystem_ECOSYSTEM_GO,
|
2026-01-07 13:22:08 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsSupported checks if an ecosystem supports proxy-based interception
|
|
|
|
|
func IsSupported(ecosystem packagev1.Ecosystem) bool {
|
|
|
|
|
for _, supported := range SupportedEcosystems() {
|
|
|
|
|
if ecosystem == supported {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|