Skip to content

Should throw if specversion attribute is missing #332

New issue

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

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

Already on GitHub? # to your account

Closed
satazor opened this issue Sep 2, 2020 · 14 comments · Fixed by #419
Closed

Should throw if specversion attribute is missing #332

satazor opened this issue Sep 2, 2020 · 14 comments · Fixed by #419
Assignees
Labels
module/lib Related to the main source code module/transport/http Issues related to the HTTP transport protocol implementation

Comments

@satazor
Copy link

satazor commented Sep 2, 2020

Describe the Bug

Does not throw if event doesn't have a specversion attribute, and fallbacks to 1.0. The spec says that the specversion attribute is mandatory, so I think this library should fail when it's missing, instead of "magically" falling back.

Steps to Reproduce

  1. Receiver.accept({ 'Content-Type': 'application/cloudevents+json' }, { id: 'foo', source: 'foo', type: 'foo' });
  2. Prints console.error saying it will fallback to 1.0
  3. Validation passes

Expected Behavior
It should throw, since the specversion attribute is missing

@satazor
Copy link
Author

satazor commented Sep 2, 2020

@lance lance added module/lib Related to the main source code module/transport/http Issues related to the HTTP transport protocol implementation labels Sep 2, 2020
@lance
Copy link
Member

lance commented Sep 2, 2020

@satazor thanks for pointing this out. I will look into dealing with this in #328

@lance lance self-assigned this Sep 2, 2020
@lance
Copy link
Member

lance commented Sep 3, 2020

@satazor in this PR, there was discussion around loose validation on receipt of a CloudEvent. Based on input from the community, noted in that comment, receipt of a CloudEvent should have loose validation, since it should be possible to receive and recover from poorly formed/invalid events. The example you provide is when receiving events on the wire - just such a scenario. If you would like to check whether the received event is valid, you can call CloudEvent#validate().

That method returns a boolean. I think it might be nice to see what the validation errors actually were, and we don't really have a convenience function for that. In any case, this is expected behavior.

@lance
Copy link
Member

lance commented Sep 3, 2020

Oh - but I see the problem here. Since it's defaulting the specversion, property, by the time it's unmarshalled as a CloudEvent, it is valid, so calling ce.validate() won't help.

@lance
Copy link
Member

lance commented Sep 3, 2020

The other required attribute that we supply a default for if it's not provided is id. The same would happen for this.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 4, 2020

This issue is stale because it has been open 30 days with no activity.

@lholmquist
Copy link
Contributor

I know this has been open for a while, but the idea is that we shouldn't be defaulting the specversion and id fields?

This would be a good one to get into the 4.0 release. i can assign myself to take of it

@lance
Copy link
Member

lance commented Oct 7, 2020

I know this has been open for a while, but the idea is that we shouldn't be defaulting the specversion and id fields?

I think defaulting them on creation with new CloudEvent() is fine. But when receiving events, we should not just assume that they are v1.0. That's what we are doing here.

