Skip to content

Commit

Permalink
Add error message if protofile is attempted to be read or written fro…
Browse files Browse the repository at this point in the history
…m stdio (#2095)
  • Loading branch information
bufdev committed May 16, 2023
1 parent f44f755 commit 04faf60
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
8 changes: 8 additions & 0 deletions private/buf/buffetch/internal/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ func NewInvalidPathError(format string, path string) error {
return fmt.Errorf("invalid %spath: %q", format, path)
}

// NewProtoFileCannotBeDevPathError is a fetch error.
func NewProtoFileCannotBeDevPathError(format string, path string) error {
if format != "" {
format = format + " "
}
return fmt.Errorf("invalid %spath: %q (protofiles cannot be read or written to or from stdio)", format, path)
}

// NewRealCleanPathError is a fetch error.
func NewRealCleanPathError(path string) error {
return fmt.Errorf("could not clean relative path %q", path)
Expand Down
9 changes: 7 additions & 2 deletions private/buf/buffetch/internal/proto_file_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package internal

import "github.com/bufbuild/buf/private/pkg/app"

var (
_ ParsedProtoFileRef = &protoFileRef{}
)
Expand All @@ -24,12 +26,15 @@ type protoFileRef struct {
includePackageFiles bool
}

func newProtoFileRef(format string, path string, includePackageFiles bool) *protoFileRef {
func newProtoFileRef(format string, path string, includePackageFiles bool) (*protoFileRef, error) {
if app.IsDevPath(path) || path == "-" {
return nil, NewProtoFileCannotBeDevPathError(format, path)
}
return &protoFileRef{
format: format,
path: path,
includePackageFiles: includePackageFiles,
}
}, nil
}

func (s *protoFileRef) Format() string {
Expand Down
4 changes: 2 additions & 2 deletions private/buf/buffetch/internal/ref_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (a *refParser) getParsedRef(
return getArchiveRef(rawRef, archiveFormatInfo.archiveType, archiveFormatInfo.defaultCompressionType)
}
if protoFileOK {
return getProtoFileRef(rawRef), nil
return getProtoFileRef(rawRef)
}
if dirOK {
return getDirRef(rawRef)
Expand Down Expand Up @@ -342,7 +342,7 @@ func getGitRefName(path string, branch string, tag string, ref string) (git.Name
return git.NewTagName(tag), nil
}

func getProtoFileRef(rawRef *RawRef) ParsedProtoFileRef {
func getProtoFileRef(rawRef *RawRef) (ParsedProtoFileRef, error) {
return newProtoFileRef(
rawRef.Format,
rawRef.Path,
Expand Down
3 changes: 3 additions & 0 deletions private/buf/bufwire/image_config_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ func filterImageConfigs(imageConfigs []ImageConfig, protoFileRef buffetch.ProtoF
}
images = append(images, imageConfig.Image())
}
if path == "" {
return nil, errors.New("did not find a matching image file for the ProtoFileRef")
}
image, err := bufimage.MergeImages(images...)
if err != nil {
return nil, err
Expand Down
6 changes: 6 additions & 0 deletions private/pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ func IsDevNull(path string) bool {
return path != "" && path == DevNullFilePath
}

// IsDevPath returns true if the path is the equivalent of /dev/stdin, /dev/stdout,
// /dev/stderr, or /dev/null.
func IsDevPath(path string) bool {
return IsDevStdin(path) || IsDevStdout(path) || IsDevStderr(path) || IsDevNull(path)
}

// Main runs the application using the OS Container and calling os.Exit on the return value of Run.
func Main(ctx context.Context, f func(context.Context, Container) error) {
container, err := NewContainerForOS()
Expand Down

0 comments on commit 04faf60

Please sign in to comment.