Python · security
no-optional-tenant-predicate
python:no-optional-tenant-predicate Tenant predicate is added only conditionally, allowing an unscoped query.
- Code
- SARJ056
- Default
- error
- Fix
- none
- Languages
- python
Why
Fail-open tenant filtering can expose rows across organizations when a tenant value is absent.
Fix
Require the tenant identifier or seed the query with its tenant predicate unconditionally.
Before / after
Executed by this rule’s unit tests.
Before
Tenant clause depends on an optional filter
def build(args):
conditions = []
if args.organization_id:
conditions.append(SQL("organization_id = %s"))
return conditions
After
Tenant clause is unconditional
def build(args):
conditions = [SQL("organization_id = %s")]
return conditions
Limits
- Detection follows SQL fragments inside each function and recognizes configured tenant column names.
- Test files and functions containing any unconditional tenant predicate are excluded.