Skip to content

Commit 9ec2c7c

Browse files
committed
frontend: address source filter review feedback
Scope source filter config loading to the requested YAML path and decode it strictly so misspelled policy keys fail the build instead of silently disabling filtering. Apply global excludes to generated node module source states as well, keep Windows preprocessing consistent with the unfiltered build source path, and make RPM provenance comments safe for multi-line exclusion patterns. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
1 parent 8b69774 commit 9ec2c7c

7 files changed

Lines changed: 70 additions & 38 deletions

File tree

frontend/request.go

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -155,36 +155,23 @@ func getSigningConfigFromContext(ctx context.Context, client gwclient.Client, cf
155155
return pc.Signer, nil
156156
}
157157

158-
func getSourceFilterConfigFromContext(ctx context.Context, client gwclient.Client, cfgPath string, configState llb.State) (dalec.SourceFilterConfig, error) {
159-
scDef, err := configState.Marshal(ctx)
160-
if err != nil {
161-
return dalec.SourceFilterConfig{}, err
162-
}
163-
164-
res, err := client.Solve(ctx, gwclient.SolveRequest{
165-
Definition: scDef.ToPB(),
166-
})
167-
if err != nil {
168-
return dalec.SourceFilterConfig{}, err
169-
}
170-
171-
ref, err := res.SingleRef()
158+
func getSourceFilterConfigFromContext(ctx context.Context, client gwclient.Client, cfgPath string, configCtxName string, getContext func(string, ...llb.LocalOption) (*llb.State, error), opts ...llb.ConstraintsOpt) (dalec.SourceFilterConfig, error) {
159+
dt, err := readConfigFromContext(ctx, client, cfgPath, configCtxName, dalec.SourceOpts{
160+
GetContext: getContext,
161+
SourceFilter: dalec.NoSourceFilter,
162+
}, opts...)
172163
if err != nil {
173164
return dalec.SourceFilterConfig{}, err
174165
}
175166

176-
dt, err := ref.ReadFile(ctx, gwclient.ReadRequest{
177-
Filename: cfgPath,
178-
})
179-
if err != nil {
180-
return dalec.SourceFilterConfig{}, err
181-
}
167+
return decodeSourceFilterConfig(ctx, dt)
168+
}
182169

170+
func decodeSourceFilterConfig(ctx context.Context, dt []byte) (dalec.SourceFilterConfig, error) {
183171
var cfg dalec.SourceFilterConfig
184-
if err := yaml.Unmarshal(dt, &cfg); err != nil {
172+
if err := yaml.UnmarshalContext(ctx, dt, &cfg, yaml.Strict()); err != nil {
185173
return dalec.SourceFilterConfig{}, err
186174
}
187-
188175
return cfg, nil
189176
}
190177

@@ -291,22 +278,13 @@ func getSourceFilterContextNameWithDefault(client gwclient.Client) string {
291278
return configCtxName
292279
}
293280

294-
func loadSourceFilterConfig(ctx context.Context, client gwclient.Client, getContext func(string, ...llb.LocalOption) (*llb.State, error), opts ...llb.LocalOption) (dalec.SourceFilterConfig, error) {
281+
func loadSourceFilterConfig(ctx context.Context, client gwclient.Client, getContext func(string, ...llb.LocalOption) (*llb.State, error)) (dalec.SourceFilterConfig, error) {
295282
cfgPath := getSourceFilterConfigPath(client)
296283
if cfgPath == "" {
297284
return dalec.SourceFilterConfig{}, nil
298285
}
299286

300-
configCtxName := getSourceFilterContextNameWithDefault(client)
301-
configState, err := getContext(configCtxName, opts...)
302-
if err != nil {
303-
return dalec.SourceFilterConfig{}, err
304-
}
305-
if configState == nil {
306-
return dalec.SourceFilterConfig{}, errors.Errorf("context %q not found", configCtxName)
307-
}
308-
309-
return getSourceFilterConfigFromContext(ctx, client, cfgPath, *configState)
287+
return getSourceFilterConfigFromContext(ctx, client, cfgPath, getSourceFilterContextNameWithDefault(client), getContext)
310288
}
311289

312290
func forwardToSigner(ctx context.Context, client gwclient.Client, cfg *dalec.PackageSigner, s llb.State, opts ...llb.ConstraintsOpt) (llb.State, error) {

frontend/request_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package frontend
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"gotest.tools/v3/assert"
8+
)
9+
10+
func TestDecodeSourceFilterConfigStrict(t *testing.T) {
11+
t.Parallel()
12+
13+
_, err := decodeSourceFilterConfig(context.Background(), []byte(`
14+
global_exclude:
15+
- vendor/**
16+
`))
17+
assert.Assert(t, err != nil, "expected unknown source filter fields to fail")
18+
}

generator_nodemodules.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (s *Spec) NodeModDeps(sOpt SourceOpts, worker llb.State, opts ...llb.Constr
104104
}
105105
merged = merged.With(withNodeMod(gen, worker, key, opts...))
106106
}
107-
result[key] = merged
107+
result[key] = merged.With(sourceFilterAtPath(sOpt, key, opts...))
108108
}
109109
return result
110110
}

