关于 privacy-by-design-rails
privacy-by-design-rails 是面向 Rails 8 开发者的个人数据保护技能,帮助在生成或审查相关代码时应用隐私设计原则。它提供扫描器和参考资料,涉及数据最小化、加密、日志保护、同意管理及数据主体请求。
主要功能
- 01扫描涉及个人数据的文件
- 02提供 Rails 隐私保护参考
- 03涵盖加密与密钥轮换
- 04指导日志和错误报告脱敏
- 05说明同意及数据保留流程
适用场景
- 01审查处理个人数据的 Rails 代码
- 02规划 Rails 应用的日志脱敏
- 03实现同意管理和数据主体请求
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ codeminer42/ skills --skill privacy-by-design-rails安装后在 Claude Code 中输入/privacy-by-design-rails确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/anonymization.mdconsent-management.mddata-minimization.mddata-retention.mddsar.mdencryption.mdlog-and-output-protection.mdpii-definition.mdpseudonymization.mdsecrets-management.mdsession-security.mdrules/consent-use-purposes-constant.mdemail-no-pii-in-body.mdencrypt-filter-parameters.mdencrypt-pii-fields.mdexport-dsar-vs-processing.mdexport-no-pii-at-rest.mdlog-no-job-arguments.mdlog-no-pii-in-cache.mdlog-pass-ids-not-data.mdscripts/rules/base_rule.rbconsent_use_purposes_constant.rbemail_no_pii_in_body.rbencrypt_filter_parameters.rbencrypt_pii_fields.rberror_reporter_scrubbing.rbexport_dsar_vs_processing.rbexport_no_pii_at_rest.rblog_no_job_arguments.rblog_no_pii_in_cache.rb详细介绍
使用 Rails 8 实现隐私保护设计
生成或审查处理个人数据的代码时,请先运行扫描器:
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 地址。完整定义和可关联性测试请参阅 references/pii-definition.md。如有疑问,请将其视为 PII。
快速参考
| 原则 | Rails 功能 | 文件 |
|---|---|---|
| 过滤日志和检查输出 | config.filter_parameters(两者都涵盖) | references/log-and-output-protection.md |
| 按模型声明 PII | self.filter_attributes(自我说明) | references/log-and-output-protection.md |
| 保护作业参数(ActiveJob) | self.log_arguments = false(在 ApplicationJob 中设置) | references/log-and-output-protection.md |
| 保护作业参数(Sidekiq) | 服务端中间件 + 仅传递 ID | references/log-and-output-protection.md |
| 对日志中的 PII 进行脱敏 | logstop gem | references/log-and-output-protection.md |
| IP 匿名化 | ip_anonymizer middleware | 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 |
| 数据最小化 | Strong parameters + serializers | references/data-minimization.md |
| 可搜索加密 | encrypts :field, deterministic: true | references/encryption.md |
| 不可搜索加密 | encrypts :field | references/encryption.md |
| 密码哈希 | has_secure_password | references/encryption.md |
| 密钥轮换 | encrypts :field, previous: [...] | references/encryption.md |
| 密钥管理 | bin/rails credentials:edit | references/secrets-management.md |
| 安全审计 | bundler-audit、Brakeman、pdscan | references/secrets-management.md |
| 强制使用 HTTPS | config.force_ssl = true + HSTS | references/session-security.md |
| 会话安全 | 签名 Cookie / Bearer token | references/session-security.md |
| 同意管理 | 不可变、只追加的审计记录 | references/consent-management.md |
| 同意执行 | 控制器 concern,用于限制操作 | references/consent-management.md |
| 同意门控的导出 | 使用 ConsentGatedExportSerializer 处理 | references/consent-management.md |
| 匿名化 | 将 PII 替换为 [ANONYMIZED] | references/anonymization.md |
| 假名化 | 使用 HMAC 哈希保留分析关联 | references/pseudonymization.md |
| DSAR 工作流 | 访问 / 更正 / 删除 | references/dsar.md |
| 数据保留 | Solid Queue 定时作业 | references/data-retention.md |
综合示例 — User 模型
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 }
end
常见错误(扫描器无法检测)
| 错误 | 修复方法 |
|---|---|
| 为满足删除权而删除有依赖项的记录 | 如果记录有依赖项,优先选择匿名化——这样可以保持引用完整性。有些监管机构更倾向于彻底删除,因此请确认当地要求 |
| 将同意存储为布尔标记 | 使用不可变、只追加的记录作为审计轨迹 |
| 在 API 中暴露顺序 ID | 使用 UUID 作为公开标识符 |
| 手动管理数据保留 | 使用 Solid Queue 定时作业自动处理 |
外部参考资料
需要最新指南或根据官方来源验证实践时,请查阅这些资料。必要时使用 WebFetch——不要一开始就全部获取。
Rails
- 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
- 第 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(巴西)
- 全文(英文):https://lgpd-brazil.info
- 第 18 条 — 数据主体的权利:https://lgpd-brazil.info/chapter_03/article_18
- 第 46 条 — 安全措施:https://lgpd-brazil.info/chapter_07/article_46
评论
支持 Markdown