Skip to content

Commit e1924dc

Browse files
committed
sourcepolicy: add validations for nil values
Signed-off-by: Tonis Tiigi <[email protected]> (cherry picked from commit 4e2569e796aae398648082689d70ca1d4f4f74a8) (cherry picked from commit caea271063973c6903be08c1ebbc7c103f67805f)
1 parent 96663dd commit e1924dc

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

client/client_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ func TestIntegration(t *testing.T) {
210210
testValidateInvalidConfig,
211211
testValidatePlatformsEmpty,
212212
testValidatePlatformsInvalid,
213+
testValidateSourcePolicy,
213214
)
214215
}
215216

client/validation_test.go

+102
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/moby/buildkit/client/llb"
1010
"github.com/moby/buildkit/exporter/containerimage/exptypes"
1111
"github.com/moby/buildkit/frontend/gateway/client"
12+
sppb "github.com/moby/buildkit/sourcepolicy/pb"
1213
"github.com/moby/buildkit/util/testutil/integration"
1314
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
1415
"github.com/stretchr/testify/require"
@@ -204,3 +205,104 @@ func testValidatePlatformsInvalid(t *testing.T, sb integration.Sandbox) {
204205
})
205206
}
206207
}
208+
209+
func testValidateSourcePolicy(t *testing.T, sb integration.Sandbox) {
210+
requiresLinux(t)
211+
212+
ctx := sb.Context()
213+
214+
c, err := New(ctx, sb.Address())
215+
require.NoError(t, err)
216+
defer c.Close()
217+
218+
tcases := []struct {
219+
name string
220+
value *sppb.Policy
221+
exp string
222+
}{
223+
// this condition fails on marshaling atm
224+
// {
225+
// name: "nilrule",
226+
// value: &sppb.Policy{
227+
// Rules: []*sppb.Rule{nil},
228+
// },
229+
// exp: "",
230+
// },
231+
{
232+
name: "nilselector",
233+
value: &sppb.Policy{
234+
Rules: []*sppb.Rule{
235+
{
236+
Action: sppb.PolicyAction_CONVERT,
237+
},
238+
},
239+
},
240+
exp: "invalid nil selector in policy",
241+
},
242+
{
243+
name: "emptyaction",
244+
value: &sppb.Policy{
245+
Rules: []*sppb.Rule{
246+
{
247+
Action: sppb.PolicyAction(9000),
248+
Selector: &sppb.Selector{
249+
Identifier: "docker-image://docker.io/library/alpine:latest",
250+
},
251+
},
252+
},
253+
},
254+
exp: "unknown type",
255+
},
256+
{
257+
name: "nilupdates",
258+
value: &sppb.Policy{
259+
Rules: []*sppb.Rule{
260+
{
261+
Action: sppb.PolicyAction_CONVERT,
262+
Selector: &sppb.Selector{
263+
Identifier: "docker-image://docker.io/library/alpine:latest",
264+
},
265+
},
266+
},
267+
},
268+
exp: "missing destination for convert rule",
269+
},
270+
}
271+
272+
for _, tc := range tcases {
273+
t.Run(tc.name, func(t *testing.T) {
274+
275+
var viaFrontend bool
276+
277+
b := func(ctx context.Context, c client.Client) (*client.Result, error) {
278+
def, err := llb.Image("alpine").Marshal(ctx)
279+
if err != nil {
280+
return nil, err
281+
}
282+
283+
req := client.SolveRequest{
284+
Evaluate: true,
285+
Definition: def.ToPB(),
286+
}
287+
if viaFrontend {
288+
req.SourcePolicies = []*sppb.Policy{
289+
tc.value,
290+
}
291+
}
292+
return c.Solve(ctx, req)
293+
}
294+
295+
_, err = c.Build(ctx, SolveOpt{
296+
SourcePolicy: tc.value,
297+
}, "", b, nil)
298+
require.Error(t, err)
299+
require.Contains(t, err.Error(), tc.exp)
300+
301+
viaFrontend = true
302+
_, err = c.Build(ctx, SolveOpt{}, "", b, nil)
303+
require.Error(t, err)
304+
require.Contains(t, err.Error(), tc.exp)
305+
306+
})
307+
}
308+
}

solver/llbsolver/bridge.go

+8
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ func (b *llbBridge) loadResult(ctx context.Context, def *pb.Definition, cacheImp
7979
}
8080
var polEngine SourcePolicyEvaluator
8181
if srcPol != nil || len(pol) > 0 {
82+
for _, p := range pol {
83+
if p == nil {
84+
return nil, errors.Errorf("invalid nil policy")
85+
}
86+
if err := validateSourcePolicy(*p); err != nil {
87+
return nil, err
88+
}
89+
}
8290
if srcPol != nil {
8391
pol = append([]*spb.Policy{srcPol}, pol...)
8492
}

solver/llbsolver/solver.go

+23
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,9 @@ func (s *Solver) Solve(ctx context.Context, id string, sessionID string, req fro
447447
j.SetValue(keyEntitlements, set)
448448

449449
if srcPol != nil {
450+
if err := validateSourcePolicy(*srcPol); err != nil {
451+
return nil, err
452+
}
450453
j.SetValue(keySourcePolicy, *srcPol)
451454
}
452455

@@ -595,6 +598,23 @@ func (s *Solver) Solve(ctx context.Context, id string, sessionID string, req fro
595598
}, nil
596599
}
597600

601+
func validateSourcePolicy(pol spb.Policy) error {
602+
for _, r := range pol.Rules {
603+
if r == nil {
604+
return errors.New("invalid nil rule in policy")
605+
}
606+
if r.Selector == nil {
607+
return errors.New("invalid nil selector in policy")
608+
}
609+
for _, c := range r.Selector.Constraints {
610+
if c == nil {
611+
return errors.New("invalid nil constraint in policy")
612+
}
613+
}
614+
}
615+
return nil
616+
}
617+
598618
func runCacheExporters(ctx context.Context, exporters []RemoteCacheExporter, j *solver.Job, cached *result.Result[solver.CachedResult], inp *result.Result[cache.ImmutableRef]) (map[string]string, error) {
599619
eg, ctx := errgroup.WithContext(ctx)
600620
g := session.NewGroup(j.SessionID)
@@ -991,6 +1011,9 @@ func loadSourcePolicy(b solver.Builder) (*spb.Policy, error) {
9911011
return errors.Errorf("invalid source policy %T", v)
9921012
}
9931013
for _, f := range x.Rules {
1014+
if f == nil {
1015+
return errors.Errorf("invalid nil policy rule")
1016+
}
9941017
r := *f
9951018
srcPol.Rules = append(srcPol.Rules, &r)
9961019
}

sourcepolicy/matcher.go

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010

1111
func match(ctx context.Context, src *selectorCache, ref string, attrs map[string]string) (bool, error) {
1212
for _, c := range src.Constraints {
13+
if c == nil {
14+
return false, errors.Errorf("invalid nil constraint for %v", src)
15+
}
1316
switch c.Condition {
1417
case spb.AttrMatch_EQUAL:
1518
if attrs[c.Key] != c.Value {

0 commit comments

Comments
 (0)