Fix: reject non-revision names in ExtractRevision (#7210)

ExtractRevision parsed the last hyphen segment as the revision number
without validating the name shape, so a bareword like "5" returned
5,nil and any name lacking a trailing v-prefixed segment silently
mis-parsed instead of erroring. Mirror the existing ExtractRevisionNum
guards: error with ErrBadRevision when there is no delimiter or the
last segment is not v-prefixed. Extend the colocated bad-name test
cases accordingly.

Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
This commit is contained in:
Anas Khan
2026-07-14 20:56:51 +05:30
committed by GitHub
parent 1ba52548ed
commit 2f401979c4
2 changed files with 10 additions and 1 deletions

View File

@@ -17,6 +17,7 @@ limitations under the License.
package utils
import (
"errors"
"fmt"
"strconv"
"strings"
@@ -53,6 +54,14 @@ var ExtractComponentName = util.ExtractComponentName
// ExtractRevision will extract the revision from a revisionName
func ExtractRevision(revisionName string) (int, error) {
splits := strings.Split(revisionName, "-")
// check some bad revision name, eg: 5
if len(splits) == 1 {
return 0, errors.New(util.ErrBadRevision)
}
// check some bad revision name, eg: myapp-a1
if !strings.HasPrefix(splits[len(splits)-1], "v") {
return 0, errors.New(util.ErrBadRevision)
}
// the revision is the last string without the prefix "v"
return strconv.Atoi(strings.TrimPrefix(splits[len(splits)-1], "v"))
}

View File

@@ -42,7 +42,7 @@ func TestConstructExtract(t *testing.T) {
}
})
}
badRevision := []string{"xx", "yy-", "zz-0.1"}
badRevision := []string{"xx", "yy-", "zz-0.1", "5", "myapp-a1"}
t.Run(fmt.Sprintf("tests %s for extractRevision", badRevision), func(t *testing.T) {
for _, revisionName := range badRevision {
_, err := ExtractRevision(revisionName)