]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MEDIUM: sample: parse json_query integers within the token length
authorGihyeon Ryu <rlgusryu@gmail.com>
Wed, 11 Nov 2026 10:00:12 +0000 (10:00 +0000)
committerOlivier Houchard <cognet@ci0.org>
Mon, 16 Nov 2026 14:55:56 +0000 (15:55 +0100)
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.

src/sample.c

index 548d8c232e49f97c482fe1f17208ee567d1a307c..1cde534d8891dd79a538b7575aecbca1e600c24a 100644 (file)
@@ -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;
+
+                               /* <token> 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;