Search code examples
pythonevalcode-injection

Improper Neutralization of Directives in Dynamically Evaluated Code ('Eval Injection')


Why am I receiving an eval injection error?

 if len(sys.argv) > 1:
   eval(sys.argv[1])(logger, *sys.argv[2:])

Solution

  • Use ast.literal_eval instead of eval.

    Code:

    from ast import literal_eval as eval
    if len(sys.argv) > 1:
         eval(sys.argv[1])(logger, *sys.argv[2:])
    

    Eval is dangerous