Skip to content
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

feat: implement RestrictedPython for secure code execution #251

Closed
wants to merge 15 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
ae7e5ba
feat: add RestrictedPython dependency for secure code execution
devin-ai-integration[bot] Jan 22, 2025
2625015
chore: update Python version constraint to <3.12 for RestrictedPython…
devin-ai-integration[bot] Jan 22, 2025
eae2bab
chore: update poetry.lock with RestrictedPython dependency
devin-ai-integration[bot] Jan 22, 2025
9d7dd6e
feat: implement RestrictedPython for secure code execution
devin-ai-integration[bot] Jan 22, 2025
06d7052
feat: implement RestrictedPython for secure code execution
devin-ai-integration[bot] Jan 22, 2025
e6e37a1
fix: improve type safety in visit_Attribute method
devin-ai-integration[bot] Jan 22, 2025
e6603ab
style: apply Ruff formatting and linting fixes
devin-ai-integration[bot] Jan 22, 2025
bc5565f
fix: resolve MyPy type issues in custom_code_compiler.py
devin-ai-integration[bot] Jan 22, 2025
cde2f1a
fix: update check_name signature to handle allow_magic_methods parameter
devin-ai-integration[bot] Jan 22, 2025
f7afb52
style: fix code formatting
devin-ai-integration[bot] Jan 22, 2025
672dcd3
fix: improve dataclass attribute handling in RestrictedPython transfo…
devin-ai-integration[bot] Jan 22, 2025
80ae60b
fix: improve environment variable handling for custom code execution
devin-ai-integration[bot] Jan 22, 2025
cf72024
fix: update environment variable handling and test expectations
devin-ai-integration[bot] Jan 22, 2025
6bbf8e6
style: fix code formatting and whitespace
devin-ai-integration[bot] Jan 22, 2025
572d55e
Merge branch 'main' into devin/1737586578-restrictedpython
aaronsteers Jan 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: update check_name signature to handle allow_magic_methods parameter
Co-Authored-By: Aaron <AJ> Steers <[email protected]>
devin-ai-integration[bot] and aaronsteers committed Jan 22, 2025

Verified

This commit was signed with the committer’s verified signature.
commit cde2f1a27446971875fd32c0cf8b1856c261e8e9
41 changes: 37 additions & 4 deletions airbyte_cdk/sources/declarative/parsers/custom_code_compiler.py
Original file line number Diff line number Diff line change
@@ -115,16 +115,27 @@ def visit_Attribute(self, node: ast.Attribute) -> ast.AST:

elif isinstance(visited_node.ctx, ast.Del):
raise SyntaxError("Attribute deletion is not allowed")
if not isinstance(visited_node, ast.AST):
raise TypeError(f"Expected ast.AST but got {type(visited_node)}")
return visited_node

def check_name(
self,
node: ast.AST,
name: str,
args: tuple[Any, ...],
kwargs: dict[str, Any],
allow_magic_methods: bool = True,
*args: Any,
**kwargs: Any,
) -> ast.AST:
"""Allow specific private names that are required for dataclasses and type hints."""
"""Allow specific private names that are required for dataclasses and type hints.
Args:
node: The AST node being checked
name: The name being validated
allow_magic_methods: Whether to allow magic methods (defaults to True)
*args: Additional positional arguments
**kwargs: Additional keyword arguments
"""
if name.startswith("_"):
# Allow specific private names
allowed_private = {
@@ -202,14 +213,36 @@ def visit_Call(self, node: ast.Call) -> ast.Call:
return result

def visit_AnnAssign(self, node: ast.AnnAssign) -> ast.AnnAssign:
"""Allow type annotations in variable assignments."""
"""Allow type annotations in variable assignments and dataclass field definitions."""
# Visit the target and annotation nodes
node.target = self.visit(node.target)
node.annotation = self.visit(node.annotation)
if node.value:
node.value = self.visit(node.value)
return node

def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef:
"""Allow dataclass definitions with their attributes."""
# Check if this is a dataclass by looking for the decorator
is_dataclass = any(
isinstance(d, ast.Name) and d.id == "dataclass"
or (isinstance(d, ast.Call) and isinstance(d.func, ast.Name) and d.func.id == "dataclass")
for d in node.decorator_list
)

if is_dataclass:
# For dataclasses, we need to allow attribute statements
node.body = [self.visit(n) for n in node.body]
node.decorator_list = [self.visit(d) for d in node.decorator_list]
if node.bases:
node.bases = [self.visit(b) for b in node.bases]
return node

result = super().visit_ClassDef(node)
if not isinstance(result, ast.ClassDef):
raise TypeError(f"Expected ast.ClassDef but got {type(result)}")
return result


from typing_extensions import Literal