Server-side code injection (Python)

Description

The target application was found vulnerable to code injection. A malicious actor could inject arbitrary Python code to be executed on the server. This could lead to a full system compromise by accessing stored secrets, injecting code to take over accounts, or executing OS commands.

Remediation

Never pass user input directly into functions which evaluate string data as code, such as eval, or exec. There is almost no benefit of passing string values to these methods, as such the best recommendation is to replace the current logic with more safe implementations of dynamically evaluating logic with user input. One alternative is to store functions or methods in a hashmap that can be looked up using a key. If the key exists, the function can be executed.

def func_to_run():
    print('hello world')

function_map = {'fn': func_to_run}

input = 'fn'

if input in function_map:
    function_map[input]()
else:
    print('invalid input')

Details

ID Aggregated CWE Type Risk
94.3 false 94 Active high

Links