Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
has_private_attributes
Advanced tools
This Ruby gem provides a simple and elegant way to define private attributes in your classes. It allows you to create static, lazy-evaluated, and argument-based private attributes with automatic caching and freezing of the returned values, all in a thread-safe manner.
Add this line to your application's Gemfile:
gem 'has_private_attributes'
And then execute:
bundle install
Here's a simple example of how to use the HasPrivateAttributes
gem:
class MyClass
include HasPrivateAttributes
# definition of private attributes
private_attribute :static_servers, [
{ ip: '1.1.1.1', location: 'US' },
{ ip: '8.8.8.8', location: 'US' }
]
private_attribute :lazy_servers do
[
{ ip: '2.2.2.2', location: 'EU' },
{ ip: '3.3.3.3', location: 'EU' }
]
end
private_attribute :servers_by_region do |region|
case region
when 'us'
[
{ ip: '1.1.1.1', location: 'US' },
{ ip: '8.8.8.8', location: 'US' }
]
when 'eu'
[
{ ip: '2.2.2.2', location: 'EU' }
]
end
end
# usage of private attributes
def get_static_servers
static_servers
end
def get_lazy_servers
lazy_servers
end
def get_servers_by_region(region)
servers_by_region(region)
end
end
instance = MyClass.new
puts instance.get_static_servers
# Output:
# [
# { ip: '1.1.1.1', location: 'US' },
# { ip: '8.8.8.8', location: 'US' }
# ]
puts instance.get_lazy_servers
# Output:
# [
# { ip: '2.2.2.2', location: 'EU' },
# { ip: '3.3.3.3', location: 'EU' }
# ]
puts instance.get_servers_by_region('us')
# Output:
# [
# { ip: '1.1.1.1', location: 'US' },
# { ip: '8.8.8.8', location: 'US' }
# ]
puts instance.get_servers_by_region('eu')
# Output:
# [
# { ip: '2.2.2.2', location: 'EU' }
# ]
Here are some more examples of using the HasPrivateAttributes
gem:
instance.static_servers # => [{ ip: '1.1.1.1', location: 'US' }, { ip: '8.8.8.8', location: 'US' }]
instance.static_servers.frozen? # => true
instance.lazy_servers # => [{ ip: '2.2.2.2', location: 'EU' }, { ip: '3.3.3.3', location: 'EU' }]
instance.lazy_servers.object_id # => 12345678
instance.lazy_servers.object_id # => 12345678 (same object)
instance.servers_by_region('us') # => [{ ip: '1.1.1.1', location: 'US' }, { ip: '8.8.8.8', location: 'US' }]
instance.servers_by_region('eu') # => [{ ip: '2.2.2.2', location: 'EU' }]
All operations in HasPrivateAttributes are thread-safe. This means you can safely use private attributes in multi-threaded environments without worrying about race conditions or data inconsistencies. The gem uses Ruby's Monitor
and Mutex
classes to ensure proper synchronization.
For example, you can safely access private attributes from multiple threads:
threads = []
10.times do
threads << Thread.new do
puts instance.get_lazy_servers
end
end
threads.each(&:join)
This will safely initialize and return the lazy servers, even if multiple threads try to access it simultaneously.
Bug reports and pull requests are welcome on GitHub at https://github.com/sebyx07/has_private_attributes. 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 HasPrivateAttributes project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
FAQs
Unknown package
We found that has_private_attributes 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.