Skip to content

Commit

Permalink
Disallow sources with path separators
Browse files Browse the repository at this point in the history
This was already done for nested files/dirs in inline sources.
This makes sense to do for all sources since this can cause some
unexpected errors.

This doesn't mean we should never support it, just that the codebase is
not currently capable of supporting it.

Signed-off-by: Brian Goff <[email protected]>
  • Loading branch information
cpuguy83 committed Feb 14, 2024
1 parent 6a05e92 commit 140e400
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion files.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (s *SourceInlineDir) validate() error {

for k, f := range s.Files {
if strings.ContainsRune(k, os.PathSeparator) {
errs = append(errs, errors.Errorf("file name %q must not contain path separator", k))
errs = append(errs, errors.Wrapf(sourceNamePathSeparatorError, "file %q", k))
}
if err := f.validate(); err != nil {
errs = append(errs, errors.Wrapf(err, "file %q", k))
Expand Down
8 changes: 6 additions & 2 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dalec
import (
goerrors "errors"
"fmt"
"os"
"path"
"strings"

Expand Down Expand Up @@ -363,14 +364,17 @@ func (s *Spec) FillDefaults() {

func (s Spec) Validate() error {
for name, src := range s.Sources {
if strings.ContainsRune(name, os.PathSeparator) {
return &InvalidSourceError{Name: name, Err: sourceNamePathSeparatorError}
}
if err := src.validate(); err != nil {
return fmt.Errorf("error validating source ref %q: %w", name, err)
return &InvalidSourceError{Name: name, Err: fmt.Errorf("error validating source ref %q: %w", name, err)}
}

if src.DockerImage != nil && src.DockerImage.Cmd != nil {
for p, cfg := range src.DockerImage.Cmd.CacheDirs {
if _, err := sharingMode(cfg.Mode); err != nil {
return errors.Wrapf(err, "invalid sharing mode for source %q with cache mount at path %q", name, p)
return &InvalidSourceError{Name: name, Err: errors.Wrapf(err, "invalid sharing mode for source %q with cache mount at path %q", name, p)}
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dalec

import (
"encoding/json"
"errors"
"fmt"
"os"
"reflect"
Expand Down Expand Up @@ -383,3 +384,33 @@ sources:
})
}
}

func TestSourceNameWithPathSeparator(t *testing.T) {
spec := &Spec{
Sources: map[string]Source{
"forbidden/name": {
Inline: &SourceInline{
File: &SourceInlineFile{},
},
},
},
}

err := spec.Validate()
if err == nil {
t.Fatal("expected error, but received none")
}

var expected *InvalidSourceError
if !errors.As(err, &expected) {
t.Fatalf("expected %T, got %T", expected, err)
}

if expected.Name != "forbidden/name" {
t.Error("expected error to contain source name")
}

if !errors.Is(err, sourceNamePathSeparatorError) {
t.Errorf("expected error to be sourceNamePathSeparatorError, got: %v", err)
}
}
16 changes: 16 additions & 0 deletions source.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ import (
"github.com/pkg/errors"
)

// InvalidSourceError is an error type returned when a source is invalid.
type InvalidSourceError struct {
Name string
Err error
}

func (s *InvalidSourceError) Error() string {
return fmt.Sprintf("invalid source %s: %v", s.Name, s.Err)
}

func (s *InvalidSourceError) Unwrap() error {
return s.Err
}

var sourceNamePathSeparatorError = errors.New("source name must not container path separator")

type LLBGetter func(sOpts SourceOpts, opts ...llb.ConstraintsOpt) (llb.State, error)

type ForwarderFunc func(llb.State, *SourceBuild) (llb.State, error)
Expand Down

0 comments on commit 140e400

Please sign in to comment.