mirror of
https://github.com/prometheus/prometheus.git
synced 2025-12-16 23:11:02 +01:00
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:
commit
e77dd5bec2
@ -1346,7 +1346,7 @@ GET /api/v1/status/tsdb
|
|||||||
```
|
```
|
||||||
URL query parameters:
|
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:
|
The `data` section of the query result consists of:
|
||||||
|
|
||||||
|
|||||||
@ -1837,12 +1837,16 @@ func (api *API) serveTSDBBlocks(*http.Request) apiFuncResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (api *API) serveTSDBStatus(r *http.Request) apiFuncResult {
|
func (api *API) serveTSDBStatus(r *http.Request) apiFuncResult {
|
||||||
|
const maxTSDBLimit = 10000
|
||||||
limit := 10
|
limit := 10
|
||||||
if s := r.FormValue("limit"); s != "" {
|
if s := r.FormValue("limit"); s != "" {
|
||||||
var err error
|
var err error
|
||||||
if limit, err = strconv.Atoi(s); err != nil || limit < 1 {
|
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}
|
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)
|
s, err := api.db.Stats(labels.MetricName, limit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -4465,6 +4465,18 @@ func TestTSDBStatus(t *testing.T) {
|
|||||||
values: map[string][]string{"limit": {"0"}},
|
values: map[string][]string{"limit": {"0"}},
|
||||||
errType: errorBadData,
|
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) {
|
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||||
api := &API{db: tc.db, gatherer: prometheus.DefaultGatherer}
|
api := &API{db: tc.db, gatherer: prometheus.DefaultGatherer}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user