Skip to content

Commit 3924ef2

Browse files
authored
SpectoLabs#1165 add validation to the put state endpoint (SpectoLabs#1166)
1 parent b30e00f commit 3924ef2

File tree

150 files changed

+30188
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

150 files changed

+30188
-3
lines changed

core/handlers/v2/state_handler.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import (
88

99
"github.com/SpectoLabs/hoverfly/core/handlers"
1010
"github.com/codegangsta/negroni"
11+
"github.com/go-playground/validator/v10"
1112
"github.com/go-zoo/bone"
1213
)
1314

15+
var validate = validator.New()
16+
1417
type StateHandler struct {
1518
Hoverfly Hoverfly
1619
}
@@ -61,10 +64,14 @@ func (this *StateHandler) Put(w http.ResponseWriter, req *http.Request, next htt
6164

6265
toPut := &StateView{}
6366

64-
responseBody, err := ioutil.ReadAll(req.Body)
67+
err := json.NewDecoder(req.Body).Decode(toPut)
6568

66-
err = json.Unmarshal(responseBody, &toPut)
69+
if err != nil {
70+
handlers.WriteErrorResponse(w, err.Error(), http.StatusBadRequest)
71+
return
72+
}
6773

74+
err = validate.Struct(toPut)
6875
if err != nil {
6976
handlers.WriteErrorResponse(w, err.Error(), http.StatusBadRequest)
7077
return

core/handlers/v2/views.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ type JournalEntryFilterView struct {
117117
}
118118

119119
type StateView struct {
120-
State map[string]string `json:"state"`
120+
State map[string]string `json:"state" validate:"required"`
121121
}
122122

123123
type DiffView struct {

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/codegangsta/negroni v1.0.0
1616
github.com/dghubble/sling v1.4.2
1717
github.com/dsnet/compress v0.0.1
18+
github.com/go-playground/validator/v10 v10.23.0
1819
github.com/go-zoo/bone v1.3.0
1920
github.com/golang-jwt/jwt/v4 v4.5.1
2021
github.com/gorilla/mux v1.8.1
@@ -49,12 +50,16 @@ require (
4950
github.com/aymerick/raymond v2.0.2+incompatible // indirect
5051
github.com/corpix/uarand v0.2.0 // indirect
5152
github.com/fsnotify/fsnotify v1.8.0 // indirect
53+
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
54+
github.com/go-playground/locales v0.14.1 // indirect
55+
github.com/go-playground/universal-translator v0.18.1 // indirect
5256
github.com/goccmack/goutil v1.2.3 // indirect
5357
github.com/google/go-cmp v0.6.0 // indirect
5458
github.com/google/go-querystring v1.1.0 // indirect
5559
github.com/google/uuid v1.6.0 // indirect
5660
github.com/hashicorp/hcl v1.0.0 // indirect
5761
github.com/inconshreveable/mousetrap v1.1.0 // indirect
62+
github.com/leodido/go-urn v1.4.0 // indirect
5863
github.com/magiconair/properties v1.8.9 // indirect
5964
github.com/mattn/go-runewidth v0.0.16 // indirect
6065
github.com/mitchellh/mapstructure v1.5.0 // indirect

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,18 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
4545
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
4646
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
4747
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
48+
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
49+
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
4850
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
4951
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
52+
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
53+
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
54+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
55+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
56+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
57+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
58+
github.com/go-playground/validator/v10 v10.23.0 h1:/PwmTwZhS0dPkav3cdK9kV1FsAmrL8sThn8IHr/sO+o=
59+
github.com/go-playground/validator/v10 v10.23.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
5060
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
5161
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
5262
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
@@ -103,6 +113,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
103113
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
104114
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
105115
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
116+
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
117+
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
106118
github.com/magiconair/properties v1.8.9 h1:nWcCbLq1N2v/cpNsy5WvQ37Fb+YElfq20WJ/a8RkpQM=
107119
github.com/magiconair/properties v1.8.9/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
108120
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=

vendor/github.com/gabriel-vasile/mimetype/.gitattributes

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gabriel-vasile/mimetype/CODE_OF_CONDUCT.md

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gabriel-vasile/mimetype/CONTRIBUTING.md

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gabriel-vasile/mimetype/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gabriel-vasile/mimetype/README.md

Lines changed: 105 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)