Skip to content

Commit 1ea6aa0

Browse files
committed
Ensure base64 string validation works
1 parent fe014d2 commit 1ea6aa0

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

inventory/internal/store/util.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ var MetadataPatternKey = regexp.MustCompile(
6565
// MetadataPatternValue representing the metadata pattern for value.
6666
var MetadataPatternValue = regexp.MustCompile("^$|^[a-zA-Z0-9]$|^[a-zA-Z0-9][a-zA-Z0-9+._-]*[a-zA-Z0-9]$")
6767

68+
// Base64MedatadaPatternValue representing the metadata pattern for value when the value is expected to be a base64 encoded string.
69+
var Base64MedatadaPatternValue = regexp.MustCompile("^[A-Za-z0-9+/]*={0,2}$")
70+
6871
// Metadata struct representing the JSON metadata.
6972
type Metadata struct {
7073
Key string `json:"key"`
@@ -139,7 +142,10 @@ func validateKeyValue(meta []Metadata) error {
139142
if len(rmetadata.Value) > MetadataValueMaxLength {
140143
return errors.Errorfc(codes.InvalidArgument, "Invalid length of metadata value")
141144
}
142-
if !MetadataPatternValue.MatchString(rmetadata.Value) {
145+
146+
// check if string matches MetadataPatternValue or a base64 encoded string that matches Base64MedatadaPatternValue,
147+
// as some of the metadata values can be base64 encoded, we want to allow those as well.
148+
if !(MetadataPatternValue.MatchString(rmetadata.Value) || Base64MedatadaPatternValue.MatchString(rmetadata.Value)) {
143149
return errors.Errorfc(codes.InvalidArgument, "Invalid metadata value")
144150
}
145151
}

0 commit comments

Comments
 (0)