-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore[Security]: restrict libs to allow specific functionalities #1429
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3850595
chore[Security]: restrict libs to allow specific functionalities
ArslanSaleem e5ec4ae
remove: extra lib handling
ArslanSaleem 0b09313
fix: ruff errors
ArslanSaleem 557e5cd
fix: error message for bad import
ArslanSaleem e7eb9e3
fix: add io library in the blacklist
ArslanSaleem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class BaseRestrictedModule: | ||
def _wrap_function(self, func): | ||
def wrapper(*args, **kwargs): | ||
# Check for any suspicious arguments that might be used for importing | ||
for arg in args + tuple(kwargs.values()): | ||
if isinstance(arg, str) and any( | ||
module in arg.lower() | ||
for module in ["io", "os", "subprocess", "sys", "importlib"] | ||
): | ||
raise SecurityError( | ||
f"Potential security risk: '{arg}' is not allowed" | ||
) | ||
return func(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
def _wrap_class(self, cls): | ||
class WrappedClass(cls): | ||
def __getattribute__(self, name): | ||
attr = super().__getattribute__(name) | ||
return self._wrap_function(self, attr) if callable(attr) else attr | ||
|
||
return WrappedClass | ||
|
||
|
||
class SecurityError(Exception): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import base64 | ||
|
||
from .base_restricted_module import BaseRestrictedModule | ||
|
||
|
||
class RestrictedBase64(BaseRestrictedModule): | ||
def __init__(self): | ||
self.allowed_functions = [ | ||
"b64encode", # Safe function to encode data into base64 | ||
"b64decode", # Safe function to decode base64 encoded data | ||
] | ||
|
||
# Bind the allowed functions to the object | ||
for func in self.allowed_functions: | ||
if hasattr(base64, func): | ||
setattr(self, func, self._wrap_function(getattr(base64, func))) | ||
|
||
def __getattr__(self, name): | ||
if name not in self.allowed_functions: | ||
raise AttributeError(f"'{name}' is not allowed in RestrictedBase64") | ||
return getattr(base64, name) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import datetime | ||
|
||
from .base_restricted_module import BaseRestrictedModule | ||
|
||
|
||
class RestrictedDatetime(BaseRestrictedModule): | ||
def __init__(self): | ||
self.allowed_attributes = [ | ||
# Classes | ||
"date", | ||
"time", | ||
"datetime", | ||
"timedelta", | ||
"tzinfo", | ||
"timezone", | ||
# Constants | ||
"MINYEAR", | ||
"MAXYEAR", | ||
# Time zone constants | ||
"UTC", | ||
# Functions | ||
"now", | ||
"utcnow", | ||
"today", | ||
"fromtimestamp", | ||
"utcfromtimestamp", | ||
"fromordinal", | ||
"combine", | ||
"strptime", | ||
# Timedelta operations | ||
"timedelta", | ||
# Date operations | ||
"weekday", | ||
"isoweekday", | ||
"isocalendar", | ||
"isoformat", | ||
"ctime", | ||
"strftime", | ||
"year", | ||
"month", | ||
"day", | ||
"hour", | ||
"minute", | ||
"second", | ||
"microsecond", | ||
# Time operations | ||
"replace", | ||
"tzname", | ||
"dst", | ||
"utcoffset", | ||
# Comparison methods | ||
"min", | ||
"max", | ||
] | ||
|
||
for attr in self.allowed_attributes: | ||
if hasattr(datetime, attr): | ||
setattr(self, attr, self._wrap_function(getattr(datetime, attr))) | ||
|
||
def __getattr__(self, name): | ||
if name not in self.allowed_attributes: | ||
raise AttributeError(f"'{name}' is not allowed in RestrictedDatetime") | ||
|
||
return getattr(datetime, name) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import json | ||
|
||
from .base_restricted_module import BaseRestrictedModule | ||
|
||
|
||
class RestrictedJson(BaseRestrictedModule): | ||
def __init__(self): | ||
self.allowed_functions = [ | ||
"load", | ||
"loads", | ||
"dump", | ||
"dumps", | ||
] | ||
|
||
# Bind the allowed functions to the object | ||
for func in self.allowed_functions: | ||
if hasattr(json, func): | ||
setattr(self, func, self._wrap_function(getattr(json, func))) | ||
|
||
def __getattr__(self, name): | ||
if name not in self.allowed_functions: | ||
raise AttributeError(f"'{name}' is not allowed in RestrictedJson") | ||
return getattr(json, name) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
io
?