Skip to content

Commit 23ae76b

Browse files
committed
test: add basic sql injection tests
close #10
1 parent 9d3bd14 commit 23ae76b

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

integration_security_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package main
22

33
import (
4+
"bytes"
5+
"io"
6+
"net/http"
47
"testing"
58

69
"github.com/stretchr/testify/assert"
@@ -28,3 +31,66 @@ func TestSecurityNegativeCases(t *testing.T) {
2831
assert.Contains(t, err.Error(), "Access Restricted")
2932
})
3033
}
34+
35+
func TestSecuritySQLInjection(t *testing.T) {
36+
t.Run("Update", func(t *testing.T) {
37+
tc := createTestContextWithHMACTokenAuth(t)
38+
defer tc.CleanUp(t)
39+
40+
tc.ExecuteSQL(t, "CREATE TABLE test (id int)")
41+
tc.ExecuteSQL(t, "insert into test values (1)")
42+
43+
p := bytes.NewBufferString(`{"id": 2}`)
44+
req := tc.NewRequest(t, http.MethodPost, "test", p)
45+
req.Header.Set("content-type", "application/json")
46+
q := req.URL.Query()
47+
q.Set("select", "1; drop table test;select *")
48+
req.URL.RawQuery = q.Encode()
49+
50+
resp := tc.ExecuteRequest(t, req)
51+
defer resp.Body.Close()
52+
53+
assert.Equal(t, http.StatusCreated, resp.StatusCode)
54+
55+
_, err := io.ReadAll(resp.Body)
56+
assert.NoError(t, err)
57+
58+
client := tc.Client()
59+
res, _, err := client.From("test").Select("*", "", false).Execute()
60+
assert.NoError(t, err)
61+
62+
var rv []map[string]interface{}
63+
tc.DecodeResult(t, res, &rv)
64+
assert.Len(t, rv, 2)
65+
})
66+
67+
t.Run("Select", func(t *testing.T) {
68+
tc := createTestContextWithHMACTokenAuth(t)
69+
defer tc.CleanUp(t)
70+
71+
tc.ExecuteSQL(t, "CREATE TABLE test (id int)")
72+
tc.ExecuteSQL(t, "insert into test values (1)")
73+
74+
req := tc.NewRequest(t, http.MethodGet, "test", nil)
75+
req.Header.Set("content-type", "application/json")
76+
q := req.URL.Query()
77+
q.Set("select", "1; drop table test;select *")
78+
req.URL.RawQuery = q.Encode()
79+
80+
resp := tc.ExecuteRequest(t, req)
81+
defer resp.Body.Close()
82+
83+
assert.Equal(t, http.StatusOK, resp.StatusCode)
84+
85+
_, err := io.ReadAll(resp.Body)
86+
assert.NoError(t, err)
87+
88+
client := tc.Client()
89+
res, _, err := client.From("test").Select("*", "", false).Execute()
90+
assert.NoError(t, err)
91+
92+
var rv []map[string]interface{}
93+
tc.DecodeResult(t, res, &rv)
94+
assert.Len(t, rv, 1)
95+
})
96+
}

0 commit comments

Comments
 (0)