privacy-by-design-rails
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePrivacy by Design with Rails 8
Rails 8 中的隐私设计
When generating or reviewing code that handles personal data, run the scanner first:
ruby .claude/skills/privacy-by-design-rails/scripts/scanner.rb --files <relevant files>For deeper context on any topic, read the relevant reference file from .
references/What counts as PII? Any data that can identify a person directly or indirectly (GDPR Art. 4, LGPD Art. 5, NIST SP 800-122). This includes data that isn't identifying alone but becomes PII when linked to a person — farm names, animal names with known owners, license plates, vehicle VINs, student IDs, property addresses, social media handles, company names of sole proprietors, device IDs linked to accounts, IP addresses. See for the full definition and linkability test. When in doubt, treat it as PII.
references/pii-definition.md在生成或审查处理个人数据的代码时,先运行扫描器:
ruby .claude/skills/privacy-by-design-rails/scripts/scanner.rb --files <relevant files>如需了解任何主题的更深入内容,请阅读目录下的相关参考文件。
references/什么是PII? 任何可以直接或间接识别个人的数据(GDPR第4条、LGPD第5条、NIST SP 800-122)。这包括单独不具备识别性,但与个人关联后成为PII的数据——农场名称、已知所有者的动物名称、车牌、车辆VIN码、学生ID、物业地址、社交媒体账号、个体经营者的公司名称、与账户关联的设备ID、IP地址。完整定义和关联性测试请参阅。如有疑问,一律视为PII处理。
references/pii-definition.mdQuick Reference
快速参考
| Principle | Rails Feature | File |
|---|---|---|
| Log + inspect filtering | | references/log-and-output-protection.md |
| Per-model PII declaration | | references/log-and-output-protection.md |
| Job arg protection (ActiveJob) | | references/log-and-output-protection.md |
| Job arg protection (Sidekiq) | Server middleware + pass IDs only | references/log-and-output-protection.md |
| PII redaction in logs | | references/log-and-output-protection.md |
| IP anonymization | | references/log-and-output-protection.md |
| Error reporter scrubbing | Scrub PII from Sentry/Rollbar/etc. | references/log-and-output-protection.md |
| APM/monitoring scrubbing | Scrub PII from AppSignal/NewRelic/etc. | references/log-and-output-protection.md |
| Data minimization | Strong parameters + serializers | references/data-minimization.md |
| Searchable encryption | | references/encryption.md |
| Non-searchable encryption | | references/encryption.md |
| Password hashing | | references/encryption.md |
| Key rotation | | references/encryption.md |
| Secrets management | | references/secrets-management.md |
| Security auditing | bundler-audit, Brakeman, pdscan | references/secrets-management.md |
| HTTPS enforcement | | references/session-security.md |
| Session security | Signed cookies / Bearer tokens | references/session-security.md |
| Consent management | Immutable append-only audit trail | references/consent-management.md |
| Consent enforcement | Controller concern gating actions | references/consent-management.md |
| Consent-gated exports | | references/consent-management.md |
| Anonymization | Replace PII with | references/anonymization.md |
| Pseudonymization | HMAC hash preserving analytics links | references/pseudonymization.md |
| DSAR workflow | Access / Rectification / Erasure | references/dsar.md |
| Data retention | Solid Queue scheduled jobs | references/data-retention.md |
| 原则 | Rails特性 | 文件 |
|---|---|---|
| 日志与检查过滤 | | references/log-and-output-protection.md |
| 按模型声明PII | | references/log-and-output-protection.md |
| 作业参数保护(ActiveJob) | | references/log-and-output-protection.md |
| 作业参数保护(Sidekiq) | 服务器中间件 + 仅传递ID | references/log-and-output-protection.md |
| 日志中的PII脱敏 | | references/log-and-output-protection.md |
| IP匿名化 | | references/log-and-output-protection.md |
| 错误报告脱敏 | 从Sentry/Rollbar等工具中清理PII | references/log-and-output-protection.md |
| APM/监控脱敏 | 从AppSignal/NewRelic等工具中清理PII | references/log-and-output-protection.md |
| 数据最小化 | 强参数 + 序列化器 | references/data-minimization.md |
| 可搜索加密 | | references/encryption.md |
| 不可搜索加密 | | references/encryption.md |
| 密码哈希 | | references/encryption.md |
| 密钥轮换 | | references/encryption.md |
| 密钥管理 | | references/secrets-management.md |
| 安全审计 | bundler-audit、Brakeman、pdscan | references/secrets-management.md |
| HTTPS强制启用 | | references/session-security.md |
| 会话安全 | 签名Cookie / Bearer令牌 | references/session-security.md |
| 同意管理 | 不可变的追加式审计追踪 | references/consent-management.md |
| 同意执行 | 控制器关注点 gated actions | references/consent-management.md |
| 基于同意的导出 | 使用 | references/consent-management.md |
| 匿名化 | 用 | references/anonymization.md |
| 伪匿名化 | 保留分析关联的HMAC哈希 | references/pseudonymization.md |
| DSAR工作流 | 访问 / 更正 / 删除 | references/dsar.md |
| 数据留存 | Solid Queue定时作业 | references/data-retention.md |
Putting It All Together — The User Model
综合实践——用户模型
ruby
class User < ApplicationRecord
has_secure_password
has_many :sessions, dependent: :destroy
has_many :addresses, dependent: :destroy
has_many :orders, dependent: :restrict_with_error
has_many :consents, dependent: :destroy
has_many :data_subject_requests, dependent: :destroy
include Anonymizable
include Pseudonymizable
include DataExportable
include HasConsent
encrypts :email_address, deterministic: true, downcase: true
encrypts :first_name
encrypts :last_name
encrypts :phone
encrypts :date_of_birth
self.filter_attributes = %i[
first_name last_name phone date_of_birth
email_address password_digest
]
enum :role, { customer: 0, admin: 1 }
anonymizable :first_name, :last_name, :phone, :date_of_birth
exportable :uuid, :email_address, :first_name, :last_name, :phone, :date_of_birth
normalizes :email_address, with: ->(e) { e.strip.downcase }
endruby
class User < ApplicationRecord
has_secure_password
has_many :sessions, dependent: :destroy
has_many :addresses, dependent: :destroy
has_many :orders, dependent: :restrict_with_error
has_many :consents, dependent: :destroy
has_many :data_subject_requests, dependent: :destroy
include Anonymizable
include Pseudonymizable
include DataExportable
include HasConsent
encrypts :email_address, deterministic: true, downcase: true
encrypts :first_name
encrypts :last_name
encrypts :phone
encrypts :date_of_birth
self.filter_attributes = %i[
first_name last_name phone date_of_birth
email_address password_digest
]
enum :role, { customer: 0, admin: 1 }
anonymizable :first_name, :last_name, :phone, :date_of_birth
exportable :uuid, :email_address, :first_name, :last_name, :phone, :date_of_birth
normalizes :email_address, with: ->(e) { e.strip.downcase }
endCommon Mistakes (not caught by scanner)
常见错误(扫描器无法检测)
| Mistake | Fix |
|---|---|
| Deleting records with dependents for right to erasure | Prefer anonymization when the record has dependents — preserves referential integrity. Some authorities prefer true deletion, so check local requirements |
| Storing consents as a boolean flag | Use immutable append-only records for audit trail |
| Exposing sequential IDs in APIs | Use UUIDs as public identifiers |
| Manual data retention | Automate with Solid Queue scheduled jobs |
| 错误 | 修复方案 |
|---|---|
| 为满足删除权而删除带有依赖项的记录 | 当记录存在依赖项时,优先选择匿名化——保留引用完整性。部分监管机构要求真正删除,因此请查阅当地要求 |
| 将同意存储为布尔值标记 | 使用不可变的追加式记录来保留审计追踪 |
| 在API中暴露自增ID | 使用UUID作为公开标识符 |
| 手动管理数据留存 | 使用Solid Queue定时作业实现自动化 |
External References
外部参考
Consult these when you need the latest guidance or to verify patterns against official sources. Use WebFetch when needed — don't fetch all of them upfront.
当你需要最新指导或验证模式是否符合官方来源时,请参考以下内容。必要时使用WebFetch——无需预先获取全部内容。
Rails
Rails相关
- Active Record Encryption: https://guides.rubyonrails.org/active_record_encryption.html
- Active Record Encryption API: https://api.rubyonrails.org/classes/ActiveRecord/Encryption.html
- Securing Rails Applications: https://guides.rubyonrails.org/security.html
- Active Job Basics (logging): https://guides.rubyonrails.org/active_job_basics.html
- Rails Credentials: https://guides.rubyonrails.org/security.html#custom-credentials
- Active Record加密:https://guides.rubyonrails.org/active_record_encryption.html
- Active Record加密API:https://api.rubyonrails.org/classes/ActiveRecord/Encryption.html
- Rails应用安全:https://guides.rubyonrails.org/security.html
- Active Job基础(日志):https://guides.rubyonrails.org/active_job_basics.html
- Rails凭据:https://guides.rubyonrails.org/security.html#custom-credentials
GDPR
GDPR相关
- Art. 5 — Principles (data minimization, purpose limitation): https://gdpr-info.eu/art-5-gdpr/
- Art. 6 — Lawfulness of processing: https://gdpr-info.eu/art-6-gdpr/
- Art. 7 — Conditions for consent: https://gdpr-info.eu/art-7-gdpr/
- Art. 15 — Right of access: https://gdpr-info.eu/art-15-gdpr/
- Art. 16 — Right to rectification: https://gdpr-info.eu/art-16-gdpr/
- Art. 17 — Right to erasure: https://gdpr-info.eu/art-17-gdpr/
- Art. 25 — Data protection by design and by default: https://gdpr-info.eu/art-25-gdpr/
- Art. 32 — Security of processing: https://gdpr-info.eu/art-32-gdpr/
- 第5条——原则(数据最小化、目的限制):https://gdpr-info.eu/art-5-gdpr/
- 第6条——处理的合法性:https://gdpr-info.eu/art-6-gdpr/
- 第7条——同意的条件:https://gdpr-info.eu/art-7-gdpr/
- 第15条——访问权:https://gdpr-info.eu/art-15-gdpr/
- 第16条——更正权:https://gdpr-info.eu/art-16-gdpr/
- 第17条——删除权:https://gdpr-info.eu/art-17-gdpr/
- 第25条——设计和默认数据保护:https://gdpr-info.eu/art-25-gdpr/
- 第32条——处理的安全性:https://gdpr-info.eu/art-32-gdpr/
LGPD (Brazil)
LGPD(巴西)相关
- Full text (English): https://lgpd-brazil.info
- Art. 18 — Rights of the data subject: https://lgpd-brazil.info/chapter_03/article_18
- Art. 46 — Security measures: https://lgpd-brazil.info/chapter_07/article_46
- 全文(英文):https://lgpd-brazil.info
- 第18条——数据主体的权利:https://lgpd-brazil.info/chapter_03/article_18
- 第46条——安全措施:https://lgpd-brazil.info/chapter_07/article_46