Merge pull request #17647 from roidelapluie/roidelapluie/resource-limit-fix

web/api: Add maximum limit validation to TSDB status endpoint
This commit is contained in:
Julien 2025-12-09 16:06:09 +01:00 committed by GitHub
commit e77dd5bec2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 1 deletions

View File

@ -1346,7 +1346,7 @@ GET /api/v1/status/tsdb
```
URL query parameters:
- `limit=<number>`: Limit the number of returned items to a given number for each set of statistics. By default, 10 items are returned.
- `limit=<number>`: Limit the number of returned items to a given number for each set of statistics. By default, 10 items are returned. The maximum allowed limit is 10000.
The `data` section of the query result consists of:

View File

@ -1837,12 +1837,16 @@ func (api *API) serveTSDBBlocks(*http.Request) apiFuncResult {
}
func (api *API) serveTSDBStatus(r *http.Request) apiFuncResult {
const maxTSDBLimit = 10000
limit := 10
if s := r.FormValue("limit"); s != "" {
var err error
if limit, err = strconv.Atoi(s); err != nil || limit < 1 {
return apiFuncResult{nil, &apiError{errorBadData, errors.New("limit must be a positive number")}, nil, nil}
}
if limit > maxTSDBLimit {
return apiFuncResult{nil, &apiError{errorBadData, fmt.Errorf("limit must not exceed %d", maxTSDBLimit)}, nil, nil}
}
}
s, err := api.db.Stats(labels.MetricName, limit)
if err != nil {

View File

@ -4465,6 +4465,18 @@ func TestTSDBStatus(t *testing.T) {
values: map[string][]string{"limit": {"0"}},
errType: errorBadData,
},
{
db: tsdb,
endpoint: tsdbStatusAPI,
values: map[string][]string{"limit": {"10000"}},
errType: errorNone,
},
{
db: tsdb,
endpoint: tsdbStatusAPI,
values: map[string][]string{"limit": {"10001"}},
errType: errorBadData,
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
api := &API{db: tc.db, gatherer: prometheus.DefaultGatherer}