fix(kinesis): reject non-blob Data instead of coercing it with asText (#2551)

PutRecord and each PutRecords entry read the blob with
path("Data").asText(), which renders a missing or container Data as
"", and null, booleans and numbers as their literal text. Whether
such a request was rejected then came down to whether that text
happened to be legal base64: a missing Data was stored as a
successful empty record, JSON null, true or 1234 landed as a few
garbage bytes, while false or 12345 happened to fail. AWS models Data
as required base64-encoded binary on PutRecord and on each PutRecords
entry, so none of these shapes is a valid request.

Accept exactly the two shapes a real client can send — a base64 text
string on the JSON protocol, or a binary node on the CBOR transports,
where an unmodified AWS SDK for Java Kinesis client encodes Data as a
byte string that Jackson decodes to a binary node, not text — and
route anything else the way each path already handles undecodable
base64: a 400 SerializationException on PutRecord, a per-record
InternalFailure in the PutRecords write loop. The issue proposed
rejecting non-textual Data; a textual-only gate turns out to break
every CBOR-default SDK client, so the shipped rule is non-blob.

The batch pre-pass keeps a null data for the rejected shapes so the
request cap still counts them at the bytes the caller sent, and the
record-size check now runs for every entry, so a malformed entry no
longer skips partition-key accounting. A binary Data now decodes in
the pre-pass, so both caps see its real decoded size: an oversized
CBOR blob fails the whole request as an oversized JSON-encoded record
already did, and a batch of CBOR blobs counts toward the request cap
at decoded length rather than its base64 rendering. An explicit
"Data": "" remains legal and stores an empty payload as before.

Closes #2516
This commit is contained in:
soreavis authored and GitHub committed 2026-08-25 23:27:42 -05:00
1 parent 3bdcd5b84f
commit df5581e5bd
3 files changed
+264 -15

No files matched your search

@@ -465,9 +465,15 @@ public class KinesisJsonHandler {
private Response handlePutRecord(JsonNode request, String region) {
String streamName = resolveStreamName(request);
JsonNode dataNode = request.path("Data");
// The CBOR transports (the AWS SDK for Java's default for Kinesis) decode Data
// as a binary node rather than base64 text, so both blob shapes are accepted.
if (!dataNode.isTextual() && !dataNode.isBinary()) {
throw new AwsException("SerializationException", "Data must be a base64-encoded string.", 400);
}
byte[] data;
try {
data = Base64.getDecoder().decode(request.path("Data").asText());
data = Base64.getDecoder().decode(dataNode.asText());
} catch (IllegalArgumentException e) {
throw new AwsException("SerializationException", "Data is not valid base64.", 400);
}
@@ -489,32 +495,32 @@ public class KinesisJsonHandler {
// An oversized record fails the whole request before anything is
// written — per-record ErrorCode is reserved for throughput/internal
// failures. Successful decodes are kept for the write loop; malformed
// Data is left null so it stays a per-record failure there. The count
// cap is checked upfront and the byte cap as the loop goes, so neither
// an over-long batch nor an oversized one is fully decoded before it
// is rejected.
// failures. Successful decodes are kept for the write loop; a Data
// that is not a decodable blob is left null and fails per-record
// there, with its partition key still counted by the record-size
// check. The count cap is checked upfront and the byte cap as the
// loop goes, so neither an over-long batch nor an oversized one is
// fully decoded before it is rejected.
record Entry(JsonNode node, byte[] data) {}
List<Entry> entries = new ArrayList<>();
long totalBytes = 0;
for (JsonNode node : recordsNode) {
JsonNode dataNode = node.path("Data");
byte[] data = null;
if (dataNode.isTextual()) {
if (dataNode.isTextual() || dataNode.isBinary()) {
try {
data = Base64.getDecoder().decode(dataNode.textValue());
data = Base64.getDecoder().decode(dataNode.asText());
} catch (IllegalArgumentException e) {
data = null;
}
}
String partitionKey = node.path("PartitionKey").asText();
if (data != null) {
service.validateRecordSize(stream, data, partitionKey);
}
service.validateRecordSize(stream, data, partitionKey);
// Data that did not decode still travelled in the request, so it counts toward the
// request cap at the bytes the caller sent rather than as nothing — otherwise a batch
// of undecodable records could carry any payload past the limit. A non-string Data is
// measured the same way, since it is not a blob AWS would have accepted either.
// of undecodable records could carry any payload past the limit. A Data that is
// neither text nor binary is measured the same way, since it is not a blob AWS
// would have accepted either.
totalBytes += KinesisService.recordSize(
data != null ? data.length
: dataNode.toString().getBytes(StandardCharsets.UTF_8).length,
@@ -529,8 +535,14 @@ public class KinesisJsonHandler {
for (Entry entry : entries) {
try {
byte[] data = entry.data() != null ? entry.data()
: Base64.getDecoder().decode(entry.node().path("Data").asText());
byte[] data = entry.data();
if (data == null) {
JsonNode dataNode = entry.node().path("Data");
if (!dataNode.isTextual() && !dataNode.isBinary()) {
throw new IllegalArgumentException("Data must be a base64-encoded string.");
}
data = Base64.getDecoder().decode(dataNode.asText());
}
String partitionKey = entry.node().path("PartitionKey").asText();
KinesisService.PutRecordResult result = service.putRecordWithShardId(streamName, data, partitionKey, region);
results.addObject()