GitLabgitlab-org
cli
main
1package policy
3import (
4 "context"
5 "errors"
6 "fmt"
7 "net/http"
9 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
11 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/purl"
12 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/verdict"
13)
15// errFirewallNotEvaluating reports that the Dependency Firewall is not
16// evaluating this project, so there is no verdict to apply. The API signals
17// this with 404 (feature flag off, project not found, or token without access)
18// or 422 (firewall configured but not enforced). CachingChecker maps it to
19// allow; restChecker never decides the fail-open policy itself.
20var errFirewallNotEvaluating = errors.New("dependency firewall is not evaluating this project")
22// IsNotEvaluating reports whether err is the "firewall not evaluating this
23// project" sentinel, so a caller that checks a coordinate directly (without a
24// CachingChecker to translate the fail-mode) can make the same fail-open
25// decision the cache makes: treat a not-evaluating project as an allow rather
26// than a block.
27func IsNotEvaluating(err error) bool {
28 return errors.Is(err, errFirewallNotEvaluating)
29}
31// restChecker is the real GitLab-backed policy source, selected whenever no
32// GLAB_DF_FAKE_* variable is set. It evaluates each coordinate against the
33// firewall for the request's project and maps the outcome to a Result. It
34// returns errFirewallNotEvaluating when the firewall is not evaluating the
35// project (so the caller can allow), and a wrapped transport/server/
36// unexpected-response error on any other failure (so the caller can block).
37type restChecker struct {
38 client *gitlab.Client
39}
41func newRESTChecker(client *gitlab.Client) Checker {
42 return restChecker{client: client}
43}
45func (c restChecker) Check(ctx context.Context, req Request) (Result, error) {
46 ecosystem, err := ecosystemValue(req.Coordinate.Ecosystem)
47 if err != nil {
48 return Result{}, err
49 }
51 opt := &gitlab.EvaluatePackageOptions{
52 Ecosystem: ecosystem,
53 Name: req.Coordinate.Name,
54 Version: req.Coordinate.Version,
55 }
57 eval, _, err := c.client.SecurityDependencyFirewall.EvaluatePackage(req.ProjectID, opt, gitlab.WithContext(ctx))
59 // The firewall is deliberately not evaluating this project on 404 (feature
60 // flag off / project not found / no access) or 422 (not enforced). Signal
61 // that with a sentinel so the caller, not this checker, owns the fail-open
62 // decision. EvaluatePackage carries the status on the returned error, read
63 // via gitlab.HasStatusCode. Wrap the underlying error so the sentinel keeps
64 // matching under errors.Is while the caller's log still carries the status
65 // code and server message: a 404 (bad path / no access) and a 422 (not
66 // enforced) then stay distinguishable instead of collapsing to one opaque
67 // line.
68 if gitlab.HasStatusCode(err, http.StatusNotFound) || gitlab.HasStatusCode(err, http.StatusUnprocessableEntity) {
69 return Result{}, fmt.Errorf("%w: %w", errFirewallNotEvaluating, err)
70 }
72 if err != nil {
73 return Result{}, fmt.Errorf("dependency firewall evaluation failed: %w", err)
74 }
76 return resultForOutcome(eval)
77}
79// ecosystemValue maps a coordinate ecosystem to the client-go enum, erroring
80// on an unknown value rather than passing a free-form string that the API
81// would reject with a 400. Normalizing at this seam keeps an unsupported
82// ecosystem out of the request instead of turning it into an opaque
83// fail-closed block. It switches on purl's exported type constants, the single
84// owner of the supported-ecosystem list, so a newly supported PURL type cannot
85// pass purl.Parse and then silently fail closed here.
86func ecosystemValue(ecosystem string) (gitlab.DependencyFirewallEcosystemValue, error) {
87 switch ecosystem {
88 case purl.TypeNpm:
89 return gitlab.DependencyFirewallEcosystemNPM, nil
90 case purl.TypePypi:
91 return gitlab.DependencyFirewallEcosystemPyPI, nil
92 case purl.TypeMaven:
93 return gitlab.DependencyFirewallEcosystemMaven, nil
94 case purl.TypeGem:
95 return gitlab.DependencyFirewallEcosystemGem, nil
96 case purl.TypeNuget:
97 return gitlab.DependencyFirewallEcosystemNuGet, nil
98 case purl.TypeCargo:
99 return gitlab.DependencyFirewallEcosystemCargo, nil
100 case purl.TypeGolang:
101 return gitlab.DependencyFirewallEcosystemGolang, nil
102 default:
103 return "", fmt.Errorf("unsupported dependency firewall ecosystem %q", ecosystem)
104 }
105}
107// resultForOutcome maps a package evaluation outcome to a Result, erroring on
108// a missing body or an unknown outcome so the caller fails closed.
109func resultForOutcome(eval *gitlab.PackageEvaluation) (Result, error) {
110 if eval == nil {
111 return Result{}, errors.New("dependency firewall returned no evaluation")
112 }
114 switch eval.Outcome {
115 case gitlab.DependencyFirewallOutcomeAllowed:
116 return Result{}, nil
117 case gitlab.DependencyFirewallOutcomeWarned:
118 return Result{Verdict: verdict.Warning, Reason: reasonText(eval.Reason)}, nil
119 case gitlab.DependencyFirewallOutcomeBlocked:
120 return Result{Verdict: verdict.Blocked, Reason: reasonText(eval.Reason)}, nil
121 default:
122 return Result{}, fmt.Errorf("unknown dependency firewall outcome %q", eval.Outcome)
123 }
124}
126// reasonText dereferences the optional reason the firewall returns for a
127// warned or blocked outcome, mapping a nil pointer to an empty string.
128func reasonText(reason *string) string {
129 if reason == nil {
130 return ""
131 }
132 return *reason
133}
Diagnostics7 git calls · 258 ms
CallStatusSizeTime0258 ms
discover200185 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchtree:0200361 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=5 git.response.bytes=361 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=506cba622ceda1473ee3cde63969809c144b7247 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=361 git.pkt.payload.max=334 git.sideband.band1.bytes=334 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=334 git.pack.trailer=26d32585a3a474717e00a318584ccd16918c9d32Open pack map
fetchtree:0200332 B · hit5 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=244 git.response.bytes=332 git.response.cache=HIT git.response.duration_ms=5 git.fetch.filter=tree:0 git.fetch.wants=cb813f0d675e9dca14f167e8c7788d61eb19627f git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=332 git.pkt.payload.max=305 git.sideband.band1.bytes=305 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=305 git.pack.trailer=5e210c1831ecf78390a6fe6e1784efd898336da0Open pack map
fetch2001.9 KiB244 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1993 git.response.cache=MISS git.response.duration_ms=244 git.fetch.wants=f85bf249a86d4a252763a0493c886abec74b1a03 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1993 git.pkt.payload.max=1966 git.sideband.band1.bytes=1966 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e229eaae4ea151
- Request
- 846f5236…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format