From 5ec367b2a1830d04d39459e6dd7023e996624165 Mon Sep 17 00:00:00 2001 From: Klaus Deissner Date: Tue, 13 Apr 2021 18:39:53 +0200 Subject: [PATCH] By default, do not restrict the length of extension attribute names Signed-off-by: Klaus Deissner --- v2/event/eventcontext_v1.go | 5 ++--- v2/event/extensions.go | 25 ++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/v2/event/eventcontext_v1.go b/v2/event/eventcontext_v1.go index 01f1774eb..6695e1509 100644 --- a/v2/event/eventcontext_v1.go +++ b/v2/event/eventcontext_v1.go @@ -1,7 +1,6 @@ package event import ( - "errors" "fmt" "mime" "sort" @@ -72,8 +71,8 @@ func (ec EventContextV1) ExtensionAs(name string, obj interface{}) error { // SetExtension adds the extension 'name' with value 'value' to the CloudEvents context. // This function fails if the name doesn't respect the regex ^[a-zA-Z0-9]+$ func (ec *EventContextV1) SetExtension(name string, value interface{}) error { - if !IsExtensionNameValid(name) { - return errors.New("bad key, CloudEvents attribute names MUST consist of lower-case letters ('a' to 'z') or digits ('0' to '9') from the ASCII character set") + if err := validateExtensionName(name); err != nil { + return err } name = strings.ToLower(name) diff --git a/v2/event/extensions.go b/v2/event/extensions.go index ff19f5cb2..3d0210fb0 100644 --- a/v2/event/extensions.go +++ b/v2/event/extensions.go @@ -1,6 +1,8 @@ package event import ( + "errors" + "fmt" "strings" ) @@ -10,6 +12,12 @@ const ( DataContentEncodingKey = "datacontentencoding" ) +var ( + // This determines the behavior of validateExtensionName(). For MaxExtensionNameLength > 0, an error will be returned, + // if len(key) > MaxExtensionNameLength + MaxExtensionNameLength = 0 +) + func caseInsensitiveSearch(key string, space map[string]interface{}) (interface{}, bool) { lkey := strings.ToLower(key) for k, v := range space { @@ -21,13 +29,24 @@ func caseInsensitiveSearch(key string, space map[string]interface{}) (interface{ } func IsExtensionNameValid(key string) bool { - if len(key) < 1 || len(key) > 20 { + if err := validateExtensionName(key); err != nil { return false } + return true +} + +func validateExtensionName(key string) error { + if len(key) < 1 { + return errors.New("bad key, CloudEvents attribute names MUST NOT be empty") + } + if MaxExtensionNameLength > 0 && len(key) > MaxExtensionNameLength { + return fmt.Errorf("bad key, CloudEvents attribute name '%s' is longer than %d characters", key, MaxExtensionNameLength) + } + for _, c := range key { if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) { - return false + return errors.New("bad key, CloudEvents attribute names MUST consist of lower-case letters ('a' to 'z') or digits ('0' to '9') from the ASCII character set") } } - return true + return nil }