Skip to content

fix: prevent integer overflow in query parameter parsing#6612

Merged
mdisibio merged 4 commits intografana:mainfrom
bejaratommy:fix/integer-overflow-query-params
Mar 17, 2026
Merged

fix: prevent integer overflow in query parameter parsing#6612
mdisibio merged 4 commits intografana:mainfrom
bejaratommy:fix/integer-overflow-query-params

Conversation

@ricardbejarano
Copy link
Contributor

What this PR does:

Replace strconv.Atoi and strconv.ParseInt with strconv.ParseUint for query parameters stored as unsigned integers (uint32/uint64) in pkg/api/http.go.

Passing negative values or values exceeding the target type's range silently overflow/wrap instead of returning an error. For example:

  • start=-1 wraps to 4294967295 (uint32 max)
  • limit=4294967298 wraps to 2

This is because strconv.Atoi parses into a platform-dependent int (64-bit on 64-bit systems) and strconv.ParseInt(s, 10, 32) accepts negative values, both of which silently truncate or wrap when cast to uint32.

Replaced all 13 affected call sites across ParseSearchRequestWithDefault, ParseSpanMetricsRequest, and ParseQueryRangeRequest. Also removed now-redundant sign checks since ParseUint rejects negative values at parse time.

Which issue(s) this PR fixes:
Fixes #6503

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

Replace strconv.Atoi and strconv.ParseInt with strconv.ParseUint for
query parameters that are stored as unsigned integers (uint32/uint64).

Previously, passing negative values or values exceeding the target
type's range would silently overflow/wrap. For example:
  - start=-1 would wrap to 4294967295 (uint32 max)
  - limit=4294967298 would wrap to 2

Now, out-of-range and negative values are properly rejected with
descriptive parse errors.

Affected parameters in ParseSearchRequestWithDefault:
  - start, end: ParseInt(s, 10, 32) -> ParseUint(s, 10, 32)
  - limit, spss: Atoi -> ParseUint(s, 10, 32)

Affected parameters in ParseSpanMetricsRequest:
  - start, end: ParseInt(s, 10, 32) -> ParseUint(s, 10, 32)
  - limit: Atoi -> ParseUint(l, 10, 64)

Affected parameters in ParseQueryRangeRequest:
  - startPage, pagesToSearch, footerSize, exemplars, maxSeries:
    Atoi -> ParseUint(s, 10, 32)
  - size: Atoi -> ParseUint(s, 10, 64)

Fixes: grafana#6503
Signed-off-by: bejaratommy <tommy@bejara.net>
Copy link
Contributor

@mdisibio mdisibio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fixes. Honestly not sure why we did signed parsing, and amusingly we checked for negatives for spansPerSpanSet. LGTM and will merge after CI.

@mdisibio
Copy link
Contributor

There are a couple of CI issues.

(1) It looks like we need to update one of the tests for the new error message:

    search_sharder_test.go:935: 
        	Error Trace:	/home/runner/work/tempo/tempo/modules/frontend/search_sharder_test.go:935
        	            				/home/runner/work/tempo/tempo/modules/frontend/search_sharder_test.go:927
        	            				/home/runner/work/tempo/tempo/modules/frontend/search_sharder_test.go:894
        	Error:      	Not equal: 
        	            	expected: "invalid start: strconv.ParseInt: parsing \"asdf\": invalid syntax"
        	            	actual  : "invalid start: strconv.ParseUint: parsing \"asdf\": invalid syntax"

(2) Not sure why but this is triggering lint errors on unrelated (existing) code in modules/generator and modules/querier. Haven't run into this before, I don't seem them changed in any commit in this branch or any force pushes. I'm thinking there are a couple options: (a) Change these in this PR or (b) I can fix this separately and then you would need to merge latest main into this branch to get past them. Leaning towards (b) unless you are interested in (a). I'll keep looking to see what the explanation is.

@ricardbejarano
Copy link
Contributor Author

Hi @mdisibio, i'll look at the tests. I'd prefer you fix the linting issues elsewhere and then i can rebase (option B), yeah. Thanks!

@cla-assistant
Copy link

cla-assistant bot commented Mar 11, 2026

CLA assistant check
All committers have signed the CLA.

@zhxiaogg
Copy link
Contributor

Not sure why but this is triggering lint errors on unrelated (existing) code in modules/generator and modules/querier....fix this separately and then you would need to merge latest main into this branch to get past them

Seems like the PR is in healthy state. Just FYI, I'm working on to fix the historical lint errors #6529.

@mdisibio mdisibio merged commit db0e9e1 into grafana:main Mar 17, 2026
25 checks passed
@mdisibio
Copy link
Contributor

Seems like the PR is in healthy state

Thanks, missed that. In that case we are good, and it is merged.

@ricardbejarano Thanks for the fix 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Integer overflow in query parameters

4 participants