mirror of
https://github.com/affaan-m/ECC.git
synced 2026-11-17 05:22:17 +00:00
feat(rails-security): add Rails security skill
Adds a rails-security skill covering production configuration, authentication and session rotation, authorization, strong parameters, SQL injection, XSS and output escaping, CSRF, file uploads, API security, credentials, untrusted boundaries, and dependency scanning. Fills the Rails gap alongside the existing laravel-security, django-security, and springboot-security framework skills. Defers decisions to rules/ruby/security.md and focuses on the how, per the rules-vs-skills split. Registers skills/rails-security in the framework-language install module, agent.yaml, the package.json files list, the ruby entry in config/project-stack-mappings.json, and the plugin and marketplace manifests. Adds the reciprocal skill reference to rules/ruby/security.md, matching how the python, php, and java security rules point at their framework skills. Updates the catalog count to 294 across the documented surfaces, and bumps registryDigest and coverage.skills in the skill-triggers context pack by hand: scripts/dev/generate-skill-triggers.js requires Codex auth to regenerate, and no new triggers are introduced, so triggersDigest is unchanged.
This commit is contained in:
1 parent
d2ef268059
commit
da713084fc
17 files changed
+475
-19
No files matched your search
@@ -11,7 +11,7 @@
|
||||
{
|
||||
"name": "ecc",
|
||||
"source": "./",
|
||||
"description": "Harness-native ECC operator layer - 71 agents, 314 skills, 98 legacy command shims, reusable hooks, rules, selective install profiles, and production-ready workflows for Claude Code, Codex, OpenCode, Cursor, and related agent harnesses",
|
||||
"description": "Harness-native ECC operator layer - 71 agents, 315 skills, 98 legacy command shims, reusable hooks, rules, selective install profiles, and production-ready workflows for Claude Code, Codex, OpenCode, Cursor, and related agent harnesses",
|
||||
"version": "2.2.3",
|
||||
"author": {
|
||||
"name": "Affaan Mustafa",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "ecc",
|
||||
"version": "2.2.3",
|
||||
"description": "Harness-native ECC plugin for engineering teams - 71 agents, 314 skills, 98 legacy command shims, reusable hooks, rules, MCP conventions, and operator workflows for Claude Code plus adjacent agent harnesses",
|
||||
"description": "Harness-native ECC plugin for engineering teams - 71 agents, 315 skills, 98 legacy command shims, reusable hooks, rules, MCP conventions, and operator workflows for Claude Code plus adjacent agent harnesses",
|
||||
"author": {
|
||||
"name": "Affaan Mustafa",
|
||||
"url": "https://x.com/affaanmustafa"
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ This file provides Gemini CLI with the baseline ECC workflow, review standards,
|
||||
|
||||
## Overview
|
||||
|
||||
ECC is a cross-harness coding system with 71 specialized agents, 314 skills, and 98 commands.
|
||||
ECC is a cross-harness coding system with 71 specialized agents, 315 skills, and 98 commands.
|
||||
|
||||
Gemini support is currently focused on a strong project-local instruction layer via `.gemini/GEMINI.md`, plus the shared MCP catalog and package-manager setup assets shipped by the installer.
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# ECC — Agent Instructions
|
||||
|
||||
This is a **production-ready AI coding plugin** providing 71 specialized agents, 314 skills, 98 commands, and automated hook workflows for software development.
|
||||
This is a **production-ready AI coding plugin** providing 71 specialized agents, 315 skills, 98 commands, and automated hook workflows for software development.
|
||||
|
||||
**Version:** 2.2.3
|
||||
|
||||
@@ -202,7 +202,7 @@ Troubleshoot failures: check test isolation → verify mocks → fix implementat
|
||||
|
||||
```
|
||||
agents/ — 71 specialized subagents
|
||||
skills/ — 314 workflow skills and domain knowledge
|
||||
skills/ — 315 workflow skills and domain knowledge
|
||||
commands/ — 98 slash commands
|
||||
hooks/ — Trigger-based automations
|
||||
rules/ — Always-follow guidelines (common + per-language)
|
||||
|
||||
@@ -141,12 +141,12 @@ Instead of rebuilding that process in every prompt, you install it once and make
|
||||
|
||||
ECC is MIT-licensed open source. It works best with Claude Code today, has native Codex and Qoder plugin paths, and provides capability-limited adapters for Cursor, OpenCode, Gemini, Zed, GitHub Copilot, Antigravity, Qwen, and other harnesses. See the [support status matrix](#platform-support) before assuming feature parity.
|
||||
|
||||
Access to 71 agents, 314 skills, and 98 legacy command shims, plus hooks, rules, memory, continuous learning, and AgentShield security scanning. The agents are specialized for planning, review, build repair, security, architecture, and domain work.
|
||||
Access to 71 agents, 315 skills, and 98 legacy command shims, plus hooks, rules, memory, continuous learning, and AgentShield security scanning. The agents are specialized for planning, review, build repair, security, architecture, and domain work.
|
||||
|
||||
| Included | Count | What it gives you |
|
||||
| ---------------- | ----------: | ------------------------------------------------------------------------------------ |
|
||||
| Agents | 71 agents | Planning, review, build repair, security, architecture, and domain work |
|
||||
| Skills | 314 skills | TDD, research, security, docs, frontend, data, ML, operations, and more |
|
||||
| Skills | 315 skills | TDD, research, security, docs, frontend, data, ML, operations, and more |
|
||||
| Commands | 98 commands | Convenient entry points while ECC moves to a skills-first surface |
|
||||
| Hooks and memory | Runtime | Enforcement, session summaries, continuous learning, instincts, and context controls |
|
||||
| Rules | Selective | Always-loaded standards you choose by language or project |
|
||||
@@ -861,7 +861,7 @@ Stable graduation of the 2.0 line: control-pane substrate, worktree lifecycle se
|
||||
```text
|
||||
ECC/
|
||||
|-- agents/ # 71 specialized subagents for delegation
|
||||
|-- skills/ # 314 reusable workflows loaded on demand
|
||||
|-- skills/ # 315 reusable workflows loaded on demand
|
||||
|-- commands/ # 98 maintained slash-command shims
|
||||
|-- rules/ # opt-in common and language standards
|
||||
|-- hooks/ # runtime automation and enforcement
|
||||
@@ -957,6 +957,7 @@ ECC/
|
||||
| |-- quarkus-tdd/ # Quarkus TDD
|
||||
| |-- quarkus-verification/ # Quarkus verification
|
||||
| |-- rails-patterns/ # Rails architecture patterns
|
||||
| |-- rails-security/ # Rails security practices
|
||||
| |-- springboot-patterns/ # Java Spring Boot patterns
|
||||
| |-- springboot-security/ # Spring Boot security
|
||||
| |-- springboot-tdd/ # Spring Boot TDD
|
||||
|
||||
+1
-1
@@ -200,7 +200,7 @@ Copy-Item -Recurse rules/typescript "$HOME/.claude/rules/"
|
||||
/plugin list ecc@ecc
|
||||
```
|
||||
|
||||
**完成!** 你现在可以使用 71 个代理、314 个技能和 98 个命令。
|
||||
**完成!** 你现在可以使用 71 个代理、315 个技能和 98 个命令。
|
||||
|
||||
### multi-* 命令需要额外配置
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Soul
|
||||
|
||||
## Core Identity
|
||||
ECC is a production-ready AI coding plugin with 71 specialized agents, 314 skills, 98 commands, and automated hook workflows for software development.
|
||||
ECC is a production-ready AI coding plugin with 71 specialized agents, 315 skills, 98 commands, and automated hook workflows for software development.
|
||||
|
||||
## Core Principles
|
||||
1. **Agent-First** — route work to the right specialist as early as possible.
|
||||
|
||||
@@ -138,6 +138,7 @@ skills:
|
||||
- quarkus-tdd
|
||||
- quarkus-verification
|
||||
- rails-patterns
|
||||
- rails-security
|
||||
- ralphinho-rfc-pipeline
|
||||
- react-patterns
|
||||
- react-performance
|
||||
|
||||
@@ -360,6 +360,7 @@
|
||||
"rules": ["common"],
|
||||
"skills": [
|
||||
"rails-patterns",
|
||||
"rails-security",
|
||||
"tdd-workflow",
|
||||
"verification-loop"
|
||||
],
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
# ECC — Agent Talimatları
|
||||
|
||||
Bu, yazılım geliştirme için 71 özel agent, 314 skill, 98 command ve otomatik hook iş akışları sağlayan **üretime hazır bir AI kodlama eklentisidir**.
|
||||
Bu, yazılım geliştirme için 71 özel agent, 315 skill, 98 command ve otomatik hook iş akışları sağlayan **üretime hazır bir AI kodlama eklentisidir**.
|
||||
|
||||
**Sürüm:** 2.2.3
|
||||
|
||||
@@ -142,7 +142,7 @@ Başarısızlık sorunlarını giderin: test izolasyonunu kontrol edin → mockl
|
||||
|
||||
```
|
||||
agents/ — 71 özel subagent
|
||||
skills/ — 314 iş akışı skillleri ve alan bilgisi
|
||||
skills/ — 315 iş akışı skillleri ve alan bilgisi
|
||||
commands/ — 98 slash command
|
||||
hooks/ — Tetikleyici tabanlı otomasyonlar
|
||||
rules/ — Her zaman uyulması gereken kurallar (ortak + dile özel)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# ECC — 智能体指令
|
||||
|
||||
这是一个**生产就绪的 AI 编码插件**,提供 71 个专业代理、314 项技能、98 条命令以及自动化钩子工作流,用于软件开发。
|
||||
这是一个**生产就绪的 AI 编码插件**,提供 71 个专业代理、315 项技能、98 条命令以及自动化钩子工作流,用于软件开发。
|
||||
|
||||
**版本:** 2.2.3
|
||||
|
||||
@@ -147,7 +147,7 @@
|
||||
|
||||
```
|
||||
agents/ — 71 个专业子代理
|
||||
skills/ — 314 个工作流技能和领域知识
|
||||
skills/ — 315 个工作流技能和领域知识
|
||||
commands/ — 98 个斜杠命令
|
||||
hooks/ — 基于触发的自动化
|
||||
rules/ — 始终遵循的指导方针(通用 + 每种语言)
|
||||
|
||||
@@ -264,7 +264,7 @@ Copy-Item -Recurse rules/typescript "$HOME/.claude/rules/"
|
||||
/plugin list ecc@ecc
|
||||
```
|
||||
|
||||
**搞定!** 你现在可以使用 71 个智能体、314 项技能和 98 个命令了。
|
||||
**搞定!** 你现在可以使用 71 个智能体、315 项技能和 98 个命令了。
|
||||
|
||||
***
|
||||
|
||||
@@ -1178,7 +1178,7 @@ opencode
|
||||
|---------|---------------|----------|--------|
|
||||
| 智能体 | PASS: 71 个 | PASS: 12 个 | **Claude Code 领先** |
|
||||
| 命令 | PASS: 98 个 | PASS: 35 个 | **Claude Code 领先** |
|
||||
| 技能 | PASS: 314 项 | PASS: 37 项 | **Claude Code 领先** |
|
||||
| 技能 | PASS: 315 项 | PASS: 37 项 | **Claude Code 领先** |
|
||||
| 钩子 | PASS: 8 种事件类型 | PASS: 11 种事件 | **OpenCode 更多!** |
|
||||
| 规则 | PASS: 29 条 | PASS: 13 条指令 | **Claude Code 领先** |
|
||||
| MCP 服务器 | PASS: 14 个 | PASS: 完整 | **完全对等** |
|
||||
@@ -1286,7 +1286,7 @@ ECC 是**第一个最大化利用每个主要 AI 编码工具的插件**。以
|
||||
|---------|-----------------------|------------|-----------|----------|
|
||||
| **智能体** | 71 | 共享 (AGENTS.md) | 共享 (AGENTS.md) | 12 |
|
||||
| **命令** | 98 | 共享 | 基于指令 | 35 |
|
||||
| **技能** | 314 | 共享 | 10 (原生格式) | 37 |
|
||||
| **技能** | 315 | 共享 | 10 (原生格式) | 37 |
|
||||
| **钩子事件** | 8 种类型 | 15 种类型 | SessionStart(1 种类型) | 11 种类型 |
|
||||
| **钩子脚本** | 20+ 个脚本 | 16 个脚本 (DRY 适配器) | 1 个 SessionStart 引导脚本 | 插件钩子 |
|
||||
| **规则** | 34 (通用 + 语言) | 34 (YAML 前页) | 基于指令 | 13 条指令 |
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"coverage": {
|
||||
"skills": 314,
|
||||
"skills": 315,
|
||||
"withTriggers": 34
|
||||
},
|
||||
"generatedAt": "2026-10-30T00:01:34.569Z",
|
||||
@@ -10,7 +10,7 @@
|
||||
"id": "hand-seeded",
|
||||
"source": "manual-curation-pending-regeneration"
|
||||
},
|
||||
"registryDigest": "6f213dc845937b4093bc018d041b25b538d88d78451b9d799ad614989b360ee4",
|
||||
"registryDigest": "dbd96f7a907526ec7ef7d5438644aed708e72021c4428d3a0df3211089f9bb9f",
|
||||
"schemaVersion": 1,
|
||||
"triggers": {
|
||||
"skill:agent-runtime-gateway-smoke-test": [
|
||||
|
||||
@@ -230,6 +230,7 @@
|
||||
"skills/quarkus-tdd",
|
||||
"skills/quarkus-verification",
|
||||
"skills/rails-patterns",
|
||||
"skills/rails-security",
|
||||
"skills/react-patterns",
|
||||
"skills/react-performance",
|
||||
"skills/react-testing",
|
||||
|
||||
@@ -343,6 +343,7 @@
|
||||
"skills/quarkus-tdd/",
|
||||
"skills/quarkus-verification/",
|
||||
"skills/rails-patterns/",
|
||||
"skills/rails-security/",
|
||||
"skills/ralphinho-rfc-pipeline/",
|
||||
"skills/react-patterns/",
|
||||
"skills/react-performance/",
|
||||
|
||||
@@ -48,4 +48,6 @@ bundle exec brakeman --no-progress
|
||||
|
||||
## Reference
|
||||
|
||||
See skill: `rails-security` for Rails-specific security implementation patterns.
|
||||
|
||||
See skill: `security-review` for secure-by-default review patterns.
|
||||
@@ -0,0 +1,449 @@
|
||||
---
|
||||
name: rails-security
|
||||
description: Ruby on Rails security practices for Rails 7.1+ and 8.x apps. Covers production configuration, authentication, authorization, strong parameters, SQL injection, XSS and output escaping, CSRF, file uploads, API security, credentials, untrusted boundaries, and dependency scanning. Use when reviewing or writing Rails authentication, authorization, input handling, uploads, or deployment configuration.
|
||||
origin: community
|
||||
---
|
||||
|
||||
# Rails Security
|
||||
|
||||
Security practices for Ruby on Rails applications (Rails 7.1+ and 8.x). Rails is secure by
|
||||
default in most places; the vulnerabilities that reach production usually come from opting
|
||||
out of a default rather than failing to add protection. This skill is the "how." For the
|
||||
decisions about which control to reach for, see the Ruby security rules —
|
||||
`rules/ruby/security.md` in this repository, installed as `rules/ecc/ruby/security.md`.
|
||||
|
||||
## When to Activate
|
||||
|
||||
- Writing or reviewing authentication, sessions, or account recovery
|
||||
- Adding authorization checks to controllers or views
|
||||
- Handling user input, file uploads, or webhook payloads
|
||||
- Configuring a Rails app for production
|
||||
- Reviewing a PR that touches `config/`, `app/controllers/`, or `Gemfile`
|
||||
- Any use of `html_safe`, `raw`, `Arel.sql`, or raw SQL strings
|
||||
|
||||
## Production Configuration
|
||||
|
||||
```ruby
|
||||
# config/environments/production.rb
|
||||
Rails.application.configure do
|
||||
config.force_ssl = true
|
||||
config.assume_ssl = true # behind a proxy that terminates TLS
|
||||
|
||||
# Reject requests with unexpected Host headers.
|
||||
config.hosts << "example.com"
|
||||
config.hosts << /.*\.example\.com/
|
||||
|
||||
config.action_dispatch.cookies_same_site_protection = :lax
|
||||
config.log_level = :info
|
||||
end
|
||||
```
|
||||
|
||||
Filter sensitive values out of logs:
|
||||
|
||||
```ruby
|
||||
# config/initializers/filter_parameter_logging.rb
|
||||
Rails.application.config.filter_parameters += %i[
|
||||
passw secret token _key crypt salt certificate otp ssn cvv card_number
|
||||
]
|
||||
```
|
||||
|
||||
## Authentication
|
||||
|
||||
Rails 8 ships an authentication generator that produces session-based auth with password
|
||||
reset and metadata tracking:
|
||||
|
||||
```bash
|
||||
bin/rails generate authentication
|
||||
```
|
||||
|
||||
Use Devise when you need OAuth, MFA, confirmable, lockable, or multi-model auth. For
|
||||
hand-rolled auth, `has_secure_password` handles bcrypt hashing:
|
||||
|
||||
```ruby
|
||||
class User < ApplicationRecord
|
||||
has_secure_password
|
||||
|
||||
validates :email, presence: true, uniqueness: { case_sensitive: false }
|
||||
validates :password, length: { minimum: 12 }, allow_nil: true
|
||||
end
|
||||
```
|
||||
|
||||
`authenticate_by` looks up and verifies in one call, so a missing user and a wrong password
|
||||
take the same time:
|
||||
|
||||
```ruby
|
||||
# Bad: the timing difference reveals whether the email exists
|
||||
user = User.find_by(email: params[:email])
|
||||
if user&.authenticate(params[:password])
|
||||
|
||||
# Good
|
||||
user = User.authenticate_by(email: params[:email], password: params[:password])
|
||||
```
|
||||
|
||||
Rotate the session on sign-in and on any privilege change:
|
||||
|
||||
```ruby
|
||||
class SessionsController < ApplicationController
|
||||
def create
|
||||
user = User.authenticate_by(email: params[:email], password: params[:password])
|
||||
return render :new, status: :unprocessable_entity unless user
|
||||
|
||||
reset_session
|
||||
session[:user_id] = user.id
|
||||
redirect_to root_path
|
||||
end
|
||||
|
||||
def destroy
|
||||
reset_session
|
||||
redirect_to root_path
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
### Account recovery
|
||||
|
||||
Password reset tokens must expire, be single-use, and not reveal whether an account exists.
|
||||
Signed tokens generated by the model give you both:
|
||||
|
||||
```ruby
|
||||
class User < ApplicationRecord
|
||||
generates_token_for :password_reset, expires_in: 15.minutes do
|
||||
password_salt&.last(10) # invalidates the token once the password changes
|
||||
end
|
||||
end
|
||||
|
||||
user = User.find_by_token_for(:password_reset, params[:token])
|
||||
```
|
||||
|
||||
Render the same response whether or not the address matched.
|
||||
|
||||
## Authorization
|
||||
|
||||
Authentication is not authorization. Every action that touches a record must check that the
|
||||
current user may act on it.
|
||||
|
||||
```ruby
|
||||
# Bad: any signed-in user can load any invoice
|
||||
@invoice = Invoice.find(params[:id])
|
||||
|
||||
# Good: scope through the owner
|
||||
@invoice = current_user.invoices.find(params[:id])
|
||||
```
|
||||
|
||||
For anything past ownership, use a policy object (Pundit, Action Policy, or CanCanCan):
|
||||
|
||||
```ruby
|
||||
class InvoicePolicy < ApplicationPolicy
|
||||
def update?
|
||||
user.admin? || record.user_id == user.id
|
||||
end
|
||||
end
|
||||
|
||||
class InvoicesController < ApplicationController
|
||||
def update
|
||||
@invoice = Invoice.find(params[:id])
|
||||
authorize @invoice
|
||||
# ...
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
Fail closed — verify that authorization ran rather than trusting each action to remember:
|
||||
|
||||
```ruby
|
||||
class ApplicationController < ActionController::Base
|
||||
include Pundit::Authorization
|
||||
|
||||
after_action :verify_authorized, except: :index
|
||||
after_action :verify_policy_scoped, only: :index
|
||||
end
|
||||
```
|
||||
|
||||
## Strong Parameters and Mass Assignment
|
||||
|
||||
Never pass raw `params` to a model. Permit an explicit list:
|
||||
|
||||
```ruby
|
||||
# Bad: a crafted request can set admin
|
||||
User.new(params[:user])
|
||||
|
||||
# Good
|
||||
def user_params
|
||||
params.require(:user).permit(:email, :name)
|
||||
end
|
||||
```
|
||||
|
||||
Rails 8 adds `expect`, which raises on a malformed structure instead of returning an empty
|
||||
hash:
|
||||
|
||||
```ruby
|
||||
def user_params
|
||||
params.expect(user: [:email, :name])
|
||||
end
|
||||
```
|
||||
|
||||
Attributes such as `admin`, `role`, `user_id`, and `account_id` should never appear in a
|
||||
permit list driven by request data. Assign them explicitly from the session or a checked
|
||||
policy.
|
||||
|
||||
## SQL Injection
|
||||
|
||||
Active Record parameterizes by default. Injection appears when a string is built by hand:
|
||||
|
||||
```ruby
|
||||
# Bad
|
||||
User.where("email = '#{params[:email]}'")
|
||||
Order.order("#{params[:sort]} #{params[:dir]}")
|
||||
|
||||
# Good
|
||||
User.where(email: params[:email])
|
||||
User.where("email = ?", params[:email])
|
||||
```
|
||||
|
||||
`order`, `pluck`, `group`, and `select` accept raw SQL and are not sanitized. Allowlist the
|
||||
input rather than escaping it:
|
||||
|
||||
```ruby
|
||||
SORTABLE = { "created_at" => "created_at", "total" => "total" }.freeze
|
||||
DIRECTIONS = { "asc" => "ASC", "desc" => "DESC" }.freeze
|
||||
|
||||
column = SORTABLE.fetch(params[:sort], "created_at")
|
||||
direction = DIRECTIONS.fetch(params[:dir], "DESC")
|
||||
Order.order(Arel.sql("#{column} #{direction}"))
|
||||
```
|
||||
|
||||
`LIKE` needs its own escaping, since `%` and `_` are wildcards:
|
||||
|
||||
```ruby
|
||||
term = User.sanitize_sql_like(params[:q])
|
||||
User.where("name LIKE ?", "%#{term}%")
|
||||
```
|
||||
|
||||
## XSS and Output Escaping
|
||||
|
||||
ERB escapes interpolated values by default. Every bypass is security-sensitive:
|
||||
|
||||
```erb
|
||||
<%# Bad: renders attacker-controlled markup %>
|
||||
<%= @comment.body.html_safe %>
|
||||
<%= raw @comment.body %>
|
||||
|
||||
<%# Good: escaped %>
|
||||
<%= @comment.body %>
|
||||
|
||||
<%# Good: allowlisted markup when the field is genuinely rich text %>
|
||||
<%= sanitize @comment.body, tags: %w[p br strong em a], attributes: %w[href] %>
|
||||
```
|
||||
|
||||
`html_safe` does not sanitize — it marks a string as already safe and skips escaping. Only
|
||||
call it on strings you constructed.
|
||||
|
||||
Interpolating into JavaScript needs the matching escape, not the HTML one:
|
||||
|
||||
```erb
|
||||
<script>
|
||||
const payload = <%= raw @data.to_json %>; // Bad
|
||||
const payload = <%= raw json_escape(@data.to_json) %>; // Good
|
||||
</script>
|
||||
```
|
||||
|
||||
`link_to`'s href accepts `javascript:` URLs, so validate user-supplied links:
|
||||
|
||||
```ruby
|
||||
uri = URI.parse(url) rescue nil
|
||||
safe = uri.is_a?(URI::HTTP) # URI::HTTPS subclasses URI::HTTP
|
||||
```
|
||||
|
||||
Set a Content Security Policy:
|
||||
|
||||
```ruby
|
||||
# config/initializers/content_security_policy.rb
|
||||
Rails.application.configure do
|
||||
config.content_security_policy do |policy|
|
||||
policy.default_src :self
|
||||
policy.script_src :self
|
||||
policy.object_src :none
|
||||
policy.frame_ancestors :none
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
## CSRF
|
||||
|
||||
Rails enables CSRF protection for new apps. Keep it on, and do not exempt actions to make a
|
||||
client work:
|
||||
|
||||
```ruby
|
||||
# Bad: disables protection for every action in the controller
|
||||
skip_before_action :verify_authenticity_token
|
||||
```
|
||||
|
||||
If an endpoint genuinely cannot carry the token — a third-party webhook, say — authenticate
|
||||
it another way (signature verification) and scope the exemption to that action alone.
|
||||
|
||||
API-only controllers inheriting from `ActionController::API` have no cookie session, so CSRF
|
||||
does not apply; token or session-cookie APIs still need it.
|
||||
|
||||
## File Uploads
|
||||
|
||||
The `content_type` a client sends is attacker-controlled. Validate against what the file
|
||||
actually contains, and constrain size:
|
||||
|
||||
```ruby
|
||||
class Document < ApplicationRecord
|
||||
has_one_attached :file
|
||||
|
||||
ALLOWED = %w[application/pdf image/png image/jpeg].freeze
|
||||
MAX_BYTES = 10.megabytes
|
||||
|
||||
validate :acceptable_file
|
||||
|
||||
private
|
||||
|
||||
def acceptable_file
|
||||
return unless file.attached?
|
||||
|
||||
errors.add(:file, "is too large") if file.blob.byte_size > MAX_BYTES
|
||||
|
||||
# identify sets content_type from the file's own bytes, not the client's header.
|
||||
file.blob.identify unless file.blob.identified?
|
||||
errors.add(:file, "type not allowed") unless ALLOWED.include?(file.blob.content_type)
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
Never interpolate a user-supplied path into `send_file`:
|
||||
|
||||
```ruby
|
||||
# Bad: path traversal
|
||||
send_file Rails.root.join("storage", params[:name])
|
||||
```
|
||||
|
||||
## API Security
|
||||
|
||||
Rate-limit authentication and other abusable endpoints. Action Controller ships
|
||||
`rate_limit` from Rails 7.2; use `rack-attack` on 7.1 or for app-wide throttling:
|
||||
|
||||
```ruby
|
||||
class SessionsController < ApplicationController
|
||||
rate_limit to: 10, within: 3.minutes, only: :create
|
||||
end
|
||||
```
|
||||
|
||||
Verify webhook signatures with a constant-time comparison:
|
||||
|
||||
```ruby
|
||||
class WebhooksController < ApplicationController
|
||||
skip_before_action :verify_authenticity_token, only: :create
|
||||
|
||||
def create
|
||||
expected = OpenSSL::HMAC.hexdigest("SHA256", ENV.fetch("WEBHOOK_SECRET"), request.raw_post)
|
||||
given = request.headers["X-Signature"].to_s
|
||||
|
||||
unless ActiveSupport::SecurityUtils.secure_compare(expected, given)
|
||||
return head :unauthorized
|
||||
end
|
||||
|
||||
ProcessWebhookJob.perform_later(request.raw_post)
|
||||
head :ok
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
Keep CORS narrow — never reflect an arbitrary origin alongside credentials:
|
||||
|
||||
```ruby
|
||||
# Bad
|
||||
origins "*"
|
||||
|
||||
# Good
|
||||
origins "https://app.example.com"
|
||||
```
|
||||
|
||||
Serialize responses explicitly so new columns do not leak:
|
||||
|
||||
```ruby
|
||||
# Bad: ships every column, including ones added later
|
||||
render json: @user
|
||||
|
||||
# Good
|
||||
render json: @user.as_json(only: %i[id name created_at])
|
||||
```
|
||||
|
||||
## Secrets and Credentials
|
||||
|
||||
Secrets belong in Rails credentials, environment variables, or a secret manager:
|
||||
|
||||
```bash
|
||||
bin/rails credentials:edit --environment production
|
||||
```
|
||||
|
||||
`config/credentials/production.key` must stay out of version control. Read secrets through
|
||||
`Rails.application.credentials` or `ENV.fetch` — `ENV[...]` returns `nil` and fails silently
|
||||
when a variable is missing:
|
||||
|
||||
```ruby
|
||||
# Bad
|
||||
ENV["STRIPE_SECRET"]
|
||||
|
||||
# Good
|
||||
Rails.application.credentials.dig(:stripe, :secret)
|
||||
ENV.fetch("STRIPE_SECRET")
|
||||
```
|
||||
|
||||
If a key is ever committed, rotate it. Removing the commit does not un-expose it.
|
||||
|
||||
## Untrusted Boundaries
|
||||
|
||||
Controller params are not the only entry point. Job arguments, webhook payloads, Action
|
||||
Cable messages, and Turbo Stream targets all carry outside data:
|
||||
|
||||
- Pass record IDs to jobs and re-scope inside `perform`; a queued job runs without the
|
||||
original request's session.
|
||||
- Authorize in `ApplicationCable::Connection` and in each channel's `subscribed`.
|
||||
- Never build a Turbo Stream target from user input.
|
||||
|
||||
```ruby
|
||||
class ExportJob < ApplicationJob
|
||||
def perform(user_id, invoice_id)
|
||||
user = User.find(user_id)
|
||||
invoice = user.invoices.find(invoice_id) # scoped, not Invoice.find
|
||||
# ...
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
## Dependency Scanning
|
||||
|
||||
Run these when the lockfile changes, and in CI:
|
||||
|
||||
```bash
|
||||
bundle exec bundle-audit check --update
|
||||
bundle exec brakeman --no-progress
|
||||
```
|
||||
|
||||
Brakeman flags the patterns above statically. Treat new warnings as blocking, and record
|
||||
deliberate exceptions in `config/brakeman.ignore` with a reason rather than lowering the
|
||||
confidence threshold.
|
||||
|
||||
## Verification Checklist
|
||||
|
||||
- [ ] `force_ssl` and `config.hosts` set in production
|
||||
- [ ] Sensitive parameters filtered from logs
|
||||
- [ ] Sessions rotated on sign-in and privilege change
|
||||
- [ ] Every record lookup scoped to the current user or checked by a policy
|
||||
- [ ] No permit list contains a role, ownership, or account column
|
||||
- [ ] No raw SQL string interpolation; `order` and `pluck` inputs allowlisted
|
||||
- [ ] Every `html_safe` and `raw` reviewed
|
||||
- [ ] CSRF exemptions scoped to specific actions with another auth mechanism
|
||||
- [ ] Uploads validated by detected type and size
|
||||
- [ ] Auth endpoints rate-limited; webhook signatures compared in constant time
|
||||
- [ ] No secrets in version control; master key excluded
|
||||
- [ ] `bundle-audit` and `brakeman` clean
|
||||
|
||||
## Related Skills
|
||||
|
||||
- `rails-patterns` — the architectural patterns these controls sit inside
|
||||
- `security-review` — secure-by-default review patterns across languages
|
||||
- Ruby security rules (`rules/ruby/security.md`, installed as `rules/ecc/ruby/security.md`)
|
||||
— the decisions and when-to-use guidance this skill implements
|
||||
Reference in new issue
Block a user