Fix Shadow API Exposure in Rails
Shadow APIs in Rails are the result of developer laziness meeting 'Magic' framework defaults. They occur when internal database fields or undocumented endpoints are exposed via implicit serialization or permissive routing. If you're rendering raw models or using wildcards in your parameters, you're leaking internal state and inviting mass-assignment exploits. Stop treating your database schema as your API contract.
The Vulnerable Pattern
class UsersController < ApplicationController # LEAK: Implicitly serializes the entire model including password_digest and tokens def show @user = User.find(params[:id]) render json: @user endEXPLOIT: permit! allows attackers to inject ‘admin: true’ or ‘role: superuser’
def update @user = User.find(params[:id]) @user.update(params[:user].permit!) render json: @user end end
The Secure Implementation
To kill Shadow APIs, you must implement a strict serialization layer and rigorous input filtering. The vulnerable code suffers from Over-posting (Mass Assignment) and sensitive data exposure because it trusts the model's structure. By using ActiveModelSerializers (or Jbuilder/Blueprinter), you create an allow-list for outgoing data, ensuring 'password_digest' or 'internal_notes' never hit the wire. On the ingress side, never use 'permit!'. Explicitly define every field allowed for modification to prevent attackers from escalating privileges via hidden fields like 'is_admin'.
class UserSerializer < ActiveModel::Serializer # DEFENSE: Explicitly define the egress schema attributes :id, :username, :bio endclass UsersController < ApplicationController def show @user = User.find(params[:id]) render json: @user, serializer: UserSerializer end
def update @user = User.find(params[:id]) if @user.update(user_params) render json: @user, serializer: UserSerializer end end
private
DEFENSE: Strict ingress whitelisting
def user_params params.require(:user).permit(:username, :bio) end end
Your Rails API
might be exposed to Shadow API Exposure
74% of Rails apps fail this check. Hackers use automated scanners to find this specific flaw. Check your codebase before they do.
Free Tier • No Credit Card • Instant Report
Verified by Ghost Labs Security Team
This content is continuously validated by our automated security engine and reviewed by our research team. Ghost Labs analyzes over 500+ vulnerability patterns across 40+ frameworks to provide up-to-date remediation strategies.