test_doxyfile_injection.py¶
tests/test_doxyfile_injection.py
A repo cannot smuggle a directive into the Doxyfile, nor declare a filter command.
test_a_file_inlining_comment_leaks_nothing_through_a_real_doxygen_run
None test_doxyfile_injection.test_a_file_inlining_comment_leaks_nothing_through_a_real_doxygen_run(Path tmp_path)
End to end: a hostile source comment inlines nothing into the index.
tests/test_doxyfile_injection.py:346
Checks
assert "SENTINEL-INLINE-LEAK" not in served, (f"a repo file was inlined into a served description: {rows}" )assert any(name == "ordinary" for name, *_ in rows)assert "Ordinary brief" in served
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CLI-002 | inferred | test_a_file_inlining_comment_leaks_nothing_through_a_real_doxygen_run → run_doxygen → doxygen_supports_sqlite3 |
| REQ-DDB-INDEX-001 | measured | test_a_file_inlining_comment_leaks_nothing_through_a_real_doxygen_run → run_doxygen |
| REQ-DDB-PIPE-003 | measured | test_a_file_inlining_comment_leaks_nothing_through_a_real_doxygen_run → run_doxygen → _build_doxyfile_content → extra_input_clears_exclude |
| REQ-DDB-PIPE-006 | measured | test_a_file_inlining_comment_leaks_nothing_through_a_real_doxygen_run → run_doxygen → clean_subprocess_env |
| REQ-DDB-PIPE-014 | catalog |
Source · 47 lines
def test_a_file_inlining_comment_leaks_nothing_through_a_real_doxygen_run(tmp_path: Path) -> None:
"""THE TEXT TEST ABOVE ASSERTS THE DOXYFILE; THIS ONE ASSERTS DOXYGEN. The forced block is
only a defence if doxygen honours it the way it was measured to, and a version bump could
change that silently — the Doxyfile would still contain the lines and the leak would be
back. Both arms of the original PoC run here: a relative argument (which resolves against
cwd, the repo root) and an absolute one (which ignores EXAMPLE_PATH entirely).
@brief No sentinel from an inlined file reaches any description column.
@return None.
@version 1
"""
repo = tmp_path / "repo"
repo.mkdir()
(repo / "secret.txt").write_text("SENTINEL-INLINE-LEAK\n", encoding="utf-8")
(repo / "a.c").write_text(
"/**\n"
" * @brief Ordinary brief.\n"
" * @details Relative:\n"
" * @verbinclude secret.txt\n"
" * @details Absolute:\n"
" * @verbinclude /etc/hostname\n"
" */\n"
"int ordinary(void) { return 0; }\n",
encoding="utf-8",
)
doxyfile = repo / "Doxyfile"
doxyfile.write_text("PROJECT_NAME = probe\nINPUT = .\nQUIET = YES\n", encoding="utf-8")
db = run_doxygen(doxyfile, repo, output_dir=tmp_path / "out")
conn = sqlite3.connect(str(db))
rows = conn.execute(
"SELECT name, briefdescription, detaileddescription, inbodydescription FROM memberdef"
).fetchall()
conn.close()
served = " ".join(str(cell) for row in rows for cell in row)
assert "SENTINEL-INLINE-LEAK" not in served, (
f"a repo file was inlined into a served description: {rows}"
)
## THE OTHER HALF OF THE ARGUMENT: the function is still indexed and still documented, so
## the refusal costs the ordinary case nothing.
assert any(name == "ordinary" for name, *_ in rows)
assert "Ordinary brief" in served
@pytest.mark.skipif(shutil.which("doxygen") is None, reason="asserted against a real doxygen run")test_a_newline_in_a_path_cannot_inject_a_directive
None test_doxyfile_injection.test_a_newline_in_a_path_cannot_inject_a_directive(Path tmp_path)
A newline in a file name must not become a Doxyfile directive.
tests/test_doxyfile_injection.py:76
Checks
assert not value, (f"a file name injected a non-empty {directive} = {value!r}; doxygen runs that " f"as a command over every input file, so indexing a hostile repo executes it" )assert value.upper() in ("", "NO"), (f"a file name set FILTER_SOURCE_FILES = {value!r}, re-enabling filtering" )assert "/bin/sh" not in text, "the injected command survived into the rendered configuration"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-001 | measured | test_a_newline_in_a_path_cannot_inject_a_directive → _build_doxyfile_content |
| REQ-DDB-PIPE-003 | measured | test_a_newline_in_a_path_cannot_inject_a_directive → _build_doxyfile_content → extra_input_clears_exclude |
| REQ-DDB-PIPE-014 | catalog |
Source · 27 lines
def test_a_newline_in_a_path_cannot_inject_a_directive(tmp_path: Path) -> None:
"""@brief Path injection is neutralised.
@return None.
@version 1
"""
doxyfile = tmp_path / "Doxyfile"
doxyfile.write_text("PROJECT_NAME = probe\nINPUT = .\n", encoding="utf-8")
hostile = "/repo/a.c\nINPUT_FILTER = /bin/sh -c 'pwn' #"
text = _build_doxyfile_content(doxyfile, [hostile], None, True, tmp_path, "")
## The two COMMAND-valued directives must end up empty. `FILTER_SOURCE_FILES` is a
## boolean and its safe value is NO, not empty — asserting "empty" there was a bug in the
## first version of this test.
for directive in ("INPUT_FILTER", "FILTER_PATTERNS"):
for value in _assignments(text, directive):
assert not value, (
f"a file name injected a non-empty {directive} = {value!r}; doxygen runs that "
f"as a command over every input file, so indexing a hostile repo executes it"
)
for value in _assignments(text, "FILTER_SOURCE_FILES"):
assert value.upper() in ("", "NO"), (
f"a file name set FILTER_SOURCE_FILES = {value!r}, re-enabling filtering"
)
assert "/bin/sh" not in text, "the injected command survived into the rendered configuration"
Activity · 10 steps
test_a_repos_own_doxyfile_cannot_declare_a_filter_command
None test_doxyfile_injection.test_a_repos_own_doxyfile_cannot_declare_a_filter_command(Path tmp_path)
A target's own Doxyfile must not be able to declare a filter command.
tests/test_doxyfile_injection.py:108
Checks
assert values, f"{directive} is never assigned, so the repo's declaration stands"assert not values[-1], (f"the LAST {directive} assignment is {values[-1]!r}; doxygen takes the last one, " f"so the repo's filter command still runs" )assert sources and sources[-1].upper() == "NO", (f"FILTER_SOURCE_FILES ends as {sources[-1:]!r}, so a filter still runs over sources" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-001 | measured | test_a_repos_own_doxyfile_cannot_declare_a_filter_command → _build_doxyfile_content |
| REQ-DDB-PIPE-003 | measured | test_a_repos_own_doxyfile_cannot_declare_a_filter_command → _build_doxyfile_content → extra_input_clears_exclude |
| REQ-DDB-PIPE-014 | catalog |
Source · 32 lines
def test_a_repos_own_doxyfile_cannot_declare_a_filter_command(tmp_path: Path) -> None:
"""NEEDS NO INJECTION. The repo just writes the directive into the Doxyfile it ships,
which clew reads and honours. Only forcing the value off closes it.
@brief A declared filter is overridden.
@return None.
@version 1
"""
doxyfile = tmp_path / "Doxyfile"
doxyfile.write_text(
"PROJECT_NAME = probe\nINPUT = .\n"
"INPUT_FILTER = /bin/sh -c 'pwn'\n"
"FILTER_PATTERNS = *.c=/bin/sh\n"
"FILTER_SOURCE_FILES = YES\n",
encoding="utf-8",
)
text = _build_doxyfile_content(doxyfile, None, None, False, tmp_path, "")
for directive in ("INPUT_FILTER", "FILTER_PATTERNS"):
values = _assignments(text, directive)
assert values, f"{directive} is never assigned, so the repo's declaration stands"
assert not values[-1], (
f"the LAST {directive} assignment is {values[-1]!r}; doxygen takes the last one, "
f"so the repo's filter command still runs"
)
sources = _assignments(text, "FILTER_SOURCE_FILES")
assert sources and sources[-1].upper() == "NO", (
f"FILTER_SOURCE_FILES ends as {sources[-1:]!r}, so a filter still runs over sources"
)
Activity · 9 steps
test_a_symlink_does_not_walk_doxygen_out_of_the_repository
None test_doxyfile_injection.test_a_symlink_does_not_walk_doxygen_out_of_the_repository(Path tmp_path)
A committed symlink does not carry an outside file into the index.
tests/test_doxyfile_injection.py:398
Checks
assert "hidden_fn" not in names, f"a symlink carried an outside file into the index: {paths}"assert not any("outside" in path for path in paths), f"an outside path was indexed: {paths}"assert "own_fn" in names, "the repository's own source must still be indexed"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CLI-002 | inferred | test_a_symlink_does_not_walk_doxygen_out_of_the_repository → run_doxygen → doxygen_supports_sqlite3 |
| REQ-DDB-INDEX-001 | measured | test_a_symlink_does_not_walk_doxygen_out_of_the_repository → run_doxygen |
| REQ-DDB-PIPE-003 | measured | test_a_symlink_does_not_walk_doxygen_out_of_the_repository → run_doxygen → _build_doxyfile_content → extra_input_clears_exclude |
| REQ-DDB-PIPE-006 | measured | test_a_symlink_does_not_walk_doxygen_out_of_the_repository → run_doxygen → clean_subprocess_env |
| REQ-DDB-PIPE-014 | catalog |
Source · 39 lines
def test_a_symlink_does_not_walk_doxygen_out_of_the_repository(tmp_path: Path) -> None:
"""M1, 1.1.0 SECURITY AUDIT. doxygen follows a symlinked FILE and a symlinked DIRECTORY
out of its INPUT roots and records the resolved outside path, so a committed
`src/linked.c -> ../../elsewhere/secret.c` had its functions, docs and BODIES indexed and
served. clew's own tree walk has always passed `followlinks=False`, so forcing
EXCLUDE_SYMLINKS makes the two enumerations agree as well as closing the leak — a
disagreement between them is how the index cache hashes one file set while doxygen reads
another.
@brief No symlinked path reaches the doxygen database.
@return None.
@version 1
"""
outside = tmp_path / "outside"
outside.mkdir()
(outside / "hidden.c").write_text(
"/** @brief Hidden. */\nint hidden_fn(void) { return 0; }\n", encoding="utf-8"
)
repo = tmp_path / "repo"
(repo / "src").mkdir(parents=True)
(repo / "src" / "own.c").write_text(
"/** @brief Own. */\nint own_fn(void) { return 0; }\n", encoding="utf-8"
)
(repo / "src" / "linked.c").symlink_to(outside / "hidden.c")
(repo / "linkdir").symlink_to(outside)
doxyfile = repo / "Doxyfile"
doxyfile.write_text("PROJECT_NAME = probe\nINPUT = .\nQUIET = YES\n", encoding="utf-8")
db = run_doxygen(doxyfile, repo, output_dir=tmp_path / "out")
conn = sqlite3.connect(str(db))
names = [row[0] for row in conn.execute("SELECT name FROM memberdef")]
paths = [row[0] for row in conn.execute("SELECT name FROM path")]
conn.close()
assert "hidden_fn" not in names, f"a symlink carried an outside file into the index: {paths}"
assert not any("outside" in path for path in paths), f"an outside path was indexed: {paths}"
assert "own_fn" in names, "the repository's own source must still be indexed"1 more lines, not shown
Activity · 18 steps
test_declared_predefined_cannot_inject_a_directive
None test_doxyfile_injection.test_declared_predefined_cannot_inject_a_directive(Path tmp_path)
A declared predefined value cannot inject a directive.
tests/test_doxyfile_injection.py:231
Checks
assert not value, (f"a declared predefined macro injected INPUT_FILTER = {value!r}, which doxygen " f"runs as a command over every input file" )assert "/bin/sh" not in text, "the injected command survived into the configuration"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-001 | measured | test_declared_predefined_cannot_inject_a_directive → _build_doxyfile_content |
| REQ-DDB-PIPE-003 | measured | test_declared_predefined_cannot_inject_a_directive → _build_doxyfile_content → extra_input_clears_exclude |
| REQ-DDB-PIPE-014 | catalog |
Source · 26 lines
def test_declared_predefined_cannot_inject_a_directive(tmp_path: Path) -> None:
"""THE THIRD CHANNEL, and the one the previous fix missed entirely. `predefined` is
concatenated AFTER the forced block, so anything it smuggles wins — and it comes from the
TARGET REPO's own `.clew.yaml`, parsed by `_declared_macros` with only `str(v).strip()`,
so an interior newline survives and a trailing `#` defeats the quoting.
Both existing tests in this module pass `""` here, which is why this was invisible.
@brief A predefined macro cannot become a directive.
@return None.
@version 1
"""
doxyfile = tmp_path / "Doxyfile"
doxyfile.write_text("PROJECT_NAME = probe\nINPUT = .\n", encoding="utf-8")
hostile = "PREDEFINED += X=1\nINPUT_FILTER = /bin/sh -c 'pwn' #\n"
text = _build_doxyfile_content(doxyfile, None, None, False, tmp_path, hostile)
for value in _assignments(text, "INPUT_FILTER"):
assert not value, (
f"a declared predefined macro injected INPUT_FILTER = {value!r}, which doxygen "
f"runs as a command over every input file"
)
assert "/bin/sh" not in text, "the injected command survived into the configuration"
Activity · 7 steps
test_no_command_valued_option_survives_a_hostile_doxyfile
None test_doxyfile_injection.test_no_command_valued_option_survives_a_hostile_doxyfile(Path tmp_path)
A hostile Doxyfile cannot leave any command-valued option set.
tests/test_doxyfile_injection.py:185
Checks
assert values, f"{key} is never assigned, so the repo's value stands"assert not values[-1], (f"the LAST {key} assignment is {values[-1]!r}; doxygen takes the last one, so the " f"repo's command still runs" )assert values and values[-1].upper() == "NO", (f"{key} ends as {values[-1:]!r}; clew reads only the sqlite3 database, so every " f"other generator is surface it does not need" )assert _assignments(text, "HTML_EXTRA_FILES")[-1] == "", ("HTML_EXTRA_FILES still copies a repo-chosen file into the output tree with its exec " "bit preserved, which is what supplies an attacker a runnable path" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-001 | measured | test_no_command_valued_option_survives_a_hostile_doxyfile → _build_doxyfile_content |
| REQ-DDB-PIPE-003 | measured | test_no_command_valued_option_survives_a_hostile_doxyfile → _build_doxyfile_content → extra_input_clears_exclude |
| REQ-DDB-PIPE-014 | catalog |
Source · 41 lines
def test_no_command_valued_option_survives_a_hostile_doxyfile(tmp_path: Path) -> None:
"""TABLE-DRIVEN ON PURPOSE. The previous fix forced INPUT_FILTER, FILTER_PATTERNS and
FILTER_SOURCE_FILES and its docstring declared the path closed. `QHG_LOCATION` then
executed anyway — doxygen runs qhelpgenerator, and `HTML_EXTRA_FILES` copies a repo file
into the output tree with its exec bit intact to supply the path. Verified against 1.9.8.
Driving the assertion off an enumerated list means the only way to satisfy it is to
actually force the option, so a newly-discovered option cannot be quietly omitted.
@brief Every exec-valued option ends empty.
@return None.
@version 1
"""
doxyfile = tmp_path / "Doxyfile"
doxyfile.write_text(
"PROJECT_NAME = probe\nINPUT = .\n"
+ "".join(f"{k} = /bin/sh\n" for k in _EXEC_VALUED)
+ "GENERATE_QHP = YES\nGENERATE_HTML = YES\nHTML_EXTRA_FILES = ./pwn.sh\n",
encoding="utf-8",
)
text = _build_doxyfile_content(doxyfile, None, None, False, tmp_path, "")
for key in _EXEC_VALUED:
values = _assignments(text, key)
assert values, f"{key} is never assigned, so the repo's value stands"
assert not values[-1], (
f"the LAST {key} assignment is {values[-1]!r}; doxygen takes the last one, so the "
f"repo's command still runs"
)
for key in _UNUSED_GENERATORS:
values = _assignments(text, key)
assert values and values[-1].upper() == "NO", (
f"{key} ends as {values[-1:]!r}; clew reads only the sqlite3 database, so every "
f"other generator is surface it does not need"
)
assert _assignments(text, "HTML_EXTRA_FILES")[-1] == "", (
"HTML_EXTRA_FILES still copies a repo-chosen file into the output tree with its exec "
"bit preserved, which is what supplies an attacker a runnable path"
)
Activity · 11 steps
test_no_file_reading_command_survives_a_hostile_source_comment
None test_doxyfile_injection.test_no_file_reading_command_survives_a_hostile_source_comment(Path tmp_path)
Every file-reading doxygen command is neutered by a forced alias.
tests/test_doxyfile_injection.py:291
Checks
assert ours, f"@{command} reads a repo-chosen file and is not neutered"assert "ATTACKER-WINS" not in ours[-1], (f"the LAST ALIASES assignment for @{command} is the repository's " f"({ours[-1]!r}); doxygen takes the last one, so the command still reads files" )assert values and not values[-1], (f"{key} ends as {values[-1:]!r}, so the repository still chooses where " f"doxygen looks for files to inline" )assert _assignments(text, "EXAMPLE_RECURSIVE")[-1].upper() == "NO", ("EXAMPLE_RECURSIVE is a boolean, and an empty value would read as doxygen's own " "default rather than as a refusal" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-001 | measured | test_no_file_reading_command_survives_a_hostile_source_comment → _build_doxyfile_content |
| REQ-DDB-PIPE-003 | measured | test_no_file_reading_command_survives_a_hostile_source_comment → _build_doxyfile_content → extra_input_clears_exclude |
| REQ-DDB-PIPE-014 | catalog |
Source · 50 lines
def test_no_file_reading_command_survives_a_hostile_source_comment(tmp_path: Path) -> None:
"""H1, 1.1.0 SECURITY AUDIT. A doc comment reading `@verbinclude /etc/hostname` made
doxygen inline that file into the served description — an arbitrary read whose result is
presented to a model as this repository's documentation, which is the provenance an agent
has no way to doubt. `EXAMPLE_PATH` is NOT the fix (measured: empty still resolves against
cwd, and an absolute argument ignores it entirely) and neither is stripping `<verbatim>`
from the served text — doxygen emits the identical element for an owner's legitimate
`@verbatim` block, so a content filter cannot tell a leak from documentation.
TABLE-DRIVEN for the reason the exec-valued test above is: the only way to satisfy it is
to actually force each command, so the next one cannot be quietly omitted.
@brief Each file-reading command's last ALIASES assignment is clew's.
@return None.
@version 1
"""
doxyfile = tmp_path / "Doxyfile"
doxyfile.write_text(
"PROJECT_NAME = probe\nINPUT = .\nEXAMPLE_PATH = /\nEXAMPLE_RECURSIVE = YES\n"
## The target taking the command BACK is the half a naive fix misses.
+ "".join(f'ALIASES += "{c}=ATTACKER-WINS "\n' for c in _FILE_READING_COMMANDS),
encoding="utf-8",
)
text = _build_doxyfile_content(doxyfile, None, None, False, tmp_path, "")
aliases = _assignments(text, "ALIASES")
for command in _FILE_READING_COMMANDS:
ours = [a for a in aliases if a.strip('"').startswith(f"{command}=")]
assert ours, f"@{command} reads a repo-chosen file and is not neutered"
assert "ATTACKER-WINS" not in ours[-1], (
f"the LAST ALIASES assignment for @{command} is the repository's "
f"({ours[-1]!r}); doxygen takes the last one, so the command still reads files"
)
## AND THE OPTIONS that steer where those commands look, restated for the same reason
## every other read-external option in the forced block is.
for key in ("EXAMPLE_PATH", "INCLUDE_PATH", "IMAGE_PATH"):
values = _assignments(text, key)
assert values and not values[-1], (
f"{key} ends as {values[-1:]!r}, so the repository still chooses where "
f"doxygen looks for files to inline"
)
assert _assignments(text, "EXAMPLE_RECURSIVE")[-1].upper() == "NO", (
"EXAMPLE_RECURSIVE is a boolean, and an empty value would read as doxygen's own "
"default rather than as a refusal"
)
@pytest.mark.skipif(shutil.which("doxygen") is None, reason="asserted against a real doxygen run")