//go:build !integration package policy import ( "context" "errors" "fmt" "strings" "sync" "sync/atomic" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/verdict" ) type countingChecker struct { calls atomic.Int64 result Result err error // entered, when non-nil, receives once each time a call enters inner, and // gate, when non-nil, blocks that call until closed. Together they let a // test observe a call in flight and then release it without sleeping, so // the coalescing window is exercised deterministically. entered chan<- struct{} gate <-chan struct{} } func (c *countingChecker) Check(context.Context, Request) (Result, error) { c.calls.Add(1) if c.entered != nil { c.entered <- struct{}{} } if c.gate != nil { <-c.gate } return c.result, c.err } // stalledChecker models an API that accepts the request and never answers: // it returns only when its context ends. type stalledChecker struct{} func (stalledChecker) Check(ctx context.Context, _ Request) (Result, error) { <-ctx.Done() return Result{}, ctx.Err() } type ctxErrChecker struct{} func (ctxErrChecker) Check(ctx context.Context, _ Request) (Result, error) { return Result{}, ctx.Err() } func TestCacheStalledCheckFailsClosedAtCallerDeadline(t *testing.T) { t.Parallel() c := NewCachingChecker(stalledChecker{}, nil) ctx, cancel := context.WithTimeout(t.Context(), 50*time.Millisecond) defer cancel() type outcome struct { res Result err error } done := make(chan outcome, 1) go func() { res, err := c.Check(ctx, req("npm", "x", "1.0.0")) done <- outcome{res, err} }() select { case out := <-done: require.NoError(t, out.err) assert.Equal(t, verdict.Blocked, out.res.Verdict) assert.Contains(t, out.res.Reason, context.DeadlineExceeded.Error()) case <-time.After(5 * time.Second): t.Fatal("Check outlived the caller's deadline, so a stalled policy API hangs the download") } } func TestCacheCallerCancellationDoesNotEndSharedCheck(t *testing.T) { t.Parallel() ctx, cancel := context.WithCancel(t.Context()) cancel() c := NewCachingChecker(ctxErrChecker{}, nil) res, err := c.Check(ctx, req("npm", "x", "1.0.0")) require.NoError(t, err) assert.Equal(t, Result{}, res, "one caller walking away must not fail the check shared with every coalesced caller") } func TestCacheMemoizesByRequestKey(t *testing.T) { t.Parallel() inner := &countingChecker{result: Result{Verdict: verdict.Warning, Reason: "abc"}} c := NewCachingChecker(inner, nil) r1, err := c.Check(t.Context(), req("npm", "x", "1.0.0")) require.NoError(t, err) r2, err := c.Check(t.Context(), req("npm", "x", "1.0.0")) require.NoError(t, err) assert.Equal(t, "abc", r1.Reason) assert.Equal(t, "abc", r2.Reason) assert.Equal(t, int64(1), inner.calls.Load(), "second call must hit cache") } func TestCacheMemoizesBlockedResult(t *testing.T) { t.Parallel() inner := &countingChecker{result: Result{Verdict: verdict.Blocked, Reason: "denied"}} c := NewCachingChecker(inner, nil) r1, err := c.Check(t.Context(), req("npm", "x", "1.0.0")) require.NoError(t, err) r2, err := c.Check(t.Context(), req("npm", "x", "1.0.0")) require.NoError(t, err) assert.Equal(t, verdict.Blocked, r1.Verdict) assert.Equal(t, verdict.Blocked, r2.Verdict) assert.Equal(t, "denied", r2.Reason) assert.Equal(t, int64(1), inner.calls.Load(), "a blocked result must round-trip through the cache") } func TestCacheDistinctRequestKeys(t *testing.T) { t.Parallel() inner := &countingChecker{result: Result{}} c := NewCachingChecker(inner, nil) _, err := c.Check(t.Context(), req("npm", "x", "1.0.0")) require.NoError(t, err) _, err = c.Check(t.Context(), req("npm", "x", "2.0.0")) require.NoError(t, err) assert.Equal(t, int64(2), inner.calls.Load()) } func TestCacheDistinctByProjectID(t *testing.T) { t.Parallel() inner := &countingChecker{result: Result{}} c := NewCachingChecker(inner, nil) r := req("npm", "x", "1.0.0") rA := r rA.ProjectID = "group/project-a" rB := r rB.ProjectID = "group/project-b" _, err := c.Check(t.Context(), rA) require.NoError(t, err) _, err = c.Check(t.Context(), rB) require.NoError(t, err) assert.Equal(t, int64(2), inner.calls.Load(), "same coordinate in different projects must not share a cache entry") } func TestCacheDistinctByOperation(t *testing.T) { t.Parallel() inner := &countingChecker{result: Result{}} c := NewCachingChecker(inner, nil) r := req("npm", "x", "1.0.0") rDownload := r rDownload.Operation = Download rUpload := r rUpload.Operation = Upload _, err := c.Check(t.Context(), rDownload) require.NoError(t, err) _, err = c.Check(t.Context(), rUpload) require.NoError(t, err) assert.Equal(t, int64(2), inner.calls.Load(), "same coordinate under different operations must not share a cache entry") } func TestCacheFailsClosedOnError(t *testing.T) { t.Parallel() inner := &countingChecker{err: errors.New("boom")} c := NewCachingChecker(inner, nil) r, err := c.Check(t.Context(), req("npm", "x", "1.0.0")) require.NoError(t, err, "fail-closed returns a blocked Result, not an error") assert.Equal(t, verdict.Blocked, r.Verdict) assert.Contains(t, r.Reason, "policy check failed") assert.True(t, r.CheckFailed, "a block standing in for a failed check must say so") } func TestCacheCheckFailedOnlyMarksFailures(t *testing.T) { t.Parallel() t.Run("when the policy blocks", func(t *testing.T) { t.Parallel() c := NewCachingChecker(&countingChecker{result: Result{Verdict: verdict.Blocked, Reason: "denied"}}, nil) r, err := c.Check(t.Context(), req("maven", "a:b", "1.0")) require.NoError(t, err) assert.True(t, r.Blocked()) assert.False(t, r.CheckFailed, "a rule's block is a decision, not a failure") }) } func TestCacheDoesNotCacheErrors(t *testing.T) { t.Parallel() inner := &countingChecker{err: errors.New("boom")} c := NewCachingChecker(inner, nil) _, err := c.Check(t.Context(), req("npm", "x", "1.0.0")) require.NoError(t, err) _, err = c.Check(t.Context(), req("npm", "x", "1.0.0")) require.NoError(t, err) assert.Equal(t, int64(2), inner.calls.Load(), "errored results must not be cached") } func TestCacheLogsOnError(t *testing.T) { t.Parallel() inner := &countingChecker{err: errors.New("boom")} var gotFormat string var gotArgs []any log := func(format string, a ...any) { gotFormat = format gotArgs = a } c := NewCachingChecker(inner, log) _, err := c.Check(t.Context(), req("npm", "x", "1.0.0")) require.NoError(t, err) require.NotEmpty(t, gotFormat, "log callback must be invoked on a failed check") // The key and the underlying error are passed to the callback; the raw // error is not surfaced verbatim in the caller-facing Result. require.Len(t, gotArgs, 2) assert.Contains(t, gotArgs[0], "npm") assert.Contains(t, gotArgs[0], "x") } func TestCacheFailsOpenWhenFirewallNotEvaluating(t *testing.T) { t.Parallel() inner := &countingChecker{err: errFirewallNotEvaluating} c := NewCachingChecker(inner, nil) r, err := c.Check(t.Context(), req("npm", "x", "1.0.0")) require.NoError(t, err) assert.Equal(t, Result{}, r, "not-evaluating maps to allow, not block") } func TestCacheLogsWhenFirewallNotEvaluating(t *testing.T) { t.Parallel() inner := &countingChecker{err: errFirewallNotEvaluating} var gotFormat string log := func(format string, a ...any) { gotFormat = format } c := NewCachingChecker(inner, log) _, err := c.Check(t.Context(), req("npm", "x", "1.0.0")) require.NoError(t, err) assert.Contains(t, gotFormat, "not evaluating", "an unexpected allow-all must be visible in the log") } func TestCacheLatchesNotEvaluatingPerProject(t *testing.T) { t.Parallel() // Once a project is observed not evaluating, later coordinates for that // same project short-circuit to allow without another inner call. inner := &countingChecker{err: errFirewallNotEvaluating} c := NewCachingChecker(inner, nil) base := req("npm", "a", "1.0.0") base.ProjectID = "group/project" first := base r1, err := c.Check(t.Context(), first) require.NoError(t, err) assert.Equal(t, Result{}, r1) second := base second.Coordinate = Coordinate{Ecosystem: "npm", Name: "b", Version: "2.0.0"} r2, err := c.Check(t.Context(), second) require.NoError(t, err) assert.Equal(t, Result{}, r2) assert.Equal(t, int64(1), inner.calls.Load(), "a not-evaluating project must be evaluated once, then latched for later coordinates") } func TestCacheLatchLogsOncePerProject(t *testing.T) { t.Parallel() // The not-evaluating diagnostic is emitted once per project, not once per // coordinate, so a large install does not flood stderr. inner := &countingChecker{err: errFirewallNotEvaluating} var logCount int log := func(string, ...any) { logCount++ } c := NewCachingChecker(inner, log) base := req("npm", "a", "1.0.0") base.ProjectID = "group/project" for i, name := range []string{"a", "b", "c"} { r := base r.Coordinate = Coordinate{Ecosystem: "npm", Name: name, Version: "1.0.0"} res, err := c.Check(t.Context(), r) require.NoError(t, err, "coordinate %d", i) assert.Equal(t, Result{}, res) } assert.Equal(t, 1, logCount, "not-evaluating must be logged once per project, not per coordinate") } func TestCacheLatchIsPerProject(t *testing.T) { t.Parallel() // Latching one project must not short-circuit a different project: the // second project is still evaluated by inner. inner := &countingChecker{err: errFirewallNotEvaluating} c := NewCachingChecker(inner, nil) rA := req("npm", "x", "1.0.0") rA.ProjectID = "group/project-a" rB := req("npm", "x", "1.0.0") rB.ProjectID = "group/project-b" _, err := c.Check(t.Context(), rA) require.NoError(t, err) _, err = c.Check(t.Context(), rB) require.NoError(t, err) assert.Equal(t, int64(2), inner.calls.Load(), "a not-evaluating latch is per project, so a different project is still evaluated") } func TestCacheConcurrentSameKeySingleFetch(t *testing.T) { t.Parallel() // The winning caller enters inner and signals on entered; it blocks on // gate while the other callers pile up on the entry's Once. Receiving the // entry signal proves a call is in flight, so we can release it without a // sleep. Coalescing means exactly one call ever enters inner. entered := make(chan struct{}) gate := make(chan struct{}) inner := &countingChecker{ result: Result{Verdict: verdict.Blocked, Reason: "denied"}, entered: entered, gate: gate, } c := NewCachingChecker(inner, nil) r := req("npm", "left-pad", "1.3.0") const n = 50 var wg sync.WaitGroup results := make([]Result, n) errs := make([]error, n) wg.Add(n) for i := range n { go func() { defer wg.Done() results[i], errs[i] = c.Check(t.Context(), r) }() } // Block until a call is provably in flight, then release it. Any caller // that had to run inner would deadlock here, since the test only ever // drains a single entry signal. <-entered close(gate) wg.Wait() for i := range n { require.NoError(t, errs[i]) assert.Equal(t, verdict.Blocked, results[i].Verdict) assert.Equal(t, "denied", results[i].Reason) } // Coalescing collapses concurrent identical requests into one inner call. assert.Equal(t, int64(1), inner.calls.Load(), "concurrent identical requests must coalesce into a single inner check") } type panickingChecker struct { calls atomic.Int64 entered chan<- struct{} gate <-chan struct{} } func (c *panickingChecker) Check(context.Context, Request) (Result, error) { if c.calls.Add(1) == 1 { c.entered <- struct{}{} <-c.gate panic("boom") } return Result{Verdict: verdict.Blocked, Reason: "denied"}, nil } func TestCachePanicFailsClosed(t *testing.T) { t.Parallel() entered, gate := make(chan struct{}, 1), make(chan struct{}) inner := &panickingChecker{entered: entered, gate: gate} var warned strings.Builder var warnedMu sync.Mutex c := NewCachingChecker(inner, func(format string, a ...any) { warnedMu.Lock() defer warnedMu.Unlock() fmt.Fprintf(&warned, format, a...) }) r := req("maven", "a:b", "1.0") type outcome struct { res Result panicked any } first := make(chan outcome, 1) go func() { var o outcome defer func() { o.panicked = recover(); first <- o }() o.res, _ = c.Check(t.Context(), r) }() <-entered c.mu.Lock() waiter := c.cache[r] c.mu.Unlock() close(gate) got := <-first require.Nil(t, got.panicked, "the cache must not pass a checker's panic on to its caller") assert.Equal(t, verdict.Blocked, got.res.Verdict, "the caller that ran the panicked check is blocked") assert.True(t, got.res.CheckFailed) assert.True(t, got.res.Internal, "a glab panic is told apart from a policy service failure") assert.Equal(t, "glab hit an internal error while checking a:b@1.0 and blocked it. Rerun to retry, and report this with the output of GLAB_DEBUG=1.", got.res.Reason) warnedMu.Lock() assert.Equal(t, "warning: glab hit an internal error while checking a:b@1.0. Rerun to retry, and report this with the output of GLAB_DEBUG=1.\n", warned.String(), "the warning names only the checked package: the caller decides the verdict") warnedMu.Unlock() assert.NotContains(t, got.res.Reason, "boom", "the panic value stays out of the reason") waiter.once.Do(func() {}) assert.Equal(t, verdict.Blocked, waiter.res.Verdict, "a caller waiting on the panicked check must be blocked") assert.True(t, waiter.res.CheckFailed) again, err := c.Check(t.Context(), r) require.NoError(t, err) assert.Equal(t, int64(2), inner.calls.Load(), "a panicked check must not be cached") assert.Equal(t, "denied", again.Reason) }