Skip to content

Commit 99fce71

Browse files
committed
fix: add validation for containerID
Signed-off-by: karthik balasubramanian <karthikbalasubramanian08@gmail.com>
1 parent f6ddb03 commit 99fce71

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

cmd/urunc/create.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ func createUnikontainer(cmd *cli.Command, uruncCfg *unikontainers.UruncConfig) (
9494
err = fmt.Errorf("container id cannot be empty")
9595
return err
9696
}
97+
if err = validateID(containerID); err != nil {
98+
return err
99+
}
97100
metrics.SetLoggerContainerID(containerID)
98101
metrics.Capture(m.TS00)
99102

cmd/urunc/utils.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os"
2222
"os/exec"
2323
"syscall"
24+
"path/filepath"
2425

2526
"github.com/moby/sys/userns"
2627
"github.com/sirupsen/logrus"
@@ -37,6 +38,7 @@ const (
3738
)
3839

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

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

7681
// We have already made sure in main.go that root is not nil
7782
rootDir := cmd.String("root")
@@ -160,3 +165,33 @@ func prepareXDGRuntimeDir(root string) error {
160165
}
161166
return nil
162167
}
168+
169+
// validateID validates the given ID string against the allowed characters.
170+
func validateID(id string) error {
171+
if len(id) < 1 {
172+
return ErrInvalidID
173+
}
174+
175+
// Allowed characters: 0-9 A-Z a-z _ + - .
176+
for i := range len(id) {
177+
c := id[i]
178+
switch {
179+
case c >= 'a' && c <= 'z':
180+
case c >= 'A' && c <= 'Z':
181+
case c >= '0' && c <= '9':
182+
case c == '_':
183+
case c == '+':
184+
case c == '-':
185+
case c == '.':
186+
default:
187+
return ErrInvalidID
188+
}
189+
190+
}
191+
192+
if string(os.PathSeparator)+id != filepath.Clean(string(os.PathSeparator)+id) {
193+
return ErrInvalidID
194+
}
195+
196+
return nil
197+
}

0 commit comments

Comments
 (0)