Skip to content

Commit cd4988c

Browse files
committed
CloudEventBuilder should fail when providing an invalid extension name
Signed-off-by: Arghya Sadhu <arghya88@gmail.com>
1 parent 554198d commit cd4988c

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

core/src/main/java/io/cloudevents/core/impl/BaseCloudEventBuilder.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,25 @@ public SELF withData(String dataContentType, URI dataSchema, CloudEventData data
9797
}
9898

9999
public SELF withExtension(@Nonnull String key, String value) {
100+
if(!isValidExtensionName(key)){
101+
throw CloudEventRWException.newInvalidExtensionName(key);
102+
}
100103
this.extensions.put(key, value);
101104
return self;
102105
}
103106

104107
public SELF withExtension(@Nonnull String key, Number value) {
108+
if(!isValidExtensionName(key)){
109+
throw CloudEventRWException.newInvalidExtensionName(key);
110+
}
105111
this.extensions.put(key, value);
106112
return self;
107113
}
108114

109115
public SELF withExtension(@Nonnull String key, Boolean value) {
116+
if(!isValidExtensionName(key)){
117+
throw CloudEventRWException.newInvalidExtensionName(key);
118+
}
110119
this.extensions.put(key, value);
111120
return self;
112121
}
@@ -151,4 +160,25 @@ public CloudEvent end() {
151160
protected static IllegalStateException createMissingAttributeException(String attributeName) {
152161
return new IllegalStateException("Attribute '" + attributeName + "' cannot be null");
153162
}
163+
/**
164+
* Validates the extension name as defined in CloudEvents spec
165+
* See <a href="https://github.com/cloudevents/spec/blob/master/spec.md#attribute-naming-convention">attribute-naming-convention</a>
166+
* @param name the extension name
167+
* @return true if extension name is valid, false otherwise
168+
*/
169+
private static boolean isValidExtensionName(String name) {
170+
if(name.length() > 20){
171+
return false;
172+
}
173+
char[] chars = name.toCharArray();
174+
for (char c: chars)
175+
if (!isValidChar(c)) {
176+
return false;
177+
}
178+
return true;
179+
}
180+
181+
private static boolean isValidChar(char c) {
182+
return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
183+
}
154184
}

core/src/test/java/io/cloudevents/core/impl/BaseCloudEventBuilderTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import io.cloudevents.core.builder.CloudEventBuilder;
55
import io.cloudevents.core.extensions.DistributedTracingExtension;
66
import io.cloudevents.core.test.Data;
7+
import io.cloudevents.rw.CloudEventRWException;
78
import org.junit.jupiter.api.Test;
89

910
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
12+
import static org.junit.jupiter.api.Assertions.assertTrue;
1013

1114
public class BaseCloudEventBuilderTest {
1215

@@ -45,4 +48,28 @@ public void copyAndRemoveMaterializedExtension() {
4548
.isEqualTo(have);
4649
}
4750

51+
@Test
52+
public void testLongExtensionName() {
53+
Exception exception = assertThrows(RuntimeException.class, () -> {
54+
CloudEvent cloudEvent = CloudEventBuilder.v1(Data.V1_WITH_JSON_DATA_WITH_EXT)
55+
.withExtension("thisextensionnameistoolong", "")
56+
.build();
57+
});
58+
String expectedMessage = "Invalid extensions name: thisextensionnameistoolong";
59+
String actualMessage = exception.getMessage();
60+
61+
assertTrue(actualMessage.contains(expectedMessage));
62+
}
63+
@Test
64+
public void testInvalidExtensionName() {
65+
Exception exception = assertThrows(RuntimeException.class, () -> {
66+
CloudEvent cloudEvent = CloudEventBuilder.v1(Data.V1_WITH_JSON_DATA_WITH_EXT)
67+
.withExtension("ExtensionName", "")
68+
.build();
69+
});
70+
String expectedMessage = "Invalid extensions name: ExtensionName";
71+
String actualMessage = exception.getMessage();
72+
73+
assertTrue(actualMessage.contains(expectedMessage));
74+
}
4875
}

0 commit comments

Comments
 (0)