Skip to content

Commit f3db5c0

Browse files
Merge pull request #261 from supertokens/input-validation-fix
fix: build fix
2 parents 95dcb7b + 6ca9752 commit f3db5c0

File tree

4 files changed

+131
-50
lines changed

4 files changed

+131
-50
lines changed

examples/go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt
214214
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
215215
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
216216
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
217+
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
217218
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
218219
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
219220
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -423,6 +424,7 @@ github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uY
423424
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
424425
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
425426
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
427+
github.com/nyaruka/phonenumbers v1.0.73 h1:bP2WN8/NUP8tQebR+WCIejFaibwYMHOaB7MQVayclUo=
426428
github.com/nyaruka/phonenumbers v1.0.73/go.mod h1:3aiS+PS3DuYwkbK3xdcmRwMiPNECZ0oENH8qUT1lY7Q=
427429
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
428430
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
@@ -542,6 +544,7 @@ github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69
542544
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
543545
github.com/tklauser/go-sysconf v0.3.10/go.mod h1:C8XykCvCb+Gn0oNCWPIlcb0RuglQTYaQ2hGm7jmxEFk=
544546
github.com/tklauser/numcpus v0.4.0/go.mod h1:1+UI3pD8NW14VMwdgJNJ1ESk2UnwhAnz5hMwiKKqXCQ=
547+
github.com/twilio/twilio-go v0.26.0 h1:wFW4oTe3/LKt6bvByP7eio8JsjtaLHjMQKOUEzQry7U=
545548
github.com/twilio/twilio-go v0.26.0/go.mod h1:lz62Hopu4vicpQ056H5TJ0JE4AP0rS3sQ35/ejmgOwE=
546549
github.com/twitchtv/twirp v8.1.0+incompatible h1:KGXanpa9LXdVE/V5P/tA27rkKFmXRGCtSNT7zdeeVOY=
547550
github.com/twitchtv/twirp v8.1.0+incompatible/go.mod h1:RRJoFSAmTEh2weEqWtpPE3vFK5YBhA6bqp2l1kfCC5A=

recipe/emailpassword/api/utils.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,49 @@ package api
1717

1818
import (
1919
"encoding/json"
20-
defaultErrors "errors"
2120
"strings"
2221

2322
"github.com/supertokens/supertokens-golang/recipe/emailpassword/epmodels"
2423
"github.com/supertokens/supertokens-golang/recipe/emailpassword/errors"
24+
"github.com/supertokens/supertokens-golang/supertokens"
2525
)
2626

