@@ -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
3940var 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