Skip to content

Commit 5829ca4

Browse files
egibsantitree
andauthored
Merge commit from fork
The compilePipeline function passes the 'uses' value directly to filepath.Join(pd, uses+".yaml") without validation. A crafted uses value like "../../../etc/passwd" could read arbitrary files outside the pipeline directory. This fix rejects absolute paths and '..' sequences in uses, and verifies the resolved path remains within the pipeline directory using filepath.Rel after cleaning. Co-authored-by: Mark <mark.manning@chainguard.dev>
1 parent 9311e39 commit 5829ca4

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

pkg/build/compile.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,24 @@ func (c *Compiled) compilePipeline(ctx context.Context, sm *SubstitutionMap, pip
225225
// When compiling an already-compiled config, `uses` will be redundant and FYI only,
226226
// so ignore it if there is also a `pipelines` spelled out.
227227
if uses != "" && len(pipeline.Pipeline) == 0 {
228+
// Validate that 'uses' does not contain path traversal sequences or absolute paths.
229+
if filepath.IsAbs(uses) || strings.Contains(uses, "..") {
230+
return fmt.Errorf("invalid pipeline 'uses' value %q: must not contain absolute paths or '..' sequences", uses)
231+
}
232+
228233
var data []byte
229234
// Set this to fail up front in case there are no pipeline dirs specified
230235
// and we can't find them.
231236
err := fmt.Errorf("could not find 'uses' pipeline %q", uses)
232237

233238
for _, pd := range c.PipelineDirs {
234239
log.Debugf("trying to load pipeline %q from %q", uses, pd)
235-
data, err = os.ReadFile(filepath.Join(pd, uses+".yaml")) // #nosec G304 - Loading pipeline definition from configured directory
240+
target := filepath.Join(pd, uses+".yaml")
241+
// Verify the resolved path is still within the pipeline directory.
242+
if rel, err := filepath.Rel(pd, filepath.Clean(target)); err != nil || strings.HasPrefix(rel, "..") {
243+
return fmt.Errorf("pipeline 'uses' value %q resolves outside pipeline directory %q", uses, pd)
244+
}
245+
data, err = os.ReadFile(target) // #nosec G304 - Loading pipeline definition from configured directory
236246
if err == nil {
237247
log.Debugf("Found pipeline %s", string(data))
238248
break

0 commit comments

Comments
 (0)