2727
func validateFormFieldsOrThrowError(configFormFields []epmodels.NormalisedFormField, formFieldsRaw interface{}) ([]epmodels.TypeFormField, error) {
2828
if formFieldsRaw == nil {
29-
return nil, defaultErrors.New("Missing input param: formFields")
29+
return nil, supertokens.BadInputError{
30+
Msg: "Missing input param: formFields",
31+
}
3032
}
3133

3234
if _, ok := formFieldsRaw.([]interface{}); !ok {
33-
return nil, defaultErrors.New("formFields must be an array")
35+
return nil, supertokens.BadInputError{
36+
Msg: "formFields must be an array",
37+
}
3438
}
3539

3640
var formFields []epmodels.TypeFormField
3741
for _, rawFormField := range formFieldsRaw.([]interface{}) {
3842

3943
if _, ok := rawFormField.(map[string]interface{}); !ok {
40-
return nil, defaultErrors.New("formFields must be an array of objects containing id and value of type string")
44+
return nil, supertokens.BadInputError{
45+
Msg: "formFields must be an array of objects containing id and value of type string",
46+
}
4147
}
4248

43-
if _, ok := rawFormField.(map[string]interface{})["id"].(string); !ok {
44-
return nil, defaultErrors.New("formFields must be an array of objects containing id and value of type string")
49+
if rawFormField.(map[string]interface{})["id"] != nil {
50+
if _, ok := rawFormField.(map[string]interface{})["id"].(string); !ok {
51+
return nil, supertokens.BadInputError{
52+
Msg: "formFields must be an array of objects containing id and value of type string",
53+
}
54+
}
4555
}
4656

47-
if _, ok := rawFormField.(map[string]interface{})["value"].(string); !ok {
48-
return nil, defaultErrors.New("formFields must be an array of objects containing id and value of type string")
57+
if rawFormField.(map[string]interface{})["value"] != nil {
58+
if _, ok := rawFormField.(map[string]interface{})["value"].(string); !ok {
59+
return nil, supertokens.BadInputError{
60+
Msg: "formFields must be an array of objects containing id and value of type string",
61+
}
62+
}
4963
}
5064

5165
jsonformField, err := json.Marshal(rawFormField)
@@ -77,7 +91,9 @@ func validateFormFieldsOrThrowError(configFormFields []epmodels.NormalisedFormFi
7791
func validateFormOrThrowError(configFormFields []epmodels.NormalisedFormField, inputs []epmodels.TypeFormField) error {
7892
var validationErrors []errors.ErrorPayload
7993
if len(configFormFields) != len(inputs) {
80-
return defaultErrors.New("Are you sending too many / too few formFields?")
94+
return supertokens.BadInputError{
95+
Msg: "Are you sending too many / too few formFields?",
96+
}
8197
}
8298
for _, field := range configFormFields {
8399
var input epmodels.TypeFormField

recipe/emailpassword/authFlow_test.go

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -854,8 +854,13 @@ func TestFormFieldsHasNoEmailField(t *testing.T) {
854854

855855
resp.Body.Close()
856856

857-
assert.Equal(t, "Are you sending too many / too few formFields?\n", string(dataInBytes1))
858-
assert.Equal(t, 500, resp.StatusCode)
857+
assert.Equal(t, 400, resp.StatusCode)
858+
859+
err = json.Unmarshal(dataInBytes1, &data)
860+
if err != nil {
861+
t.Error(err.Error())
862+
}
863+
assert.Equal(t, "Are you sending too many / too few formFields?", data["message"].(string))
859864

860865
}
861866

@@ -938,8 +943,12 @@ func TestFormFieldsHasNoPasswordField(t *testing.T) {
938943

939944
resp.Body.Close()
940945

941-
assert.Equal(t, "Are you sending too many / too few formFields?\n", string(dataInBytes1))
942-
assert.Equal(t, 500, resp.StatusCode)
946+
assert.Equal(t, 400, resp.StatusCode)
947+
err = json.Unmarshal(dataInBytes1, &data)
948+
if err != nil {
949+
t.Error(err.Error())
950+
}
951+
assert.Equal(t, "Are you sending too many / too few formFields?", data["message"].(string))
943952

944953
}
945954

@@ -2147,8 +2156,13 @@ func TestFormFieldsAddedInConfigButNotInInputToSignupCheckErrorAboutItBeingMissi
21472156
t.Error(err.Error())
21482157
}
21492158
res.Body.Close()
2150-
assert.Equal(t, 500, res.StatusCode)
2151-
assert.Equal(t, "Are you sending too many / too few formFields?\n", string(dataInBytes))
2159+
assert.Equal(t, 400, res.StatusCode)
2160+
var data map[string]interface{}
2161+
err = json.Unmarshal(dataInBytes, &data)
2162+
if err != nil {
2163+
t.Error(err.Error())
2164+
}
2165+
assert.Equal(t, "Are you sending too many / too few formFields?", data["message"].(string))
21522166

21532167
}
21542168

@@ -2380,16 +2394,19 @@ func TestInputFormFieldWithoutEmailField(t *testing.T) {
23802394
t.Error(err.Error())
23812395
}
23822396

2383-
assert.Equal(t, 500, resp.StatusCode)
2384-
23852397
dataInBytes, err := io.ReadAll(resp.Body)
23862398
if err != nil {
23872399
t.Error(err.Error())
23882400
}
23892401
resp.Body.Close()
23902402

2391-
assert.Equal(t, 500, resp.StatusCode)
2392-
assert.Equal(t, "Are you sending too many / too few formFields?\n", string(dataInBytes))
2403+
assert.Equal(t, 400, resp.StatusCode)
2404+
var data map[string]interface{}
2405+
err = json.Unmarshal(dataInBytes, &data)
2406+
if err != nil {
2407+
t.Error(err.Error())
2408+
}
2409+
assert.Equal(t, "Are you sending too many / too few formFields?", data["message"].(string))
23932410

23942411
}
23952412

@@ -2444,17 +2461,19 @@ func TestInputFormFieldWithoutPasswordField(t *testing.T) {
24442461
t.Error(err.Error())
24452462
}
24462463

2447-
assert.Equal(t, 500, resp.StatusCode)
2448-
24492464
dataInBytes, err := io.ReadAll(resp.Body)
24502465
if err != nil {
24512466
t.Error(err.Error())
24522467
}
24532468
resp.Body.Close()
24542469

2455-
assert.Equal(t, 500, resp.StatusCode)
2456-
assert.Equal(t, "Are you sending too many / too few formFields?\n", string(dataInBytes))
2457-
2470+
assert.Equal(t, 400, resp.StatusCode)
2471+
var data map[string]interface{}
2472+
err = json.Unmarshal(dataInBytes, &data)
2473+
if err != nil {
2474+
t.Error(err.Error())
2475+
}
2476+
assert.Equal(t, "Are you sending too many / too few formFields?", data["message"].(string))
24582477
}
24592478

24602479
func TestInputFormFieldHasADifferentNumberOfCustomFiledsThanInConfigFormFields(t *testing.T) {
@@ -2529,16 +2548,19 @@ func TestInputFormFieldHasADifferentNumberOfCustomFiledsThanInConfigFormFields(t
25292548
t.Error(err.Error())
25302549
}
25312550

2532-
assert.Equal(t, 500, resp.StatusCode)
2533-
25342551
dataInBytes, err := io.ReadAll(resp.Body)
25352552
if err != nil {
25362553
t.Error(err.Error())
25372554
}
25382555
resp.Body.Close()
25392556

2540-
assert.Equal(t, 500, resp.StatusCode)
2541-
assert.Equal(t, "Are you sending too many / too few formFields?\n", string(dataInBytes))
2557+
assert.Equal(t, 400, resp.StatusCode)
2558+
var data map[string]interface{}
2559+
err = json.Unmarshal(dataInBytes, &data)
2560+
if err != nil {
2561+
t.Error(err.Error())
2562+
}
2563+
assert.Equal(t, "Are you sending too many / too few formFields?", data["message"].(string))
25422564

25432565
}
25442566

recipe/emailpassword/formFieldValidator_test.go

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ package emailpassword
1919
import (
2020
"bytes"
2121
"encoding/json"
22-
"io/ioutil"
22+
"io"
2323
"net/http"
2424
"net/http/httptest"
25-
"strings"
2625
"testing"
2726

2827
"github.com/stretchr/testify/assert"
@@ -98,34 +97,42 @@ func TestInvalidAPIInputForFormFields(t *testing.T) {
9897
}
9998

10099
testCases := []struct {
101-
input interface{}
102-
expected string
100+
input interface{}
101+
expected string
102+
fieldError bool
103103
}{
104104
{
105-
input: map[string]interface{}{},
106-
expected: "Missing input param: formFields",
105+
input: map[string]interface{}{},
106+
expected: "Missing input param: formFields",
107+
fieldError: false,
107108
},
108109
{
109110
input: map[string]interface{}{
110111
"formFields": "abcd",
111112
},
112-
expected: "formFields must be an array",
113+
expected: "formFields must be an array",
114+
fieldError: false,
113115
},
114116
{
115117
input: map[string]interface{}{
116118
"formFields": []string{"hello"},
117119
},
118-
expected: "formFields must be an array of objects containing id and value of type string",
120+
expected: "formFields must be an array of objects containing id and value of type string",
121+
fieldError: false,
119122
},
120123
{
121124
input: map[string]interface{}{
122125
"formFields": []map[string]interface{}{
123126
{
124127
"hello": "world",
125128
},
129+
{
130+
"world": "hello",
131+
},
126132
},
127133
},
128-
expected: "formFields must be an array of objects containing id and value of type string",
134+
expected: "Field is not optional",
135+
fieldError: true,
129136
},
130137
{
131138
input: map[string]interface{}{
@@ -135,7 +142,8 @@ func TestInvalidAPIInputForFormFields(t *testing.T) {
135142
},
136143
},
137144
},
138-
expected: "formFields must be an array of objects containing id and value of type string",
145+
expected: "formFields must be an array of objects containing id and value of type string",
146+
fieldError: false,
139147
},
140148
{
141149
input: map[string]interface{}{
@@ -146,7 +154,8 @@ func TestInvalidAPIInputForFormFields(t *testing.T) {
146154
},
147155
},
148156
},
149-
expected: "formFields must be an array of objects containing id and value of type string",
157+
expected: "formFields must be an array of objects containing id and value of type string",
158+
fieldError: false,
150159
},
151160
{
152161
input: map[string]interface{}{
@@ -157,7 +166,8 @@ func TestInvalidAPIInputForFormFields(t *testing.T) {
157166
},
158167
},
159168
},
160-
expected: "formFields must be an array of objects containing id and value of type string",
169+
expected: "formFields must be an array of objects containing id and value of type string",
170+
fieldError: false,
161171
},
162172
{
163173
input: map[string]interface{}{
@@ -167,46 +177,76 @@ func TestInvalidAPIInputForFormFields(t *testing.T) {
167177
},
168178
},
169179
},
170-
expected: "formFields must be an array of objects containing id and value of type string",
180+
expected: "formFields must be an array of objects containing id and value of type string",
181+
fieldError: false,
171182
},
172183
{
173184
input: map[string]interface{}{
174185
"formFields": []map[string]interface{}{
175186
{
176187
"id": "hello",
177188
},
189+
{
190+
"id": "world",
191+
},
178192
},
179193
},
180-
expected: "formFields must be an array of objects containing id and value of type string",
194+
expected: "Field is not optional",
195+
fieldError: true,
181196
},
182197
{
183198
input: map[string]interface{}{
184199
"formFields": []map[string]interface{}{
200+
{
201+
"value": "hello",
202+
},
185203
{
186204
"value": "world",
187205
},
188206
},
189207
},
190-
expected: "formFields must be an array of objects containing id and value of type string",
208+
expected: "Field is not optional",
209+
fieldError: true,
191210
},
192211
}
193212

