![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
bullet_train-scope_validator
Advanced tools
Bullet Train Scope Validator provides a simple pattern for protecting belongs_to
associations from malicious ID stuffing. It was created by Andrew Culver and extracted from Bullet Train.
By default in a multitenant Rails application, unless special care is given to validating the ID assigned to a belongs_to
association, malicious users can stuff arbitrary IDs into their request and cause an application to bleed data from other tenants.
Consider the following example from a customer relationship management (CRM) system that two competitive companies use:
class Team < ApplicationRecord
has_many :customers
has_many :deals
end
class Customer < ApplicationRecord
belongs_to :team
end
class Deal < ApplicationRecord
belongs_to :team
belongs_to :customer
end
class DealsController < ApplicationController
# 👋 Not illustrated: this controller loads `@team` safely, and has a `new` and `show` action.
def create
if @team.deals.create(deal_params)
redirect_to @deal
else
render :new
end
end
def deal_params
params.require(:deal).permit(:customer_id)
end
end
☝️ Note that Strong Parameters allows customer_id
to be set by incoming requests and isn't responsible for validating the value. We also wouldn't want Strong Parameters to be responible for this, since we'd end up with duplicate validation logic in our API controllers and other places. This is a responsibility of the model.
<%= form.collection_select(:customer_id, @team.customers, :id, :name) %>
☝️ Note that the @team.customers.all
is properly scoped to only show customers from the current team.
We have a deal with <%= @deal.customer.name %>!
A malicious user can:
<select>
element for customer_id
with an <input type="text">
element.Building on the example above, we can use Bullet Train Scope Validator to fix the problem like so:
First, add the following in our Gemfile
:
gem "bullet_train-scope_validator"
(Be sure to also run bundle install
and restart your Rails server.)
Then we add a scope: true
validation and def valid_customers
method in the model, like so:
class Deal < ApplicationRecord
belongs_to :team
belongs_to :customer
validates :customer, scope: true
def valid_customers
team.customers
end
end
If you're wondering what the connection between validates :customer, scope: true
and def valid_customers
is, it's just a convention that the former will call the latter based on the name of the attibute being validated. We've favored a full-blown method definition for this instead of simply passing in a proc into the validator because having a method allows us to also DRY up our form view to use the same definition of valid options, like so:
<%= form.collection_select(:customer_id, form.object.valid_customers, :id, :name) %>
So with that, you're done! Any attempts to stuff IDs will be met with an "invalid" Active Record error message.
Bug reports and pull requests are welcome on GitHub at https://github.com/bullet-train-co/bullet_train-scope_validator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Bullet Train Scope Validator project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
FAQs
Unknown package
We found that bullet_train-scope_validator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.