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

Dangerous network data parsing using eval() #30

Closed
RemiCardona opened this issue Nov 13, 2012 · 5 comments
Closed

Dangerous network data parsing using eval() #30

RemiCardona opened this issue Nov 13, 2012 · 5 comments

Comments

@RemiCardona
Copy link

Using eval() to parse data received from the network is a huge security hole. There are 2 ways to fix this:

  • the ast.literal_eval() function should happily parse Solr's pythonic output,
  • use the json module on all python versions since mysolr seems to support python 2.6 and up.

I have opted for the second solution since the code being already there, I assumed it worked properly on newer python versions.

NB: I have tested this patch on python 2.6 against solr 3.6, on Debian Squeeze.

RemiCardona pushed a commit to RemiCardona/mysolr that referenced this issue Nov 13, 2012
the 'json' module is in python's standard library since 2.6 and using
eval() is a huge security risk.
@moliware
Copy link
Contributor

Hi,

Thanks for contributing! We started to use eval because evaluating a python expression is much faster than parsing a json or xml.

It's true that is less secure but we gain a lot of speed, so I think I will use your first way that it is also safer than using eval directly.

Thanks!!

@RemiCardona
Copy link
Author

Hi Miguel,

I just did a couple of tests on a 5000-row resultset coming from solr. A friend of mine suggested I add anyjson to the list. Here's what I got:

  • eval(): ~0.5s
  • ast.literal_eval(): ~1s
  • json.loads(): ~1.2s
  • anyjson: ~0.1s

That last test just blew me off, so I triple-checked and anyjson is just a simple wrapper over various json libraries and it used simplejson on my test machine.

So I would definitely suggest using anyjson (which is available on pypi) or simplejson directly using a simple try/except ImportError.

Cheers

@moliware
Copy link
Contributor

Wow, I'm going to test it and include anyjson.

Thank you so much for your contribution

@RemiCardona
Copy link
Author

The updated patch works fine on my system. Cheers.

@moliware
Copy link
Contributor

Hi,

From anyjson doc:

"Anyjson loads whichever is the fastest JSON module"

So, I will try each json library for seeing what library is the fastest in order to include this in the documentation. Then I will merge the pull request.

Thanks!

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

No branches or pull requests

2 participants