OmniAuth Rails CSRF 保护项目教程
1、项目的目录结构及介绍
omniauth-rails_csrf_protection/
├── bin/
│ └── console
├── lib/
│ └── omniauth/
│ └── rails_csrf_protection.rb
├── test/
│ └── omniauth_rails_csrf_protection_test.rb
├── .gitignore
├── .rubocop.yml
├── CODE_OF_CONDUCT.md
├── Gemfile
├── LICENSE.txt
├── README.md
├── Rakefile
└── omniauth-rails_csrf_protection.gemspec
bin/
: 包含用于启动项目控制台的脚本。lib/
: 包含项目的主要代码文件,特别是rails_csrf_protection.rb
文件,该文件实现了 CSRF 保护功能。test/
: 包含项目的测试文件,确保功能的正确性。.gitignore
: 指定 Git 版本控制系统忽略的文件和目录。.rubocop.yml
: 配置 RuboCop 代码风格检查工具的规则。CODE_OF_CONDUCT.md
: 项目的代码行为准则。Gemfile
: 指定项目依赖的 Gem 包。LICENSE.txt
: 项目的许可证文件。README.md
: 项目的主要说明文档。Rakefile
: 包含 Rake 任务的定义。omniauth-rails_csrf_protection.gemspec
: 项目的 Gem 规范文件,包含项目的元数据和依赖信息。
2、项目的启动文件介绍
项目的启动文件主要是 lib/omniauth/rails_csrf_protection.rb
。这个文件实现了 CSRF 保护功能,具体包括:
- 禁用通过 HTTP GET 方法访问 OAuth 请求阶段。
- 在请求阶段之前插入 Rails CSRF 令牌验证器。
3、项目的配置文件介绍
项目的配置文件主要是 Gemfile
和 omniauth-rails_csrf_protection.gemspec
。
Gemfile
source 'https://rubygems.org'
gem 'omniauth-rails_csrf_protection'
source 'https://rubygems.org'
: 指定 Gem 包的来源。gem 'omniauth-rails_csrf_protection'
: 引入omniauth-rails_csrf_protection
这个 Gem 包。
omniauth-rails_csrf_protection.gemspec
Gem::Specification.new do |spec|
spec.name = "omniauth-rails_csrf_protection"
spec.version = "0.1.2"
spec.authors = ["Cookpad Inc."]
spec.email = ["developers@cookpad.com"]
spec.summary = %q{Provides CSRF protection on OmniAuth request endpoint on Rails application}
spec.description = %q{This gem provides a mitigation against CVE-2015-9284 (Cross-Site Request Forgery on the request phase when using OmniAuth gem with a Ruby on Rails application) by implementing a CSRF token verifier that directly uses ActionController::RequestForgeryProtection code from Rails}
spec.homepage = "https://github.com/cookpad/omniauth-rails_csrf_protection"
spec.license = "MIT"
spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.add_dependency "actionpack", ">= 4.2"
spec.add_dependency "omniauth", ">= 1.3.1"
spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "minitest", "~> 5.0"
end
spec.name
: 项目的名称。spec.version
: 项目的版本。spec.authors
: 项目的作者。