fix(db): enforce tenant ownership in session_pins and billing spend queries (#796)#806
Open
devin-ai-integration[bot] wants to merge 4 commits into
Open
fix(db): enforce tenant ownership in session_pins and billing spend queries (#796)#806devin-ai-integration[bot] wants to merge 4 commits into
devin-ai-integration[bot] wants to merge 4 commits into
Conversation
…r_month_spend, and GetUserMonthlySpendAndLimit (#796) Signed-off-by: N Rohith Reddy <rohithreddy2202@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com>
…writes (#796) Signed-off-by: N Rohith Reddy <rohithreddy2202@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com>
…tUserMonthlySpendAndLimit (#796) Signed-off-by: N Rohith Reddy <rohithreddy2202@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: rohith500 <rohith500@users.noreply.github.com>
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
PR author is not in the allowed authors list. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Convention-compliant rewrite of #800 (by @rohith500). The production fix is preserved verbatim; only the test surface moved to satisfy the AGENTS.md hard rule.
Closes the four SQL ownership gaps from #796 where tenant isolation relied on the app layer always passing same-tenant IDs rather than on the SQL itself (same class as the already-fixed
SoftDeleteModelRouterAPIKeyin #536):session_pins: addedAND installation_id = @installation_id::uuidtoGetSessionPin,UpdateSessionPinUsage,IncrementSessionPinUpstreamErrors,ResetSessionPinUpstreamErrors;UpsertSessionPin'sON CONFLICT DO UPDATEgainedWHERE router.session_pins.installation_id = EXCLUDED.installation_idso a mismatched caller no-ops instead of overwriting another tenant's pin.installationIDthreaded through thesessionpin.Storeinterface and every call site.SweepExpiredSessionPinsleft untouched (global GC byexpires_at).DebitOrgCreditskey_spend/user_month_spendCTEs: both now joinapi_key_id/router_user_idthrough tomodel_router_installations.external_idand require it to match theorganization_idbeing debited before bumping the counter; a mismatch no-ops just that CTE (org debit + ledger still proceed).GetUserMonthlySpendAndLimit:has_override/override_limit_usd_micros/spent_usd_microssubqueries now join throughmodel_router_userstomodel_router_installations.external_idand require a match;org_default_limit_usd_microsstays unjoined (org-scoped only).All four: mismatch = silent miss/no-op, matching how each query already handles a missing row. Hardening fix, not a response to an active exploit (confirmed in #796 that none are reachable via a current authenticated
/v1/*request).Why a rewrite
#800 added
internal/postgres/tenant_isolation_796_test.go, a live-Postgres DB-backed test ininternal/. Per rootAGENTS.md("Tests"): "No DB-backed integration tests ininternal/. If need real Postgres,docker composestack is runtime fixture; write scripts underscripts/rather than*_test.go." The four regression scenarios were ported verbatim (realSessionPinRepo/BillingRepo, no mocks) intoscripts/tenant_isolation_check/, gated onROUTER_TEST_DATABASE_URL, following the existingscripts/feedback_integration_check/pattern.go test ./...no longer touches Postgres.Testing
make precommit(fmt + vet + build + test) clean.make generateproduces no diff underinternal/sqlc.go build ./scripts/tenant_isolation_checkclean.Co-authored-by: rohith500 rohith500@users.noreply.github.com
Link to Devin session: https://app.devin.ai/sessions/055a0e376d2b4318b42c4991fb14625f
Requested by: @steventohme