-
Notifications
You must be signed in to change notification settings - Fork 3
fix(propertydata): eval propertydata calculations #351
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
Open
prasad-albert
wants to merge
4
commits into
main
Choose a base branch
from
fix/vuln
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+229
−8
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -4,4 +4,4 @@ | |
|
|
||
| __all__ = ["Albert", "AlbertClientCredentials", "AlbertSSOClient"] | ||
|
|
||
| __version__ = "1.11.2" | ||
| __version__ = "1.12.0" | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -31,15 +31,17 @@ def do_GET(self): | |
| status = "successful" if self.server.token else "failed (no token found)" | ||
| self.send_response(200) | ||
| self.send_header("Content-Type", "text/html") | ||
| self.send_header( | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. set CSP header in response |
||
| "Content-Security-Policy", | ||
| "default-src 'none'; frame-ancestors 'none'; base-uri 'none';", | ||
| ) | ||
| self.end_headers() | ||
| self.wfile.write( | ||
| f""" | ||
| <html> | ||
| <body> | ||
| <h1>Authentication {status}</h1> | ||
| <p>You can close this window now.</p> | ||
| <script>window.close()</script> | ||
| <button onclick="window.close()">Close Window</button> | ||
| </body> | ||
| </html> | ||
| """.encode() | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2,7 +2,10 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| import ast | ||
| import math | ||
| import mimetypes | ||
| import operator | ||
| import re | ||
| import uuid | ||
| from collections.abc import Callable | ||
|
|
@@ -573,6 +576,63 @@ def get_all_columns_used_in_calculations(*, first_row_data_column: list): | |
| return used_columns | ||
|
|
||
|
|
||
| _ALLOWED_BINOPS = { | ||
| ast.Add: operator.add, | ||
| ast.Sub: operator.sub, | ||
| ast.Mult: operator.mul, | ||
| ast.Div: operator.truediv, | ||
| ast.Mod: operator.mod, | ||
| ast.Pow: operator.pow, | ||
| } | ||
| _ALLOWED_UNARYOPS = { | ||
| ast.UAdd: operator.pos, | ||
| ast.USub: operator.neg, | ||
| } | ||
| _ALLOWED_FUNCS: dict[str, tuple[Callable[..., float], int]] = { | ||
| "log10": (math.log10, 1), | ||
| "ln": (math.log, 1), | ||
| "sqrt": (math.sqrt, 1), | ||
| "pi": (lambda: math.pi, 0), | ||
| } | ||
| _ALLOWED_NAMES = {"pi": math.pi} | ||
|
|
||
|
|
||
| def _safe_eval_math(*, expression: str) -> float: | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use custom eval using |
||
| """Safely evaluate supported math expressions.""" | ||
| parsed = ast.parse(expression, mode="eval") | ||
|
|
||
| def _eval(node: ast.AST) -> float: | ||
| if isinstance(node, ast.Expression): | ||
| return _eval(node.body) | ||
| if isinstance(node, ast.Constant) and isinstance(node.value, (int | float)): | ||
| return node.value | ||
| if isinstance(node, ast.BinOp) and type(node.op) in _ALLOWED_BINOPS: | ||
| return _ALLOWED_BINOPS[type(node.op)](_eval(node.left), _eval(node.right)) | ||
| if isinstance(node, ast.UnaryOp) and type(node.op) in _ALLOWED_UNARYOPS: | ||
| return _ALLOWED_UNARYOPS[type(node.op)](_eval(node.operand)) | ||
| if isinstance(node, ast.Call): | ||
| if not isinstance(node.func, ast.Name): | ||
| raise ValueError("Unsupported function call.") | ||
| func_name = node.func.id | ||
| if func_name not in _ALLOWED_FUNCS: | ||
| raise ValueError("Unsupported function.") | ||
| if node.keywords: | ||
| raise ValueError("Keyword arguments are not supported.") | ||
| func, arity = _ALLOWED_FUNCS[func_name] | ||
| if len(node.args) != arity: | ||
| raise ValueError("Unsupported function arity.") | ||
| if arity == 0: | ||
| return func() | ||
| return func(_eval(node.args[0])) | ||
| if isinstance(node, ast.Name): | ||
| if node.id in _ALLOWED_NAMES: | ||
| return _ALLOWED_NAMES[node.id] | ||
| raise ValueError("Unsupported name.") | ||
| raise ValueError("Unsupported expression.") | ||
|
|
||
| return _eval(parsed) | ||
|
|
||
|
|
||
| def evaluate_calculation(*, calculation: str, column_values: dict) -> float | None: | ||
| """Evaluate a calculation expression against column values.""" | ||
| calculation = calculation.lstrip("=") | ||
|
|
@@ -589,7 +649,7 @@ def repl(match: re.Match) -> str: | |
| calculation = pattern.sub(repl, calculation) | ||
|
|
||
| calculation = calculation.replace("^", "**") | ||
| return eval(calculation) | ||
| return _safe_eval_math(expression=calculation) | ||
| except Exception as e: | ||
| logger.info( | ||
| "Error evaluating calculation '%s': %s. Likely do not have all values needed.", | ||
|
|
||
This file contains hidden or 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
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.
use
subprocessinstead ofpopen