2026-01-07 13:22:08 +05:30
|
|
|
package interceptors
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
packagev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/package/v1"
|
|
|
|
|
"github.com/safedep/dry/log"
|
|
|
|
|
"github.com/safedep/pmg/analyzer"
|
2026-04-08 21:04:17 +05:30
|
|
|
pmgconfig "github.com/safedep/pmg/config"
|
2026-01-07 13:22:08 +05:30
|
|
|
"github.com/safedep/pmg/proxy"
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-03 13:17:07 +05:30
|
|
|
var npmRegistryDomains = registryConfigMap{
|
2026-01-23 18:38:54 +05:30
|
|
|
"registry.npmjs.org": {
|
|
|
|
|
Host: "registry.npmjs.org",
|
|
|
|
|
SupportedForAnalysis: true,
|
2026-02-03 13:17:07 +05:30
|
|
|
Parser: npmParser{},
|
2026-01-23 18:38:54 +05:30
|
|
|
},
|
|
|
|
|
"registry.yarnpkg.com": {
|
|
|
|
|
Host: "registry.yarnpkg.com",
|
|
|
|
|
SupportedForAnalysis: true,
|
2026-02-03 13:17:07 +05:30
|
|
|
Parser: npmParser{},
|
2026-01-23 18:38:54 +05:30
|
|
|
},
|
|
|
|
|
"npm.pkg.github.com": {
|
|
|
|
|
Host: "npm.pkg.github.com",
|
|
|
|
|
SupportedForAnalysis: false, // Skip analysis for now (private packages, auth complexity)
|
2026-02-03 13:17:07 +05:30
|
|
|
Parser: npmGithubParser{},
|
2026-01-23 18:38:54 +05:30
|
|
|
},
|
|
|
|
|
"pkg-npm.githubusercontent.com": {
|
|
|
|
|
Host: "pkg-npm.githubusercontent.com",
|
|
|
|
|
SupportedForAnalysis: false, // Skip analysis (blob storage, redirected downloads)
|
2026-02-03 13:17:07 +05:30
|
|
|
Parser: npmGithubBlobParser{},
|
2026-01-23 18:38:54 +05:30
|
|
|
},
|
|
|
|
|
}
|
2026-01-07 13:22:08 +05:30
|
|
|
|
|
|
|
|
// NpmRegistryInterceptor intercepts NPM registry requests and analyzes packages for malware
|
|
|
|
|
// It embeds baseRegistryInterceptor to reuse ecosystem agnostic functionality
|
|
|
|
|
type NpmRegistryInterceptor struct {
|
|
|
|
|
baseRegistryInterceptor
|
2026-04-08 21:04:17 +05:30
|
|
|
cooldownHandler *npmCooldownHandler
|
2026-01-07 13:22:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ proxy.Interceptor = (*NpmRegistryInterceptor)(nil)
|
2026-03-24 21:50:13 +05:30
|
|
|
var _ proxy.MITMDecider = (*NpmRegistryInterceptor)(nil)
|
2026-01-07 13:22:08 +05:30
|
|
|
|
|
|
|
|
// NewNpmRegistryInterceptor creates a new NPM registry interceptor
|
|
|
|
|
func NewNpmRegistryInterceptor(
|
|
|
|
|
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
|
|
|
) *NpmRegistryInterceptor {
|
|
|
|
|
return &NpmRegistryInterceptor{
|
|
|
|
|
baseRegistryInterceptor: baseRegistryInterceptor{
|
|
|
|
|
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-03 21:39:00 +05:30
|
|
|
circuitBreaker: newAnalyzerCircuitBreaker("malysis-analyzer-npm"),
|
2026-04-23 17:47:14 +05:30
|
|
|
execContext: execContext,
|
2026-01-07 13:22:08 +05:30
|
|
|
},
|
2026-04-08 21:04:17 +05:30
|
|
|
cooldownHandler: newNpmCooldownHandler(statsCollector),
|
2026-01-07 13:22:08 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Name returns the interceptor name for logging
|
|
|
|
|
func (i *NpmRegistryInterceptor) Name() string {
|
|
|
|
|
return "npm-registry-interceptor"
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-24 21:50:13 +05:30
|
|
|
func (i *NpmRegistryInterceptor) ShouldMITM(ctx *proxy.RequestContext) bool {
|
|
|
|
|
config := npmRegistryDomains.GetConfigForHostname(ctx.Hostname)
|
|
|
|
|
if config == nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return config.SupportedForAnalysis
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 13:22:08 +05:30
|
|
|
// ShouldIntercept determines if this interceptor should handle the given request
|
|
|
|
|
func (i *NpmRegistryInterceptor) ShouldIntercept(ctx *proxy.RequestContext) bool {
|
2026-02-03 13:17:07 +05:30
|
|
|
return npmRegistryDomains.ContainsHostname(ctx.Hostname)
|
2026-01-07 13:22:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HandleRequest processes the request and returns response action
|
|
|
|
|
// We take a fail-open approach here, allowing requests that we can't parse the package information from the URL.
|
|
|
|
|
func (i *NpmRegistryInterceptor) HandleRequest(ctx *proxy.RequestContext) (*proxy.InterceptorResponse, error) {
|
|
|
|
|
log.Debugf("[%s] Handling NPM registry request: %s", ctx.RequestID, ctx.URL.Path)
|
|
|
|
|
|
2026-01-23 18:38:54 +05:30
|
|
|
// Get registry configuration
|
2026-02-03 13:17:07 +05:30
|
|
|
config := npmRegistryDomains.GetConfigForHostname(ctx.Hostname)
|
2026-01-23 18:38:54 +05:30
|
|
|
if config == nil {
|
|
|
|
|
// Shouldn't happen if ShouldIntercept is working correctly
|
|
|
|
|
log.Warnf("[%s] No registry config found for hostname: %s", ctx.RequestID, ctx.Hostname)
|
|
|
|
|
return &proxy.InterceptorResponse{Action: proxy.ActionAllow}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skip analysis for registries that are not supported for analysis
|
|
|
|
|
if !config.SupportedForAnalysis {
|
|
|
|
|
log.Debugf("[%s] Skipping analysis for %s registry (not supported for analysis): %s",
|
|
|
|
|
ctx.RequestID, config.Host, ctx.URL.String())
|
|
|
|
|
return &proxy.InterceptorResponse{Action: proxy.ActionAllow}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse URL using registry-specific strategy
|
2026-02-03 13:17:07 +05:30
|
|
|
pkgInfo, err := config.Parser.ParseURL(ctx.URL.Path)
|
2026-01-07 13:22:08 +05:30
|
|
|
if err != nil {
|
2026-01-23 18:38:54 +05:30
|
|
|
log.Warnf("[%s] Failed to parse NPM registry URL %s for %s: %v",
|
|
|
|
|
ctx.RequestID, ctx.URL.Path, config.Host, err)
|
2026-01-07 13:22:08 +05:30
|
|
|
return &proxy.InterceptorResponse{Action: proxy.ActionAllow}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-08 21:04:17 +05:30
|
|
|
depCooldownConfig := pmgconfig.Get().Config.DependencyCooldown
|
|
|
|
|
|
2026-02-03 13:17:07 +05:30
|
|
|
if !pkgInfo.IsFileDownload() {
|
2026-04-08 21:04:17 +05:30
|
|
|
if depCooldownConfig.Enabled {
|
2026-06-21 18:22:15 +05:30
|
|
|
if pmgconfig.IsTrustedPackageAllVersions(packagev1.Ecosystem_ECOSYSTEM_NPM, pkgInfo.GetName()) {
|
2026-06-15 19:44:55 +05:30
|
|
|
return &proxy.InterceptorResponse{Action: proxy.ActionAllow}, nil
|
|
|
|
|
}
|
2026-06-21 18:22:15 +05:30
|
|
|
return i.cooldownHandler.HandleMetadataRequest(ctx, pkgInfo.GetName(), depCooldownConfig.Days, i.execContext.PinnedVersions[pkgInfo.GetName()])
|
2026-04-08 21:04:17 +05:30
|
|
|
}
|
|
|
|
|
|
2026-02-03 13:17:07 +05:30
|
|
|
log.Debugf("[%s] Skipping analysis for metadata request: %s", ctx.RequestID, pkgInfo.GetName())
|
2026-01-07 13:22:08 +05:30
|
|
|
return &proxy.InterceptorResponse{Action: proxy.ActionAllow}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-21 18:22:15 +05:30
|
|
|
if resp, ok := i.fastAllow(ctx, packagev1.Ecosystem_ECOSYSTEM_NPM, pkgInfo.GetName(), pkgInfo.GetVersion()); ok {
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 21:39:00 +05:30
|
|
|
result, err := i.analyzePackage(
|
2026-01-07 13:22:08 +05:30
|
|
|
ctx,
|
|
|
|
|
packagev1.Ecosystem_ECOSYSTEM_NPM,
|
2026-02-03 13:17:07 +05:30
|
|
|
pkgInfo.GetName(),
|
|
|
|
|
pkgInfo.GetVersion(),
|
2026-01-07 13:22:08 +05:30
|
|
|
)
|
|
|
|
|
if err != nil {
|
2026-02-03 13:17:07 +05:30
|
|
|
log.Errorf("[%s] Failed to analyze package %s@%s: %v", ctx.RequestID, pkgInfo.GetName(), pkgInfo.GetVersion(), err)
|
2026-01-07 13:22:08 +05:30
|
|
|
return &proxy.InterceptorResponse{Action: proxy.ActionAllow}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-03 13:17:07 +05:30
|
|
|
return i.handleAnalysisResult(ctx, packagev1.Ecosystem_ECOSYSTEM_NPM, pkgInfo.GetName(), pkgInfo.GetVersion(), result)
|
2026-01-07 13:22:08 +05:30
|
|
|
}
|