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.
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;