Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
EnumeratedField is a library that provides some nice methods when a string column is used like an enumeration, meaning there is a list of allowable values for the string column. Typically you want the display value as seen by the end user to differ from the stored value, allowing you to easily change the display value at anytime without migrating data, and this little gem helps you with that.
enum_field(field_name, choices, options = {})
Available options are:
:validate
, whether to validate that the value is in the given list
of choices. Defaults to true.:allow_nil
, whether a nil value passes validation. Defaults to
false.:allow_blank
, whether a blank (nil, "") value passes validation.
Defaults to false.The default validation uses ActiveModel's inclusion validations. If
using on a class without ActiveModel use :validate => false
to disable
these.
class Hike < ActiveRecord::Base
include EnumeratedField
# default form
enum_field :duration, [
['Short', 'short'],
['Really, really long', 'long']
]
# disable default validation
enum_field :trail, [
['Pacific Crest Trail', 'pct'],
['Continental Divide Trail', 'cdt'],
['Superior Hiking Trail', 'sht']
], :validate => false
end
> hike = Hike.create(:trail => 'pct', :duration => 'long')
> hike.trail_sht?
=> false
> hike.trail_pct?
=> true
> hike.duration_long?
=> true
> hike.duration_short?
=> false
> hike.trail_display
=> "Pacific Crest Trail"
> hike.duration_display
=> "Really, really long"
> hike.valid?
=> true
> hike.duration = 'forever'
> hike.valid?
=> false
> hike.trail_values # useful to provide to options_for_select when constructing forms
=> [['Pacific Crest Trail', 'pct'], ['Continental Divide Trail', 'cdt'], ['Superior Hiking Trail', 'sht']]
> Hike.trail_values # or get it from the class instead of the instance, if you like
=> [['Pacific Crest Trail', 'pct'], ['Continental Divide Trail', 'cdt'], ['Superior Hiking Trail', 'sht']]
> Hike.trail_values_for_json # or get a hash for injecting into JSON or wherever
=> [{:display => 'Pacific Crest Trail', :value => 'pct'}, {:display => 'Continental Divide Trail', :value => 'cdt'}, {:display => 'Superior Hiking Trail', :value => 'sht'}]
These scopes are only created when your object is an ActiveRecord model.
# performs Hike.where(:trail => Hike::TRAIL_CDT)
> Hike.trail_cdt
# performs Hike.where(:duration => Hike::DURATION_LONG)
> Hike.duration_long
# performs Hike.where(Hike.arel_table[:trail].not_eq(Hike::TRAIL_CDT))
> Hike.trail_not_cdt
# performs Hike.where(Hike.arel_table[:duration].not_eq(Hike::DURATION_LONG))
> Hike.duration_not_long
> Hike::TRAIL_PCT
=> :pct
> Hike::TRAIL_SHT
=> :sht
> hike.trail_display_for("sht")
=> "Superior Hiking Trail"
> hike.duration_display_for("short")
=> "Short"
> hike.trail_value_for("Superior Hiking Trail")
=> "sht"
> hike.duration_value_for("Short")
=> "short"
These methods are all prefixed with the field name by design, which allows multiple fields on a model to exist which potentially have the same values.
Run tests with bundle exec rake test
(or just rake test
if you're
daring).
FAQs
Unknown package
We found that enumerated_field demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.