fix(kinesis): reject non-string tag members instead of coercing them (#3099)

CreateStream, AddTagsToStream and RemoveTagsFromStream read tag
values and TagKeys entries with asText(), so a number, boolean or
null member was stored as its text ("5", "true", "null") and an
object or array member as the empty string, rather than rejected.
AWS models Tags as a map of string values and TagKeys as a list of
strings, so none of those shapes is a valid request. The CreateStream
tag change (#2494) named this as the separate fix to land on its own,
and the SES tag parser already answers the same shapes with
SerializationException (#2512).

Reject a present-but-non-string tag value or TagKeys entry, a Tags
member that is not an object, and a TagKeys member that is not a
list, with a 400 SerializationException; a missing or null member
still yields no tags, as before. The tags are parsed before the
stream is created or looked up: CreateStream would otherwise leave
an untagged stream behind on a rejected Tags member, and a malformed
member is answered ahead of the not-found or in-use error a bad
stream raises.
This commit is contained in:
soreavis authored and GitHub committed 2026-09-05 17:09:15 -04:00
1 parent 43cdbe234f
commit fcb5bd02c0
3 files changed
+209 -8

No files matched your search

@@ -118,13 +118,15 @@ public class KinesisJsonHandler {
streamMode = mode;
}
}
service.createStream(streamName, shardCount, streamMode,
optionalMaxRecordSize(request), region);
// CreateStream's optional Tags member was being dropped. That is invisible to a
// hand-written script but not to Terraform: aws_kinesis_stream sets tags at create
// time and then reads them back with ListTagsForStream, so an empty read produces a
// permanent "tags will be updated in-place" diff on an unchanged configuration.
// Parsed before the stream exists so a malformed Tags member rejects the whole
// request instead of leaving an untagged stream behind.
Map<String, String> tags = parseTags(request);
service.createStream(streamName, shardCount, streamMode,
optionalMaxRecordSize(request), region);
if (!tags.isEmpty()) {
service.addTagsToStream(streamName, tags, region);
}
@@ -406,8 +408,19 @@ public class KinesisJsonHandler {
private Response handleRemoveTagsFromStream(JsonNode request, String region) {
String streamName = resolveStreamName(request);
java.util.List<String> tagKeys = new java.util.ArrayList<>();
request.path("TagKeys").forEach(node -> tagKeys.add(node.asText()));
JsonNode tagKeysNode = request.path("TagKeys");
List<String> tagKeys = new ArrayList<>();
if (!tagKeysNode.isMissingNode() && !tagKeysNode.isNull()) {
if (!tagKeysNode.isArray()) {
throw new AwsException("SerializationException", "TagKeys must be a list of strings.", 400);
}
for (JsonNode node : tagKeysNode) {
if (!node.isTextual()) {
throw new AwsException("SerializationException", "TagKeys must be a list of strings.", 400);
}
tagKeys.add(node.textValue());
}
}
service.removeTagsFromStream(streamName, tagKeys, region);
return Response.ok(objectMapper.createObjectNode()).build();
}
@@ -428,12 +441,24 @@ public class KinesisJsonHandler {
// Shared by CreateStream and AddTagsToStream so the two paths cannot drift: the same
// request shape has to produce the same stored tags whichever operation carries it.
// A missing or non-object Tags member yields an empty map rather than an error, which
// is what AddTagsToStream already did before CreateStream started calling this.
// A missing or null Tags member yields an empty map; anything else that is not a map
// of strings is a wire deserialization error rather than a value to coerce, so a
// number or boolean is rejected instead of being stored as its text.
private Map<String, String> parseTags(JsonNode request) {
Map<String, String> tags = new HashMap<>();
request.path("Tags").fields()
.forEachRemaining(entry -> tags.put(entry.getKey(), entry.getValue().asText()));
JsonNode tagsNode = request.path("Tags");
if (tagsNode.isMissingNode() || tagsNode.isNull()) {
return tags;
}
if (!tagsNode.isObject()) {
throw new AwsException("SerializationException", "Tags must be a map of string values.", 400);
}
tagsNode.fields().forEachRemaining(entry -> {
if (!entry.getValue().isTextual()) {
throw new AwsException("SerializationException", "Tags must be a map of string values.", 400);
}
tags.put(entry.getKey(), entry.getValue().textValue());
});
return tags;
}
@@ -1319,4 +1319,35 @@ class KinesisIntegrationTest {
buf.get(payload);
return new ObjectMapper().readTree(payload);
}
@Test
@Order(67)
void createStreamRejectsANonStringTagValueWithSerializationException() {
// A non-string tag value is a wire deserialization error, not a value to coerce:
// it used to be stored as its text ("5"), and the stream was created before the
// tags were parsed. Now the whole request is rejected and nothing is created.
given()
.header("X-Amz-Target", "Kinesis_20131202.CreateStream")
.contentType(KINESIS_CONTENT_TYPE)
.body("""
{"StreamName": "create-with-bad-tags", "ShardCount": 1, "Tags": {"Foo": 5}}
""")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("SerializationException"));
given()
.header("X-Amz-Target", "Kinesis_20131202.DescribeStream")
.contentType(KINESIS_CONTENT_TYPE)
.body("""
{"StreamName": "create-with-bad-tags"}
""")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ResourceNotFoundException"));
}
}
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.kinesis;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.BinaryNode;
@@ -1220,4 +1221,148 @@ class KinesisJsonHandlerTest {
assertEquals(Map.of("Foo", "Bar", "gw:example", "kinesis"),
service.listTagsForStream("test-stream", REGION));
}
@Test
void createStreamRejectsANonStringTagValueBeforeCreatingTheStream() {
for (ObjectNode tags : nonStringTagValues()) {
ObjectNode create = MAPPER.createObjectNode();
create.put("StreamName", "rejected-tags-stream");
create.put("ShardCount", 1);
create.set("Tags", tags);
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("CreateStream", create, REGION));
assertEquals("SerializationException", ex.getErrorCode());
assertEquals("Tags must be a map of string values.", ex.getMessage());
assertEquals(400, ex.getHttpStatus());
}
ObjectNode describe = MAPPER.createObjectNode();
describe.put("StreamName", "rejected-tags-stream");
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("DescribeStream", describe, REGION));
assertEquals("ResourceNotFoundException", ex.getErrorCode());
}
@Test
void createStreamRejectsANonObjectTagsMember() {
for (JsonNode tags : List.of(MAPPER.getNodeFactory().textNode("Foo=Bar"),
MAPPER.getNodeFactory().numberNode(5), MAPPER.createArrayNode().add("Foo"))) {
ObjectNode create = MAPPER.createObjectNode();
create.put("StreamName", "rejected-tags-stream");
create.put("ShardCount", 1);
create.set("Tags", tags);
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("CreateStream", create, REGION));
assertEquals("SerializationException", ex.getErrorCode());
}
}
@Test
void createStreamWithANullTagsMemberLeavesTheStreamUntagged() {
ObjectNode create = MAPPER.createObjectNode();
create.put("StreamName", "null-tags-stream");
create.put("ShardCount", 1);
create.putNull("Tags");
assertThat(handler.handle("CreateStream", create, REGION).getStatus(), is(200));
assertTrue(service.listTagsForStream("null-tags-stream", REGION).isEmpty());
}
@Test
void addTagsToStreamRejectsANonStringTagValueWithoutTouchingExistingTags() {
createStream("test-stream");
ObjectNode add = MAPPER.createObjectNode();
add.put("StreamName", "test-stream");
add.putObject("Tags").put("Foo", "Bar");
assertThat(handler.handle("AddTagsToStream", add, REGION).getStatus(), is(200));
for (ObjectNode tags : nonStringTagValues()) {
ObjectNode bad = MAPPER.createObjectNode();
bad.put("StreamName", "test-stream");
bad.set("Tags", tags);
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("AddTagsToStream", bad, REGION));
assertEquals("SerializationException", ex.getErrorCode());
assertEquals(400, ex.getHttpStatus());
}
assertEquals(Map.of("Foo", "Bar"), service.listTagsForStream("test-stream", REGION));
}
@Test
void removeTagsFromStreamRejectsANonStringTagKey() {
createStream("test-stream");
ObjectNode add = MAPPER.createObjectNode();
add.put("StreamName", "test-stream");
add.putObject("Tags").put("Foo", "Bar");
assertThat(handler.handle("AddTagsToStream", add, REGION).getStatus(), is(200));
for (JsonNode tagKeys : List.of(MAPPER.createArrayNode().add(5), MAPPER.createArrayNode().add(true),
MAPPER.createArrayNode().addNull(), MAPPER.createArrayNode().add("Foo").add(5),
MAPPER.getNodeFactory().textNode("Foo"), MAPPER.createObjectNode().put("a", "Foo"))) {
ObjectNode remove = MAPPER.createObjectNode();
remove.put("StreamName", "test-stream");
remove.set("TagKeys", tagKeys);
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("RemoveTagsFromStream", remove, REGION));
assertEquals("SerializationException", ex.getErrorCode());
assertEquals("TagKeys must be a list of strings.", ex.getMessage());
assertEquals(400, ex.getHttpStatus());
}
assertEquals(Map.of("Foo", "Bar"), service.listTagsForStream("test-stream", REGION));
}
@Test
void removeTagsFromStreamStillRemovesStringTagKeys() {
createStream("test-stream");
ObjectNode add = MAPPER.createObjectNode();
add.put("StreamName", "test-stream");
add.putObject("Tags").put("Foo", "Bar").put("Baz", "Qux");
assertThat(handler.handle("AddTagsToStream", add, REGION).getStatus(), is(200));
ObjectNode remove = MAPPER.createObjectNode();
remove.put("StreamName", "test-stream");
remove.putArray("TagKeys").add("Foo");
assertThat(handler.handle("RemoveTagsFromStream", remove, REGION).getStatus(), is(200));
assertEquals(Map.of("Baz", "Qux"), service.listTagsForStream("test-stream", REGION));
}
@Test
void removeTagsFromStreamWithANullOrMissingTagKeysLeavesTagsUntouched() {
createStream("test-stream");
ObjectNode add = MAPPER.createObjectNode();
add.put("StreamName", "test-stream");
add.putObject("Tags").put("Foo", "Bar");
assertThat(handler.handle("AddTagsToStream", add, REGION).getStatus(), is(200));
ObjectNode nullKeys = MAPPER.createObjectNode();
nullKeys.put("StreamName", "test-stream");
nullKeys.putNull("TagKeys");
assertThat(handler.handle("RemoveTagsFromStream", nullKeys, REGION).getStatus(), is(200));
ObjectNode missingKeys = MAPPER.createObjectNode();
missingKeys.put("StreamName", "test-stream");
assertThat(handler.handle("RemoveTagsFromStream", missingKeys, REGION).getStatus(), is(200));
assertEquals(Map.of("Foo", "Bar"), service.listTagsForStream("test-stream", REGION));
}
/** Every non-string tag value shape — number, boolean, null, object, array — plus a map mixing a valid and an invalid value. */
private static List<ObjectNode> nonStringTagValues() {
ObjectNode number = MAPPER.createObjectNode();
number.put("Foo", 5);
ObjectNode bool = MAPPER.createObjectNode();
bool.put("Foo", true);
ObjectNode nul = MAPPER.createObjectNode();
nul.putNull("Foo");
ObjectNode object = MAPPER.createObjectNode();
object.putObject("Foo");
ObjectNode array = MAPPER.createObjectNode();
array.putArray("Foo");
ObjectNode mixed = MAPPER.createObjectNode();
mixed.put("Foo", "Bar");
mixed.put("Baz", 5);
return List.of(number, bool, nul, object, array, mixed);
}
}