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.
This Gem implements the concept of an abstract base class to Ruby.
This code has been heavily influenced by the code from Mark Bates (http://metabates.com/2011/02/07/building-interfaces-and-abstract-classes-in-ruby/) and James Lopez (https://github.com/bluegod/rint).
The classes provided by Mark Beates are not really "Gem ready". The AbstractInterface
module referenced Bicycle
. The work from James Lopez was much closer to what I wanted. The main problem I saw with his approach is that the must_implement
were have to be provided in the initialize
method which feels wrong and error prone. IMHO the statements should be on the module level. Hence this Gem to address this and work on the class and module level only.
This Gem will rewrite the new method of the class that implements an Interface and that new method will just do the normal init followed by a check if all required methods are implemented.
The new method now looks like this:
def self.new(*args, &block)
obj = self.allocate
obj.send :initialize, *args, &block
obj.send :__check_interface_methods
obj
end
where __check_interface_methods()
does the actuall checking to see if the Abstract Interface has been fully implemented.
Add this line to your application's Gemfile:
gem 'ainterface'
And then execute:
$ bundle
Or install it yourself as:
$ gem install ainterface
This Gem implements the concept of an abstract interface in Ruby
.
#!/usr/bin/env ruby
require 'ainterface'
# Define the abtract interface named Wheels
# Any class that implements Wheels must implement
# the methods 'number_of_wheels' and 'diameter'
module Wheels
must_implement :number_of_wheels
must_implement :diameter
end
# This class implments wheels.
class Car
implements Wheels
def number_of_wheels
4
end
def diameter
13
end
end
car = Car.new
In the above example Car fullfills the Wheels contract and will not raise any error.
if for example the following code would have been written:
class Bicycle
implements Wheels
def number_of_wheels
2
end
end
bicycle = Bicycle.new
The following error message would have been thrown:
(eval):4:in `block in __check_interface_methods': Expected Bicycle to implement diameter for interface Wheels (AInterface::Error::NotImplementedError)
from (eval):2:in `each'
from (eval):2:in `__check_interface_methods'
from (eval):4:in `new'
In the above example Wheels
was a module. If desired an Interface can also be implemented as a class.
Any method that was defined by the Interface module is also added to the class that implements the interface
For example:
module Geometry
must_implement :width
must_implement :height
def outline
2 * width + 2 * height
end
end
class Rectangle
implements Geometry
def width
4
end
def height
3
end
end
rect = Rectangle.new
p (rect.methods - Object.methods).sort
puts "Outline = #{rect.outline}"
will product the following output:
[:height, :outline, :width]
Outline = 14
As such implements
acts as an include
statement.
The environment variable
DISABLE_RUBY_INTERFACE=1
can be set in order to globally disable the abstract interfaces - no Error will get thrown. This might be particularly useful in production for performance reasons if we are confident enough through tests that the interfaces are all implemented.
FAQs
Unknown package
We found that ainterface 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.
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.