Skip to content

Commit 7718bd5

Browse files
committed
pb: add extra validation to protobuf types
Signed-off-by: Tonis Tiigi <[email protected]> (cherry picked from commit 00060c60c26b07015133edacfa32f569ceefea2e) (cherry picked from commit c890068b0da9d746cfa0f2627e0ee5cc60f869d3)
1 parent e1924dc commit 7718bd5

File tree

6 files changed

+64
-12
lines changed

6 files changed

+64
-12
lines changed

client/validation_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
func testValidateNullConfig(t *testing.T, sb integration.Sandbox) {
1919
requiresLinux(t)
20+
integration.CheckFeatureCompat(t, sb, integration.FeatureOCIExporter)
2021

2122
ctx := sb.Context()
2223

@@ -55,6 +56,7 @@ func testValidateNullConfig(t *testing.T, sb integration.Sandbox) {
5556

5657
func testValidateInvalidConfig(t *testing.T, sb integration.Sandbox) {
5758
requiresLinux(t)
59+
integration.CheckFeatureCompat(t, sb, integration.FeatureOCIExporter)
5860

5961
ctx := sb.Context()
6062

@@ -96,11 +98,12 @@ func testValidateInvalidConfig(t *testing.T, sb integration.Sandbox) {
9698
},
9799
}, "", b, nil)
98100
require.Error(t, err)
99-
require.Contains(t, err.Error(), "invalid image config for export: missing os")
101+
require.Contains(t, err.Error(), "invalid image config: os and architecture must be specified together")
100102
}
101103