194213
APIs := []string{
195214
"/auth/signup",
196215
"/auth/signin",
197-
"/auth/user/password/reset/token",
198-
"/auth/user/password/reset",
199216
}
200217

201218
for _, testCase := range testCases {
202219
for _, api := range APIs {
203220
resp, err := http.Post(testServer.URL+api, "application/json", bytes.NewBuffer(objToJson(testCase.input)))
204221
assert.NoError(t, err)
205-
assert.Equal(t, 500, resp.StatusCode)
206-
data, err := ioutil.ReadAll(resp.Body)
207-
assert.NoError(t, err)
208-
errorMessage := strings.Trim(string(data), "\n \t")
209-
assert.Equal(t, testCase.expected, errorMessage)
222+
223+
if testCase.fieldError {
224+
assert.Equal(t, 200, resp.StatusCode)
225+
} else {
226+
assert.Equal(t, 400, resp.StatusCode)
227+
}
228+
dataInBytes1, err := io.ReadAll(resp.Body)
229+
if err != nil {
230+
t.Error(err.Error())
231+
}
232+
resp.Body.Close()
233+
var data map[string]interface{}
234+
err = json.Unmarshal(dataInBytes1, &data)
235+
if err != nil {
236+
t.Error(err.Error())
237+
}
238+
239+
if testCase.fieldError {
240+
assert.Equal(t, "FIELD_ERROR", data["status"].(string))
241+
242+
for _, formField := range data["formFields"].([]interface{}) {
243+
errorMessage := formField.(map[string]interface{})["error"]
244+
assert.Equal(t, testCase.expected, errorMessage)
245+
}
246+
} else {
247+
assert.Equal(t, testCase.expected, data["message"])
248+
}
249+
210250
}
211251
}
212252
}

0 commit comments

Comments
 (0)