Skip to content

Commit e25a024

Browse files
authored
fix: reject reserved CloudEvents attributes passed to invoke -e (#4195)
Names such as id, type, and source were accepted as extensions and only failed later inside the CloudEvents SDK. Reject them while parsing -e and point at the flag that sets the attribute. Signed-off-by: Parameshwaran Krishnasamy <Parameshwaran.K@ibm.com>
1 parent 6ef861f commit e25a024

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

‎cmd/invoke.go‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,17 +318,38 @@ func newInvokeConfig() (cfg invokeConfig, err error) {
318318
// extensionName matches the CloudEvents spec restriction on attribute names.
319319
var extensionName = regexp.MustCompile(`^[a-z0-9]+$`)
320320

321+
// reservedCloudEventAttributes are CloudEvents context attribute names.
322+
// They cannot be set with --extension. The value is the invoke flag that
323+
// sets the attribute, when one exists.
324+
var reservedCloudEventAttributes = map[string]string{
325+
"id": "--id",
326+
"source": "--source",
327+
"type": "--type",
328+
"datacontenttype": "--content-type",
329+
"dataschema": "",
330+
"specversion": "",
331+
"subject": "",
332+
"time": "",
333+
}
334+
321335
func (c invokeConfig) parseExtensions() (map[string]string, error) {
322336
result := make(map[string]string)
323337
for _, ext := range c.Extensions {
324338
parts := strings.SplitN(ext, "=", 2)
325339
if len(parts) != 2 || strings.TrimSpace(parts[0]) == "" {
326340
return nil, fmt.Errorf("invalid --extension %q: must be in key=value format", ext)
327341
}
328-
if !extensionName.MatchString(parts[0]) {
342+
name := parts[0]
343+
if flag, reserved := reservedCloudEventAttributes[strings.ToLower(name)]; reserved {
344+
if flag == "" {
345+
return nil, fmt.Errorf("invalid --extension %q: %q is a reserved CloudEvents attribute", ext, name)
346+
}
347+
return nil, fmt.Errorf("invalid --extension %q: %q is a reserved CloudEvents attribute; set it with %s", ext, name, flag)
348+
}
349+
if !extensionName.MatchString(name) {
329350
return nil, fmt.Errorf("invalid --extension %q: name must consist of lower-case letters (a-z) or digits (0-9)", ext)
330351
}
331-
result[parts[0]] = parts[1]
352+
result[name] = parts[1]
332353
}
333354
return result, nil
334355
}

‎cmd/invoke_test.go‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,49 @@ func TestInvokeExtensionsRequireCloudEvent(t *testing.T) {
182182
}
183183
}
184184

185+
// TestInvokeExtensionsReservedAttributes ensures core CloudEvents attribute
186+
// names are rejected as extensions, and that the error points at the flag
187+
// which sets the attribute when one exists.
188+
func TestInvokeExtensionsReservedAttributes(t *testing.T) {
189+
cases := []struct {
190+
name string
191+
ext string
192+
flag string // invoke flag that sets this attribute, if any
193+
}{
194+
{"id", "id=abc", "--id"},
195+
{"type", "type=com.example", "--type"},
196+
{"source", "source=/example", "--source"},
197+
{"datacontenttype", "datacontenttype=text/plain", "--content-type"},
198+
{"specversion", "specversion=1.0", ""},
199+
{"subject", "subject=hello", ""},
200+
{"time", "time=2020-01-01T00:00:00Z", ""},
201+
{"dataschema", "dataschema=https://example.com/schema", ""},
202+
{"mixed case", "ID=abc", "--id"},
203+
}
204+
for _, tc := range cases {
205+
t.Run(tc.name, func(t *testing.T) {
206+
c := invokeConfig{Extensions: []string{"ok=1", tc.ext}}
207+
_, err := c.parseExtensions()
208+
if err == nil {
209+
t.Fatalf("expected error for %q", tc.ext)
210+
}
211+
msg := err.Error()
212+
if !strings.Contains(msg, "reserved CloudEvents attribute") {
213+
t.Fatalf("expected reserved-attribute error, got %v", err)
214+
}
215+
if tc.flag == "" {
216+
if strings.Contains(msg, "set it with") {
217+
t.Fatalf("expected no flag hint, got %v", err)
218+
}
219+
return
220+
}
221+
if !strings.Contains(msg, tc.flag) {
222+
t.Fatalf("expected error to mention %s, got %v", tc.flag, err)
223+
}
224+
})
225+
}
226+
}
227+
185228
// TestInvoke_WrapsNotInitialized ensures invoke wraps uninitialized errors
186229
// through the CLI error wrapping layer instead of inline fmt.Errorf.
187230
func TestInvoke_WrapsNotInitialized(t *testing.T) {

0 commit comments

Comments
 (0)