Audit Guide

Purpose

Rails applications accumulate technical debt, security vulnerabilities and performance regressions over time.

This guide provides a systematic approach to auditing a Rails application, covering code quality, performance, database health, security and operational concerns.

For each area, it recommends both process steps and the appropriate tools — many of which can be automated in CI to catch issues before they reach production.

Preparation and Scope

Before beginning an audit, establish clear goals and boundaries.

Define Goals

What is the primary objective of this audit?

Define Scope

Set Up the Environment

Static Code Quality and Code Smells

RuboCop

RuboCop is the standard Ruby linter. It enforces style conventions and catches common errors.

Plugins:

Run:

bundle exec rubocop --auto-correct

Reek

Reek detects code smells: Feature Envy, Long Methods, God Classes, Utility Functions and more.

Configure via .reek.yml and run:

bundle exec reek

Rails Best Practices

The rails_best_practices gem flags controller bloat, unused routes, missing specs and other Rails-specific anti-patterns.

Run:

bundle exec rails_best_practices .

Flog and Flay — Complexity and Duplication

Both are useful for identifying refactoring targets that other tools may miss.

CodeClimate / SonarQube — Aggregated Reports

For teams that want continuous quality metrics, integrate an aggregated reporting tool:

These tools provide dashboards and enforce minimum quality thresholds in CI.

Code Review Process

Auditing is not just automated tools. Human code review remains essential.

Pull Request Template Checklist

Every PR should include a checklist covering:

Peer Review Guidelines

Reviewers should verify:

Automated Checks in CI

CI should run the full audit suite on every PR:

Performance Profiling and Monitoring

Rack::MiniProfiler — In-Browser Profiling

Measures SQL time, view rendering and call stack hotspots directly in the browser. Install the gem and mount it in development.

Bullet — N+1 Detection

The bullet gem catches missing or redundant eager loads (includes) at runtime. Configure it to raise errors, log to console, or notify in the browser.

memory_profiler and derailed_benchmarks

Application Performance Monitoring (APM)

Production profiling tools provide transaction traces, slow endpoint identification and flamegraphs under real load.

Use these to identify hotspots that only appear under production traffic patterns.

Database and Query Analysis

ActiveRecord Query Logging

Enable verbose query logging in development to see the code-to-SQL mapping:

config.active_record.verbose_query_logs = true

EXPLAIN and EXPLAIN ANALYZE

Use the rails db console or a tool like PgHero to inspect query plans. Look for sequential scans on large tables and missing indexes.

PgHero

PgHero provides a dashboard showing:

APM Database Breakdown

Use Skylight or Scout to see the percentage of time spent in the database vs application code. This reveals whether performance issues are query-bound or code-bound.

Index Audit

Regularly review the schema for:

Security Auditing

Brakeman — Static Analysis

Brakeman scans for SQL injection, mass-assignment vulnerabilities, XSS, CSRF gaps and other Rails-specific security issues.

Run:

bundle exec brakeman -q -o brakeman-report.html

bundler-audit — Dependency Vulnerabilities

Scans Gemfile.lock for gems with known vulnerabilities.

Run:

bundle exec bundler-audit check --update

OWASP ZAP / Arachni — Dynamic Scanning

For comprehensive security testing, use a dynamic scanner to crawl endpoints and test for SQL injection, XSS, CSRF and other injection attacks.

Rails Secrets Audit

Ensure no credentials are committed to git history:

Credentials should use config*credentials.yml.enc for encrypted storage.

Content Security Policy and Secure Headers

Use the secure_headers gem to set:

Authorization Checks

Verify that:

Dependency and License Management

Dependency Monitoring

License Compliance

Use license_finder to enforce approved licenses and prevent introduction of incompatible dependencies.

Version Hygiene

Testing and Coverage

Test Quality

Code Coverage

SimpleCov measures test coverage. Enforce minimum thresholds and break the CI build if coverage drops below the agreed level.

Mutation Testing

Mutation testing (Mutant, mutation-test) verifies test suite quality by mutating code and expecting tests to fail. A test suite that passes against mutated code has gaps.

Logging, Monitoring and Alerting

Structured Logging

Use lograge to produce structured JSON logs in production. This makes logs searchable and consumable by log aggregation systems.

Exception Monitoring

These provide real-time exception tracking with context (request parameters, user, environment).

Metrics and Alerting

For operational visibility:

Set SLAs and alert on latency spikes or error percentage thresholds.

CI*CD and Release Hygiene

CI Pipeline

Every push should run the full audit suite:

Deployment Safety

Pre-Release Audit

Before tagging a release, run a comprehensive audit pass:

Audit Frequency

Audit TypeFrequencyToolsOwner
Pre-commitEveryRuboCop, FastererDeveloper
commit
CI auditEveryRuboCop, Brakeman, bundler-audit, Reek,CI pipeline
pushrails_best_practices, Flog, Flay, tests
Security auditWeeklyBrakeman, bundler-audit, OWASP ZAPSecurity
lead
Dependency reviewWeeklyDependabot, bundler-audit, LicenseFinderPlatform
Performance auditMonthlyRack::MiniProfiler, Bullet, APM tools,Performance
memory_profiler, PgHerolead
Full quality auditMonthlyAll tools + manual reviewTeam
Pre-release auditPerAll tools + manual reviewRelease
releasemanager

Interpreting Audit Results

Prioritization Framework

PriorityCriteriaAction
CriticalSecurity vulnerability in productionFix immediately. Deploy out of
dependencies.cycle if needed.
HighSecurity warnings, data exposure, severeFix within the current sprint.
performance issues.
MediumCode smells, style violations, inadequateSchedule in a future sprint.
coverage.
LowCosmetic issues, minor style preferences.Fix opportunistically.

False Positives

Every audit tool produces false positives.

Trend Tracking

Track audit results over time:

Rails-Specific Audit Areas

Migration Audit

Review recent migrations for:

Route Audit

Review config/routes.rb for:

Configuration Audit

Review environment configuration for:

Establishing Quality Standards

Choosing a Baseline

Run the full audit suite on the current codebase to establish a baseline.

Enforcement

Documentation and Knowledge Transfer

Maintain a living runbook that covers:

The audit process itself should be audited — regularly review whether the tools and thresholds are still serving the team's goals.

AI Integration

AI assistants can support Rails audits by:

When asking AI for audit assistance, provide:

Related Documents