Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
activerecord-rescue_from_duplicate
Advanced tools
This gem will rescue SQL errors when trying to insert records that fail uniqueness validation.
It complements :validates_uniqueness_of
and will add appropriate errors.
Additionally, a macro allows you to assume that the record will be unique and rescue gracefully otherwise.
Tested with:
Note:
after_validation
, before_save
, before_create
will have been run. Make sure they don't have undesired side-effects.ActiveRecord::RecordNotSaved
will be raised when using create!
, save!
or other !
methods.Add the rescue_from_duplicate: true
to any regular uniqueness validation.
This will use the Rails standard validation, performing a SELECT
to ensure the record is valid. In the case of a race condition, an error will be added to the model, and no exception will be thrown.
class ModelWithUniquenessValidator < ActiveRecord::Base
validates_uniqueness_of :name, scope: :shop_id, rescue_from_duplicate: true
end
If two of this statement go in at the same time, and the original validation on uniqueness of name passes, the DBMS will raise an duplicate record error.
a = ModelWithUniquenessValidator.create(name: "name")
# in a different thread, causing race condition
b = ModelWithUniquenessValidator.create(name: "name")
a.persisted? #=> true
b.persisted? #=> false
b.errors[:name] #=> ["has already been taken"]
You can use this if you don't need to check that the record is unique before attempting to insert it. It will not add any validation to the model, but it will add an error if the persistance fails.
class ModelWithUniqueToken < ActiveRecord::Base
rescue_from_duplicate :token, scope: :shop_id
end
Add this line to your application's Gemfile:
gem 'activerecord-rescue_from_duplicate'
And then execute:
$ bundle
Or install it yourself as:
$ gem install activerecord-rescue_from_duplicate
Install:
docker-compose up -d
Run tests:
rspec
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)FAQs
Unknown package
We found that activerecord-rescue_from_duplicate 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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.