Skip to content

Commit bf27ac4

Browse files
committed
fix: Validate that source is non-empty
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 <[email protected]>
1 parent c4a938c commit bf27ac4

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/CloudNative.CloudEvents/CloudEventsSpecVersion.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public sealed class CloudEventsSpecVersion
3838
public static CloudEventsSpecVersion V1_0 { get; } = new CloudEventsSpecVersion(
3939
"1.0",
4040
CreateRequired("id", CloudEventAttributeType.String, NonEmptyString),
41-
CreateRequired("source", CloudEventAttributeType.UriReference, NonEmptyUri),
41+
CreateRequired("source", CloudEventAttributeType.UriReference, NonEmptyUriReference),
4242
CreateRequired("type", CloudEventAttributeType.String, NonEmptyString),
43-
CreateOptional("datacontenttype", CloudEventAttributeType.String, Rfc2046String), // TODO: Do we want some way of adding validation that this is per RFC 2046?
44-
CreateOptional("dataschema", CloudEventAttributeType.Uri, NonEmptyUri),
43+
CreateOptional("datacontenttype", CloudEventAttributeType.String, Rfc2046String),
44+
CreateOptional("dataschema", CloudEventAttributeType.Uri, NonEmptyUriReference),
4545
CreateOptional("subject", CloudEventAttributeType.String, NonEmptyString),
4646
CreateOptional("time", CloudEventAttributeType.Timestamp, null));
4747

@@ -166,9 +166,13 @@ private static void NonEmptyString(object value)
166166
}
167167
}
168168

169-
private static void NonEmptyUri(object value)
169+
private static void NonEmptyUriReference(object value)
170170
{
171-
// TODO: is this actually useful?
171+
Uri uri = (Uri) value;
172+
if (uri.OriginalString.Length == 0)
173+
{
174+
throw new ArgumentException("URI reference must be non-empty");
175+
}
172176
}
173177

174178
private static void Rfc2046String(object value)

test/CloudNative.CloudEvents.UnitTests/CloudEventsSpecVersionTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,13 @@ public void FromVersionId_Known(string versionId)
2626
Assert.NotNull(version);
2727
Assert.Equal(versionId, version!.VersionId);
2828
}
29+
30+
[Fact]
31+
public void V1Source_MustBeNonEmpty()
32+
{
33+
var attribute = CloudEventsSpecVersion.V1_0.SourceAttribute;
34+
var uri = new Uri("", UriKind.RelativeOrAbsolute);
35+
Assert.Throws<ArgumentException>(() => attribute.Validate(uri));
36+
}
2937
}
3038
}

0 commit comments

Comments
 (0)