@@ -1084,8 +1084,6 @@ func fetchExtraStages(stages []config.KanikoStage, opts *config.KanikoOptions) e
10841084 t := timing .Start ("Fetching Extra Stages" )
10851085 defer timing .DefaultRun .Stop (t )
10861086
1087- var names []string
1088-
10891087 for _ , s := range stages {
10901088 for _ , cmd := range s .Commands {
10911089 c , ok := cmd .(* instructions.CopyCommand )
@@ -1094,14 +1092,13 @@ func fetchExtraStages(stages []config.KanikoStage, opts *config.KanikoOptions) e
10941092 }
10951093
10961094 // FROMs at this point are guaranteed to be either an integer referring to a previous stage,
1097- // the name of a previous stage, or a name of a remote image.
1095+ // or a name of a remote image.
10981096
1099- // If it is an integer stage index, validate that it is actually a previous index
1100- if fromIndex , err := strconv .Atoi (c .From ); err == nil && s .Index > fromIndex && fromIndex >= 0 {
1101- continue
1102- }
1103- // Check if the name is the alias of a previous stage
1104- if fromPreviousStage (c , names ) {
1097+ if fromIndex , err := strconv .Atoi (c .From ); err == nil {
1098+ // If it is an integer stage index, validate that it is actually a previous index
1099+ if s .Index <= fromIndex || fromIndex < 0 {
1100+ return fmt .Errorf ("%s refers to invalid stage: %d" , c .String (), fromIndex )
1101+ }
11051102 continue
11061103 }
11071104
@@ -1118,23 +1115,10 @@ func fetchExtraStages(stages []config.KanikoStage, opts *config.KanikoOptions) e
11181115 return err
11191116 }
11201117 }
1121- // Store the name of the current stage in the list with names, if applicable.
1122- if s .Name != "" {
1123- names = append (names , s .Name )
1124- }
11251118 }
11261119 return nil
11271120}
11281121
1129- func fromPreviousStage (copyCommand * instructions.CopyCommand , previousStageNames []string ) bool {
1130- for _ , previousStageName := range previousStageNames {
1131- if previousStageName == copyCommand .From {
1132- return true
1133- }
1134- }
1135- return false
1136- }
1137-
11381122func extractImageToDependencyDir (name string , image v1.Image ) error {
11391123 t := timing .Start ("Extracting Image to Dependency Dir" )
11401124 defer timing .DefaultRun .Stop (t )
@@ -1214,7 +1198,6 @@ func ResolveCrossStageInstructions(stages []config.KanikoStage) map[string]int {
12141198 if stage .Name != "" {
12151199 nameToIndex [stage .Name ] = stage .Index
12161200 }
1217- dockerfile .ResolveCrossStageCommands (stage .Commands , nameToIndex )
12181201 }
12191202
12201203 logrus .Debugf ("Built stage name to index map: %v" , nameToIndex )
0 commit comments