|
| 1 | +-- Row Level Security (RLS) Migration |
| 2 | +-- Adds database-level tenant isolation as defense-in-depth |
| 3 | +-- The application already scopes queries by workspaceId; RLS adds a second layer |
| 4 | + |
| 5 | +-- ============================================================================ |
| 6 | +-- ENABLE RLS ON WORKSPACE-SCOPED TABLES |
| 7 | +-- ============================================================================ |
| 8 | + |
| 9 | +-- Agent table |
| 10 | +ALTER TABLE "Agent" ENABLE ROW LEVEL SECURITY; |
| 11 | +ALTER TABLE "Agent" FORCE ROW LEVEL SECURITY; |
| 12 | + |
| 13 | +-- Service table |
| 14 | +ALTER TABLE "Service" ENABLE ROW LEVEL SECURITY; |
| 15 | +ALTER TABLE "Service" FORCE ROW LEVEL SECURITY; |
| 16 | + |
| 17 | +-- ApiKey table |
| 18 | +ALTER TABLE "ApiKey" ENABLE ROW LEVEL SECURITY; |
| 19 | +ALTER TABLE "ApiKey" FORCE ROW LEVEL SECURITY; |
| 20 | + |
| 21 | +-- DebugSession table |
| 22 | +ALTER TABLE "DebugSession" ENABLE ROW LEVEL SECURITY; |
| 23 | +ALTER TABLE "DebugSession" FORCE ROW LEVEL SECURITY; |
| 24 | + |
| 25 | +-- EnvironmentShare table |
| 26 | +ALTER TABLE "EnvironmentShare" ENABLE ROW LEVEL SECURITY; |
| 27 | +ALTER TABLE "EnvironmentShare" FORCE ROW LEVEL SECURITY; |
| 28 | + |
| 29 | +-- AgentMessage table |
| 30 | +ALTER TABLE "AgentMessage" ENABLE ROW LEVEL SECURITY; |
| 31 | +ALTER TABLE "AgentMessage" FORCE ROW LEVEL SECURITY; |
| 32 | + |
| 33 | +-- Webhook table |
| 34 | +ALTER TABLE "Webhook" ENABLE ROW LEVEL SECURITY; |
| 35 | +ALTER TABLE "Webhook" FORCE ROW LEVEL SECURITY; |
| 36 | + |
| 37 | +-- ============================================================================ |
| 38 | +-- CREATE RLS POLICIES |
| 39 | +-- ============================================================================ |
| 40 | +-- Policy logic: |
| 41 | +-- - If app.current_workspace_id = '__rls_bypass__' → allow (for admin/migrations) |
| 42 | +-- - If app.current_workspace_id matches workspaceId → allow |
| 43 | +-- - Otherwise → deny (including when NULL/unset for security) |
| 44 | +-- |
| 45 | +-- This is a deny-by-default policy. The application MUST set the context before queries. |
| 46 | + |
| 47 | +-- Agent policies |
| 48 | +CREATE POLICY workspace_isolation_select ON "Agent" |
| 49 | + FOR SELECT |
| 50 | + USING ( |
| 51 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 52 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 53 | + ); |
| 54 | + |
| 55 | +CREATE POLICY workspace_isolation_insert ON "Agent" |
| 56 | + FOR INSERT |
| 57 | + WITH CHECK ( |
| 58 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 59 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 60 | + ); |
| 61 | + |
| 62 | +CREATE POLICY workspace_isolation_update ON "Agent" |
| 63 | + FOR UPDATE |
| 64 | + USING ( |
| 65 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 66 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 67 | + ); |
| 68 | + |
| 69 | +CREATE POLICY workspace_isolation_delete ON "Agent" |
| 70 | + FOR DELETE |
| 71 | + USING ( |
| 72 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 73 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 74 | + ); |
| 75 | + |
| 76 | +-- Service policies |
| 77 | +CREATE POLICY workspace_isolation_select ON "Service" |
| 78 | + FOR SELECT |
| 79 | + USING ( |
| 80 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 81 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 82 | + ); |
| 83 | + |
| 84 | +CREATE POLICY workspace_isolation_insert ON "Service" |
| 85 | + FOR INSERT |
| 86 | + WITH CHECK ( |
| 87 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 88 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 89 | + ); |
| 90 | + |
| 91 | +CREATE POLICY workspace_isolation_update ON "Service" |
| 92 | + FOR UPDATE |
| 93 | + USING ( |
| 94 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 95 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 96 | + ); |
| 97 | + |
| 98 | +CREATE POLICY workspace_isolation_delete ON "Service" |
| 99 | + FOR DELETE |
| 100 | + USING ( |
| 101 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 102 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 103 | + ); |
| 104 | + |
| 105 | +-- ApiKey policies |
| 106 | +CREATE POLICY workspace_isolation_select ON "ApiKey" |
| 107 | + FOR SELECT |
| 108 | + USING ( |
| 109 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 110 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 111 | + ); |
| 112 | + |
| 113 | +CREATE POLICY workspace_isolation_insert ON "ApiKey" |
| 114 | + FOR INSERT |
| 115 | + WITH CHECK ( |
| 116 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 117 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 118 | + ); |
| 119 | + |
| 120 | +CREATE POLICY workspace_isolation_update ON "ApiKey" |
| 121 | + FOR UPDATE |
| 122 | + USING ( |
| 123 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 124 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 125 | + ); |
| 126 | + |
| 127 | +CREATE POLICY workspace_isolation_delete ON "ApiKey" |
| 128 | + FOR DELETE |
| 129 | + USING ( |
| 130 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 131 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 132 | + ); |
| 133 | + |
| 134 | +-- DebugSession policies |
| 135 | +CREATE POLICY workspace_isolation_select ON "DebugSession" |
| 136 | + FOR SELECT |
| 137 | + USING ( |
| 138 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 139 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 140 | + ); |
| 141 | + |
| 142 | +CREATE POLICY workspace_isolation_insert ON "DebugSession" |
| 143 | + FOR INSERT |
| 144 | + WITH CHECK ( |
| 145 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 146 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 147 | + ); |
| 148 | + |
| 149 | +CREATE POLICY workspace_isolation_update ON "DebugSession" |
| 150 | + FOR UPDATE |
| 151 | + USING ( |
| 152 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 153 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 154 | + ); |
| 155 | + |
| 156 | +CREATE POLICY workspace_isolation_delete ON "DebugSession" |
| 157 | + FOR DELETE |
| 158 | + USING ( |
| 159 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 160 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 161 | + ); |
| 162 | + |
| 163 | +-- EnvironmentShare policies |
| 164 | +CREATE POLICY workspace_isolation_select ON "EnvironmentShare" |
| 165 | + FOR SELECT |
| 166 | + USING ( |
| 167 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 168 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 169 | + ); |
| 170 | + |
| 171 | +CREATE POLICY workspace_isolation_insert ON "EnvironmentShare" |
| 172 | + FOR INSERT |
| 173 | + WITH CHECK ( |
| 174 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 175 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 176 | + ); |
| 177 | + |
| 178 | +CREATE POLICY workspace_isolation_update ON "EnvironmentShare" |
| 179 | + FOR UPDATE |
| 180 | + USING ( |
| 181 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 182 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 183 | + ); |
| 184 | + |
| 185 | +CREATE POLICY workspace_isolation_delete ON "EnvironmentShare" |
| 186 | + FOR DELETE |
| 187 | + USING ( |
| 188 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 189 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 190 | + ); |
| 191 | + |
| 192 | +-- AgentMessage policies |
| 193 | +CREATE POLICY workspace_isolation_select ON "AgentMessage" |
| 194 | + FOR SELECT |
| 195 | + USING ( |
| 196 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 197 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 198 | + ); |
| 199 | + |
| 200 | +CREATE POLICY workspace_isolation_insert ON "AgentMessage" |
| 201 | + FOR INSERT |
| 202 | + WITH CHECK ( |
| 203 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 204 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 205 | + ); |
| 206 | + |
| 207 | +CREATE POLICY workspace_isolation_update ON "AgentMessage" |
| 208 | + FOR UPDATE |
| 209 | + USING ( |
| 210 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 211 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 212 | + ); |
| 213 | + |
| 214 | +CREATE POLICY workspace_isolation_delete ON "AgentMessage" |
| 215 | + FOR DELETE |
| 216 | + USING ( |
| 217 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 218 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 219 | + ); |
| 220 | + |
| 221 | +-- Webhook policies |
| 222 | +CREATE POLICY workspace_isolation_select ON "Webhook" |
| 223 | + FOR SELECT |
| 224 | + USING ( |
| 225 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 226 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 227 | + ); |
| 228 | + |
| 229 | +CREATE POLICY workspace_isolation_insert ON "Webhook" |
| 230 | + FOR INSERT |
| 231 | + WITH CHECK ( |
| 232 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 233 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 234 | + ); |
| 235 | + |
| 236 | +CREATE POLICY workspace_isolation_update ON "Webhook" |
| 237 | + FOR UPDATE |
| 238 | + USING ( |
| 239 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 240 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 241 | + ); |
| 242 | + |
| 243 | +CREATE POLICY workspace_isolation_delete ON "Webhook" |
| 244 | + FOR DELETE |
| 245 | + USING ( |
| 246 | + current_setting('app.current_workspace_id', true) = '__rls_bypass__' |
| 247 | + OR "workspaceId" = current_setting('app.current_workspace_id', true) |
| 248 | + ); |
| 249 | + |
| 250 | +-- ============================================================================ |
| 251 | +-- NOTES |
| 252 | +-- ============================================================================ |
| 253 | +-- 1. Policies are DENY by default - if app.current_workspace_id is not set, access is denied |
| 254 | +-- 2. Use '__rls_bypass__' for admin/migration operations that need cross-workspace access |
| 255 | +-- 3. FORCE ROW LEVEL SECURITY ensures even table owners are subject to RLS |
| 256 | +-- 4. The application must SET app.current_workspace_id before queries |
| 257 | +-- 5. Tables without direct workspaceId (like DiagnosticResult) inherit isolation |
| 258 | +-- through their foreign key relationships |
0 commit comments