Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

By default, do not restrict the length of extension attribute names #678

Merged
merged 1 commit into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions v2/event/eventcontext_v1.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package event

import (
"errors"
"fmt"
"mime"
"sort"
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 22 additions & 3 deletions v2/event/extensions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package event

import (
"errors"
"fmt"
"strings"
)

Expand All @@ -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 {
Expand All @@ -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
}