Skip to content

Commit bdaab70

Browse files
dhhDavid Heinemeier Hansson
andauthored
Add a default bin/bundle-audit configuration (rails#54695)
* Add a default bin/bundle-audit configuration Finding known security issues in the application's Gemfile should be part of every CI flow. * Include bundle-audit in Gemfile * Config part of default generation * Fix ext * Include in api apps too * Include in the generator * Generate if missing * Use rake style format * Style * Improve description * Add CHANGELOG * Use the right gem * Match gem name * Remember the binstubs! * Include bundler-audit scan as part of the default GitHub CI --------- Co-authored-by: David Heinemeier Hansson <[email protected]>
1 parent 954a3be commit bdaab70

File tree

11 files changed

+36
-0
lines changed

11 files changed

+36
-0
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ group :test do
147147

148148
# Needed for Railties tests because it is included in generated apps.
149149
gem "brakeman"
150+
gem "bundler-audit"
150151
end
151152

152153
platforms :ruby, :windows do

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ GEM
162162
brakeman (7.0.0)
163163
racc
164164
builder (3.3.0)
165+
bundler-audit (0.9.2)
166+
bundler (>= 1.2.0, < 3)
167+
thor (~> 1.0)
165168
bunny (2.23.0)
166169
amq-protocol (~> 2.3, >= 2.3.1)
167170
sorted_set (~> 1, >= 1.0.2)
@@ -678,6 +681,7 @@ DEPENDENCIES
678681
bcrypt (~> 3.1.11)
679682
bootsnap (>= 1.4.4)
680683
brakeman
684+
bundler-audit
681685
capybara (>= 3.39)
682686
connection_pool
683687
cssbundling-rails

railties/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Add bin/bundler-audit and config/bundler-audit.yml for discovering and managing known security problems with app gems.
2+
3+
*DHH*
4+
15
* Rails no longer generates a `bin/bundle` binstub when creating new applications.
26

37
The `bin/bundle` binstub used to help activate the right version of bundler.

railties/lib/rails/generators/rails/app/app_generator.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def config
127127
template "routes.rb" unless options[:update]
128128
template "application.rb"
129129
template "environment.rb"
130+
template "bundler-audit.yml"
130131
template "cable.yml" unless options[:update] || options[:skip_action_cable]
131132
template "puma.rb"
132133
template "storage.yml" unless options[:update] || skip_active_storage?
@@ -140,6 +141,7 @@ def config
140141
def config_when_updating
141142
action_cable_config_exist = File.exist?("config/cable.yml")
142143
active_storage_config_exist = File.exist?("config/storage.yml")
144+
bundle_audit_config_exist = File.exist?("config/bundler-audit.yml")
143145
rack_cors_config_exist = File.exist?("config/initializers/cors.rb")
144146
assets_config_exist = File.exist?("config/initializers/assets.rb")
145147
asset_app_stylesheet_exist = File.exist?("app/assets/stylesheets/application.css")
@@ -169,6 +171,10 @@ def config_when_updating
169171
remove_file "config/initializers/cors.rb"
170172
end
171173

174+
if !bundle_audit_config_exist
175+
template "config/bundler-audit.yml"
176+
end
177+
172178
if options[:api]
173179
unless csp_config_exist
174180
remove_file "config/initializers/content_security_policy.rb"

railties/lib/rails/generators/rails/app/templates/Gemfile.tt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ gem "thruster", require: false
5454
group :development, :test do
5555
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
5656
gem "debug", platforms: %i[ mri windows ], require: "debug/prelude"
57+
58+
# Audits gems for known security defects (use config/bundler-audit.yml to ignore issues)
59+
gem "bundler-audit", require: false
5760
<%- unless options.skip_brakeman? -%>
5861

5962
# Static analysis for security vulnerabilities [https://brakemanscanner.org/]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require_relative "../config/boot"
2+
require "bundler/audit/cli"
3+
4+
ARGV.concat %w[ --config config/bundler-audit.yaml ] if ARGV.empty? || ARGV.include?("check")
5+
Bundler::Audit::CLI.start
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Audit all gems listed in the Gemfile for known security problems by running bin/bundler-audit.
2+
# CVEs that are not relevant to the application can be enumerated on the ignore list below.
3+
#
4+
# ignore:
5+
# - CVE-2018-25032

railties/lib/rails/generators/rails/app/templates/github/ci.yml.tt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ jobs:
2323
- name: Scan for common Rails security vulnerabilities using static analysis
2424
run: bin/brakeman --no-pager
2525

26+
- name: Scan for known security vulnerabilities in gems used
27+
run: bin/bundler-audit
28+
2629
<% end -%>
2730
<%- if options[:javascript] == "importmap" && !options[:api] && !options[:skip_javascript] -%>
2831
scan_js:

railties/test/generators/api_app_generator_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def default_files
163163
bin/setup
164164
config/application.rb
165165
config/boot.rb
166+
config/bundler-audit.yml
166167
config/cable.yml
167168
config/environment.rb
168169
config/environments

railties/test/generators/app_generator_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
app/views/pwa/manifest.json.erb
3333
app/views/pwa/service-worker.js
3434
bin/brakeman
35+
bin/bundler-audit
3536
bin/dev
3637
bin/docker-entrypoint
3738
bin/rails
@@ -42,6 +43,7 @@
4243
config.ru
4344
config/application.rb
4445
config/boot.rb
46+
config/bundler-audit.yml
4547
config/cable.yml
4648
config/credentials.yml.enc
4749
config/database.yml

0 commit comments

Comments
 (0)