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

How to sanitize user input to prevent Python injection? #91

Closed
holdenmatt opened this issue Feb 9, 2023 · 5 comments
Closed

How to sanitize user input to prevent Python injection? #91

holdenmatt opened this issue Feb 9, 2023 · 5 comments
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@holdenmatt
Copy link

Hi, I'm starting to use this in a new project, and really like how simple you make it to use pyodide in a React app (nice work!).

One issue I've run into is how to sanitize user input. I have a user-provided string in JS that I want to use as a parameter for a Python function, very similar to your example here:
https://elilambnz.github.io/react-py/docs/examples/user-input#user-input-field

However, the approach you use there is vulnerable to injecting arbitrary code. For example, I can trick it into running arbitrary Python commands in pyodide, like this:

image

Any recommendation for how to sanitize a string to prevent this?

Another approach might be to expose the globals object from Pyodide, as described here:
https://pyodide.org/en/stable/usage/faq.html#how-can-i-execute-code-in-a-custom-namespace

Then variables could be set that way instead of via string concatenation. Sounds like that might already be planned?

@elilambnz
Copy link
Owner

Hi, thanks for your interest in react-py. This method of capturing user input is a bit of a hack, and is very susceptible as you've pointed out.

I've been working on a patch for stdin which will allow pausing code execution to capture user input. This will remove the need for a workaround and will also work for scenarios where you might have input in a loop for example.

Due to the complexity of getting this to work with web workers, for multiple instances and designing a nice API, this is taking me a little while to figure out. Here's a preview of a paused script waiting for input:

PHOTO-2023-01-15-17-49-24

If you're just wanting to set a global before executing code instead of an interactive approach, this will also be available eventually through getter/setter methods.

@elilambnz elilambnz self-assigned this Feb 9, 2023
@elilambnz elilambnz added documentation Improvements or additions to documentation enhancement New feature or request labels Feb 9, 2023
@holdenmatt
Copy link
Author

Thanks for your reply, makes sense.

Some way to pass in globals from JS to Python would be ideal for my use case. Will keep an eye out for that!

@jothikannan89
Copy link

@elilambnz

this pausing the execution to capture the input is available in the current version? I am in need of it. Just wanted to make sure, before I start working on it

@elilambnz
Copy link
Owner

@jothikannan89 pausing execution to capture input is not currently available on the main branch. The method I'm trying relies on using an additional Service Worker (not a Web Worker) to listen for input events.

The code is on this branch https://github.com/elilambnz/react-py/tree/stdin-patch specifically in the src/workers/python-worker.ts file.

Please feel free to open a new issue if you plan to work on this and would like to discuss further.

@elilambnz
Copy link
Owner

@jothikannan89 the latest version of react-py now supports a patch for stdin. You can try in out here, please let me know if you encounter any issues with using it in your project.

@holdenmatt support for accessing Python scope is being tracked here #67

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants