Skip to content

Commit

Permalink
addresses PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBarberini committed Sep 11, 2024
1 parent 6015453 commit 568debc
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions lib/services/rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,16 @@ def check_parachute_trigger(expression: str) -> bool:
bool: True if the expression is valid, False otherwise.
"""

class InvalidParachuteTrigger(Exception):
pass

# Parsing the expression into an AST
try:
parsed_expression = ast.parse(expression, mode="eval")
except SyntaxError:
raise SyntaxError("Invalid expression syntax.")
except SyntaxError as e:
raise InvalidParachuteTrigger(
f"Invalid expression syntax: {str(e)}"
) from None

# Constant case (supported after beta v1)
if isinstance(parsed_expression.body, ast.Constant):
Expand All @@ -254,32 +259,36 @@ def check_parachute_trigger(expression: str) -> bool:

# Validating the expression structure
if not isinstance(parsed_expression.body, ast.Lambda):
raise SyntaxError("Invalid expression structure: not a Lambda.")
raise InvalidParachuteTrigger(
"Invalid expression structure: not a lambda."
) from None

lambda_node = parsed_expression.body
if len(lambda_node.args.args) != 3:
raise SyntaxError("Invalid expression structure: invalid arity.")
raise InvalidParachuteTrigger(
"Invalid expression structure: lambda should have 3 arguments."
) from None

if not isinstance(lambda_node.body, ast.Compare):
try:
for operand in lambda_node.body.values:
if not isinstance(operand, ast.Compare):
raise SyntaxError(
"Invalid expression structure (not a Compare)."
)
raise InvalidParachuteTrigger(
"Invalid expression structure: not a Compare."
) from None
except AttributeError:
raise SyntaxError(
"Invalid expression structure (not a Compare)."
)
raise InvalidParachuteTrigger(
"Invalid expression structure: not a Compare."
) from None

# Restricting access to functions or attributes
for node in ast.walk(lambda_node):
if isinstance(node, ast.Call):
raise SyntaxError(
raise InvalidParachuteTrigger(
"Calling functions is not allowed in the expression."
)
) from None
if isinstance(node, ast.Attribute):
raise SyntaxError(
raise InvalidParachuteTrigger(
"Accessing attributes is not allowed in the expression."
)
) from None
return True

0 comments on commit 568debc

Please sign in to comment.