-
-
Notifications
You must be signed in to change notification settings - Fork 131
fix(rbac): stop mapping TUS auth DB failures to 403 insufficient_permissions #2843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+389
−56
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
39e04ed
fix(rbac): map permission-check DB failures to 503
cursoragent 794a939
fix(rbac): only map transient DB errors to 503
cursoragent 3a6614f
test(stats): bypass Kong for rollout metadata channel writes
cursoragent ec31f09
fix(rbac): address review comments on transient ACL mapping
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| import { HTTPException } from 'hono/http-exception' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const executeMock = vi.fn() | ||
| const getPgClientMock = vi.fn(() => ({})) | ||
| const getDrizzleClientMock = vi.fn(() => ({ | ||
| execute: executeMock, | ||
| })) | ||
| const closeClientMock = vi.fn() | ||
|
|
||
| vi.mock('../supabase/functions/_backend/utils/logging.ts', () => ({ | ||
| cloudlog: vi.fn(), | ||
| cloudlogErr: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('../supabase/functions/_backend/utils/pg.ts', () => ({ | ||
| closeClient: closeClientMock, | ||
| getDrizzleClient: getDrizzleClientMock, | ||
| getPgClient: getPgClientMock, | ||
| })) | ||
|
|
||
| const { checkPermission, checkPermissionPg } = await import('../supabase/functions/_backend/utils/rbac.ts') | ||
|
|
||
| function makeContext(auth: Record<string, unknown> | undefined = { | ||
| userId: '00000000-0000-4000-8000-000000000001', | ||
| authType: 'apikey', | ||
| apikey: { | ||
| key: 'capgo_test_key', | ||
| rbac_id: 42, | ||
| }, | ||
| }) { | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| return { | ||
| get: (key: string) => { | ||
| if (key === 'auth') | ||
| return auth | ||
| if (key === 'requestId') | ||
| return 'req-test' | ||
| if (key === 'capgkey') | ||
| return 'capgo_test_key' | ||
| return undefined | ||
| }, | ||
| } as any | ||
| } | ||
|
|
||
| describe('rbac permission infra errors', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('checkPermission returns false for real ACL denials', async () => { | ||
| executeMock.mockResolvedValueOnce({ rows: [{ allowed: false }] }) | ||
|
|
||
| await expect(checkPermission(makeContext(), 'app.upload_bundle', { appId: 'ai.offthetools.app' })) | ||
| .resolves | ||
| .toBe(false) | ||
| }) | ||
|
|
||
| it('checkPermission surfaces connection failures as 503 upstream_unavailable', async () => { | ||
| executeMock.mockRejectedValueOnce(Object.assign(new Error('Connection terminated unexpectedly'), { | ||
| code: 'ECONNRESET', | ||
| })) | ||
|
|
||
| await expect(checkPermission(makeContext(), 'app.upload_bundle', { appId: 'ai.offthetools.app' })) | ||
| .rejects | ||
| .toMatchObject({ | ||
| status: 503, | ||
| cause: { | ||
| error: 'upstream_unavailable', | ||
| message: 'Permission check temporarily unavailable', | ||
| }, | ||
| }) | ||
|
|
||
| expect(closeClientMock).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('checkPermissionPg surfaces Hyperdrive connect timeouts as 503', async () => { | ||
| executeMock.mockRejectedValueOnce(new Error('timeout exceeded when trying to connect')) | ||
|
|
||
| await expect(checkPermissionPg( | ||
| makeContext(), | ||
| 'app.upload_bundle', | ||
| { appId: 'ai.offthetools.app' }, | ||
| getDrizzleClientMock() as any, | ||
| '00000000-0000-4000-8000-000000000001', | ||
| 'capgo_test_key', | ||
| )).rejects.toMatchObject({ | ||
| status: 503, | ||
| cause: { | ||
| error: 'upstream_unavailable', | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('checkPermissionPg treats invalid UUID cast errors as ACL deny, not 503', async () => { | ||
| const invalidUuidError = Object.assign(new Error('invalid input syntax for type uuid: "non-user-org-id"'), { | ||
| code: '22P02', | ||
| }) | ||
| executeMock.mockRejectedValueOnce(invalidUuidError) | ||
|
|
||
| await expect(checkPermissionPg( | ||
| makeContext(), | ||
| 'org.read', | ||
| { orgId: 'non-user-org-id' }, | ||
| getDrizzleClientMock() as any, | ||
| '00000000-0000-4000-8000-000000000001', | ||
| 'capgo_test_key', | ||
| )).resolves.toBe(false) | ||
| }) | ||
|
|
||
| it('checkPermission treats Drizzle-wrapped invalid UUID cast as ACL deny', async () => { | ||
| const drizzleWrapped = Object.assign(new Error('Failed query: SELECT ...'), { | ||
| name: 'DrizzleQueryError', | ||
| cause: Object.assign(new Error('invalid input syntax for type uuid: "046a...-missing"'), { | ||
| code: '22P02', | ||
| }), | ||
| }) | ||
| executeMock.mockRejectedValueOnce(drizzleWrapped) | ||
|
|
||
| await expect(checkPermission(makeContext(), 'org.invite_user', { orgId: '046a36ac-e03c-4590-9257-bd6c9dba9ee8-missing' })) | ||
| .resolves | ||
| .toBe(false) | ||
| }) | ||
|
|
||
| it('checkPermissionPg unwraps Drizzle cause for transient PG connection codes', async () => { | ||
| const drizzleWrapped = Object.assign(new Error('Failed query: SELECT ...'), { | ||
| name: 'DrizzleQueryError', | ||
| cause: Object.assign(new Error('terminating connection due to administrator command'), { | ||
| code: '57P01', | ||
| }), | ||
| }) | ||
| executeMock.mockRejectedValueOnce(drizzleWrapped) | ||
|
|
||
| await expect(checkPermissionPg( | ||
| makeContext(), | ||
| 'app.upload_bundle', | ||
| { appId: 'ai.offthetools.app' }, | ||
| getDrizzleClientMock() as any, | ||
| '00000000-0000-4000-8000-000000000001', | ||
| 'capgo_test_key', | ||
| )).rejects.toMatchObject({ | ||
| status: 503, | ||
| cause: { error: 'upstream_unavailable' }, | ||
| }) | ||
| }) | ||
|
|
||
| it('checkPermissionPg rethrows existing HTTPException without remapping', async () => { | ||
| const httpError = new HTTPException(409, { message: 'conflict' }) | ||
| executeMock.mockRejectedValueOnce(httpError) | ||
|
|
||
| await expect(checkPermissionPg( | ||
| makeContext(), | ||
| 'app.upload_bundle', | ||
| { appId: 'ai.offthetools.app' }, | ||
| getDrizzleClientMock() as any, | ||
| '00000000-0000-4000-8000-000000000001', | ||
| 'capgo_test_key', | ||
| )).rejects.toBe(httpError) | ||
| }) | ||
|
|
||
| it('checkPermissionPg returns false for real ACL denials', async () => { | ||
| executeMock.mockResolvedValueOnce({ rows: [{ allowed: false }] }) | ||
|
|
||
| await expect(checkPermissionPg( | ||
| makeContext(), | ||
| 'app.upload_bundle', | ||
| { appId: 'ai.offthetools.app' }, | ||
| getDrizzleClientMock() as any, | ||
| '00000000-0000-4000-8000-000000000001', | ||
| 'capgo_test_key', | ||
| )).resolves.toBe(false) | ||
| }) | ||
|
|
||
| it('checkPermission allows JWT auth through the non-rbac_id query path', async () => { | ||
| executeMock.mockResolvedValueOnce({ rows: [{ allowed: true }] }) | ||
|
|
||
| await expect(checkPermission( | ||
| makeContext({ | ||
| userId: '00000000-0000-4000-8000-000000000001', | ||
| authType: 'jwt', | ||
| apikey: null, | ||
| }), | ||
| 'app.upload_bundle', | ||
| { appId: 'ai.offthetools.app' }, | ||
| )).resolves.toBe(true) | ||
| }) | ||
|
|
||
| it('checkPermission surfaces statement timeouts on the JWT path as 503', async () => { | ||
| executeMock.mockRejectedValueOnce(Object.assign( | ||
| new Error('canceling statement due to statement timeout'), | ||
| { code: '57014' }, | ||
| )) | ||
|
|
||
| await expect(checkPermission( | ||
| makeContext({ | ||
| userId: '00000000-0000-4000-8000-000000000001', | ||
| authType: 'jwt', | ||
| apikey: null, | ||
| }), | ||
| 'org.invite_user', | ||
| { orgId: '00000000-0000-4000-8000-000000000099' }, | ||
| )).rejects.toMatchObject({ | ||
| status: 503, | ||
| cause: { error: 'upstream_unavailable' }, | ||
| }) | ||
| }) | ||
|
|
||
| it('checkPermissionPg surfaces statement timeouts as 503 on the non-rbac_id path', async () => { | ||
| executeMock.mockRejectedValueOnce(Object.assign( | ||
| new Error('canceling statement due to statement timeout'), | ||
| { code: '57014' }, | ||
| )) | ||
|
|
||
| await expect(checkPermissionPg( | ||
| makeContext({ | ||
| userId: '00000000-0000-4000-8000-000000000001', | ||
| authType: 'jwt', | ||
| apikey: null, | ||
| }), | ||
| 'app.upload_bundle', | ||
| { appId: 'ai.offthetools.app' }, | ||
| getDrizzleClientMock() as any, | ||
| '00000000-0000-4000-8000-000000000001', | ||
| null, | ||
| )).rejects.toMatchObject({ | ||
| status: 503, | ||
| cause: { error: 'upstream_unavailable' }, | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: A permission query canceled for any reason is now classified as transient and converted to a 503. PostgreSQL's
57014is the generalquery_canceledSQLSTATE, not a statement-timeout-only code, so client/request cancellations or other operator cancellations can be mislabeled asupstream_unavailable, producing misleading retry behavior and diagnostics. Restrict this code to errors whose message or other metadata confirms a timeout, and handle the distinct lock-timeout SQLSTATE separately; add a regression case for a non-timeout57014cancellation.Prompt for AI agents