fix(msk): reject numberOfBrokerNodes values that only look whole after double rounding (#2571)

asBrokerCount validated the incoming JSON number by parsing it as a double and
comparing d == Math.rint(d), but a literal like 1.0000000000000001 has no exact
double representation and collapses to precisely 1.0 during that conversion -
so the check ran after the precision that would have caught it was already
gone, and malformed input was silently accepted as a valid integer.

Binding numberOfBrokerNodes straight to Integer isn't a fix either: Jackson's
default float-to-int coercion truncates a JSON float token the same way
intValue() would, reintroducing the exact bug this field's validation exists
to prevent.

BrokerCountDeserializer reads the token as an exact BigDecimal via
JsonParser#getDecimalValue(), which Jackson builds directly from the source
text rather than routing through a lossy double, so it can tell a genuinely
fractional literal from one that only looks fractional (or only looks whole)
after floating-point rounding.

It can't throw directly on malformed input, though: an exception raised while
Jackson is still reading the request body is caught by
ServerJacksonMessageBodyReader before the JAX-RS resource method - and this
service's AwsException/AwsExceptionMapper handling - ever run, producing a
bodyless 400 instead of MSK's usual {message, invalidParameter} shape.
BrokerCount carries "malformed" as a value instead, and
MskService#validateCreateRequest raises the proper error once binding has
finished successfully, the same way it already does for every other
CreateCluster validation failure.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-08-27 13:08:28 -04:00
1 parent f411a316a2
commit bd436eeea1
6 files changed
+259 -7

No files matched your search

@@ -1048,6 +1048,102 @@ class MskControllerIntegrationTest {
.body("clusterInfo.numberOfBrokerNodes", equalTo(30));
}
/**
* A fractional count must not be narrowed into a plausible one: Jackson's default
* float-to-int coercion would otherwise turn 2.7 into 2 and report a cluster the caller
* never asked for.
*/
@Test
void createClusterRejectsAFractionalBrokerCountInsteadOfTruncatingIt() {
given()
.contentType("application/json")
.body("""
{"clusterName": "v1-brokers-fractional-test", "kafkaVersion": "3.6.0",
"numberOfBrokerNodes": 2.7}
""")
.when()
.post("/v1/clusters")
.then()
.statusCode(400)
.body("invalidParameter", equalTo("numberOfBrokerNodes"));
// ...and nothing was created under that name.
given()
.when()
.get("/v1/clusters")
.then()
.statusCode(200)
.body("clusterInfoList.clusterName", not(hasItem("v1-brokers-fractional-test")));
}
/**
* A literal like 1.0000000000000001 has no exact double representation - parsing it as a
* double collapses it to precisely 1.0, so a d == Math.rint(d) check performed after that
* conversion would wrongly accept it as whole. This value must still be rejected: reading
* the token as a BigDecimal (see BrokerCountDeserializer) catches the fractional part a
* double comparison already lost.
*/
@Test
void createClusterRejectsABrokerCountThatOnlyLooksWholeAsADouble() {
given()
.contentType("application/json")
.body("""
{"clusterName": "v1-brokers-precision-test", "kafkaVersion": "3.6.0",
"numberOfBrokerNodes": 1.0000000000000001}
""")
.when()
.post("/v1/clusters")
.then()
.statusCode(400)
.body("invalidParameter", equalTo("numberOfBrokerNodes"));
given()
.when()
.get("/v1/clusters")
.then()
.statusCode(200)
.body("clusterInfoList.clusterName", not(hasItem("v1-brokers-precision-test")));
}
/** A whole number written with a decimal point (e.g. 3.0) is not fractional and is accepted. */
@Test
void createClusterAcceptsAWholeNumberBrokerCountWrittenAsADecimal() {
String clusterArn = given()
.contentType("application/json")
.body("""
{"clusterName": "v1-brokers-whole-decimal-test", "kafkaVersion": "3.6.0",
"numberOfBrokerNodes": 3.0}
""")
.when()
.post("/v1/clusters")
.then()
.statusCode(200)
.extract().path("clusterArn");
given()
.when()
.get("/v1/clusters/{clusterArn}", clusterArn)
.then()
.statusCode(200)
.body("clusterInfo.numberOfBrokerNodes", equalTo(3));
}
/** The V2 path nests the count under provisioned and must reject a fractional value too. */
@Test
void createClusterV2RejectsAFractionalBrokerCount() {
given()
.contentType("application/json")
.body("""
{"clusterName": "v2-brokers-fractional-test",
"provisioned": {"kafkaVersion": "3.6.0", "numberOfBrokerNodes": 2.7}}
""")
.when()
.post("/api/v2/clusters")
.then()
.statusCode(400)
.body("invalidParameter", equalTo("numberOfBrokerNodes"));
}
// ── Serverless clusters ──────────────────────────────────────────────────────────────
@Test