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.
A fast implementation of hashes as objects, benchmarked against OpenStruct. It allows chaining hash attributes as method calls instead of standard hash syntax.
Since an Intellihash extends the native Ruby Hash
, this means instantiating a new intelligent hash is just as fast as a normal hash, and you retain the ability to call all the normal instance methods on it.
intellihash = {
foo: {
bar: {
baz: {
bat: :bam
}
}
}
}
intellihash.foo.bar.baz.bat
#=> :bam
You can achieve similar results by converting your hash to an OpenStruct:
hash = {
foo: {
bar: {
baz: {
bat: :bam
}
}
}
}
open_struct = OpenStruct.new(hash)
open_struct.foo.bar.baz.bat
#=> :bam
But you lose access to your instance methods, and pay a performance penalty to instantiate it!
intellihash.size
#=> 1
open_struct.size
#=> nil
Creating an Intellihash is approximately 5x faster than instantiating an OpenStruct.
user system total real
OpenStruct: 4.046875 0.906250 4.953125 ( 4.979611)
Intellihash: 0.828125 0.125000 0.953125 ( 0.956110)
See Testing Performance for details on running benchmarks.
Add this line to your application's Gemfile:
gem 'intellihash'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install intellihash
If you need to customize Intellihash, you may create a configuration:
Intellihash.configure do |config|
config.enabled = true # (Boolean) Default: false
config.default_format = :symbol # (Symbol) Default: :symbol
config.intelligent_by_default = false # (Boolean) Default: false
end
config.enabled = Rails.env.test?
:sym, :symbol, :str, :string, :any
false
, new hashes can still be converted to intelligent hashes via hash.is_intelligent = true
or hash.to_intellihash!
Place the above configuration in an initializer (such as config/initializers/intellihash.rb
)
If you've configured Intellihash intelligent_by_default = true
, you need to do nothing else as all new hashes are Intellihashes.
However, if you prefer not to use this configuration, you can create an Intellihash from any existing hash:
hash = { foo: :bar }
hash.is_intelligent?
#=> false
hash.is_intelligent = true
# or
hash.to_intellihash!
hash.is_intelligent?
#=> true
Intellihash-powered hashes can access values via method chaining:
intellihash = { foo: :bar }
intellihash.foo
#=> :bar
They can also store values in a similar way:
intellihash.baz = :bat
intellihash
#=> { foo: :bar, baz: :bat }
When configured correctly, they work even when the object contains arrays and other classes:
Intellihash.configure do |config|
config.enabled = true
config.intelligent_by_default = true
config.default_format = :any
end
intellihash = {
a: {
complicated: [
{
object: {
"containing_many" => {
types_of_things: Also::Works
}
}
}
]
}
}
intellihash.a.complicated.first.object.containing_many.types_of_things
#=> Also::Works
When using an Intellihash that has an ambiguous key (i.e. the key exists both as a symbol and a string in the same hash), you can define which you would like to return at runtime:
intellihash ={
foo: :bar
'foo' => 'bar'
}
intellihash.foo(from: :symbol) # or from: :sym
#=> :bar
intellihash.foo(from: :string) # or from: :str
#=> 'bar'
If you prefer, you can use the original hash syntax:
intellihash[:foo]
#=> :bar
intellihash['foo']
#=> 'bar'
NOTE: Intellihashes always store values as the configured default_format
, which defaults to :symbol
. Override this in the config as needed, or use hash syntax: intellihash['bar'] = 'bat'
.
A powerful feature of Intellihash is the ability to use Ruby Hash instance methods on Intellihash. This also means that it's possible for some of the keys in the hash to collide with the names of the instance method you're trying to call.
For instance, consider Hash#size
, which returns the number of keys in the hash:
intellihash = { size: 2 }
intellihash.size
#=> 1
For this reason, it's better to use hash syntax to fetch these keys:
# Bad
intellihash.size
# Good
intellihash[:size]
intellihash.fetch(:size)
You can, however, set keys in the hash using these instance methods. This is not recommended as the syntax leads to confusion when attempting to access the key.
intellihash.size = 3
intellihash
#=> { size: 3 }
intellihash.size
#=> 1
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
Run unit tests:
bundle exec rspec
Run performance benchmarks:
bundle exec rspec --tag performance:true
Bug reports and pull requests are welcome on GitHub at https://github.com/ty-porter/intellihash.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that intellihash demonstrated a not healthy version release cadence and project activity because the last version was released 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
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.