packaging/linux/rpm/template.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,13 @@ func docSourceFilter(w io.Writer, name string, values []string) error {
393393
return err
394394
}
395395
for _, value := range values {
396-
if _, err := fmt.Fprintf(w, "# \t%s\n", value); err != nil {
396+
scanner := bufio.NewScanner(strings.NewReader(value))
397+
for scanner.Scan() {
398+
if _, err := fmt.Fprintf(w, "# \t%s\n", scanner.Text()); err != nil {
399+
return err
400+
}
401+
}
402+
if err := scanner.Err(); err != nil {
397403
return err
398404
}
399405
}

packaging/linux/rpm/template_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,23 @@ func TestTemplateSources(t *testing.T) {
297297
assert.Check(t, strings.Contains(s, "# \tExcludes:\n# \t\t testdata/**\n"))
298298
assert.Check(t, strings.Contains(s, "# Exclusions:\n# \tvendor/**\n"))
299299
})
300+
301+
t.Run("source filter docs multiline exclusion", func(t *testing.T) {
302+
w := &specWrapper{
303+
Spec: &dalec.Spec{
304+
Sources: map[string]dalec.Source{
305+
"src1": {
306+
Inline: &dalec.SourceInline{Dir: &dalec.SourceInlineDir{}},
307+
},
308+
},
309+
},
310+
SourceFilter: dalec.SourceFilterConfig{GlobalExcludes: []string{"vendor/**\nmalicious: value"}},
311+
}
312+
313+
out, err := w.Sources()
314+
assert.NilError(t, err)
315+
assert.Check(t, strings.Contains(out.String(), "# Exclusions:\n# \tvendor/**\n# \tmalicious: value\n"))
316+
})
300317
}
301318

302319
func TestTemplate_Artifacts(t *testing.T) {

source_filter.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ func sourceFilter(sOpt SourceOpts, opts ...llb.ConstraintsOpt) llb.StateOption {
6060
}
6161
}
6262

63+
func sourceFilterAtPath(sOpt SourceOpts, base string, opts ...llb.ConstraintsOpt) llb.StateOption {
64+
return func(in llb.State) llb.State {
65+
excludes, err := sOpt.sourceFilterExcludes()
66+
if err != nil {
67+
return ErrorState(in, err)
68+
}
69+
if len(excludes) == 0 {
70+
return in
71+
}
72+
return in.With(SourceFilterAtPath(base, SourceFilterConfig{GlobalExcludes: excludes}, opts...))
73+
}
74+
}
75+
6376
// SourceFilter filters source package content from the root of the input state.
6477
func SourceFilter(cfg SourceFilterConfig, opts ...llb.ConstraintsOpt) llb.StateOption {
6578
return func(in llb.State) llb.State {

targets/windows/handle_zip.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,17 @@ func buildBinaries(ctx context.Context, spec *dalec.Spec, worker llb.State, clie
161161
worker = worker.With(distroConfig.InstallBuildDeps(ctx, sOpt, spec, targetKey, opts...))
162162
}
163163

164+
buildSourceOpts := sOpt.WithoutSourceFilter()
165+
164166
// Preprocess the spec to generate patches for gomod edits and other generators
165167
// This must happen after build deps are installed so Go is available
166-
if err := spec.Preprocess(sOpt, worker, opts...); err != nil {
168+
if err := spec.Preprocess(buildSourceOpts, worker, opts...); err != nil {
167169
return dalec.ErrorState(worker, err)
168170
}
169171

170172
// Apply source map constraints for build steps
171173
opts = append(opts, spec.Build.Steps.GetSourceLocation(worker))
172174

173-
buildSourceOpts := sOpt
174-
buildSourceOpts = buildSourceOpts.WithoutSourceFilter()
175175
sources := sources(worker, spec, buildSourceOpts, opts...)
176176

177177
addGoCache(spec, targetKey)

0 commit comments

Comments
 (0)