if (version !== Version.V03 && version !== Version.V1) {
console.error(`Unknown spec version ${version}. Default to ${Version.V1}`);
version = Version.V1;

Throwing here would also remove that console.error() and resolve #333

@lholmquist
Copy link
Contributor

@lance that sounds good. i'll take of it

@lholmquist lholmquist assigned lholmquist and unassigned lance Oct 7, 2020
lholmquist added a commit to lholmquist/sdk-javascript that referenced this issue Oct 7, 2020
* When receiving an event,  we should not default the specversion to 1.0 if no specverion field is detected.
* If no specversion is detected or the specversion does not conform to a currently supported version, we will now throw a ValidationError

This is a BREAKING_CHANGE

fixes cloudevents#332, cloudevents#333

Signed-off-by: Lucas Holmquist <lholmqui@redhat.com>
lholmquist added a commit to lholmquist/sdk-javascript that referenced this issue Oct 7, 2020
* When receiving an event,  we should not default the specversion to 1.0 if no specverion field is detected.
* If no specversion is detected or the specversion does not conform to a currently supported version, we will now throw a ValidationError

This is a BREAKING_CHANGE

fixes cloudevents#332, cloudevents#333

Signed-off-by: Lucas Holmquist <lholmqui@redhat.com>
@c-pius
Copy link

c-pius commented Feb 4, 2021

I think defaulting them on creation with new CloudEvent() is fine. But when receiving events, we should not just assume that they are v1.0. That's what we are doing here.

I think this should also be the same for the id attribute, right?

As of now, HTTP.toEvent() will also assign a new uuidv4 to an event if passed in structured mode with no id present. This time even implicitly without a log message that it was defaulted.


parseStructured will call the CloudEvent constructor with eventObj missing the id attribute and generate it.

return new CloudEvent(eventObj as CloudEventV1 | CloudEventV03, false);

this.id = (properties.id as string) || uuidv4();


PS: the same would also happen with the time attribute. Maybe less important though because not a required attribute.

@c-pius
Copy link

c-pius commented Feb 11, 2021

FYI, there is another effect that when calling HTTP.toEvent with a structured mode event and a specversion value different from 1.0 or 0.3 (e.g. 2), than the defaulting here is applied:

if (version !== Version.V03 && version !== Version.V1) {

but the parsed event is actually getting the original value (e.g. 2) assigned by this block:

for (const key in parserMap) {


in binary mode this is prevented by this block:

no sure if therefore this block for structured should use the body attributes instead (I think sending ce-header even though structured mode is used is valid as long as the context attributes are present in the body)?

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity.

@lance
Copy link
Member

lance commented May 17, 2021

@lholmquist ping - were you still thinking you'd take this? If not, I'll pick it up. Running into this as an issue now with function runtime.

lance added a commit to lance/faas-js-runtime that referenced this issue May 17, 2021
This commit adds type definitions for the following:
  * `Invokable`: The user provided function
  * `Context`: The invocation context
  * `CloudEventResponse`: Convenience class for constructing events
  * `Logger`: The logging interface
  * `LogLevel`: Logging enumeration
  * `start`: A function to start the invoker
  * `InvokerOptions`: Options for `start`

The definitions are statically tested with `tsd`.

These type definitions are not actually used by the JS files. This does not
convert the module to TypeScript; it just adds these type definitions to our
exports. This will allow us to create TypeScript function templates without
a full conversion of this code base to TypeScript. Ultimately, I think it's
probably best to make the change.

This change also bumps to `cloudevents@4.0.2` which does have a breaking
change. See: cloudevents/sdk-javascript#332
As a result, we accept all versions of CloudEvents. Actually, I think this
is probably a fine change in the long run. Loose validation upon receipt
of an event allows functions to handle event sources that may be producing
malformed events. I've commented out the test for now. If we decide it makes
sense to leave this as-is instead of waiting for a fix from upstream, then
we should remove the test and the existing logic for dealing with this,
since it's no longer necessary.

Finally, I've really simplified the `start` funtion interface. It now just
takes a function and a bag of options. It doesn't deal with callbacks anymore.
Promises are used to deal with async server startup.

Signed-off-by: Lance Ball <lball@redhat.com>
lance added a commit to lance/faas-js-runtime that referenced this issue May 17, 2021
This commit adds type definitions for the following:
  * `Invokable`: The user provided function
  * `Context`: The invocation context
  * `CloudEventResponse`: Convenience class for constructing events
  * `Logger`: The logging interface
  * `LogLevel`: Logging enumeration
  * `start`: A function to start the invoker
  * `InvokerOptions`: Options for `start`

The definitions are statically tested with `tsd`.

These type definitions are not actually used by the JS files. This does not
convert the module to TypeScript; it just adds these type definitions to our
exports. This will allow us to create TypeScript function templates without
a full conversion of this code base to TypeScript. Ultimately, I think it's
probably best to make the change.

This change also bumps to `cloudevents@4.0.2` which does have a breaking
change. See: cloudevents/sdk-javascript#332
As a result, we accept all versions of CloudEvents. Actually, I think this
is probably a fine change in the long run. Loose validation upon receipt
of an event allows functions to handle event sources that may be producing
malformed events. I've commented out the test for now. If we decide it makes
sense to leave this as-is instead of waiting for a fix from upstream, then
we should remove the test and the existing logic for dealing with this,
since it's no longer necessary.

Finally, I've really simplified the `start` funtion interface. It now just
takes a function and a bag of options. It doesn't deal with callbacks anymore.
Promises are used to deal with async server startup.

Signed-off-by: Lance Ball <lball@redhat.com>
@lholmquist
Copy link
Contributor

@lance it isn't on my radar atm, so feel free to pick it up

lance added a commit to lance/sdk-javascript that referenced this issue May 21, 2021
Even if the specversion is totally invalid, we should not change the value
received in an incoming `Message`. Previously we defaulted to 1.0 if we did
not recognize the version number. This commit changes that, leaving the value
unmodified. We default to parsing this mystery event with the 1.0 spec. When
the event is validated with `event.validate()` we return `false`.

One additional small change to eliminate a prettier warning about `parer`
being previously declared.

Fixes: cloudevents#332
Fixes: cloudevents#333

Signed-off-by: Lance Ball <lball@redhat.com>
lance added a commit that referenced this issue May 25, 2021
Even if the specversion is totally invalid, we should not change the value
received in an incoming `Message`. Previously we defaulted to 1.0 if we did
not recognize the version number. This commit changes that, leaving the value
unmodified. We default to parsing this mystery event with the 1.0 spec. When
the event is validated with `event.validate()` we return `false`.

One additional small change to eliminate a prettier warning about `parer`
being previously declared.

Fixes: #332
Fixes: #333

Signed-off-by: Lance Ball <lball@redhat.com>
lholmquist pushed a commit to lholmquist/sdk-javascript that referenced this issue Jul 6, 2021
Even if the specversion is totally invalid, we should not change the value
received in an incoming `Message`. Previously we defaulted to 1.0 if we did
not recognize the version number. This commit changes that, leaving the value
unmodified. We default to parsing this mystery event with the 1.0 spec. When
the event is validated with `event.validate()` we return `false`.

One additional small change to eliminate a prettier warning about `parer`
being previously declared.

Fixes: cloudevents#332
Fixes: cloudevents#333

Signed-off-by: Lance Ball <lball@redhat.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
module/lib Related to the main source code module/transport/http Issues related to the HTTP transport protocol implementation
Projects
None yet
4 participants