Thanks to visit codestin.com
Credit goes to github.com

Skip to content

An alternative approach to avoiding arbitrary evaluation of code when parsing strings as units #251

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

Merged
merged 3 commits into from
Feb 5, 2025

Conversation

apdavison
Copy link
Contributor

Based on https://stackoverflow.com/a/11952618 by https://stackoverflow.com/users/567292/ecatmur

Fixes #250

Also added test for the original issue: #221

@apdavison
Copy link
Contributor Author

@codingchipmunk @zm711 please could you take a look at this?

@zm711
Copy link
Contributor

zm711 commented Dec 3, 2024

Sure I'll look this afternoon :)

Copy link
Contributor

@zm711 zm711 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One initial comment.

Comment on lines 14 to 24
whitelist = (
ast.Expression,
ast.Constant,
ast.Name,
ast.Load,
ast.BinOp,
ast.UnaryOp,
ast.operator,
ast.unaryop,
ast.Num,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would need to carefully check this for our whitelist to make sure everything gets through. But this seems like a great idea overall. Do we want to add a comment to the stackoverflow so we know where the idea came from? This strategy is super cool, but I don't think it would be understandable to a new developer without a comment or link.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the quantities and Neo core tests pass with this whitelist, but yes, I would like some more eyes on this.

I originally thought adding the link to the SO post in the PR was sufficient, but on reflection I think you're right, it should also be added as a comment in the code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I accidentally deleted my quantities dev env. I'll make a new one and run some tests to see, but neo tests passing makes me feel better! I'll try to read a bit more on the ast stuff and let you know soon if I find anything.

Copy link
Contributor

@zm711 zm711 Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Goofy example of what a user could do with current white list. Not security risk, but also nonsense.
try this:

>>> pq.CompoundUnit('5*5*5')
1 (5*5*5)

actually I guess we can't defend against this since we have compound units that need multiply and divide in them. I wish we could think of something that would protect against this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, we could write a formal grammar for these strings, using pyparsing for example, but that seems like overkill, and might have a performance impact.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I've been reflecting more and I think this is fine as is. I think that my example is misuse by the user. I wish we could protect the user, but if they want to do something like this it's on them. We've protected from the security risk so I'm good with this.

Copy link
Contributor

@zm711 zm711 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I tested deleted each of these and they all seem to be required except ast.Num which is deprecated anyway. So we can still have users do nonsensical things (see my example), but I guess that is on them.

Comment on lines 14 to 24
whitelist = (
ast.Expression,
ast.Constant,
ast.Name,
ast.Load,
ast.BinOp,
ast.UnaryOp,
ast.operator,
ast.unaryop,
ast.Num,
)
Copy link
Contributor

@zm711 zm711 Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Goofy example of what a user could do with current white list. Not security risk, but also nonsense.
try this:

>>> pq.CompoundUnit('5*5*5')
1 (5*5*5)

actually I guess we can't defend against this since we have compound units that need multiply and divide in them. I wish we could think of something that would protect against this.

apdavison and others added 2 commits February 5, 2025 17:44
Copy link
Contributor

@zm711 zm711 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are all passing so I think we go forward with this!

@apdavison apdavison merged commit 7cc5dde into python-quantities:master Feb 5, 2025
39 checks passed
@apdavison apdavison deleted the avoid-eval branch February 5, 2025 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

quantities 0.16.0 and newer cannot parse 'angstrom'
2 participants