-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Avoid using string parsing for ParameterExpression.sympify() #14391
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
Conversation
In the recently merged Qiskit#13278 the implementation for the sympify method for ParameterExpression was changed because we no longer rely on symengine internally. Previously we would just return the inner symengine object used to represent the symbolic expression. Without symengine available Qiskit#13278 updated the implementation of the method to generate a string representation of the expression and pass that to sympify which has a "parser" for converting that expression string to a sympy object. However, sympify() method is insecure as it internally relies on Python's eval() to parse the string and can't be used for untrusted input. While this doesn't have the same exact exposure as in GHSA-6m2c-76ff-6vrf because you have to opt-in to using this function with input that is untrusted and the degrees of freedom are less because it has to go through the rust symbolic expression it is still a potential vulnerability waiting to happen. This commit reworks the sympify implementation to avoid using sympy's parser and instead just builds the sympy expression from the internal state directly.
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 15165457978Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
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.
This looks much cleaner than relying on sympify
👍🏻 Would it make sense to add some concrete test cases, or do you think this is sufficiently tested implicitly? Right now I think there's only a single test that calls sympify
directly, and it's not comparing to a hardcoded SymPy expression.
I'll add some specific tests, there is definitely some coverage from template optimization. It caught some bugs, but I can reuse the circuits from the qpy tests and test |
Test added in: 39e360a it caught several issues with the implementation |
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.
LGTM
…14391) * Avoid using string parsing for ParameterExpression.sympify() In the recently merged Qiskit#13278 the implementation for the sympify method for ParameterExpression was changed because we no longer rely on symengine internally. Previously we would just return the inner symengine object used to represent the symbolic expression. Without symengine available Qiskit#13278 updated the implementation of the method to generate a string representation of the expression and pass that to sympify which has a "parser" for converting that expression string to a sympy object. However, sympify() method is insecure as it internally relies on Python's eval() to parse the string and can't be used for untrusted input. While this doesn't have the same exact exposure as in GHSA-6m2c-76ff-6vrf because you have to opt-in to using this function with input that is untrusted and the degrees of freedom are less because it has to go through the rust symbolic expression it is still a potential vulnerability waiting to happen. This commit reworks the sympify implementation to avoid using sympy's parser and instead just builds the sympy expression from the internal state directly. * Remove unused Rust functions that support sympy string generation * Add test coverage for all of parameter expression * Add .sign() to the megaexpression
Summary
In the recently merged #13278 the implementation for the sympify method for ParameterExpression was changed because we no longer rely on symengine internally. Previously we would just return the inner symengine object used to represent the symbolic expression. Without symengine available #13278 updated the implementation of the method to generate a string representation of the expression and pass that to sympify which has a "parser" for converting that expression string to a sympy object. However, sympify() method is insecure as it internally relies on Python's eval() to parse the string and can't be used for untrusted input. While this doesn't have the same exact exposure as in GHSA-6m2c-76ff-6vrf because you have to opt-in to using this function with input that is untrusted and the degrees of freedom are less because it has to go through the rust symbolic expression it is still a potential vulnerability waiting to happen. This commit reworks the sympify implementation to avoid using sympy's parser and instead just builds the sympy expression from the internal state directly.
Details and comments