102104
func testValidatePlatformsEmpty(t *testing.T, sb integration.Sandbox) {
103105
requiresLinux(t)
106+
integration.CheckFeatureCompat(t, sb, integration.FeatureOCIExporter)
104107

105108
ctx := sb.Context()
106109

@@ -139,6 +142,7 @@ func testValidatePlatformsEmpty(t *testing.T, sb integration.Sandbox) {
139142

140143
func testValidatePlatformsInvalid(t *testing.T, sb integration.Sandbox) {
141144
requiresLinux(t)
145+
integration.CheckFeatureCompat(t, sb, integration.FeatureOCIExporter)
142146

143147
ctx := sb.Context()
144148

@@ -271,7 +275,6 @@ func testValidateSourcePolicy(t *testing.T, sb integration.Sandbox) {
271275

272276
for _, tc := range tcases {
273277
t.Run(tc.name, func(t *testing.T) {
274-
275278
var viaFrontend bool
276279

277280
b := func(ctx context.Context, c client.Client) (*client.Result, error) {
@@ -302,7 +305,6 @@ func testValidateSourcePolicy(t *testing.T, sb integration.Sandbox) {
302305
_, err = c.Build(ctx, SolveOpt{}, "", b, nil)
303306
require.Error(t, err)
304307
require.Contains(t, err.Error(), tc.exp)
305-
306308
})
307309
}
308310
}

control/control.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,9 @@ func (c *Controller) Solve(ctx context.Context, req *controlapi.SolveRequest) (*
405405

406406
var cacheImports []frontend.CacheOptionsEntry
407407
for _, im := range req.Cache.Imports {
408+
if im == nil {
409+
continue
410+
}
408411
cacheImports = append(cacheImports, frontend.CacheOptionsEntry{
409412
Type: im.Type,
410413
Attrs: im.Attrs,

frontend/gateway/client/attestation.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ func AttestationToPB[T any](a *result.Attestation[T]) (*pb.Attestation, error) {
3030
}
3131

3232
func AttestationFromPB[T any](a *pb.Attestation) (*result.Attestation[T], error) {
33+
if a == nil {
34+
return nil, errors.Errorf("invalid nil attestation")
35+
}
3336
subjects := make([]result.InTotoSubject, len(a.InTotoSubjects))
3437
for i, subject := range a.InTotoSubjects {
38+
if subject == nil {
39+
return nil, errors.Errorf("invalid nil attestation subject")
40+
}
3541
subjects[i] = result.InTotoSubject{
3642
Kind: subject.Kind,
3743
Name: subject.Name,

frontend/gateway/gateway.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,12 +646,21 @@ func (lbf *llbBridgeForwarder) registerResultIDs(results ...solver.Result) (ids
646646
func (lbf *llbBridgeForwarder) Solve(ctx context.Context, req *pb.SolveRequest) (*pb.SolveResponse, error) {
647647
var cacheImports []frontend.CacheOptionsEntry
648648
for _, e := range req.CacheImports {
649+
if e == nil {
650+
return nil, errors.Errorf("invalid nil cache import")
651+
}
649652
cacheImports = append(cacheImports, frontend.CacheOptionsEntry{
650653
Type: e.Type,
651654
Attrs: e.Attrs,
652655
})
653656
}
654657

658+
for _, p := range req.SourcePolicies {
659+
if p == nil {
660+
return nil, errors.Errorf("invalid nil source policy")
661+
}
662+
}
663+
655664
ctx = tracing.ContextWithSpanFromContext(ctx, lbf.callCtx)
656665
res, err := lbf.llbBridge.Solve(ctx, frontend.SolveRequest{
657666
Evaluate: req.Evaluate,
@@ -1077,6 +1086,12 @@ func (lbf *llbBridgeForwarder) ReleaseContainer(ctx context.Context, in *pb.Rele
10771086
}
10781087

10791088
func (lbf *llbBridgeForwarder) Warn(ctx context.Context, in *pb.WarnRequest) (*pb.WarnResponse, error) {
1089+
// validate ranges are valid
1090+
for _, r := range in.Ranges {
1091+
if r == nil {
1092+
return nil, status.Errorf(codes.InvalidArgument, "invalid source range")
1093+
}
1094+
}
10801095
err := lbf.llbBridge.Warn(ctx, in.Digest, string(in.Short), frontend.WarnOpts{
10811096
Level: int(in.Level),
10821097
SourceInfo: in.Info,

util/tracing/transform/attribute.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ func Attributes(attrs []*commonpb.KeyValue) []attribute.KeyValue {
1313

1414
out := make([]attribute.KeyValue, 0, len(attrs))
1515
for _, a := range attrs {
16+
if a == nil {
17+
continue
18+
}
1619
kv := attribute.KeyValue{
1720
Key: attribute.Key(a.Key),
1821
Value: toValue(a.Value),
@@ -42,37 +45,45 @@ func toValue(v *commonpb.AnyValue) attribute.Value {
4245
func boolArray(kv []*commonpb.AnyValue) attribute.Value {
4346
arr := make([]bool, len(kv))
4447
for i, v := range kv {
45-
arr[i] = v.GetBoolValue()
48+
if v != nil {
49+
arr[i] = v.GetBoolValue()
50+
}
4651
}
4752
return attribute.BoolSliceValue(arr)
4853
}
4954

5055
func intArray(kv []*commonpb.AnyValue) attribute.Value {
5156
arr := make([]int64, len(kv))
5257
for i, v := range kv {
53-
arr[i] = v.GetIntValue()
58+
if v != nil {
59+
arr[i] = v.GetIntValue()
60+
}
5461
}
5562
return attribute.Int64SliceValue(arr)
5663
}
5764

5865
func doubleArray(kv []*commonpb.AnyValue) attribute.Value {
5966
arr := make([]float64, len(kv))
6067
for i, v := range kv {
61-
arr[i] = v.GetDoubleValue()
68+
if v != nil {
69+
arr[i] = v.GetDoubleValue()
70+
}
6271
}
6372
return attribute.Float64SliceValue(arr)
6473
}
6574

6675
func stringArray(kv []*commonpb.AnyValue) attribute.Value {
6776
arr := make([]string, len(kv))
6877
for i, v := range kv {
69-
arr[i] = v.GetStringValue()
78+
if v != nil {
79+
arr[i] = v.GetStringValue()
80+
}
7081
}
7182
return attribute.StringSliceValue(arr)
7283
}
7384

7485
func arrayValues(kv []*commonpb.AnyValue) attribute.Value {
75-
if len(kv) == 0 {
86+
if len(kv) == 0 || kv[0] == nil {
7687
return attribute.StringSliceValue([]string{})
7788
}
7889

util/tracing/transform/span.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,20 @@ func Spans(sdl []*tracepb.ResourceSpans) []tracesdk.ReadOnlySpan {
3232
}
3333

3434
for _, sdi := range sd.ScopeSpans {
35-
sda := make([]tracesdk.ReadOnlySpan, len(sdi.Spans))
36-
for i, s := range sdi.Spans {
37-
sda[i] = &readOnlySpan{
35+
if sdi == nil {
36+
continue
37+
}
38+
sda := make([]tracesdk.ReadOnlySpan, 0, len(sdi.Spans))
39+
for _, s := range sdi.Spans {
40+
if s == nil {
41+
continue
42+
}
43+
sda = append(sda, &readOnlySpan{
3844
pb: s,
3945
is: sdi.Scope,
4046
resource: sd.Resource,
4147
schemaURL: sd.SchemaUrl,
42-
}
48+
})
4349
}
4450
out = append(out, sda...)
4551
}
@@ -170,6 +176,9 @@ var _ tracesdk.ReadOnlySpan = &readOnlySpan{}
170176

171177
// status transform a OTLP span status into span code.
172178
func statusCode(st *tracepb.Status) codes.Code {
179+
if st == nil {
180+
return codes.Unset
181+
}
173182
switch st.Code {
174183
case tracepb.Status_STATUS_CODE_ERROR:
175184
return codes.Error
@@ -186,6 +195,9 @@ func links(links []*tracepb.Span_Link) []tracesdk.Link {
186195

187196
sl := make([]tracesdk.Link, 0, len(links))
188197
for _, otLink := range links {
198+
if otLink == nil {
199+
continue
200+
}
189201
// This redefinition is necessary to prevent otLink.*ID[:] copies
190202
// being reused -- in short we need a new otLink per iteration.
191203
otLink := otLink
@@ -226,6 +238,9 @@ func spanEvents(es []*tracepb.Span_Event) []tracesdk.Event {
226238
if messageEvents >= maxMessageEventsPerSpan {
227239
break
228240
}
241+
if e == nil {
242+
continue
243+
}
229244
messageEvents++
230245
events = append(events,
231246
tracesdk.Event{

0 commit comments

Comments
 (0)