
Research
/Security News
Toptal’s GitHub Organization Hijacked: 10 Malicious Packages Published
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
The constify
library provides the freezeparams
decorator to make mutable default values and arguments passed to functions immutable.
freezeparams
decorator ?[1] The default value of a parameter is evaluated only once, which can lead to unexpected behavior if the value is a mutable object, such as a list or a dictionary. For example, the following function accumulates arguments across calls:
def f(a, L=[]):
L.append(a)
return L
print(f(1)) # [1]
print(f(2)) # [1, 2]
print(f(3)) # [1, 2, 3]
To prevent this default value from being shared, the function can be rewritten as follows:
def f(a, L=None):
if L is None:
L = []
L.append(a)
return L
This solution is effective, but with multiple mutable default values, it requires many checks. The freezeparams
decorator simplifies the use of mutable objects as default values and arguments.
pip install constify
from constify import freezeparams
@freezeparams
def f(a, L=[]):
L.append(a)
return L
# Default value (list) remains unchanged
print("f(1) =>", f(1)) # [1]
print("f(2) =>", f(2)) # [2]
print("f(3) =>", f(3)) # [3]
print("-" * 25) # just a separator
# Passed list remains intact
x = [1, 2]
print("x =", x) # [1, 2]
print(f"f(3, {x}) =>", f(3, x)) # [1, 2, 3]
print(f"f(4, {x}) =>", f(4, x)) # [1, 2, 4]
print("x =", x) # [1, 2]
You can comment out the decorator and run the code again to see the difference.
Simple and effective!
[1] https://docs.python.org/3.13/tutorial/controlflow.html#default-argument-values
FAQs
Makes functions parameters immutable
We found that constify 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.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
Research
/Security News
Socket researchers investigate 4 malicious npm and PyPI packages with 56,000+ downloads that install surveillance malware.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.