Skip to content

Commit 787d1c9

Browse files
authored
🔧 chore(test): add opt-in Atheris fuzz harness (#3246)
OpenSSF Scorecard's Fuzzing check scores 0 for this repo. Its detection only checks for `import atheris` (or a ClusterFuzzLite/OSS-Fuzz setup) anywhere in the tree, so this adds a real target rather than a token import that would move the number without doing anything. `NushellActivator.quote` and `PowerShellActivator.quote` are hand-rolled escaping schemes over arbitrary strings pulled from paths and prompts: Nushell counts the longest run of `#` and wraps in one more than that, PowerShell doubles single quotes. Both take untrusted-shaped input, and a broken escape is a correctness bug worth catching, not just a lint target. Each harness in `tasks/fuzz_*.py` recomputes the expected output from the escaping rule on its own and asserts equality against the real function, so it fails on any scheme violation, not only on a crash. I ran both assertions against 200k+ randomized adversarial strings (embedded quotes, `#` runs, control characters, Unicode) against the real functions before wiring them into Atheris. Atheris only ships prebuilt wheels for Linux + CPython 3.12-3.14 - it doesn't build on this machine at all, since Apple Clang doesn't ship libFuzzer. It lives in its own `fuzz` dependency group with a marker matching that exact combination, so nothing else ever tries to install it or falls into a doomed source build. `tox -e fuzz` is opt-in and outside `env_list`, so no existing environment or CI job changes.
1 parent a30f995 commit 787d1c9

5 files changed

Lines changed: 80 additions & 2 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add an opt-in ``tox -e fuzz`` environment that runs Atheris fuzz targets against ``NushellActivator.quote`` and
2+
``PowerShellActivator.quote``, checking each output against the escaping rule it implements. Isolated in its own
3+
dependency group; no other environment installs Atheris, and it is not part of the default test matrix.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Fuzz NushellActivator.quote for crashes and correct raw-string escaping.
2+
3+
Run directly: python tasks/fuzz_nushell_quote.py -runs=100000
4+
5+
"""
6+
7+
from __future__ import annotations
8+
9+
import sys
10+
11+
import atheris
12+
from fuzz_shared import consume_text
13+
14+
from virtualenv.activation.nushell import NushellActivator
15+
16+
17+
def test_one_input(data: bytes) -> None:
18+
string = consume_text(data)
19+
quoted = NushellActivator.quote(string)
20+
wrapping = "#" * (_longest_run(string, "#") + 1)
21+
expected = f"r{wrapping}'{string}'{wrapping}"
22+
assert quoted == expected, (quoted, expected) # ruff:ignore[assert] atheris treats AssertionError as the crash signal
23+
24+
25+
def _longest_run(string: str, char: str) -> int:
26+
current = longest = 0
27+
for c in string:
28+
current = current + 1 if c == char else 0
29+
longest = max(longest, current)
30+
return longest
31+
32+
33+
if __name__ == "__main__":
34+
atheris.Setup(sys.argv, test_one_input)
35+
atheris.Fuzz()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Fuzz PowerShellActivator.quote for crashes and correct single-quote escaping.
2+
3+
Run directly: python tasks/fuzz_powershell_quote.py -runs=100000
4+
5+
"""
6+
7+
from __future__ import annotations
8+
9+
import sys
10+
11+
import atheris
12+
from fuzz_shared import consume_text
13+
14+
from virtualenv.activation.powershell import PowerShellActivator
15+
16+
17+
def test_one_input(data: bytes) -> None:
18+
string = consume_text(data)
19+
quoted = PowerShellActivator.quote(string)
20+
expected = f"'{string.replace(chr(39), chr(39) * 2)}'"
21+
assert quoted == expected, (quoted, expected) # ruff:ignore[assert] atheris treats AssertionError as the crash signal
22+
23+
24+
if __name__ == "__main__":
25+
atheris.Setup(sys.argv, test_one_input)
26+
atheris.Fuzz()

‎tasks/fuzz_shared.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Shared helpers for the fuzz targets in this directory."""
2+
3+
from __future__ import annotations
4+
5+
import atheris
6+
7+
8+
def consume_text(data: bytes) -> str:
9+
provider = atheris.FuzzedDataProvider(data)
10+
return provider.ConsumeUnicodeNoSurrogates(provider.remaining_bytes())

‎tox.toml‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,13 @@ commands = [
153153
]
154154

155155
[env.fuzz]
156-
description = "run the atheris fuzz target for pyvenv.cfg (Linux + CPython 3.12-3.14 only)"
156+
description = "run the atheris fuzz targets (Linux + CPython 3.12-3.14 only)"
157157
dependency_groups = [ "fuzz" ]
158-
commands = [ [ "python", "{tox_root}{/}tasks{/}fuzz_pyenv_cfg.py", "-runs=20000" ] ]
158+
commands = [
159+
[ "python", "{tox_root}{/}tasks{/}fuzz_pyenv_cfg.py", "-runs=20000" ],
160+
[ "python", "{tox_root}{/}tasks{/}fuzz_nushell_quote.py", "-runs=20000" ],
161+
[ "python", "{tox_root}{/}tasks{/}fuzz_powershell_quote.py", "-runs=20000" ],
162+
]
159163

160164
[env.property]
161165
description = "run the hypothesis property tests"

0 commit comments

Comments
 (0)