From: Gihyeon Ryu Date: Wed, 11 Nov 2026 10:00:12 +0000 (+0000) Subject: BUG/MEDIUM: sample: parse json_query integers within the token length X-Git-Tag: v3.5-dev13~62 X-Git-Url: https://git.kaiwu.me/web/%22data:,/doc/static/%7B@url%7D?a=commitdiff_plain;h=c1ba5a4b6692b501264ce3f6a9698e20e10bb499;p=haproxy.git BUG/MEDIUM: sample: parse json_query integers within the token length For the "int" output type, sample_conv_json_query() converts the token with strtoll(token, NULL, 0). The token points into the sample, which is not NUL-terminated, and its length is ignored. When the input ends inside a number (for example a truncated body '{"amount":1'), the parsing goes on past the end of the sample, into the remaining bytes of the buffer, which may hold digits left by a previous request, possibly from another client. Those digits then become part of the returned value, which may bypass a range check, or be reflected to the client. Numbers using an exponent or a fraction are also misparsed ("1e5" gives 1). The number is now parsed with mjson_get_number(), which stays within the token, then range checked as a double before being converted. Truncation of fractions is kept. This should be backported to all stable branches. --- diff --git a/src/sample.c b/src/sample.c index 548d8c232..1cde534d8 100644 --- a/src/sample.c +++ b/src/sample.c @@ -4798,11 +4798,16 @@ static int sample_conv_json_query(const struct arg *args, struct sample *smp, vo switch (token_type) { case MJSON_TOK_NUMBER: if (args[1].type == ARGT_SINT) { - smp->data.u.sint = strtoll(token, NULL, 0); + double double_val; + + /* is not NUL-terminated, use mjson_get_number() */ + if (mjson_get_number(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &double_val) == 0) + return 0; - if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX) + if (!(double_val >= JSON_INT_MIN && double_val <= JSON_INT_MAX)) return 0; + smp->data.u.sint = (long long)double_val; smp->data.type = SMP_T_SINT; return 1;