|
| 1 | +package query |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/jackc/pgconn" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "github.com/supabase/cli/internal/utils" |
| 13 | + "github.com/supabase/cli/pkg/pgtest" |
| 14 | +) |
| 15 | + |
| 16 | +func TestCheckRLSAdvisoryWithUnprotectedTables(t *testing.T) { |
| 17 | + utils.Config.Hostname = "127.0.0.1" |
| 18 | + utils.Config.Db.Port = 5432 |
| 19 | + |
| 20 | + conn := pgtest.NewConn() |
| 21 | + defer conn.Close(t) |
| 22 | + conn.Query(rlsCheckSQL). |
| 23 | + Reply("SELECT 2", []any{"public.users"}, []any{"public.posts"}) |
| 24 | + |
| 25 | + config := pgconn.Config{ |
| 26 | + Host: "127.0.0.1", |
| 27 | + Port: 5432, |
| 28 | + User: "admin", |
| 29 | + Password: "password", |
| 30 | + Database: "postgres", |
| 31 | + } |
| 32 | + pgConn, err := utils.ConnectByConfig(context.Background(), config, conn.Intercept) |
| 33 | + require.NoError(t, err) |
| 34 | + defer pgConn.Close(context.Background()) |
| 35 | + |
| 36 | + advisory := checkRLSAdvisory(context.Background(), pgConn) |
| 37 | + require.NotNil(t, advisory) |
| 38 | + assert.Equal(t, "rls_disabled", advisory.ID) |
| 39 | + assert.Equal(t, 1, advisory.Priority) |
| 40 | + assert.Equal(t, "critical", advisory.Level) |
| 41 | + assert.Contains(t, advisory.Message, "2 table(s)") |
| 42 | + assert.Contains(t, advisory.Message, "public.users") |
| 43 | + assert.Contains(t, advisory.Message, "public.posts") |
| 44 | + assert.Equal(t, |
| 45 | + "ALTER TABLE public.users ENABLE ROW LEVEL SECURITY;\nALTER TABLE public.posts ENABLE ROW LEVEL SECURITY;", |
| 46 | + advisory.RemediationSQL, |
| 47 | + ) |
| 48 | +} |
| 49 | + |
| 50 | +func TestCheckRLSAdvisoryNoUnprotectedTables(t *testing.T) { |
| 51 | + utils.Config.Hostname = "127.0.0.1" |
| 52 | + utils.Config.Db.Port = 5432 |
| 53 | + |
| 54 | + conn := pgtest.NewConn() |
| 55 | + defer conn.Close(t) |
| 56 | + conn.Query(rlsCheckSQL). |
| 57 | + Reply("SELECT 0") |
| 58 | + |
| 59 | + config := pgconn.Config{ |
| 60 | + Host: "127.0.0.1", |
| 61 | + Port: 5432, |
| 62 | + User: "admin", |
| 63 | + Password: "password", |
| 64 | + Database: "postgres", |
| 65 | + } |
| 66 | + pgConn, err := utils.ConnectByConfig(context.Background(), config, conn.Intercept) |
| 67 | + require.NoError(t, err) |
| 68 | + defer pgConn.Close(context.Background()) |
| 69 | + |
| 70 | + advisory := checkRLSAdvisory(context.Background(), pgConn) |
| 71 | + assert.Nil(t, advisory) |
| 72 | +} |
| 73 | + |
| 74 | +func TestWriteJSONWithAdvisory(t *testing.T) { |
| 75 | + advisory := &Advisory{ |
| 76 | + ID: "rls_disabled", |
| 77 | + Priority: 1, |
| 78 | + Level: "critical", |
| 79 | + Title: "Row Level Security is disabled", |
| 80 | + Message: "1 table(s) do not have RLS enabled: public.test.", |
| 81 | + RemediationSQL: "ALTER TABLE public.test ENABLE ROW LEVEL SECURITY;", |
| 82 | + DocURL: "https://supabase.com/docs/guides/database/postgres/row-level-security", |
| 83 | + } |
| 84 | + |
| 85 | + cols := []string{"id", "name"} |
| 86 | + data := [][]interface{}{{int64(1), "test"}} |
| 87 | + |
| 88 | + var buf bytes.Buffer |
| 89 | + err := writeJSON(&buf, cols, data, true, advisory) |
| 90 | + assert.NoError(t, err) |
| 91 | + |
| 92 | + var envelope map[string]interface{} |
| 93 | + require.NoError(t, json.Unmarshal(buf.Bytes(), &envelope)) |
| 94 | + |
| 95 | + // Verify standard envelope fields |
| 96 | + assert.Contains(t, envelope["warning"], "untrusted data") |
| 97 | + assert.NotEmpty(t, envelope["boundary"]) |
| 98 | + rows, ok := envelope["rows"].([]interface{}) |
| 99 | + require.True(t, ok) |
| 100 | + assert.Len(t, rows, 1) |
| 101 | + |
| 102 | + // Verify advisory is present |
| 103 | + advisoryMap, ok := envelope["advisory"].(map[string]interface{}) |
| 104 | + require.True(t, ok) |
| 105 | + assert.Equal(t, "rls_disabled", advisoryMap["id"]) |
| 106 | + assert.Equal(t, float64(1), advisoryMap["priority"]) |
| 107 | + assert.Equal(t, "critical", advisoryMap["level"]) |
| 108 | + assert.Contains(t, advisoryMap["message"], "public.test") |
| 109 | + assert.Contains(t, advisoryMap["remediation_sql"], "ENABLE ROW LEVEL SECURITY") |
| 110 | + assert.Contains(t, advisoryMap["doc_url"], "row-level-security") |
| 111 | +} |
| 112 | + |
| 113 | +func TestWriteJSONWithoutAdvisory(t *testing.T) { |
| 114 | + cols := []string{"id"} |
| 115 | + data := [][]interface{}{{int64(1)}} |
| 116 | + |
| 117 | + var buf bytes.Buffer |
| 118 | + err := writeJSON(&buf, cols, data, true, nil) |
| 119 | + assert.NoError(t, err) |
| 120 | + |
| 121 | + var envelope map[string]interface{} |
| 122 | + require.NoError(t, json.Unmarshal(buf.Bytes(), &envelope)) |
| 123 | + |
| 124 | + // Verify advisory is NOT present |
| 125 | + _, exists := envelope["advisory"] |
| 126 | + assert.False(t, exists) |
| 127 | +} |
| 128 | + |
| 129 | +func TestWriteJSONNonAgentModeNoAdvisory(t *testing.T) { |
| 130 | + advisory := &Advisory{ |
| 131 | + ID: "rls_disabled", |
| 132 | + Priority: 1, |
| 133 | + Level: "critical", |
| 134 | + Title: "Row Level Security is disabled", |
| 135 | + Message: "test", |
| 136 | + RemediationSQL: "test", |
| 137 | + DocURL: "test", |
| 138 | + } |
| 139 | + |
| 140 | + cols := []string{"id"} |
| 141 | + data := [][]interface{}{{int64(1)}} |
| 142 | + |
| 143 | + var buf bytes.Buffer |
| 144 | + err := writeJSON(&buf, cols, data, false, advisory) |
| 145 | + assert.NoError(t, err) |
| 146 | + |
| 147 | + // Non-agent mode: plain JSON array, no envelope or advisory |
| 148 | + var rows []map[string]interface{} |
| 149 | + require.NoError(t, json.Unmarshal(buf.Bytes(), &rows)) |
| 150 | + assert.Len(t, rows, 1) |
| 151 | +} |
| 152 | + |
| 153 | +func TestFormatOutputThreadsAdvisory(t *testing.T) { |
| 154 | + advisory := &Advisory{ |
| 155 | + ID: "rls_disabled", |
| 156 | + Priority: 1, |
| 157 | + Level: "critical", |
| 158 | + Title: "test", |
| 159 | + Message: "test", |
| 160 | + RemediationSQL: "test", |
| 161 | + DocURL: "test", |
| 162 | + } |
| 163 | + |
| 164 | + cols := []string{"id"} |
| 165 | + data := [][]interface{}{{int64(1)}} |
| 166 | + |
| 167 | + // JSON agent mode should include advisory |
| 168 | + var buf bytes.Buffer |
| 169 | + err := formatOutput(&buf, "json", true, cols, data, advisory) |
| 170 | + assert.NoError(t, err) |
| 171 | + |
| 172 | + var envelope map[string]interface{} |
| 173 | + require.NoError(t, json.Unmarshal(buf.Bytes(), &envelope)) |
| 174 | + _, exists := envelope["advisory"] |
| 175 | + assert.True(t, exists) |
| 176 | +} |
| 177 | + |
| 178 | +func TestFormatOutputCSVIgnoresAdvisory(t *testing.T) { |
| 179 | + advisory := &Advisory{ |
| 180 | + ID: "rls_disabled", |
| 181 | + Priority: 1, |
| 182 | + Level: "critical", |
| 183 | + Title: "test", |
| 184 | + Message: "test", |
| 185 | + RemediationSQL: "test", |
| 186 | + DocURL: "test", |
| 187 | + } |
| 188 | + |
| 189 | + cols := []string{"id"} |
| 190 | + data := [][]interface{}{{int64(1)}} |
| 191 | + |
| 192 | + // CSV format should not include advisory (CSV has no envelope) |
| 193 | + var buf bytes.Buffer |
| 194 | + err := formatOutput(&buf, "csv", false, cols, data, advisory) |
| 195 | + assert.NoError(t, err) |
| 196 | + assert.Contains(t, buf.String(), "id") |
| 197 | + assert.Contains(t, buf.String(), "1") |
| 198 | + assert.NotContains(t, buf.String(), "advisory") |
| 199 | +} |
| 200 | + |
| 201 | +func TestFormatOutputTableIgnoresAdvisory(t *testing.T) { |
| 202 | + advisory := &Advisory{ |
| 203 | + ID: "rls_disabled", |
| 204 | + Priority: 1, |
| 205 | + Level: "critical", |
| 206 | + Title: "test", |
| 207 | + Message: "test", |
| 208 | + RemediationSQL: "test", |
| 209 | + DocURL: "test", |
| 210 | + } |
| 211 | + |
| 212 | + cols := []string{"id"} |
| 213 | + data := [][]interface{}{{int64(1)}} |
| 214 | + |
| 215 | + // Table format should not include advisory |
| 216 | + var buf bytes.Buffer |
| 217 | + err := formatOutput(&buf, "table", false, cols, data, advisory) |
| 218 | + assert.NoError(t, err) |
| 219 | + assert.NotContains(t, buf.String(), "advisory") |
| 220 | +} |
0 commit comments