Path: blob/dev/pkg/testutils/fuzzplayground/sqli_test.go
2070 views
package fuzzplayground12import (3"fmt"4"net/http"5"net/http/httptest"6"testing"78"github.com/stretchr/testify/require"9)1011func TestSQLInjectionBehavior(t *testing.T) {12server := GetPlaygroundServer()13ts := httptest.NewServer(server)14defer ts.Close()1516tests := []struct {17name string18path string19expectedStatus int20shouldContainAdmin bool21}{22{23name: "Normal request",24path: "/user/75/profile", // User 75 exists and has role 'user'25expectedStatus: 200,26shouldContainAdmin: false,27},28{29name: "SQL injection with OR 1=1",30path: "/user/75 OR 1=1/profile",31expectedStatus: 200, // Should work but might return first user (admin)32shouldContainAdmin: true, // Should return admin user data33},34{35name: "SQL injection with UNION",36path: "/user/1 UNION SELECT 1,'admin',30,'admin'/profile",37expectedStatus: 200,38shouldContainAdmin: true,39},40{41name: "Template payload test - OR True with 75",42path: "/user/75 OR True/profile", // What the template actually sends43expectedStatus: 200, // Actually works!44shouldContainAdmin: true, // Let's see if it returns admin45},46{47name: "Template payload test - OR True with 55 (non-existent)",48path: "/user/55 OR True/profile", // What the template should actually send49expectedStatus: 200, // Should work due to SQL injection50shouldContainAdmin: true, // Should return admin due to OR True51},52{53name: "Test original user 55 issue",54path: "/user/55/profile", // This should fail because user 55 doesn't exist55expectedStatus: 500,56shouldContainAdmin: false,57},58{59name: "Invalid ID - non-existent",60path: "/user/999/profile",61expectedStatus: 500, // Should error due to no such user62shouldContainAdmin: false,63},64}6566for _, tt := range tests {67t.Run(tt.name, func(t *testing.T) {68resp, err := http.Get(ts.URL + tt.path)69require.NoError(t, err)70defer func() {71if err := resp.Body.Close(); err != nil {72t.Logf("Failed to close response body: %v", err)73}74}()7576require.Equal(t, tt.expectedStatus, resp.StatusCode)7778body := make([]byte, 1024)79n, _ := resp.Body.Read(body)80bodyStr := string(body[:n])8182fmt.Printf("Request: %s\n", tt.path)83fmt.Printf("Status: %d\n", resp.StatusCode)84fmt.Printf("Response: %s\n\n", bodyStr)8586if tt.shouldContainAdmin {87require.Contains(t, bodyStr, "admin")88}89})90}91}9293