Need help securing your Rails apps? We are here to help at email@example.com.
When writing software you want to avoid introducing functional bugs or security issues.
For functional bugs, we usually write unit tests and try to cover edge cases. Integration tests then make sure that things work together.
In this article, we will walk through how we can secure a Ruby, Rails, or Rack-based app.
No matter if it is a new codebase, or we are adding it to a 10-year-old project, in the end we will have a decent level of security testing.
For this article we will be using open-source software tools to secure our codebase. We will also consider some hosted and already configured alternatives.
- rubocop (has some security rules)
The tools can be categorized into Static Application Security Testing (SAST) and Software Composition Analysis (SCA) tools.
Static analysis scans your codebase and looks for certain kinds of patterns. It will not execute your code and cannot detect problems in generated code, or at runtime.
The results will usually show the line of code the issue is detected. They will also alert you to the type of issue identified (with a severity), and sometimes an advisory on how to fix the problem.
In contrast, software composition analysis will check the dependencies you are using for vulnerabilities.
The report usually includes:
- The package name of the affected dependency.
- Severity on how bad it is. This usually spans from
- The next safe version to upgrade to.
- Some more references that explain what the problem is and more details.
- In some cases an identifier that leads to a vulnerability database.
To run the security tools, you do not need a CI pipeline but it is generally recommended to have one in place.
We will install the tools in a ruby/rails project. You will also learn how to enable the rake tasks and run them.
All the tools also have normal executables and can be used one by one, or in a shell script.
Bundler Audit will detect vulnerable dependencies in your codebase
First, add the bundler-audit gem to your Gemfile:
and install your bundle.
Now you can run a
bundle audit check --update
This will update the local database first, and then check your project for vulnerable dependencies.
Keep in mind that
bundle auditdoes not update the dependencies automatically. I recommend you to always update them before each run to ensure you get the best and most up-to-date results.
To enable the rake tasks, add the following to your project Rakefile:
require 'bundler/audit/task' Bundler::Audit::Task.new
rake bundle:auditwill now update the data and perform the checks.
If you found any vulnerabilities at this point do not freak out. You can look into fixing them by updating the version to the next safe one.
In case you can not update, you can use the
--ignore $vulnerability-idto suppress it for now.
bundle audit check --update --ignore OSVDB-108664
The output looks something like this:
$ bundle audit check --update Updating ruby-advisory-db ... From https://github.com/rubysec/ruby-advisory-db * branch master -> FETCH_HEAD Already up to date. Updated ruby-advisory-db ruby-advisory-db: 322 advisories Name: omniauth-oauth2 Version: 1.0.2 Advisory: CVE-2012-6134 Criticality: High URL: http://www.osvdb.org/show/osvdb/90264 Title: Ruby on Rails omniauth-oauth2 Gem CSRF vulnerability Solution: upgrade to >= 1.1.1 Name: rubyzip Version: 1.2.1 Advisory: CVE-2018-1000544 Criticality: Unknown URL: https://github.com/rubyzip/rubyzip/issues/369 Title: Directory Traversal in rubyzip Solution: upgrade to >= 1.2.2 Vulnerabilities found!
Next, we will add Brakeman as our first static code analysis tool. Brakeman will look for known insecure patterns and configurations in your code base. It will also check if you are using a known vulnerable rails version.
Brakeman works not only for Rails, but it also covers Sinatra and any kind of rack application.
First, add the dependency to your Gemfile:
and install it:
Then you can run it for the first time in your rails root directory.
This will invoke Brakeman and output some lengthy report. Here is a gist with a sample report: https://gist.github.com/pxlpnk/4a829b6101abe5fc0102d358d8dd028d
If your app is safe and you get no findings run it, first of all good job!
But then you want to see how it looks like, so you can try it with OWASP/RailsGoat.
If you run Brakeman for the first time on any older Rails application you might get a lot of findings.
To make things easier, you want to start with more severe issues and work down to the less critical ones.
To do so, use
brakeman -w3and it will show only high,
-w2will show high and medium,
-w1will show all other findings.
Approaching it level-by-level usually works the best and is the most efficient.
If you plan to use rake, you can install the RakeTask very easily.
will create a task file in
lib/tasks/brakeman.rakewhich can be run with:
To customize it and use more advanced options see the documentation brakeman as a library/.
While rubocop is known as a linter and formatter, it comes with some security rules and can be extended with some community extensions.
Let us get started with installing RuboCop first. Add the following to your Gemfile:
gem 'rubocop', require: false
Then run our old friend
Once this has finished, you can execute the
If you have not used rubocop before you might see a lot of output and warnings printed out in your terminal.
For this post, we are not so much interested in the linting and formatting rules. Instead we make sure we only run the security relevant rules.
We will be using the following
.rubocop.ymlconfiguration to start out.
AllCops: DisabledByDefault: true Security/Eval: Enabled: true Security/JSONLoad: Enabled: true Security/MarshalLoad: Enabled: true Security/Open: Enabled: true Security/YAMLLoad: Enabled: true Bundler/InsecureProtocolSource: Enabled: true Rails/OutputSafety: Enabled: true
We are disabling all the style and linting rules, and enabling only security-relevant ones. This is only done for simplicity in this blog post.
rubocopcommand now should be way less noisy.
This is how it looks like on the RailsGoat repository:
$ bundlex exec rubocop Inspecting 121 files ............C.................... Offenses: app/controllers/password_resets_controller.rb:6:20: C: Security/MarshalLoad: Avoid using Marshal.load. user = Marshal.load(Base64.decode64(params[:user])) unless params[:user].nil? ^^^^ 121 files inspected, 1 offense detected
Not too impressive yet, but we are getting some relevant security info already.
Now we will add some more security rules.
Add the following to your Gemfile:
gem 'rubocop-gitlab-security'and run
bundle install again.
Now add the following to the configuration file
GitlabSecurity/DeepMunge: Enabled: true GitlabSecurity/JsonSerialization: Enabled: true GitlabSecurity/RedirectToParamsUpdate: Enabled: true GitlabSecurity/SendFileParams: Enabled: true GitlabSecurity/SqlInjection: Enabled: true GitlabSecurity/SystemCommandInjection: Enabled: true
Those curated rules by the GitLab team add tremendous value to our security setup.
Now we need to require the new modules when running RuboCop:
Either by running:
bundle exec rubocop --require rubocop-gitlab-security
or add this to the
We can now find more security problems in the RailsGoat repository. Here is a gist with a sample report:https://gist.github.com/pxlpnk/958c500aab22f90b54918d6d9573251d
To configure the RakeTasks add:
require 'rubocop/rake_task' RuboCop::RakeTask.new do |task| task.requires << 'rubocop-gitlab-security' end
to your RakeFile and run it:
To not drown in findings I recommend you configure and enable one scanner at a time.
Start with high severity findings and filter out the rest.
Some of the results will be false positives or are no danger at all for various reasons. They can usually be ignored and added to a list to not be noisy.
Once you have fixed or muted the findings and it behaves as you would like, move on to the next one.
When you have reached the point that you feel happy and comfortable with your setup, enable these tools to fail your CI builds and run them on every code change you commit.
Now you will always know when something is not up to your security standards.
Hosted Services and Other Alternatives
In addition to the open source tools that are available, there are also a set of commercially available tools.
Open Source Security Tools are Great
The ruby community has put massive effort into creating those tools. They are free for everyone to use and should be supported!
If you can afford it, send them some money or other support so they can continue and improve the tools in the future.
How do you secure your codebase? Let me know at firstname.lastname@example.org.
Most security analysts have the same mindset, they want to reduce and avoid risk at any cost. They think in attack vectors, vulnerabilities, access rights and how to minimize the threat.
The security team is a nag.
They won’t allow us to ship it like this.
Why is the security team always saying no?
This works perfectly fine in a development process where the waterfall model. There the security analyst can take care of risk, plan accordingly and in the end have a penetration test and assessment to ensure everything is done in a secure way. But then agile, scrum and sprints start to happen. The code is released faster to production, from many months for a release to many releases within a month.
The process becomes faster, the company starts living and breathing agile, the security analysts and security engineers start to rotate and losing the grip on their perfectly secured applications and processes.
Everything goes so fast, the development teams release 3 new features and a selection of bug fixes during the two-week penetration test. Two findings that where reported are fixed by accident in the meanwhile. What is the report worth now? How can the security analyst even do their job in this environment and protect the assets?
Times have changed my friend, next the analyst falls into a crisis and a breakup phase. The 5 stages of grief:
denial: They won’t be doing this forever, once the product is released and stable they will go back to the old way.
Most likely not, and they will be striving for even faster releases.
anger: Why are they not following my processes, they are mandatory and there to ensure the safety.
They also slow down the developers, reduce velocity and give chance to the competitors.
bargaining: If you follow my processes, I make them a bit easier and say less no to you.
They are already faster than you, you would enter their world at a stage when they already shipped and moved on to the next thing.
depression: I cant do my job, everyone is working against me, I am always too slow and they won’t fix my vulnerabilities I put into their backlog.
This is the time where the analyst should either consider looking for alternative employment options or start un-learning and re-learning what they already know.
acceptance: The dev team won’t change what they are doing, and the rest of the company is on the agile train as well. What can I do?
This is the point where the security analysts and teams can start being part of enabling the business to strife, and turn from a nag and no-sayer to an enabler. But how does one approach this? Everyone is talking about shifting to the left.
There are two obvious starting points, technology, and people. While many would start putting effort into the technology first, they should go and talk to the people. Put some technology into the development life cycle, add some code analysis into the build pipeline and make sure nobody can release until the security checks are all passing. Going this route, will result in frustrated developers that can not make their goals anymore and hit tricky roadblocks set up by the security team (Why do I have to update a database dependency when I am just fixing a frontend bug?). At this point shortcuts will be taken, technology removed, people might even leave the company.
The nag is back.
In order to do make sustainable changes, the people who work on the codebase, the infrastructure, and the product come first. The analyst needs to start talking to people and understand what is important to them. What their goals are and how they are planning to achieve them. It is crucial to understand the tools they are currently using. Once the analyst understands the priorities and the used tools, they can get to work and start introducing security features and tooling. The easiest approach here is to start enabling some security features as warnings on the existing tools. Make sure that they don’t break the build, but people are getting notified. Define some achievable shared goals with the engineering team, and achieve them in collaboration with them.
Gradually enable more checks as the goals are achieved, and consider what tools would support the teams to deliver value in a secure manner.
Do you have some ideas, critique or want to share your story with us, you can reach us at email@example.com