|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "net/http" |
4 | 7 | "testing"
|
5 | 8 |
|
6 | 9 | "github.com/stretchr/testify/assert"
|
@@ -28,3 +31,66 @@ func TestSecurityNegativeCases(t *testing.T) {
|
28 | 31 | assert.Contains(t, err.Error(), "Access Restricted")
|
29 | 32 | })
|
30 | 33 | }
|
| 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