Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
hot fix(kinesis): decode CBOR requests from AWS Java SDK v2 (#213)
Nahuel Nucera9e5ed5acf62a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 2751ceb2..e1e5a8c7 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,13 @@ Versioning follows [Semantic Versioning](https://semver.org/).---+## [1.1.58] — 2026-04-09++### Fixed+- **Kinesis CBOR protocol support** — `PutRecord` and `PutRecords` from the AWS Java SDK v2 failed with `'utf-8' codec can't decode byte 0xbf`. The Java SDK sends Kinesis requests as CBOR (`application/x-amz-cbor-1.1`) by default, but the handler only accepted JSON. Kinesis now detects CBOR content-type, decodes with `cbor2`, and returns CBOR-encoded responses. Reported by community.++---+## [1.1.57] — 2026-04-09### Fixeddiff --git a/ministack/services/kinesis.py b/ministack/services/kinesis.pyindex 54264794..1a71b906 100644--- a/ministack/services/kinesis.py+++ b/ministack/services/kinesis.py@@ -142,10 +142,26 @@ async def handle_request(method, path, headers, body, query_params):target = headers.get("x-amz-target", "")action = target.split(".")[-1] if "." in target else ""+ content_type = headers.get("content-type", "")+ is_cbor = "cbor" in content_type+try:- data = json.loads(body) if body else {}+ if is_cbor and body:+ import cbor2+ data = cbor2.loads(body)+ # CBOR Data field arrives as raw bytes; base64-encode for uniform handling+ if "Data" in data and isinstance(data["Data"], (bytes, bytearray)):+ data["Data"] = base64.b64encode(data["Data"]).decode("ascii")+ for rec in data.get("Records", []):+ if "Data" in rec and isinstance(rec["Data"], (bytes, bytearray)):+ rec["Data"] = base64.b64encode(rec["Data"]).decode("ascii")+ else:+ data = json.loads(body) if body else {}except json.JSONDecodeError:return error_response_json("SerializationException", "Invalid JSON", 400)+ except Exception as e:+ logger.error("Failed to decode request body: %s", e)+ return error_response_json("SerializationException", f"Could not decode request body: {e}", 400)_expire_iterators()@@ -180,8 +196,22 @@ async def handle_request(method, path, headers, body, query_params):handler = handlers.get(action)if not handler:+ if is_cbor:+ return _cbor_response({"__type": "InvalidAction", "message": f"Unknown action: {action}"}, 400)return error_response_json("InvalidAction", f"Unknown action: {action}", 400)- return handler(data)++ status, resp_headers, resp_body = handler(data)+ if is_cbor:+ import cbor2+ # Re-encode JSON response body as CBOR+ try:+ json_data = json.loads(resp_body) if isinstance(resp_body, (str, bytes)) else resp_body+ except (json.JSONDecodeError, TypeError):+ json_data = {}+ cbor_body = cbor2.dumps(json_data)+ resp_headers["Content-Type"] = "application/x-amz-cbor-1.1"+ return status, resp_headers, cbor_body+ return status, resp_headers, resp_body# ---------------------------------------------------------------------------@@ -917,6 +947,12 @@ def _stream_desc(stream, shard_ids=None):}+def _cbor_response(data: dict, status: int = 200):+ import cbor2+ body = cbor2.dumps(data)+ return status, {"Content-Type": "application/x-amz-cbor-1.1"}, body++def reset():_streams.clear()_shard_iterators.clear()diff --git a/pyproject.toml b/pyproject.tomlindex f54beb9b..65705efb 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"[project]name = "ministack"-version = "1.1.57"+version = "1.1.58"description = "Free, open-source local AWS emulator — drop-in LocalStack replacement"readme = "README.md"license = { file = "LICENSE" }diff --git a/tests/test_kinesis.py b/tests/test_kinesis.pyindex e89f148c..9ac892fa 100644--- a/tests/test_kinesis.py+++ b/tests/test_kinesis.py@@ -321,3 +321,44 @@ def test_kinesis_esm_creates_and_lists(lam, kin):lam.delete_event_source_mapping(UUID=esm["UUID"])lam.delete_function(FunctionName="esm-kin-fn")+++def test_kinesis_cbor_put_record(kin):+ """Java SDK sends CBOR-encoded PutRecord; ministack must decode it."""+ import cbor2+ import urllib.request++ endpoint = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566")++ kin.create_stream(StreamName="cbor-test-stream", ShardCount=1)++ # Build a CBOR-encoded PutRecord payload (same as AWS Java SDK v2 sends)+ cbor_body = cbor2.dumps({+ "StreamName": "cbor-test-stream",+ "Data": b'{ "test": "123"}',+ "PartitionKey": "1",+ })++ req = urllib.request.Request(+ endpoint,+ data=cbor_body,+ headers={+ "Content-Type": "application/x-amz-cbor-1.1",+ "X-Amz-Target": "Kinesis_20131202.PutRecord",+ },+ method="POST",+ )+ with urllib.request.urlopen(req) as resp:+ assert resp.status == 200+ resp_body = cbor2.loads(resp.read())+ assert "ShardId" in resp_body+ assert "SequenceNumber" in resp_body++ # Verify the record is retrievable via normal JSON path+ desc = kin.describe_stream(StreamName="cbor-test-stream")+ shard_id = desc["StreamDescription"]["Shards"][0]["ShardId"]+ it = kin.get_shard_iterator(+ StreamName="cbor-test-stream", ShardId=shard_id, ShardIteratorType="TRIM_HORIZON"+ )+ records = kin.get_records(ShardIterator=it["ShardIterator"])+ assert len(records["Records"]) == 1