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.
A tool for automated delegation with abstract base classes.
This metaclass enables creation of delegating classes inheriting from an abstract base class.
This technique is impossible with regular __getattr__
approach for delegation,
so normally, you would have to define every delegated method explicitly.
Not any more
The metaclasses also enable optional validation of the delegate attributes to ensure they have all of the methods required by the parent object.
pip install abc-delegation
from abc import ABCMeta
from abc_delegation import delegation_metaclass
class A(metaclass=ABCMeta):
@abstractmethod
def bar(self):
pass
@abstractmethod
def foo(self):
pass
class B:
def bar(self):
return "B bar"
def foo(self):
return "B foo"
class C(A, metaclass=delegation_metaclass("my_delegate")):
def __init__(self, b):
self.my_delegate = b
def foo(self):
return "C foo"
c = C(B())
assert c.foo() == "C foo"
assert c.bar() == "B bar"
class A(metaclass=ABCMeta):
@abstractmethod
def bar(self):
pass
@abstractmethod
def foo(self):
pass
class B:
pass
# validation is on by default
class C(A, metaclass=delegation_metaclass("_delegate")):
def __init__(self, b):
self._delegate = b
def foo(self):
return "C foo"
C(B())
# Trying to instantiate C class with B delegate which is missing 'bar' method
# Validation raises an error:
# TypeError: Can't instantiate bar: missing attribute bar in the delegate attribute _delegate
from abc import ABCMeta
from abc_delegation import multi_delegation_metaclass
class A(metaclass=ABCMeta):
@abstractmethod
def bar(self):
pass
@abstractmethod
def foo(self):
pass
@abstractmethod
def baz(self):
pass
class B:
def bar(self):
return "B bar"
def foo(self):
return "B foo"
class X:
def baz(self):
return "X baz"
class C(A, metaclass=multi_delegation_metaclass("_delegate1", "_delegate2")):
def __init__(self, d1, d2):
self._delegate1 = d1
self._delegate2 = d2
def foo(self):
return "C foo"
c = C(B(), X())
assert c.bar() == "B bar"
assert c.foo() == "C foo"
assert c.baz() == "X baz"
Please refer to the unit tests for more examples.
FAQs
A tool for automated delegation with abstract base classes
We found that abc-delegation 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.