Codebase Audit Report (Jan 2026)
Codebase Audit Report (Jan 2026)
Section titled “Codebase Audit Report (Jan 2026)”1. Inventory & Architecture
Section titled “1. Inventory & Architecture”System Overview
Section titled “System Overview”- Framework: Rails 8.1 API (with
propshaft,turbo-rails,stimulus-rails). - Data Store: PostgreSQL (
pg). - Background Jobs:
solid_queue. - Caching:
solid_cache. - Frontend: Tailwind CSS + Stimulus (Hotwire). JS Bundling via
esbuild.
Key Modules & Entry Points
Section titled “Key Modules & Entry Points”- Authentication & Accounts:
- Models:
User,Account,CompanySeat. - Auth Engines:
devise(Sessions/Registrations) mixed withpasswordless(Magic Links). - Controllers:
Users::Registrations,Users::Sessions,Companies::Registrations.
- Models:
- Voice Infrastructure (Twilio):
- Frontend: Browser-based calling via
@twilio/voice-sdk. - Token Vending:
Api::CallsController#token(issues JWTs for the SDK). - Webhooks:
Api::Webhooks::TwilioController(Handles incoming voice logic and status updates). - Testing:
Api::TwilioCallsController(Manual call triggers).
- Frontend: Browser-based calling via
- Financials:
- Service:
Ledger::CreateTransaction(Idempotent transaction creation). - Model:
Transaction(updatesAccountbalance via callbacks).
- Service:
2. Issues & Remediations (Fixed)
Section titled “2. Issues & Remediations (Fixed)”A. Authentication & User Model
Section titled “A. Authentication & User Model”[!WARNING] Identity Confusion & Race Conditions
-
The “User vs Seat” Race Condition:
- Mechanism: The system auto-provisions
CompanySeats for users logging in with a matching company domain (CompanySeat.fetch_resource_for_passwordless). - The Bug:
fetch_resource_for_passwordlessperforms afindthencreate. This is a classic race condition. Two simultaneous login attempts for a new employee could create duplicate seats or error out, depending on database constraints. - Identity Shadowing:
Users::SessionsControllerprioritizes finding aUserrecord before checking for aCompanySeat.- If an employee signs up as an individual (
User) before theirCompanySeatis provisioned, they become a permanentUser. - They will log in as a
User(individual account) and never be routed to theCompanySeat(company account) flow, effectively blocking them from their company’s workspace unless manual intervention occurs.
- If an employee signs up as an individual (
- Mechanism: The system auto-provisions
-
Company Registration Ambiguity:
Companies::RegistrationsControllercreates aUserwith apassword, but the mainSessionsControllerforces a magic link flow upon finding a user.- The
Accountvalidationnormalize_allowed_email_domainsblindly trusts the domain of the simplified email. If someone registers a company withceo@gmail.com, does the system effectively whitelistgmail.comfor that company? - Risk: If public domains (gmail, outlook) are not explicitly blocked in
Account.rblogic (I saw no blocklist), a user could theoretically claimgmail.comas their company domain, potentially capturing other Gmail users into their account (or failing due to the multiple-account check inCompanySeat).
B. Voice & Twilio Integration
Section titled “B. Voice & Twilio Integration”[!CAUTION] Missing Authorization & Financial Risk
-
Caller Authorization Bypass (
Api::Webhooks::TwilioController#voice):- The Flow: When a user creates a call in the browser, Twilio hits this webhook to ask “What do I do?”.
- The Code: The controller validates the Twilio signature (Good!), BUT it proceeds to
response.dialimmediately without checking if the account has a positive balance. - The Risk: A user with $0.00 balance can initiate calls. The
tokenendpoint checks balance, but tokens last 1 hour. A user can get a token, drain their balance, and continue calling for the rest of the hour because the webhook (the actual gatekeeper) doesn’t check funds.
-
Testing Code in Production Path:
Api::TwilioCallsController#createcontains hardcoded fallback numbers (+15005550006).- While guarded by
ensure_non_production!, having this logic alongside real API controllers is risky. It should be strictly isolated or behind a feature flag class.
C. Financial Integrity
Section titled “C. Financial Integrity”- Robust Transaction Creation:
Ledger::CreateTransactionis well-implemented. It usesidempotency_key, wraps logic inAccount.transaction, and handles uniqueness errors.- Balance Updates: The
Transactionmodel updatesAccount#cached_balance_centsin anafter_commithook usingupdate_all(atomic increment). This is solid.
3. Implemented Fixes
Section titled “3. Implemented Fixes”-
Voice Authorization Fixed:
- Updated
Api::Webhooks::TwilioController#voiceto checkcall.account.balance_cents > 0. Returns “Insufficient funds” if balance is empty.
- Updated
-
Strict Authentication Hardening:
- Seat Provisioning: Removed Auto-Provisioning.
- Identity Strategy: Implemented “Strict Domain Control”. If a domain is claimed by a company, users CANNOT log in as individuals. They are blocked and told to request access from their admin.
-
Post-Launch Reimbursement:
- Planned process to reimburse or transfer balances from “shadowed” individual accounts created before the strict block was put in place.