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

Show on the map →

Called by none

Calls run_doxygen

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

requirementlinkedpath to the implementer
REQ-DDB-CLI-002inferredtest_a_file_inlining_comment_leaks_nothing_through_a_real_doxygen_run → run_doxygen → doxygen_supports_sqlite3
REQ-DDB-INDEX-001measuredtest_a_file_inlining_comment_leaks_nothing_through_a_real_doxygen_run → run_doxygen
REQ-DDB-PIPE-003measuredtest_a_file_inlining_comment_leaks_nothing_through_a_real_doxygen_run → run_doxygen → _build_doxyfile_content → extra_input_clears_exclude
REQ-DDB-PIPE-006measuredtest_a_file_inlining_comment_leaks_nothing_through_a_real_doxygen_run → run_doxygen → clean_subprocess_env
REQ-DDB-PIPE-014catalog
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

Show on the map →

Called by none

Calls _assignments, _build_doxyfile_content

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

requirementlinkedpath to the implementer
REQ-DDB-INDEX-001measuredtest_a_newline_in_a_path_cannot_inject_a_directive → _build_doxyfile_content
REQ-DDB-PIPE-003measuredtest_a_newline_in_a_path_cannot_inject_a_directive → _build_doxyfile_content → extra_input_clears_exclude
REQ-DDB-PIPE-014catalog
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
lines 81–81doxyfile = tmp_path / "Doxyfile"lines 82–82doxyfile.write_text("PROJECT_NAME = probe\nINPUT = .\n", encoding="utf-8")lines 83–83hostile = "/repo/a.c\nINPUT_FILTER = /bin/sh -c 'pwn' #"lines 85–85text = _build_doxyfile_content(doxyfile, [hostile], None, True, tmp_path, "")lines 90–90◇ for directive in ("INPUT_FILTER", "FILTER_PATTERNS")lines 91–91◇ for value in _assignments(text, directive)lines 92–95assert 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" )lines 96–96◇ for value in _assignments(text, "FILTER_SOURCE_FILES")lines 97–99assert value.upper() in ("", "NO"), ( f"a file name set FILTER_SOURCE_FILES = {value!r}, re-enabling filtering" )lines 100–100assert "/bin/sh" not in text, "the injected command survived into the rendered configuration"

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

Show on the map →

Called by none

Calls _assignments, _build_doxyfile_content

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

requirementlinkedpath to the implementer
REQ-DDB-INDEX-001measuredtest_a_repos_own_doxyfile_cannot_declare_a_filter_command → _build_doxyfile_content
REQ-DDB-PIPE-003measuredtest_a_repos_own_doxyfile_cannot_declare_a_filter_command → _build_doxyfile_content → extra_input_clears_exclude
REQ-DDB-PIPE-014catalog
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
lines 116–116doxyfile = tmp_path / "Doxyfile"lines 117–123doxyfile.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", )lines 125–125text = _build_doxyfile_content(doxyfile, None, None, False, tmp_path, "")lines 127–127◇ for directive in ("INPUT_FILTER", "FILTER_PATTERNS")lines 128–128values = _assignments(text, directive)lines 129–129assert values, f"{directive} is never assigned, so the repo's declaration stands"lines 130–133assert 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" )lines 134–134sources = _assignments(text, "FILTER_SOURCE_FILES")lines 135–137assert sources and sources[-1].upper() == "NO", ( f"FILTER_SOURCE_FILES ends as {sources[-1:]!r}, so a filter still runs over sources" )

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

Show on the map →

Called by none

Calls _assignments, _build_doxyfile_content

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

requirementlinkedpath to the implementer
REQ-DDB-INDEX-001measuredtest_declared_predefined_cannot_inject_a_directive → _build_doxyfile_content
REQ-DDB-PIPE-003measuredtest_declared_predefined_cannot_inject_a_directive → _build_doxyfile_content → extra_input_clears_exclude
REQ-DDB-PIPE-014catalog
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
lines 243–243doxyfile = tmp_path / "Doxyfile"lines 244–244doxyfile.write_text("PROJECT_NAME = probe\nINPUT = .\n", encoding="utf-8")lines 245–245hostile = "PREDEFINED += X=1\nINPUT_FILTER = /bin/sh -c 'pwn' #\n"lines 247–247text = _build_doxyfile_content(doxyfile, None, None, False, tmp_path, hostile)lines 249–249◇ for value in _assignments(text, "INPUT_FILTER")lines 250–253assert not value, ( f"a declared predefined macro injected INPUT_FILTER = {value!r}, which doxygen " f"runs as a command over every input file" )lines 254–254assert "/bin/sh" not in text, "the injected command survived into the configuration"

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

Show on the map →

Called by none

Calls _assignments, _build_doxyfile_content

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

requirementlinkedpath to the implementer
REQ-DDB-INDEX-001measuredtest_no_command_valued_option_survives_a_hostile_doxyfile → _build_doxyfile_content
REQ-DDB-PIPE-003measuredtest_no_command_valued_option_survives_a_hostile_doxyfile → _build_doxyfile_content → extra_input_clears_exclude
REQ-DDB-PIPE-014catalog
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
lines 198–198doxyfile = tmp_path / "Doxyfile"lines 199–204doxyfile.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", )lines 205–205text = _build_doxyfile_content(doxyfile, None, None, False, tmp_path, "")lines 207–207◇ for key in _EXEC_VALUEDlines 208–208values = _assignments(text, key)lines 209–209assert values, f"{key} is never assigned, so the repo's value stands"lines 210–213assert 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" )lines 214–214◇ for key in _UNUSED_GENERATORSlines 215–215values = _assignments(text, key)lines 216–219assert 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" )lines 220–223assert _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" )

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

Show on the map →

Called by none

Calls _assignments, _build_doxyfile_content

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

requirementlinkedpath to the implementer
REQ-DDB-INDEX-001measuredtest_no_file_reading_command_survives_a_hostile_source_comment → _build_doxyfile_content
REQ-DDB-PIPE-003measuredtest_no_file_reading_command_survives_a_hostile_source_comment → _build_doxyfile_content → extra_input_clears_exclude
REQ-DDB-PIPE-014catalog
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")