Skip to content

Commit 808af28

Browse files
author
Porcupiney Hairs
committed
Python : Arbitrary codde execution due to Js2Py
Js2Py is a Javascript to Python translation library written in Python. It allows users to invoke JavaScript code directly from Python. The Js2Py interpreter by default exposes the entire standard library to it's users. This can lead to security issues if a malicious input were directly. This PR includes a CodeQL query along with a qhelp and testcases to detect cases where an untrusted input flows to an Js2Py eval call. This query successfully detects CVE-2023-0297 in `pyload/pyload`along with it's fix. The databases can be downloaded from the links bellow. ``` https://file.io/qrMEjSJJoTq1 https://filetransfer.io/data-package/a02eab7V#link ```
1 parent b779341 commit 808af28

File tree

7 files changed

+91
-0
lines changed

7 files changed

+91
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE qhelp SYSTEM "qhelp.dtd">
2+
<qhelp>
3+
<overview>
4+
<p>
5+
Passing untrusted inputs to a JavaScript interpreter like `Js2Py` can lead to arbitrary
6+
code execution.
7+
</p>
8+
</overview>
9+
<recommendation>
10+
<p> This vulnerability can be prevented either by preventing an untrusted user input to flow
11+
to an <code>eval_js</code> call. Or, the impact of this vulnerability can be
12+
significantly reduced by disabling imports from the interepreted code (note that in a <a
13+
href="https://github.com/PiotrDabkowski/Js2Py/issues/45#issuecomment-258724406">
14+
comment</a> the author of the library highlights that Js2Py is still insecure with this
15+
option).</p>
16+
</recommendation>
17+
<example>
18+
<p>In the example below, the Javascript code being evaluated is controlled by the user and
19+
hence leads to arbitrary code execution.</p>
20+
<sample src="Js2PyBad.py" />
21+
<p>This can be fixed by disabling imports before evaluating the user passed buffer.
22+
<sample src="Js2PyGood.py" />
23+
</example>
24+
</qhelp>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @name JavaScript code execution.
3+
* @description Passing user supplied arguments to a Javascript to Python translation engine such as Js2Py can lead to remote code execution.
4+
* @severity high
5+
* @kind path-problem
6+
* @id py/js2py-rce
7+
* @tags security
8+
* experimental
9+
* external/cwe/cwe-94
10+
*/
11+
12+
import python
13+
import semmle.python.ApiGraphs
14+
import semmle.python.dataflow.new.TaintTracking
15+
import semmle.python.dataflow.new.RemoteFlowSources
16+
import semmle.python.Concepts
17+
18+
module Js2PyFlowConfig implements DataFlow::ConfigSig {
19+
predicate isSource(DataFlow::Node node) { node instanceof RemoteFlowSource }
20+
21+
predicate isSink(DataFlow::Node node) {
22+
API::moduleImport("js2py").getMember(["eval_js", "eval_js6", "EvalJs"]).getACall().getArg(_) =
23+
node
24+
}
25+
}
26+
27+
module Js2PyFlow = TaintTracking::Global<Js2PyFlowConfig>;
28+
29+
import Js2PyFlow::PathGraph
30+
31+
from Js2PyFlow::PathNode source, Js2PyFlow::PathNode sink
32+
where
33+
Js2PyFlow::flowPath(source, sink) and
34+
not exists(API::moduleImport("js2py").getMember("disable_pyimport").getACall())
35+
select sink, source, sink, "This input to Js2Py depends on a $@.", source.getNode(),
36+
"user-provided value"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@bp.route("/bad")
2+
def bad():
3+
jk = flask.request.form["jk"]
4+
jk = eval_js(f"{jk} f()")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@bp.route("/good")
2+
def good():
3+
# disable python imports to prevent execution of malicious code
4+
js2py.disable_pyimport()
5+
jk = flask.request.form["jk"]
6+
jk = eval_js(f"{jk} f()")
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
edges
2+
| Js2PyTest.py:9:5:9:6 | ControlFlowNode for jk | Js2PyTest.py:10:18:10:28 | ControlFlowNode for Fstring | provenance | |
3+
| Js2PyTest.py:9:10:9:22 | ControlFlowNode for Attribute | Js2PyTest.py:9:5:9:6 | ControlFlowNode for jk | provenance | AdditionalTaintStep |
4+
nodes
5+
| Js2PyTest.py:9:5:9:6 | ControlFlowNode for jk | semmle.label | ControlFlowNode for jk |
6+
| Js2PyTest.py:9:10:9:22 | ControlFlowNode for Attribute | semmle.label | ControlFlowNode for Attribute |
7+
| Js2PyTest.py:10:18:10:28 | ControlFlowNode for Fstring | semmle.label | ControlFlowNode for Fstring |
8+
subpaths
9+
#select
10+
| Js2PyTest.py:10:18:10:28 | ControlFlowNode for Fstring | Js2PyTest.py:9:10:9:22 | ControlFlowNode for Attribute | Js2PyTest.py:10:18:10:28 | ControlFlowNode for Fstring | This can lead to arbitrary code execution |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/Security/CWE-094/Js2Py.ql
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
import flask
3+
from js2py import eval_js, disable_pyimport
4+
5+
bp = flask.Blueprint("app", __name__, url_prefix="/")
6+
7+
@bp.route("/bad")
8+
def bad():
9+
jk = flask.request.form["jk"]
10+
jk = eval_js(f"{jk} f()")

0 commit comments

Comments
 (0)