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

Constructor is potentially dangerous #203

Closed
peterbe opened this issue May 15, 2019 · 3 comments · Fixed by #222
Closed

Constructor is potentially dangerous #203

peterbe opened this issue May 15, 2019 · 3 comments · Fixed by #222

Comments

@peterbe
Copy link

peterbe commented May 15, 2019

We use PyQuery internally in our system to parse rendered HTML strings that we store in the database to be displayed in a Django web response. Essentially the code looks a bit like this:

def render_document(document):
    parsed = PyQuery(document.rendered_html_string)
    ...

But if untrusted users can cause that document.rendered_html_string to become a string that looks something like this:

http://internaldomain/api/get_users/dangerous

Then, it becomes...

def render_document(document):
    parsed = PyQuery('http://internaldomain/api/get_users/dangerous')
   ...

which will cause PyQuery to requests.get('http://internaldomain/api/get_users/dangerous') which could be a big security risk.

It happens because of the constructor being too "naive".

On our app, we solved that by making the PyQuery constructor wrapped in a piece of code that does something like this:

def safer_pyquery(*args, **kwargs):
    # SIMPLIFIED FOR EXAMPLE
    args[0] = ' ' + args[0]
    return PyQuery(*args, **kwargs)

Ideally, we'd love to be able to always invoke PyQuery like this:

def render_document(document):
    parsed = PyQuery(html=document.rendered_html_string)
    ...

In similar style to how you can do pq(url='http://google.com/') as mentioned in the Quickstart documentation.

@gawel
Copy link
Owner

gawel commented May 15, 2019

I have no problème with that. Feel free to provide a PR if you want.

Something like this should be enough:

if 'html' in kwargs:
  kwargs['data'] = kwargs.pop('html')
elif current mess:

@jcushman
Copy link
Contributor

@gawel, what would you think of changing the default behavior of PyQuery to not look for a url, and only fetch url contents if the user explicitly provides pq(url='http://google.com/')?

This would require a new version number, because it's a breaking change, but I think it would be worth it for the security benefits. Most users calling pq(text) probably don't want it to make a web request if the text happens to start with 'http', and if users do want that it's easy enough to add url=.

If this sounds good to you I'm happy to send a PR!

@gawel
Copy link
Owner

gawel commented Aug 5, 2021

@jcushman seems fine. go for it

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 a pull request may close this issue.

3 participants