Sujet : Re: Sanitise user input for a script
De : simon.n.connah (at) *nospam* protonmail.com (Simon Connah)
Groupes : comp.lang.pythonDate : 31. Aug 2024, 05:49:15
Autres entêtes
Message-ID : <mailman.20.1725079772.2917.python-list@python.org>
References : 1 2 3
Pièces jointes : signature.asc (application/pgp-signature) On Friday, 30 August 2024 at 23:35, Thomas Passin via Python-list <
python-list@python.org> wrote:
On 8/30/2024 3:18 PM, Simon Connah via Python-list wrote:
I need to write a script that will take some user input (supplied on a website) and then execute a Python script on a host via SSH. I'm curious what the best options are for protecting against malicious input in much the smae way as you sanitise SQL to protect against SQL injections.
You should never, never, never "sanitize" SQL. Use prepared statements
instead.
Yes. Sorry. I forgot what it was called and accidentally called it sanitising instead but I'm using prepared statements in psycopg 3 for SQL.
What kind of user input do you expect to get that would need to be
"sanitized"? How are you going to use it such that malicious input might
cause trouble? I hope you aren't planning to exec() it. Are you
expecting a user to send in a script and your server will execute it?
Better read up on sandboxing, then.
No. I'm not planning on exec() a random script. I have a prepared Python script which configures various things. The web server connects to the server via SSH and runs my Python script which then runs commands like bhyve (FreeBSD) and it also does things like configure the firewall config file to change firewall rules. The customer has no direct access to the Python script.
In terms of arguments the script that deals with bhyve for instance takes arguments such as CPU count and RAM amount.
If you won't be exec()ing a script, then you can consider creating an
API where each method of the API can only do limited things, and only
with certain parameters not all of all them. The SSH message can include
the name of the method to use.
And follow what Peter Holzer wrote. Don't forget that quoting practices
are not the same between Windows and Linux.
Thank you. I'll look into this. Makes sense.
I could do it either on the website itself or by doing it on the host machine.
I'm thinking of using argparse but I'm aware it does not offer any protection itself.
If someone has any suggestions I'd appreciated it. If you need more information then please let me know.
Simon.
--
https://mail.python.org/mailman/listinfo/python-list