Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/urunc/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ func createUnikontainer(cmd *cli.Command, uruncCfg *unikontainers.UruncConfig) (
err = fmt.Errorf("container id cannot be empty")
return err
}
if err = validateID(containerID); err != nil {
return err
}
metrics.SetLoggerContainerID(containerID)
metrics.Capture(m.TS00)

Expand Down
35 changes: 35 additions & 0 deletions cmd/urunc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"os/exec"
"syscall"
"path/filepath"

"github.com/moby/sys/userns"
"github.com/sirupsen/logrus"
Expand All @@ -37,6 +38,7 @@ const (
)

var ErrEmptyContainerID = errors.New("container ID can not be empty")
var ErrInvalidID = errors.New("invalid container id format")

// checkArgs checks the number of arguments provided in the command-line context
// against the expected number, based on the specified checkType.
Expand Down Expand Up @@ -72,6 +74,9 @@ func getUnikontainer(cmd *cli.Command) (*unikontainers.Unikontainer, error) {
if containerID == "" {
return nil, ErrEmptyContainerID
}
if err := validateID(containerID); err != nil {
return nil, err
}

// We have already made sure in main.go that root is not nil
rootDir := cmd.String("root")
Expand Down Expand Up @@ -160,3 +165,33 @@ func prepareXDGRuntimeDir(root string) error {
}
return nil
}

// validateID validates the given ID string against the allowed characters.
func validateID(id string) error {
if len(id) < 1 {
return ErrInvalidID
}

// Allowed characters: 0-9 A-Z a-z _ + - .
for i := range len(id) {
c := id[i]
switch {
case c >= 'a' && c <= 'z':
case c >= 'A' && c <= 'Z':
case c >= '0' && c <= '9':
case c == '_':
case c == '+':
case c == '-':
case c == '.':
default:
return ErrInvalidID
}

}

if string(os.PathSeparator)+id != filepath.Clean(string(os.PathSeparator)+id) {
return ErrInvalidID
}

return nil
}