Skip to content

Commit bbb5675

Browse files
committed
Python: Validate YEAST parser command path
Addresses the issue brought up by github-advanced-security. Allow only supported CodeQL platforms and ensure the resolved tools directory and executable remain within the extractor pack before invoking the parser.
1 parent b1c2fdd commit bbb5675

2 files changed

Lines changed: 107 additions & 21 deletions

File tree

‎python/extractor/semmle/python/parser/yeast_parser.py‎

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,68 @@
55
import subprocess
66

77

8-
if "CODEQL_EXTRACTOR_PYTHON_ROOT" in os.environ:
9-
platform = os.environ["CODEQL_PLATFORM"]
8+
_SUPPORTED_PLATFORMS = frozenset(("linux64", "linux-arm64", "osx64", "win64"))
9+
10+
11+
def _is_within(path, root):
12+
try:
13+
return os.path.commonpath((root, path)) == root
14+
except ValueError:
15+
return False
16+
17+
18+
def _packaged_command(extractor_root, platform):
19+
if platform not in _SUPPORTED_PLATFORMS:
20+
raise ValueError("Unsupported CodeQL platform {!r}".format(platform))
21+
22+
tools_root = os.path.realpath(os.path.join(extractor_root, "tools"))
23+
tools = os.path.realpath(os.path.join(tools_root, platform))
24+
if not _is_within(tools, tools_root):
25+
raise ValueError("Platform tools directory escapes extractor tools root")
26+
1027
ext = ".exe" if platform == "win64" else ""
11-
tools = os.path.join(os.environ["CODEQL_EXTRACTOR_PYTHON_ROOT"], "tools", platform)
12-
command = [os.path.join(tools, "yeast-python" + ext)]
13-
command_separator = []
14-
else:
15-
script_path = os.path.dirname(os.path.realpath(__file__))
16-
yeast_python_path = os.path.join(script_path, "../../../yeast-python")
17-
cargo_file = os.path.join(yeast_python_path, "Cargo.toml")
18-
command = [
19-
"cargo",
20-
"run",
21-
"--quiet",
22-
"--release",
23-
"--manifest-path=" + cargo_file,
24-
]
25-
command_separator = ["--"]
28+
executable = os.path.realpath(os.path.join(tools, "yeast-python" + ext))
29+
if not _is_within(executable, tools):
30+
raise ValueError("Parser executable escapes platform tools directory")
31+
return [executable]
32+
33+
34+
_SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__))
35+
_SOURCE_TREE_ROOT = os.path.realpath(
36+
os.path.join(_SCRIPT_PATH, "../../../yeast-python")
37+
)
38+
_SOURCE_TREE_COMMAND = [
39+
"cargo",
40+
"run",
41+
"--quiet",
42+
"--release",
43+
"--manifest-path=" + os.path.join(_SOURCE_TREE_ROOT, "Cargo.toml"),
44+
]
45+
46+
47+
def _parser_command():
48+
if "CODEQL_EXTRACTOR_PYTHON_ROOT" in os.environ:
49+
return (
50+
_packaged_command(
51+
os.environ["CODEQL_EXTRACTOR_PYTHON_ROOT"],
52+
os.environ["CODEQL_PLATFORM"],
53+
),
54+
[],
55+
None,
56+
)
57+
return (
58+
_SOURCE_TREE_COMMAND,
59+
["--"],
60+
_SOURCE_TREE_ROOT,
61+
)
2662

2763

2864
def parse(path, source, _logger):
29-
command_args = command + command_separator + [path]
65+
command, command_separator, cwd = _parser_command()
66+
command_args = command + command_separator + [os.path.abspath(path)]
3067
process = subprocess.Popen(
3168
command_args,
69+
cwd=cwd,
3270
stdin=subprocess.PIPE,
3371
stdout=subprocess.PIPE,
3472
stderr=subprocess.PIPE,

‎python/extractor/tests/test_yeast_parser.py‎

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from pathlib import Path
22

3+
import pytest
4+
35
from semmle.python.parser import yeast_parser
46
from semmle.python.parser.tokenizer import encoding_from_source
57

@@ -9,8 +11,9 @@ def debug(self, *_args, **_kwargs):
911
pass
1012

1113

12-
def test_non_utf8_source_is_passed_as_utf8(mocker):
13-
path = Path(__file__).parent / "tokenizer" / "latin.py"
14+
def test_non_utf8_source_is_passed_as_utf8(mocker, monkeypatch):
15+
monkeypatch.delenv("CODEQL_EXTRACTOR_PYTHON_ROOT", raising=False)
16+
path = (Path(__file__).parent / "tokenizer" / "latin.py").resolve()
1417
encoding, source = encoding_from_source(path.read_bytes())
1518
source = source.decode(encoding)
1619
process = mocker.Mock(returncode=0)
@@ -21,10 +24,55 @@ def test_non_utf8_source_is_passed_as_utf8(mocker):
2124
)
2225

2326
assert yeast_parser.parse(str(path), source, Logger()) is None
27+
command, command_separator, cwd = yeast_parser._parser_command()
2428
popen.assert_called_once_with(
25-
yeast_parser.command + yeast_parser.command_separator + [str(path)],
29+
command + command_separator + [str(path)],
30+
cwd=cwd,
2631
stdin=yeast_parser.subprocess.PIPE,
2732
stdout=yeast_parser.subprocess.PIPE,
2833
stderr=yeast_parser.subprocess.PIPE,
2934
)
3035
process.communicate.assert_called_once_with(source.encode("utf-8"))
36+
37+
38+
def test_packaged_command_accepts_supported_platform(tmp_path):
39+
expected = tmp_path / "tools" / "linux64" / "yeast-python"
40+
41+
assert yeast_parser._packaged_command(str(tmp_path), "linux64") == [
42+
str(expected)
43+
]
44+
45+
46+
def test_packaged_command_rejects_unsupported_platform(tmp_path):
47+
with pytest.raises(ValueError, match="Unsupported CodeQL platform"):
48+
yeast_parser._packaged_command(str(tmp_path), "../linux64")
49+
50+
51+
def test_packaged_command_rejects_tools_symlink_escape(tmp_path):
52+
outside = tmp_path / "outside"
53+
outside.mkdir()
54+
tools = tmp_path / "tools"
55+
tools.mkdir()
56+
(tools / "linux64").symlink_to(outside, target_is_directory=True)
57+
58+
with pytest.raises(ValueError, match="escapes extractor tools root"):
59+
yeast_parser._packaged_command(str(tmp_path), "linux64")
60+
61+
62+
def test_packaged_command_rejects_executable_symlink_escape(tmp_path):
63+
outside = tmp_path / "outside"
64+
outside.touch()
65+
tools = tmp_path / "tools" / "linux64"
66+
tools.mkdir(parents=True)
67+
(tools / "yeast-python").symlink_to(outside)
68+
69+
with pytest.raises(ValueError, match="escapes platform tools directory"):
70+
yeast_parser._packaged_command(str(tmp_path), "linux64")
71+
72+
73+
def test_packaged_command_validation_is_deferred(monkeypatch, tmp_path):
74+
monkeypatch.setenv("CODEQL_EXTRACTOR_PYTHON_ROOT", str(tmp_path))
75+
monkeypatch.setenv("CODEQL_PLATFORM", "../linux64")
76+
77+
with pytest.raises(ValueError, match="Unsupported CodeQL platform"):
78+
yeast_parser._parser_command()

0 commit comments

Comments
 (0)