Security Improvements: Organization Filtering
How we tightened org-scoped queries and added regression coverage so one tenant cannot read or mutate another’s rows through ordinary code paths.
Overview
- Tightened org filters on tasks, CRUD, users, permissions, status utilities, and stats.
- Regression tests in
tests/backend/test_security_fixes.pyand optionalpytest -m security. - Optional script
scripts/check_organization_filtering.pyfor CI; experimental org middleware exists behind flags.
Critical Security Fixes Implemented
1. Task Management Security
Files: app/services/task_management.py, app/crud.py
Issue: Task queries lacked organization filtering, allowing cross-tenant access
Fix: Added organization_id parameters and filtering to all task operations
2. CRUD Operations Security
Files: app/crud.py
Issue: remove_tag, task queries without organization filtering
Fix: Added organization filtering to prevent cross-tenant tag manipulation
3. User Router Security
Files: app/routers/user.py
Issue: User update queries without organization filtering
Fix: Added organization filtering with superuser exceptions
4. Auth Permissions Security
Files: app/auth/permissions.py
Issue: Resource permission checks without organization filtering
Fix: Applied organization filtering before permission validation
5. Status Utility Security
Files: app/utils/status.py
Issue: Status queries without organization filtering
Fix: Added organization-aware status creation and lookup
6. Statistics Security
Files: app/services/stats/
Issue: Statistics queries without organization filtering
Fix: Added organization context to StatsCalculator constructor
Security Test Suite
Comprehensive Test Coverage
File: tests/backend/test_security_fixes.py
The security test suite includes:
- Cross-tenant access prevention tests for all fixed vulnerabilities
- Organization filtering validation for CRUD operations
- Auth permissions security tests
- Regression tests to prevent future vulnerabilities
- Security markers for targeted test execution
CI/CD Security Integration
Automated Security Scanning
File: scripts/check_organization_filtering.py
The security check script automatically scans the codebase for:
- Database queries missing organization filtering
- HIGH severity issues (queries on organization-aware models)
- MEDIUM severity issues (potentially unsafe queries)
GitHub Actions Integration
The script can generate GitHub Actions workflows for:
- Pull request security checks
- Automated security test execution
- Security issue reporting in PR comments
Query-Level Organization Filtering Middleware
Experimental Middleware Solution
File: app/middleware/organization_filter.py
Provides automatic organization filtering through:
- Context Manager Approach (Recommended)
- Organization-Aware Session Wrapper (Recommended)
- Decorator Approach
Safety Features
- Disabled by default for safety
- Bypass mechanisms for administrative operations
- Comprehensive logging for monitoring
- Query interception with automatic filtering
Security Best Practices
1. Understanding Query Safety Levels
Safe (filter often optional):
Require organization_id (list/search and similar):
2. Direct Parameter Passing (Current Approach)
3. Always Validate Organization Context
4. Use Security Tests
5. Apply Defense in Depth
Combine explicit org filters in app code, FK integrity, optional middleware, and tests.
Performance Considerations
Query Performance
- Indexed organization_id fields ensure fast filtering
- Composite indexes on (organization_id, other_fields) for complex queries
- Query plan analysis to verify efficient execution
Security vs. Performance Trade-offs
- Direct parameter passing: Best performance, explicit security
- Middleware solutions: Slight overhead, automatic security
- Choose based on: Team expertise, maintenance requirements, performance needs
Future work
Finish stats coverage, keep chipping away at script findings, and evolve middleware only where it clearly reduces missed filters.