Skip to content

Commit ab5b823

Browse files
committed
Hide regex matched sensitive values in validation errors
1 parent 14b594d commit ab5b823

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

pkg/apis/aws/validation/filter.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,26 @@ var (
5252
validateTagKey = combineValidationFuncs(regex(TagKeyRegex), notEmpty, maxLength(128))
5353
validateCapacityReservationID = combineValidationFuncs(regex(CapacityReservationIDRegex), notEmpty, maxLength(255))
5454
validateCapacityReservationGroup = combineValidationFuncs(regex(CapacityReservationGroupRegex), notEmpty, maxLength(255))
55-
validateAccessKeyID = combineValidationFuncs(regex(AccessKeyIDRegex), minLength(20), maxLength(20))
56-
validateSecretAccessKey = combineValidationFuncs(regex(SecretAccessKeyRegex), minLength(40), maxLength(40))
55+
validateAccessKeyID = hideSensitiveValue(combineValidationFuncs(regex(AccessKeyIDRegex), minLength(20), maxLength(20)))
56+
validateSecretAccessKey = hideSensitiveValue(combineValidationFuncs(regex(SecretAccessKeyRegex), minLength(40), maxLength(40)))
5757
)
5858

5959
type validateFunc[T any] func(T, *field.Path) field.ErrorList
6060

61+
// hideSensitiveValue wraps a validation function to hide the actual value in error messages
62+
func hideSensitiveValue(fn validateFunc[string]) validateFunc[string] {
63+
return func(value string, fld *field.Path) field.ErrorList {
64+
errs := fn(value, fld)
65+
// Replace the actual value with "(hidden)" in all error messages
66+
for i := range errs {
67+
if errs[i].Type == field.ErrorTypeInvalid || errs[i].Type == field.ErrorTypeRequired {
68+
errs[i].BadValue = "(hidden)"
69+
}
70+
}
71+
return errs
72+
}
73+
}
74+
6175
// combineValidationFuncs validates a value against a list of filters.
6276
func combineValidationFuncs[T any](filters ...validateFunc[T]) validateFunc[T] {
6377
return func(t T, fld *field.Path) field.ErrorList {

0 commit comments

Comments
 (0)