-
Notifications
You must be signed in to change notification settings - Fork 8
RLS enforcement analysis #3526
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
Draft
gilgardosh
wants to merge
1
commit into
main
Choose a base branch
from
rls-enforcement-analysis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
RLS enforcement analysis #3526
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| # RLS Enforcement Analysis — `accounter_schema` | ||
|
|
||
| Analysis of which tables should have Row Level Security enforced, based on data ownership and access | ||
| patterns. | ||
|
|
||
| --- | ||
|
|
||
| ## Should NOT enforce RLS (global/shared reference data) | ||
|
|
||
| | Table | Reason | | ||
| | ------------------------------ | ---------------------------------------------- | | ||
| | `business_trips_tax_variables` | Global tax rate parameters, same for all users | | ||
| | `countries` | Static country list, no ownership | | ||
| | `crypto_currencies` | Shared crypto reference data | | ||
| | `crypto_exchange_rates` | Global market data | | ||
| | `depreciation_categories` | Shared configuration | | ||
| | `exchange_rates` | Global fiat rates | | ||
| | `migration` | System tracking table | | ||
| | `permissions` | Global permission definitions | | ||
| | `recovery` | Shared annual tax recovery rates | | ||
| | `role_permissions` | Global role-permission mappings | | ||
| | `roles` | Global role definitions | | ||
| | `vat_value` | Shared historical VAT rates | | ||
|
|
||
| --- | ||
|
|
||
| ## Should enforce RLS (per-user/per-business data) | ||
|
|
||
| | Table | Ownership Column | Notes | | ||
| | -------------------------------------- | --------------------------------- | ------------------------------------------ | | ||
| | `api_keys` | `business_id` | Sensitive credentials | | ||
| | `api_key_permission_overrides` | via `api_keys.business_id` | Indirect ownership | | ||
| | `audit_logs` | `business_id` (nullable) | Null = system events, needs special policy | | ||
| | `bank_deposits` | `owner_id` | Direct ownership | | ||
| | `invitations` | `business_id` | Business-scoped invitations | | ||
| | `user_permission_overrides` | `business_id` + `user_id` | Per-business permission grants | | ||
| | `business_users` | `business_id` / `user_id` | Users can only see their own memberships | | ||
| | `transactions_raw_list` | via joined transaction tables | Indirect, needs join to `transactions` | | ||
| | `poalim_ils_account_transactions` | via `financial_accounts.owner_id` | Indirect via account_number/bank/branch | | ||
| | `poalim_eur_account_transactions` | via `financial_accounts.owner_id` | Indirect via account_number/bank/branch | | ||
| | `poalim_gbp_account_transactions` | via `financial_accounts.owner_id` | Indirect via account_number/bank/branch | | ||
| | `poalim_usd_account_transactions` | via `financial_accounts.owner_id` | Indirect via account_number/bank/branch | | ||
| | `poalim_cad_account_transactions` | via `financial_accounts.owner_id` | Indirect via account_number/bank/branch | | ||
| | `poalim_swift_account_transactions` | via `financial_accounts.owner_id` | Indirect via account_number/bank/branch | | ||
| | `poalim_foreign_account_transactions` | via `financial_accounts.owner_id` | Indirect via account_number/bank/branch | | ||
| | `poalim_deposits_account_transactions` | via `financial_accounts.owner_id` | Indirect via account_number/bank/branch | | ||
| | `amex_creditcard_transactions` | via `financial_accounts.owner_id` | Indirect via card | | ||
| | `cal_creditcard_transactions` | via `financial_accounts.owner_id` | Indirect via card | | ||
| | `isracard_creditcard_transactions` | via `financial_accounts.owner_id` | Indirect via card | | ||
| | `max_creditcard_transactions` | via `financial_accounts.owner_id` | Indirect via card | | ||
| | `bank_discount_transactions` | via `financial_accounts.owner_id` | Indirect via account_number | | ||
| | `etana_account_transactions` | via `financial_accounts.owner_id` | Indirect via account_id | | ||
| | `etherscan_transactions` | via `financial_accounts.owner_id` | Indirect via wallet_address | | ||
| | `kraken_ledger_records` | via `financial_accounts.owner_id` | Indirect via account_nickname | | ||
| | `kraken_trades` | via `financial_accounts.owner_id` | Indirect via account_nickname | | ||
|
|
||
| --- | ||
|
|
||
| ## Special / Deprecated | ||
|
|
||
| | Table | Notes | | ||
| | ------- | --------------------------------------------------------------------------------------------------- | | ||
| | `users` | Deprecated — being dropped in current migration branch (`user-accounts-migration-phase-3-7`). Skip. | | ||
|
|
||
| --- | ||
|
|
||
| ## Key challenge: indirect ownership | ||
|
|
||
| The raw transaction tables (all Poalim, Amex, Cal, Kraken, etc.) don't have a direct `owner_id` | ||
| column — they link to `financial_accounts` via account/card identifiers. RLS on these requires one | ||
| of: | ||
|
|
||
| 1. A **security definer view** that joins through `financial_accounts`, or | ||
| 2. Adding a **denormalized `owner_id` column** to each transaction table, or | ||
| 3. A **policy with a subquery**, e.g.: | ||
| ```sql | ||
| account_number IN ( | ||
| SELECT account_number FROM accounter_schema.financial_accounts | ||
| WHERE owner_id = accounter_schema.current_business_id() | ||
| ) | ||
| ``` | ||
|
|
||
| Option 3 is the most common pattern and avoids schema changes, but may have performance implications | ||
| at scale. Option 2 is simpler and faster but requires a migration per table. | ||
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.
The function name in the SQL example is
current_business_id(), but the actual function defined in the migrations (e.g., inpackages/migrations/src/actions/2026-02-10T12-05-00.create-rls-helper-function.ts) isget_current_business_id(). Updating this will ensure the documentation is accurate and the example is functional.