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

fix: disallow empty 'subject' header #679

Merged
merged 1 commit into from
Nov 4, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ protected static IllegalStateException createMissingAttributeException(String at
return new IllegalStateException("Attribute '" + attributeName + "' cannot be null");
}

protected static IllegalStateException createEmptyAttributeException(String attributeName) {
return new IllegalStateException("Attribute '" + attributeName + "' cannot be empty");
}

/**
* Validates the extension name as defined in CloudEvents spec.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ public CloudEventV03 build() {
if (type == null) {
throw createMissingAttributeException("type");
}
if (subject != null && subject.isEmpty()) {
throw createEmptyAttributeException(("subject"));
}

CloudEventV03 cloudEvent = new CloudEventV03(id, source, type, time, schemaurl, datacontenttype, subject, this.data, this.extensions);
final CloudEventValidatorProvider validator = CloudEventValidatorProvider.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public CloudEvent build() {
if (type == null) {
throw createMissingAttributeException(TYPE);
}
if (subject != null && subject.isEmpty()) {
throw createEmptyAttributeException(("subject"));
}

CloudEvent cloudEvent = new CloudEventV1(id, source, type, datacontenttype, dataschema, subject, time, this.data, this.extensions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,27 @@ void testMissingType() {
).hasMessageContaining("Attribute 'type' cannot be null");
}

@Test
void testMissingSubject() {
CloudEvent actual = CloudEventBuilder
.v03()
.withId("000")
.withSource(URI.create("http://localhost"))
.withType(TYPE)
.build();

assertThat(actual).isNotNull();
}

@Test
void testEmptySubject() {
assertThatCode(() -> CloudEventBuilder
.v03()
.withId("000")
.withSource(URI.create("http://localhost"))
.withType(TYPE)
.withSubject("")
.build()
).hasMessageContaining("Attribute 'subject' cannot be empty");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,27 @@ void testValidatorProvider(){
).hasMessageContaining("Expecting sales in namespace extension");
}

@Test
void testMissingSubject() {
CloudEvent actual = CloudEventBuilder
.v1()
.withId("000")
.withSource(URI.create("http://localhost"))
.withType(TYPE)
.build();

assertThat(actual).isNotNull();
}

@Test
void testEmptySubject() {
assertThatCode(() -> CloudEventBuilder
.v1()
.withId("000")
.withSource(URI.create("http://localhost"))
.withType(TYPE)
.withSubject("")
.build()
).hasMessageContaining("Attribute 'subject' cannot be empty");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void format() {
.withType("")
// optional
.withTime(Instant.EPOCH.atOffset(ZoneOffset.UTC))
.withSubject("")
.withSubject("subject")
.withDataSchema(URI.create(""))
// extension
// support boolean, int, long, string, bytes
Expand All @@ -71,7 +71,5 @@ void format() {
byte[] reserialized = format.serialize(deserialized);

assertArrayEquals(serialized, reserialized);


}
}
Loading