Skip to content

Commit

Permalink
fix: Validate that source is non-empty
Browse files Browse the repository at this point in the history
URI references can be empty - we have a test proving that. But the source attribute is required to be non-empty, so we should validate that.

This bug fix is a breaking change for anyone who was previously creating (and then validating) CloudEvent instances with a present-but-empty source.
However, their events were already broken, and we'd expect them to cause issues with other consumers (at least any performing comprehensive validation).

Signed-off-by: Jon Skeet <jonskeet@google.com>
  • Loading branch information
jskeet committed Feb 1, 2022
1 parent c4a938c commit bf27ac4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/CloudNative.CloudEvents/CloudEventsSpecVersion.cs
Expand Up @@ -38,10 +38,10 @@ public sealed class CloudEventsSpecVersion
public static CloudEventsSpecVersion V1_0 { get; } = new CloudEventsSpecVersion(
"1.0",
CreateRequired("id", CloudEventAttributeType.String, NonEmptyString),
CreateRequired("source", CloudEventAttributeType.UriReference, NonEmptyUri),
CreateRequired("source", CloudEventAttributeType.UriReference, NonEmptyUriReference),
CreateRequired("type", CloudEventAttributeType.String, NonEmptyString),
CreateOptional("datacontenttype", CloudEventAttributeType.String, Rfc2046String), // TODO: Do we want some way of adding validation that this is per RFC 2046?
CreateOptional("dataschema", CloudEventAttributeType.Uri, NonEmptyUri),
CreateOptional("datacontenttype", CloudEventAttributeType.String, Rfc2046String),
CreateOptional("dataschema", CloudEventAttributeType.Uri, NonEmptyUriReference),
CreateOptional("subject", CloudEventAttributeType.String, NonEmptyString),
CreateOptional("time", CloudEventAttributeType.Timestamp, null));

Expand Down Expand Up @@ -166,9 +166,13 @@ private static void NonEmptyString(object value)
}
}

private static void NonEmptyUri(object value)
private static void NonEmptyUriReference(object value)
{
// TODO: is this actually useful?
Uri uri = (Uri) value;
if (uri.OriginalString.Length == 0)
{
throw new ArgumentException("URI reference must be non-empty");
}
}

private static void Rfc2046String(object value)
Expand Down
Expand Up @@ -26,5 +26,13 @@ public void FromVersionId_Known(string versionId)
Assert.NotNull(version);
Assert.Equal(versionId, version!.VersionId);
}

[Fact]
public void V1Source_MustBeNonEmpty()
{
var attribute = CloudEventsSpecVersion.V1_0.SourceAttribute;
var uri = new Uri("", UriKind.RelativeOrAbsolute);
Assert.Throws<ArgumentException>(() => attribute.Validate(uri));
}
}
}

0 comments on commit bf27ac4

Please sign in to comment.