@@ -427,8 +427,7 @@ func Run(ctx context.Context, options Options) error {
427
427
428
428
// It's possible that the container will already have files in it, and
429
429
// we don't want to merge a new container with the old one.
430
- err = util .DeleteFilesystem ()
431
- if err != nil {
430
+ if err := maybeDeleteFilesystem (options .ForceSafe ); err != nil {
432
431
return nil , fmt .Errorf ("delete filesystem: %w" , err )
433
432
}
434
433
@@ -1063,3 +1062,27 @@ func findDevcontainerJSON(options Options) (string, string, error) {
1063
1062
1064
1063
return "" , "" , errors .New ("can't find devcontainer.json, is it a correct spec?" )
1065
1064
}
1065
+
1066
+ // maybeDeleteFilesystem wraps util.DeleteFilesystem with a guard to hopefully stop
1067
+ // folks from unwittingly deleting their entire root directory.
1068
+ func maybeDeleteFilesystem (force bool ) error {
1069
+ kanikoDir , ok := os .LookupEnv ("KANIKO_DIR" )
1070
+ if ! ok || strings .TrimSpace (kanikoDir ) != MagicDir {
1071
+ if force {
1072
+ bailoutSecs := 10
1073
+ _ , _ = fmt .Fprintln (os .Stderr , "WARNING! BYPASSING SAFETY CHECK! THIS WILL DELETE YOUR ROOT FILESYSTEM!" )
1074
+ _ , _ = fmt .Fprintf (os .Stderr , "You have %d seconds to bail out" , bailoutSecs )
1075
+ for i := 0 ; i < bailoutSecs ; i ++ {
1076
+ _ , _ = fmt .Fprintf (os .Stderr , "." )
1077
+ <- time .After (time .Second )
1078
+ }
1079
+ _ , _ = fmt .Fprintf (os .Stderr , "\n " )
1080
+ } else {
1081
+ _ , _ = fmt .Fprintf (os .Stderr , "KANIKO_DIR is not set to %s. Bailing!\n " , MagicDir )
1082
+ _ , _ = fmt .Fprintln (os .Stderr , "To bypass this check, set FORCE_SAFE=true." )
1083
+ return errors .New ("safety check failed" )
1084
+ }
1085
+ }
1086
+
1087
+ return util .DeleteFilesystem ()
1088
+ }
0 commit comments