From 2261b8fff82b8780d386fa8f8875eb9d203fa614 Mon Sep 17 00:00:00 2001 From: PJ Date: Thu, 30 Nov 2023 11:32:57 +0100 Subject: [PATCH] bus: return 400 if mandatory query params are not set --- bus/bus.go | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/bus/bus.go b/bus/bus.go index d1ffb5e37..520948db8 100644 --- a/bus/bus.go +++ b/bus/bus.go @@ -495,7 +495,7 @@ func (b *bus) bucketHandlerGET(jc jape.Context) { if jc.DecodeParam("name", &name) != nil { return } else if name == "" { - jc.Error(errors.New("no name provided"), http.StatusBadRequest) + jc.Error(errors.New("parameter 'name' is required"), http.StatusBadRequest) return } bucket, err := b.ms.Bucket(jc.Request.Context(), name) @@ -933,7 +933,7 @@ func (b *bus) contractsSetsHandlerGET(jc jape.Context) { func (b *bus) contractsSetHandlerPUT(jc jape.Context) { var contractIds []types.FileContractID if set := jc.PathParam("set"); set == "" { - jc.Error(errors.New("param 'set' can not be empty"), http.StatusBadRequest) + jc.Error(errors.New("path parameter 'set' can not be empty"), http.StatusBadRequest) } else if jc.Decode(&contractIds) == nil { jc.Check("could not add contracts to set", b.ms.SetContractSet(jc.Request.Context(), set, contractIds)) } @@ -1489,15 +1489,15 @@ func (b *bus) slabsPartialHandlerPOST(jc jape.Context) { return } if minShards <= 0 || totalShards <= minShards { - jc.Error(errors.New("min_shards must be positive and total_shards must be greater than min_shards"), http.StatusBadRequest) + jc.Error(errors.New("minShards must be positive and totalShards must be greater than minShards"), http.StatusBadRequest) return } if totalShards > math.MaxUint8 { - jc.Error(fmt.Errorf("total_shards must be less than or equal to %d", math.MaxUint8), http.StatusBadRequest) + jc.Error(fmt.Errorf("totalShards must be less than or equal to %d", math.MaxUint8), http.StatusBadRequest) return } if contractSet == "" { - jc.Error(fmt.Errorf("contract_set must be non-empty"), http.StatusBadRequest) + jc.Error(errors.New("parameter 'contractSet' is required"), http.StatusBadRequest) return } data, err := io.ReadAll(jc.Request.Body) @@ -1528,7 +1528,7 @@ func (b *bus) settingsHandlerGET(jc jape.Context) { func (b *bus) settingKeyHandlerGET(jc jape.Context) { key := jc.PathParam("key") if key == "" { - jc.Error(errors.New("param 'key' can not be empty"), http.StatusBadRequest) + jc.Error(errors.New("path parameter 'key' can not be empty"), http.StatusBadRequest) return } @@ -1555,7 +1555,7 @@ func (b *bus) settingKeyHandlerGET(jc jape.Context) { func (b *bus) settingKeyHandlerPUT(jc jape.Context) { key := jc.PathParam("key") if key == "" { - jc.Error(errors.New("param 'key' can not be empty"), http.StatusBadRequest) + jc.Error(errors.New("path parameter 'key' can not be empty"), http.StatusBadRequest) return } @@ -1606,7 +1606,7 @@ func (b *bus) settingKeyHandlerPUT(jc jape.Context) { func (b *bus) settingKeyHandlerDELETE(jc jape.Context) { key := jc.PathParam("key") if key == "" { - jc.Error(errors.New("param 'key' can not be empty"), http.StatusBadRequest) + jc.Error(errors.New("path parameter 'key' can not be empty"), http.StatusBadRequest) return } jc.Check("could not delete setting", b.ss.DeleteSetting(jc.Request.Context(), key)) @@ -2011,13 +2011,26 @@ func (b *bus) metricsHandlerPUT(jc jape.Context) { func (b *bus) metricsHandlerGET(jc jape.Context) { // parse mandatory query parameters var start time.Time - var n uint64 - var interval time.Duration if jc.DecodeForm("start", (*api.TimeRFC3339)(&start)) != nil { return - } else if jc.DecodeForm("n", &n) != nil { + } else if start.IsZero() { + jc.Error(errors.New("parameter 'start' is required"), http.StatusBadRequest) + return + } + + var n uint64 + if jc.DecodeForm("n", &n) != nil { + return + } else if n == 0 { + jc.Error(errors.New("parameter 'n' is required"), http.StatusBadRequest) + return + } + + var interval time.Duration + if jc.DecodeForm("interval", (*api.DurationMS)(&interval)) != nil { return - } else if jc.DecodeForm("interval", (*api.DurationMS)(&interval)) != nil { + } else if interval == 0 { + jc.Error(errors.New("parameter 'interval' is required"), http.StatusBadRequest) return }