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
Ask AMSI whether the installer compiles, which is the mechanism #10805 actually reports (#10994)
Daniel Han89862daecd74
diff --git a/.github/scripts/Probe-AmsiParse.ps1 b/.github/scripts/Probe-AmsiParse.ps1new file mode 100644index 00000000000..e4ca66d0d77--- /dev/null+++ b/.github/scripts/Probe-AmsiParse.ps1@@ -0,0 +1,186 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.++<#+.SYNOPSIS+ Ask AMSI whether it will let a script compile, without running the script.++.DESCRIPTION+ This is the probe that actually exercises the failure in #10805.++ PowerShell hands a script block to AMSI at COMPILE time, before the first statement runs. So the+ thing to measure is compilation, not execution, and `[scriptblock]::Create` on the file's text+ reproduces the exact failing path with none of the side effects of an install: no download, no+ venv, no registry, no shortcut. A block surfaces as a ParseException whose+ FullyQualifiedErrorId is `ScriptContainedMaliciousContent` (or `ScriptHasAdminBlockedContent`+ where a policy rather than a signature is responsible).++ Matching is on the error ID and never on message text, which is localised. install.rs already+ makes that distinction and this follows it.++ `[scriptblock]::Create` is banned in the shipped installers by+ tests/studio/test_installer_av_shapes.py::test_no_remote_script_is_executed_in_process. That test+ parametrises over the shipped scripts only, so this probe is unaffected -- and it is deliberate:+ submitting text to the compiler is the whole measurement, and there is no other way to ask AMSI+ the question. Do not "fix" this file to satisfy that test.++.PARAMETER Path+ One script to submit.++ Deliberately one, not a list. In `-File` mode -- which is how Windows PowerShell 5.1 has to be+ invoked, and 5.1 is the interpreter that matters here -- the argument binder does not split a+ comma-separated value into an array: it arrives as a single string and the probe reports "file+ not found" for a path that exists. Observed while smoke-running this. One file per invocation+ also means `-Control` can fire in the SAME process as the measurement it vouches for, rather+ than in a sibling process that merely shares a host configuration.++.PARAMETER Control+ Submit Microsoft's documented AMSI test sample instead of a file.++ Non-negotiable, and the reason this script exists rather than three lines of inline YAML: a+ runner where AMSI is not live, or where Defender is in passive mode because another product is+ primary, produces "no block" for every input. That is identical to a clean verdict and has+ shipped as one before. If the control does not fire, this probe reports that it could not+ measure, which must never be read as clean.++.PARAMETER OutFile+ Where to write the JSON result.+#>++[CmdletBinding()]+param(+ [string]$Path,+ [switch]$Control,+ [Parameter(Mandatory = $true)][string]$OutFile+)++$ErrorActionPreference = 'Continue'++# The error IDs that mean "the scanner refused this", separated because they have different causes+# and different fixes. A signature match is something we can get cleared; an administrator policy+# block is not.+$blockedIds = @{+ 'ScriptContainedMaliciousContent' = 'an AMSI provider matched the content'+ 'ScriptHasAdminBlockedContent' = 'an administrative policy blocked the content'+}++function Submit-ToCompiler {+ param([string]$Label, [string]$Text)++ $result = [ordered]@{+ label = $Label+ length = $Text.Length+ blocked = $false+ errorId = ''+ errorType = ''+ message = ''+ reason = ''+ }+ try {+ # Compilation only. Nothing is invoked, so submitting the real installer here cannot install+ # anything, and that is what makes running this on every candidate cheap and safe.+ $null = [scriptblock]::Create($Text)+ } catch {+ $result.errorType = $_.Exception.GetType().FullName+ # The OUTER record is useless for this decision. Calling a .NET static method from+ # PowerShell wraps whatever it threw in a MethodInvocationException whose+ # FullyQualifiedErrorId is the generic 'ParseException', while the id that says WHY -- and+ # so whether a scanner refused this or the script simply does not parse -- lives on the+ # inner ParseException's Errors collection. Verified by execution: a deliberate syntax error+ # gives outer 'ParseException' and inner ErrorId 'IfStatementMissingCondition'. Matching the+ # outer id alone meant ScriptContainedMaliciousContent was never seen, so not even the+ # positive control could fire and the lane could only ever report that it had not measured.+ $ids = New-Object System.Collections.Generic.List[string]+ $ids.Add([string]$_.FullyQualifiedErrorId)+ $inner = $_.Exception+ while ($inner) {+ if ($inner.PSObject.Properties.Name -contains 'Errors' -and $inner.Errors) {+ foreach ($e in $inner.Errors) {+ if ($e.ErrorId) { $ids.Add([string]$e.ErrorId) }+ }+ }+ $inner = $inner.InnerException+ }+ # Joined, so the recorded id still reads usefully in the summary and a match below can key+ # on any level of the chain.+ $result.errorId = ($ids | Where-Object { $_ } | Select-Object -Unique) -join '/'+ # Truncated: a provider can echo a long span of the submitted script back, and the whole+ # point of this work is to avoid writing suspicious-looking text into files.+ $msg = [string]$_.Exception.Message+ if ($msg.Length -gt 400) { $msg = $msg.Substring(0, 400) + ' ...' }+ $result.message = $msg+ foreach ($id in $blockedIds.Keys) {+ if ($result.errorId -like "*$id*") {+ $result.blocked = $true+ $result.reason = $blockedIds[$id]+ break+ }+ }+ if (-not $result.blocked) {+ # A syntax error is not a scanner verdict, and conflating the two would let a broken+ # candidate masquerade as a detection (or the reverse, which is worse).+ $result.reason = 'the script did not compile, for a reason unrelated to a scanner'+ }+ }+ return $result+}++$results = New-Object System.Collections.ArrayList++if ($Control) {+ # Assembled from fragments so this repository is not itself a sample carrying the signature.+ # Microsoft's own documentation splits it the same way. Reassembled only in memory, never+ # written to disk.+ $sample = 'AMSI Test Sample: ' + '7e72c3ce-' + '861b-4339-' + '8740-' + '0ac1484c1386'+ [void]$results.Add((Submit-ToCompiler -Label 'control (AMSI test sample)' -Text $sample))+}++foreach ($file in @($Path) | Where-Object { $_ }) {+ if (-not (Test-Path -LiteralPath $file)) {+ [void]$results.Add([ordered]@{+ label = $file; blocked = $false; errorId = ''; reason = 'file not found'; missing = $true+ })+ continue+ }+ $text = $null+ try {+ # Decoded as UTF-8 explicitly, never by Get-Content's default. Under Windows PowerShell+ # 5.1 -- which is the host this probe exists to reproduce -- Get-Content assumes the system+ # ANSI code page for a file with no BOM, and the shipped installers are BOM-less UTF-8 with+ # non-ASCII text in them. That turns the bytes into mojibake before AMSI ever sees them, so+ # the provider would be judging a string no user ever runs. It also does not match the+ # documented `irm ... | iex` path, where the response is decoded as Unicode.+ $text = [System.IO.File]::ReadAllText($file, [System.Text.UTF8Encoding]::new($false))+ } catch {+ [void]$results.Add([ordered]@{+ label = $file; blocked = $false; reason = "could not read: $($_.Exception.Message)"+ unreadable = $true+ })+ continue+ }+ [void]$results.Add((Submit-ToCompiler -Label $file -Text $text))+}++$payload = [ordered]@{+ host = $Host.Name+ psVersion = [string]$PSVersionTable.PSVersion+ psEdition = [string]$PSVersionTable.PSEdition+ languageMode = [string]$ExecutionContext.SessionState.LanguageMode+ results = @($results)+}++New-Item -ItemType Directory -Force -Path (Split-Path -Parent $OutFile) | Out-Null+$payload | ConvertTo-Json -Depth 6 |+ Set-Content -LiteralPath $OutFile -Encoding utf8++foreach ($r in $results) {+ $verdict = if ($r.blocked) { 'BLOCKED' } elseif ($r.errorId) { 'error' } else { 'compiled' }+ Write-Host (" {0,-9} {1}{2}" -f $verdict, $r.label,+ $(if ($r.reason) { " [$($r.reason)]" } else { '' }))+ if ($r.errorId) { Write-Host " id: $($r.errorId)" }+}++# Always zero. Whether a block is a failure is a decision about controls and about which side of a+# differential it appeared on, and the caller holds both. A probe that exits non-zero on a detection+# cannot be used to establish a baseline.+exit 0diff --git a/.github/workflows/windows-amsi-defender-differential-ci.yml b/.github/workflows/windows-amsi-defender-differential-ci.ymlnew file mode 100644index 00000000000..7d3b8fe10f6--- /dev/null+++ b/.github/workflows/windows-amsi-defender-differential-ci.yml@@ -0,0 +1,761 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.++# Will AMSI let the installer compile, and does Defender's file scanner flag its bytes?+#+# This is the lane that reproduces #10805's mechanism rather than approximating it. The user there+# was not running the installer and watching it misbehave -- the script was refused before its first+# statement ran, because PowerShell hands a script block to AMSI at COMPILE time. So the measurement+# is compilation, which `[scriptblock]::Create` performs with none of an install's side effects: no+# download, no venv, no registry, no shortcut. That makes it cheap enough to run on every candidate,+# which the two-full-installs lane (windows-installer-differential-ci.yml) is not.+#+# It is differential. "install.ps1 compiles fine" is close to meaningless on its own; "the merge base+# compiles and the candidate does not" is a finding, and so is the reverse.+#+# Every verdict here depends on a positive control, and both halves have one:+#+# AMSI Microsoft's documented test sample, assembled from fragments, submitted through the+# SAME code path in the SAME process as each measurement.+# Defender EICAR, assembled from fragments, scanned through the same MpCmdRun invocation.+#+# If a control does not fire, the lane reports that it could not measure and passes with a warning.+# It never reports clean. That distinction is not theoretical: release-desktop.yml's own comments+# record three releases that shipped unscanned while their scan step was green, because a runner+# whose scanner is not live looks exactly like a clean verdict.+#+# What this lane cannot answer: anything about Skyhigh, Bitdefender or Kaspersky, which are the+# products that have actually flagged us. No hosted runner has them, and a Defender verdict is not a+# proxy for another vendor's. See docs/windows-installer-av-shapes.md for what each detection was.++name: "Windows: AMSI and Defender differential"++on:+ workflow_dispatch:+ pull_request:+ paths:+ - 'install.ps1'+ - 'studio/setup.ps1'+ - 'scripts/uninstall.ps1'+ - '.github/scripts/Probe-AmsiParse.ps1'+ - '.github/workflows/windows-amsi-defender-differential-ci.yml'+ schedule:+ # Definitions change daily and outside our control, so a periodic run against an unchanged tree+ # is how a new signature that starts matching us gets noticed at all. It is also why this lane+ # must not be a required check: a Microsoft definition push could otherwise block every merge.+ - cron: '41 4 * * *'++permissions:+ contents: read++concurrency:+ group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}+ cancel-in-progress: true++jobs:+ measure:+ name: does the scanner refuse either side+ runs-on: windows-latest+ timeout-minutes: 30+ steps:+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1+ with:+ fetch-depth: 0+ persist-credentials: false++ - name: Exempt the comparison directory from on-access scanning+ shell: pwsh+ run: |+ $ErrorActionPreference = 'Continue'+ # BEFORE anything is written there, not before the scan. Real-time protection acts on+ # open and on write, and -DisableRemediation governs only the explicit MpCmdRun scan, so+ # a live provider could take a copy away while the layout step writes it or while the+ # AMSI step reads it. The later Get-ChildItem loops would then simply not see that file,+ # and another valid row could carry both halves to a clean verdict.+ # release-desktop.yml:1437-1444 establishes its exclusions before copying for the same+ # reason, and records that the verdict is unaffected because the explicit scan ignores+ # file exclusions.+ $root = Join-Path $env:RUNNER_TEMP 'sides'+ New-Item -ItemType Directory -Force -Path $root | Out-Null+ Add-MpPreference -ExclusionPath $root -ErrorAction Continue++ # Verified, not assumed: Add-MpPreference is non-terminating here and a hosted image or+ # tamper protection can refuse it without saying so. A refusal is not fatal -- the lane+ # still measures, it just loses this protection -- but it has to be visible, because the+ # symptom of losing it is a file quietly missing from the results.+ $pref = Get-MpPreference -ErrorAction SilentlyContinue+ if ($pref -and @($pref.ExclusionPath) -contains $root) {+ Write-Host "on-access exclusion in place for $root"+ } else {+ Write-Host "::warning::could not exempt $root from on-access scanning. If real-time protection is live, a flagged copy can be quarantined before it is scanned and would then be reported as missing rather than as a detection."+ }++ - name: Lay out both sides of the comparison+ id: sides+ shell: pwsh+ env:+ PR_BASE: ${{ github.event.pull_request.base.sha }}+ run: |+ $ErrorActionPreference = 'Stop'+ $head = (git rev-parse HEAD).Trim()+ if ($env:PR_BASE) {+ # The merge base, not the base branch tip: comparing against a tip that has moved on+ # attributes somebody else's merged change to this PR.+ $base = (git merge-base $env:PR_BASE $head).Trim()+ } else {+ $base = (git rev-parse 'HEAD~1').Trim()+ }+ Write-Host "base $base"+ Write-Host "head $head"++ $root = Join-Path $env:RUNNER_TEMP 'sides'+ foreach ($side in 'base', 'head') {+ New-Item -ItemType Directory -Force -Path (Join-Path $root $side) | Out-Null+ }++ $files = @('install.ps1', 'studio/setup.ps1', 'scripts/uninstall.ps1')+ $laid = 0+ $manifest = @()+ foreach ($f in $files) {+ $leaf = ($f -replace '/', '_')+ foreach ($pair in @(@($base, 'base'), @($head, 'head'))) {+ $sha, $side = $pair+ $dest = Join-Path (Join-Path $root $side) $leaf+ # git show, not a second checkout: it costs nothing and cannot leave the working tree+ # in a state the next step misreads.+ git cat-file -e "${sha}:${f}" 2>$null+ if ($LASTEXITCODE -ne 0) {+ Write-Host "::notice::$f does not exist at $($sha.Substring(0,8)) ($side); skipping it on that side"+ continue+ }+ # The blob is copied as a byte stream, never captured as PowerShell text, and that is+ # the whole point of the lane rather than fussiness: it scans the bytes that ship.+ # Capturing `git show` into a variable decodes the output into lines and the write+ # back re-encodes them, which silently converts CRLF to LF, drops every trailing blank+ # line but one, adds a final newline where there was none, and mangles anything that+ # is not valid UTF-8. The scanner would then be judging a file this project never+ # serves, and a clean verdict on it would not transfer to the one users get.+ $psi = New-Object System.Diagnostics.ProcessStartInfo+ $psi.FileName = 'git'+ $psi.ArgumentList.Add('show')+ $psi.ArgumentList.Add("${sha}:${f}")+ $psi.RedirectStandardOutput = $true+ $psi.UseShellExecute = $false+ $proc = [System.Diagnostics.Process]::Start($psi)+ $out = [System.IO.File]::Create($dest)+ try {+ $proc.StandardOutput.BaseStream.CopyTo($out)+ } finally {+ $out.Dispose()+ $proc.WaitForExit()+ }+ if ($proc.ExitCode -ne 0) {+ Write-Host "::error::could not read $f at $($sha.Substring(0,8)) ($side)"+ exit 1+ }+ $manifest += [pscustomobject]@{ side = $side; leaf = $leaf }+ $laid+++ }+ }+ if ($laid -lt 2) {+ Write-Host '::error::fewer than two files were laid out, so there is nothing to compare. Refusing to report a verdict.'+ exit 1+ }+ # The EXPECTED set, written down. Both measurement loops used to enumerate whatever was+ # still on disk, so a copy quarantined by real-time protection before they ran simply left+ # the set: it became neither an unmeasured row nor a detection, and a surviving sibling+ # could carry the job to a clean verdict. Recording what was laid out means an absent file+ # is a measurable fact rather than a silence.+ $manifest | ConvertTo-Json -Depth 4 | Set-Content -LiteralPath (Join-Path $root 'manifest.json') -Encoding utf8+ Write-Host "laid out $laid file copies"+ "base_sha=$base" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8+ "head_sha=$head" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8+ "root=$root" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8+ "identical=$([int]($base -eq $head))" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8++ - name: Ask AMSI, under Windows PowerShell 5.1+ id: amsi+ shell: pwsh+ env:+ ROOT: ${{ steps.sides.outputs.root }}+ run: |+ $ErrorActionPreference = 'Continue'+ $script:UnslothVanished = @()+ $script:UnslothUnknownExpected = @()+ # Reads the layout manifest and returns what SHOULD be there, with anything missing+ # reported rather than skipped. A quarantined copy is the case this exists for.+ function Get-UnslothExpected {+ param([Parameter(Mandatory = $true)][string]$Side)+ $manifestPath = Join-Path $env:ROOT 'manifest.json'+ if (-not (Test-Path -LiteralPath $manifestPath)) {+ # Recorded, not only warned about. Enumerating survivors cannot notice that an expected+ # candidate is absent, so a quarantined copy left its siblings to carry the run to a+ # clean verdict. The expected set being unknown IS an unmeasured condition.+ Write-Host '::warning::the layout manifest is missing, so the expected set is unknown and only surviving files can be measured'+ $script:UnslothUnknownExpected += $Side+ return @(Get-ChildItem -LiteralPath (Join-Path $env:ROOT $Side) -Filter '*.ps1' -ErrorAction SilentlyContinue)+ }+ $expected = @(Get-Content -Raw -LiteralPath $manifestPath | ConvertFrom-Json | Where-Object { $_.side -eq $Side })+ $out = @()+ foreach ($entry in $expected) {+ $path = Join-Path (Join-Path $env:ROOT $Side) $entry.leaf+ if (Test-Path -LiteralPath $path) {+ $out += (Get-Item -LiteralPath $path)+ } else {+ $script:UnslothVanished += "$Side/$($entry.leaf)"+ }+ }+ return $out+ }++ $probe = (Resolve-Path '.github/scripts/Probe-AmsiParse.ps1').Path+ $out = Join-Path $env:RUNNER_TEMP 'amsi'+ New-Item -ItemType Directory -Force -Path $out | Out-Null++ # 5.1 specifically. It is what install.rs spawns (install.rs:433-440), what a clean Windows+ # box ships, and what the #10805 reporter ran. pwsh 7 is a different host with a different+ # AMSI integration, so its answer does not transfer.+ $ps51 = Join-Path $env:SystemRoot 'System32\WindowsPowerShell\v1.0\powershell.exe'+ if (-not (Test-Path -LiteralPath $ps51)) {+ Write-Host "::error::Windows PowerShell 5.1 is absent at $ps51; this runner cannot answer the question that matters"+ exit 1+ }++ $rows = @()+ $noResult = @()+ foreach ($side in 'base', 'head') {+ foreach ($file in (Get-UnslothExpected $side)) {+ $json = Join-Path $out "$side-$($file.BaseName).json"+ Write-Host "--- $side/$($file.Name)"+ # -Control on every invocation, so the control fires in the same process as the+ # measurement it vouches for. A control in a sibling process only shares a host+ # configuration, which is a weaker claim than it looks.+ & $ps51 -NoProfile -NonInteractive -ExecutionPolicy RemoteSigned `+ -File $probe -Control -Path $file.FullName -OutFile $json+ if (-not (Test-Path -LiteralPath $json)) {+ # Not `continue` alone: dropping the row shrinks the measured set silently, and a+ # head file that vanishes from the set leaves headBlocked at zero, which reads as+ # clean. Record it so the verdict below has to account for it.+ Write-Host "::warning::the probe wrote no result for $side/$($file.Name)"+ $noResult += "$side/$($file.Name) [the probe wrote no result]"+ continue+ }+ # Parsed under -ErrorAction Stop and then checked for the shape this lane reads. The+ # step runs with $ErrorActionPreference = 'Continue', so a truncated or otherwise+ # invalid file made ConvertFrom-Json emit a non-terminating error and return nothing,+ # and the row went in with a null payload. Such a row iterates no results, so it is+ # neither a block nor an unmeasured candidate: the file simply left the measured set,+ # and any other process whose control fired then carried the job to a clean verdict.+ $parsed = $null+ try {+ $parsed = Get-Content -Raw -LiteralPath $json -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop+ } catch {+ Write-Host "::warning::the probe's result for $side/$($file.Name) could not be parsed: $($_.Exception.Message)"+ $noResult += "$side/$($file.Name) [the probe's result could not be parsed]"+ continue+ }+ if ($null -eq $parsed -or -not $parsed.PSObject.Properties.Name.Contains('results') -or @($parsed.results).Count -eq 0) {+ Write-Host "::warning::the probe's result for $side/$($file.Name) carries no results"+ $noResult += "$side/$($file.Name) [the probe's result carries no results]"+ continue+ }+ $rows += [pscustomobject]@{+ side = $side+ name = $file.Name+ data = $parsed+ }+ }+ }++ # A copy that was laid out and is no longer there was not measured, and saying so is the+ # whole point of tracking the expected set.+ foreach ($v in $script:UnslothVanished) {+ Write-Host "::warning::$v was laid out and is no longer on disk; it may have been quarantined before it could be compiled"+ $noResult += "$v [the laid-out copy vanished before it was measured]"+ }+ foreach ($side in $script:UnslothUnknownExpected) {+ $noResult += "$side/<unknown> [the layout manifest is missing, so a quarantined candidate cannot be distinguished from one that was never laid out]"+ }++ if ($rows.Count -eq 0) {+ Write-Host '::error::the AMSI probe produced no results at all. Refusing to report a verdict.'+ exit 1+ }++ # The control decides whether any of this means anything, and it decides it PER ROW.+ # -Control runs in the same process as the file it vouches for, so its outcome is a+ # property of that one measurement. A job-wide flag would let a process where AMSI never+ # initialised borrow the credibility of one where it did, which is precisely how "no+ # provider was listening" turns into "nothing was detected".+ foreach ($row in $rows) {+ $fired = @($row.data.results | Where-Object { $_.label -like 'control*' -and $_.blocked }).Count+ $row | Add-Member -NotePropertyName controlFired -NotePropertyValue ([bool]$fired)+ }++ $blocked = @()+ $unmeasured = @($noResult)+ $didNotCompile = @()+ foreach ($row in $rows) {+ foreach ($r in $row.data.results) {+ if ($r.label -like 'control*') { continue }+ $where = "$($row.side)/$($row.name)"+ # Order matters here, and the obvious order is wrong.+ #+ # A parse error is a property of the script, not of the scanner: the compiler rejects+ # it whether or not an AMSI provider is listening. Classifying it BEFORE the control+ # gate is therefore both valid and necessary, because gating it meant a syntax error+ # in a process whose control happened to stay silent was filed as "not measured" and+ # the run exited zero.+ if ($r.blocked) {+ # This one does depend on a live provider, so it is the control's to vouch for.+ if ($row.controlFired) { $blocked += "$where [$($r.errorId)]" }+ else { $unmeasured += "$where [reported blocked, but the control did not fire in this process]" }+ continue+ }+ if ($r.missing) { $unmeasured += "$where [the probe could not find the file]"; continue }+ if ($r.unreadable) { $unmeasured += "$where [the probe could not read the file]"; continue }+ # blocked=false with an errorId set is a compile failure, not a scanner verdict.+ if ($r.errorId) { $didNotCompile += "$where [$($r.errorId)]"; continue }+ # Compiled cleanly, and only a live control makes that mean anything.+ if (-not $row.controlFired) {+ $unmeasured += "$where [the control did not fire in this process]"+ }+ }+ }++ Write-Host ''+ # Derived before any verdict is chosen, so whichever exit is taken can still report what+ # the other axis saw. The compile-failure exit below used to run before these existed, and+ # a run with one unparseable script and one genuinely refused script reported only the+ # parse failure while the real detection went unmentioned.+ $headUnmeasured = @($unmeasured | Where-Object { $_ -like 'head/*' })+ $headBlocked = @($blocked | Where-Object { $_ -like 'head/*' })+ $baseBlocked = @($blocked | Where-Object { $_ -like 'base/*' })++ # Ahead of the control gate below, for the same reason the per-row loop classifies parse+ # errors before it: whether a script compiles is a property of the script. With this after+ # the gate, a candidate with a syntax error on a runner where no control fired anywhere+ # was filed as "not measured" and the job exited zero, which is the case this verdict+ # exists for.+ $headDidNotCompile = @($didNotCompile | Where-Object { $_ -like 'head/*' })+ if ($headDidNotCompile.Count) {+ foreach ($c in $headDidNotCompile) { Write-Host "::error::the candidate did not compile: $c" }+ # Both outcomes, not just the one that decides the exit code. A refusal by a live+ # provider is the finding this lane exists to surface, and losing it because another+ # file failed to parse would hide the more important of the two.+ foreach ($b in $headBlocked) { Write-Host "::error::AMSI also refused the candidate: $b" }+ Write-Host '::error::A shipped script that does not parse is a defect in its own right, and it also means AMSI never got to judge it. Not clean, and not merely unmeasured.'+ "verdict=broken" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8+ exit 1+ }++ $controlFired = @($rows | Where-Object { $_.controlFired }).Count -gt 0+ if (-not $controlFired) {+ # Never clean. On this runner Defender is in passive mode with both drive roots excluded,+ # so this is the expected outcome more often than not, and saying "no detections" would be+ # reporting the absence of a scanner as the absence of a problem.+ Write-Host '::warning::the AMSI positive control did NOT fire: Microsoft''s own test sample compiled without being refused.'+ Write-Host '::warning::That means no AMSI provider is live in this process, so every input here would have come back clean whatever it contained.'+ Write-Host '::warning::COULD NOT MEASURE. This is a missing scanner, not a clean verdict.'+ "verdict=unmeasured" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8+ exit 0+ }++ Write-Host 'control: the AMSI test sample was refused, so a provider is live and this process can answer.'++ # Incomplete coverage is a warning, never a reason to stop. Returning early on it meant a+ # script that a live provider genuinely REFUSED went unreported whenever some other+ # invocation happened to write no result, and the job stayed green on a real detection.+ foreach ($u in $headUnmeasured) { Write-Host "::warning::could not measure $u" }++ if ($headBlocked.Count -eq 0) {+ if ($headUnmeasured.Count) {+ Write-Host '::warning::Nothing on head was refused, but at least one candidate was never actually put to a live provider, so this is not a clean verdict.'+ "verdict=unmeasured" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8+ exit 0+ }+ Write-Host "AMSI: no script was refused on head$(if ($baseBlocked.Count) { ", and $($baseBlocked.Count) was refused on base -- an improvement" })."+ "verdict=clean" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8+ exit 0+ }+ if ($headUnmeasured.Count) {+ Write-Host '::warning::Coverage was incomplete as well, so the blocks below are what was seen, not necessarily all there is.'+ }++ # Causality is per SCRIPT, not per side. Comparing counts said "pre-existing" whenever the+ # base had any block at all, even a block on a different file, and said "introduced"+ # whenever it had none -- including when the corresponding base row was never validly+ # measured, where the honest answer is that we do not know.+ $baseState = @{}+ foreach ($row in $rows | Where-Object { $_.side -eq 'base' }) {+ $state = 'compiled'+ if (-not $row.controlFired) { $state = 'unmeasured' }+ else {+ foreach ($r in $row.data.results) {+ if ($r.label -like 'control*') { continue }+ if ($r.blocked) { $state = 'blocked' }+ elseif ($r.missing -or $r.unreadable) { $state = 'unmeasured' }+ elseif ($r.errorId) { $state = 'unmeasured' }+ }+ }+ $baseState[$row.name] = $state+ }++ foreach ($b in $headBlocked) {+ Write-Host "::error::AMSI refused $b"+ # "head/install.ps1 [id]" -> "install.ps1"+ $script = ($b -replace '^head/', '') -replace ' \[.*$', ''+ switch ($baseState[$script]) {+ 'blocked' { Write-Host "::error:: the merge base is refused for $script too, so this is pre-existing rather than introduced. It is still a shipped script that will not compile for these users." }+ 'compiled' { Write-Host "::error:: the merge base compiled $script and the candidate did not. This change introduced it." }+ 'unmeasured' { Write-Host "::error:: the merge base measurement for $script is not valid, so whether this is introduced or pre-existing cannot be said from this run." }+ default { Write-Host "::error:: there is no merge base row for $script at all, so causality cannot be assigned." }+ }+ }+ "verdict=blocked" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8+ exit 1++ - name: Ask Defender's file scanner+ id: defender+ if: always()+ shell: pwsh+ env:+ ROOT: ${{ steps.sides.outputs.root }}+ run: |+ $ErrorActionPreference = 'Continue'+ $script:UnslothVanished = @()+ $script:UnslothUnknownExpected = @()+ # Reads the layout manifest and returns what SHOULD be there, with anything missing+ # reported rather than skipped. A quarantined copy is the case this exists for.+ function Get-UnslothExpected {+ param([Parameter(Mandatory = $true)][string]$Side)+ $manifestPath = Join-Path $env:ROOT 'manifest.json'+ if (-not (Test-Path -LiteralPath $manifestPath)) {+ # Recorded, not only warned about. Enumerating survivors cannot notice that an expected+ # candidate is absent, so a quarantined copy left its siblings to carry the run to a+ # clean verdict. The expected set being unknown IS an unmeasured condition.+ Write-Host '::warning::the layout manifest is missing, so the expected set is unknown and only surviving files can be measured'+ $script:UnslothUnknownExpected += $Side+ return @(Get-ChildItem -LiteralPath (Join-Path $env:ROOT $Side) -Filter '*.ps1' -ErrorAction SilentlyContinue)+ }+ $expected = @(Get-Content -Raw -LiteralPath $manifestPath | ConvertFrom-Json | Where-Object { $_.side -eq $Side })+ $out = @()+ foreach ($entry in $expected) {+ $path = Join-Path (Join-Path $env:ROOT $Side) $entry.leaf+ if (Test-Path -LiteralPath $path) {+ $out += (Get-Item -LiteralPath $path)+ } else {+ $script:UnslothVanished += "$Side/$($entry.leaf)"+ }+ }+ return $out+ }++ $mp = Join-Path $env:ProgramFiles 'Windows Defender\MpCmdRun.exe'+ if (-not (Test-Path -LiteralPath $mp)) {+ Write-Host "::warning::MpCmdRun.exe is absent; the file-scan half could not run. This is a missing scanner, not a clean verdict."+ exit 0+ }++ # The control first, and everything after it is conditional on it. Assembled from three+ # fragments so this repository does not itself carry the EICAR signature, the same way+ # release-desktop.yml does it.+ #+ # Inside $env:ROOT, which the earlier step exempted from on-access scanning, and that is+ # deliberate. The candidates are measured ONLY through the explicit MpCmdRun scan, because+ # they are exempt from on-access too, so a control that fires by being quarantined on write+ # proves a live real-time provider and says nothing about the path the measurement uses. An+ # on-demand scanner that silently skips files would still be trusted. Put somewhere it can+ # survive, the control has to be found by the same command that reads the candidates.+ # -DisableRemediation makes that command ignore file exclusions+ # (release-desktop.yml:1295, :1440-1442), so the exemption does not weaken it.+ $controlDir = Join-Path $env:ROOT 'defender-control'+ New-Item -ItemType Directory -Force -Path $controlDir | Out-Null+ $controlFile = Join-Path $controlDir 'control.txt'+ $fired = $false+ # A control only fires on something OBSERVED. The write and the scan are therefore in+ # separate try blocks, and neither catch infers a Defender block from the mere fact that+ # something threw: a permission or I/O failure under $RUNNER_TEMP, or MpCmdRun failing to+ # launch, would otherwise be reported as live real-time protection and every candidate+ # result after it trusted. An unavailable scanner reporting no hit is the exact failure+ # this control exists to catch, so it must not be able to satisfy it.+ # A benign file of the same size written the same way into the same directory, FIRST. An+ # absent EICAR file only means Defender if an identical write that Defender has no opinion+ # about succeeds; on its own, absence is equally explained by an ACL, a full disk or a+ # transient I/O error. $ErrorActionPreference is 'Continue' in this step, so a+ # non-terminating Set-Content failure does not even raise -- which is why the write is+ # checked by looking for the file rather than by whether anything threw.+ $canaryFile = Join-Path $controlDir 'canary.txt'+ $canaryOk = $false+ try {+ Set-Content -LiteralPath $canaryFile -Encoding ascii -NoNewline -Value (+ 'X' * 68) -ErrorAction Stop+ $canaryOk = (Test-Path -LiteralPath $canaryFile)+ } catch {+ Write-Host "::warning::could not write the benign canary: $($_.Exception.Message)"+ }+ if (-not $canaryOk) {+ Write-Host '::warning::the control directory is not writable, so nothing about Defender can be concluded from a missing EICAR file.'+ Write-Host '::warning::COULD NOT MEASURE on the Defender half: no positive control.'+ Remove-Item -LiteralPath $controlDir -Recurse -Force -ErrorAction SilentlyContinue+ exit 0+ }++ $wrote = $false+ try {+ Set-Content -LiteralPath $controlFile -Encoding ascii -NoNewline -Value (+ 'X5O!P%@AP[4\PZX54(P^)7CC)7}' + '$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!' + '$H+H*') `+ -ErrorAction Stop+ # Observed, not assumed. 'Continue' would otherwise carry a failed non-terminating write+ # straight into $wrote = $true and the absence below would read as quarantine.+ $wrote = (Test-Path -LiteralPath $controlFile)+ if (-not $wrote) {+ # Real-time protection took it despite the exclusion. That says a provider is live and+ # says nothing about the on-demand command the candidates are read with, so it is NOT+ # a fired control: it is a control that could not be run.+ Write-Host '::warning::the EICAR control was removed on write even though its directory is exempt from on-access scanning, so the on-demand scan could not be exercised and there is no positive control for the path the candidates are measured through.'+ }+ } catch {+ if (Test-Path -LiteralPath $controlFile) {+ Write-Host "::warning::could not write the EICAR control and the file is still there, so this was not a Defender block: $($_.Exception.Message)"+ } else {+ Write-Host "::warning::the EICAR write was refused while the benign canary of the same size in the same directory succeeded, so a provider is live, but the on-demand scan could not be exercised and that is the path the candidates are measured through. ($($_.Exception.Message))"+ }+ }+ if ($wrote) {+ try {+ # -DisableRemediation on the CONTROL too, so it is the same invocation the candidates+ # get. It is not only about remediation: release-desktop.yml records that the flag makes+ # the explicit scan ignore file exclusions (:1295, :1440-1442), and the hosted images+ # this lane describes ship with both drive roots excluded. Without it the control scan+ # of $RUNNER_TEMP can be skipped entirely, the control reads as silent, and the step+ # exits without testing candidates whose own scans would have worked.+ $controlOut = (& $mp -Scan -ScanType 3 -File $controlFile -DisableRemediation 2>&1 | Out-String)+ $controlCode = $LASTEXITCODE+ # The one thing that fires this control: the SAME command the candidates are read+ # with reported the detection.+ $fired = [bool](($controlOut -match 'found\s+\d+\s+threat') -or ($controlOut -match 'EICAR'))+ if (-not $fired -and -not (Test-Path -LiteralPath $controlFile)) {+ Write-Host '::warning::EICAR was removed between the write and the scan, so the on-demand scan never read it and the measurement path is unproven.'+ } elseif (-not $fired) {+ Write-Host "::warning::the control scan completed (exit $controlCode) and named no threat, so the file scanner is not detecting EICAR on this runner."+ }+ } catch {+ # A launch failure is a control that did not run. Saying so is the whole point.+ Write-Host "::warning::the control scan could not be run, so there is no positive control: $($_.Exception.Message)"+ }+ }+ Remove-Item -LiteralPath $controlDir -Recurse -Force -ErrorAction SilentlyContinue++ if (-not $fired) {+ Write-Host '::warning::the EICAR positive control did NOT fire, so Defender is not scanning on this runner.'+ Write-Host '::warning::COULD NOT MEASURE. Hosted images ship with real-time protection off, passive mode forced and both drive roots excluded; release-desktop.yml spends ~180 lines undoing exactly that, and this cheap lane deliberately does not.'+ exit 0+ }+ Write-Host 'control: EICAR fired, so the file scanner is live and its verdicts mean something.'++ # EICAR proves the LOCAL engine scans, and nothing more. This lane stamps mark-of-the-web+ # on every candidate specifically so the cloud is consulted, and a+ # zero-prevalence script is judged by cloud heuristics rather than by a local signature --+ # which is the condition the #10805 reporter was under. With MAPS off or unreachable that+ # half is simply absent, and an exit-0 scan would still read as clean.+ #+ # release-desktop.yml:1290-1343 makes the same distinction and verifies MAPS separately+ # from EICAR, including MpCmdRun -ValidateMapsConnection. This lane is deliberately the+ # cheap one and does not try to REPAIR the configuration, so it checks and labels instead:+ # a Defender verdict that could not reach the cloud says so rather than claiming more.+ $cloudReady = $false+ $cloudNote = 'not determined'+ try {+ $pref = Get-MpPreference -ErrorAction Stop+ # Reported, and deliberately NOT part of the gate. Block-at-first-sight only consults+ # the cloud on an on-access OPEN (release-desktop.yml:1301-1305), and this lane never+ # opens the copies: it exempts them from on-access scanning so a detection cannot+ # quarantine the evidence before it is measured, and reads them through an explicit+ # on-demand scan instead. What that scan needs is MAPS reporting and a reachable MAPS+ # endpoint. Gating on this preference labelled a runner whose cloud the scan CAN reach+ # as local-signatures-only, which is the opposite error from the one the gate is for.+ $bafsNote = if ($pref.DisableBlockAtFirstSeen) {+ 'DisableBlockAtFirstSeen=true, so nothing would act on an on-access open; this lane does not use that path'+ } else { 'block-at-first-sight is on' }+ if ([int]$pref.MAPSReporting -eq 0) {+ $cloudNote = "MAPSReporting is Disabled ($bafsNote)"+ } else {+ $mapsOut = (& $mp -ValidateMapsConnection 2>&1 | Out-String)+ if ($LASTEXITCODE -eq 0 -and $mapsOut -notmatch 'fail') {+ $cloudReady = $true+ $cloudNote = "MAPSReporting=$($pref.MAPSReporting), connection validated ($bafsNote)"+ } else {+ $cloudNote = "ValidateMapsConnection exit $LASTEXITCODE ($bafsNote)"+ }+ }+ } catch {+ $cloudNote = "could not read Defender preferences: $($_.Exception.Message)"+ }+ if ($cloudReady) {+ Write-Host "control: cloud-delivered protection is reachable ($cloudNote), so the on-demand scan below can consult the cloud rather than local signatures alone."+ Write-Host 'note: this is an ON-DEMAND cloud scan, not block-at-first-sight. BAFS only consults the cloud on an on-access OPEN (release-desktop.yml:1301-1305), and this lane never opens the copies -- it also exempts them from on-access scanning so a detection cannot quarantine the evidence before it is measured. The low-prevalence cloud verdicts this lane is aimed at still reach an on-demand scan through MAPS.'+ } else {+ Write-Host "::warning::cloud-delivered protection is NOT available ($cloudNote)."+ Write-Host '::warning::Any clean Defender result below is LOCAL SIGNATURES ONLY. The mark of the web is still stamped, but nothing consults the cloud, so the low-prevalence heuristics this lane is aimed at were not exercised.'+ }+ Write-Host ''++ $flagged = @()+ $unscanned = @()+ # Per SCRIPT and per side, so the verdict below can say whether a detection is introduced+ # by this change or already present on the merge base. Without it a signature that flags+ # the base too reads exactly like a regression, and a base-only detection -- an+ # improvement -- reads as a plain clean head. The AMSI half already works this way.+ $defState = @{}+ foreach ($side in 'base', 'head') {+ foreach ($file in (Get-UnslothExpected $side)) {+ # Mark of the web, so the cloud is consulted rather than treating a+ # local file as trusted. A zero-prevalence script from the Internet zone is exactly what+ # the reporter in #10805 had, and it is the condition the heuristics are harshest under.+ #+ # -Stream, and -Path rather than -LiteralPath. That is the only documented way to+ # address an alternate data stream, and it is what release-desktop.yml:1470 already+ # does. Appending ":Zone.Identifier" to -LiteralPath instead relies on the path being+ # taken "exactly as typed", and if it does not bind, -ErrorAction SilentlyContinue+ # swallows it: the mark is never applied, block-at-first-sight never consults the+ # cloud, and the run still prints "clean". A silent downgrade of the very condition+ # this step exists to create.+ $stamped = $false+ try {+ "[ZoneTransfer]`r`nZoneId=3" |+ Set-Content -Path $file.FullName -Stream Zone.Identifier -Encoding Ascii -ErrorAction Stop+ $stamped = $true+ } catch {+ Write-Host "::warning::could not stamp mark-of-the-web on $($file.Name): $($_.Exception.Message)"+ }+ if (-not $stamped) {+ # Said out loud rather than swallowed. A scan of a MyComputer-zone file is a weaker+ # test than the one this lane claims to run, and a reader has to know which they got.+ Write-Host "::warning::$side/$($file.Name) was scanned WITHOUT mark-of-the-web, so the scan did not carry the zone information that makes the cloud treat it as downloaded content. This is a weaker scan than intended."+ }+ # -DisableRemediation, or a detection deletes the file and the next step reports it+ # missing rather than flagged.+ $text = (& $mp -Scan -ScanType 3 -File $file.FullName -DisableRemediation 2>&1 | Out-String)+ # Captured immediately. A native command's exit code is not affected by+ # $ErrorActionPreference and $LASTEXITCODE is overwritten by the next external call,+ # so reading it later reads something else.+ $code = $LASTEXITCODE+ # Read the output AND the exit code. MpCmdRun has returned 0 with a threat named in its+ # text, so the text alone is not enough; and a scan that never ran produces neither a+ # match nor an error here, so the text alone would call it clean. 0 is the only+ # documented clean code. This is the contract release-desktop.yml:1472-1489 already+ # uses, reused rather than re-derived.+ $hit = [bool]($text -match 'found\s+([1-9]\d*)\s+threat')+ $state = if ($hit) { 'FLAGGED' } elseif ($code -ne 0) { "UNSCANNED (exit $code)" } else { 'clean' }+ Write-Host (" {0,-8} {1,-24} {2}" -f $side, $file.Name, $state)+ $defState["$side|$($file.Name)"] = if ($hit) { 'flagged' } elseif ($code -ne 0) { 'unmeasured' } else { 'clean' }+ if ($hit) {+ $flagged += "$side/$($file.Name)"+ ($text -split "`r?`n" | Where-Object { $_ -match 'Threat|found' } | Select-Object -First 5) |+ ForEach-Object { Write-Host " $_" }+ } elseif ($code -ne 0) {+ Write-Host "::warning::MpCmdRun did not complete a scan of $side/$($file.Name) (exit $code)"+ $unscanned += "$side/$($file.Name) [exit $code]"+ }+ }+ }++ foreach ($side in $script:UnslothUnknownExpected) {+ $unscanned += "$side/<unknown> [the layout manifest is missing, so a quarantined candidate cannot be distinguished from one that was never laid out]"+ }+ foreach ($v in $script:UnslothVanished) {+ Write-Host "::warning::$v was laid out and is no longer on disk; it may have been quarantined before it could be scanned"+ $unscanned += "$v [the laid-out copy vanished before it was scanned]"+ # "base/install.ps1" -> keyed the same way as a scanned row, as unmeasured. A file that+ # vanished was not found clean, and leaving it out of the map made `default` below say+ # "no base row at all", which is a different and wrong explanation.+ if ($v -match '^(base|head)/(.+)$') { $defState["$($Matches[1])|$($Matches[2])"] = 'unmeasured' }+ }++ Write-Host ''+ $s = (Get-MpComputerStatus -ErrorAction SilentlyContinue)+ if ($s) {+ # A verdict without the definition version is not reproducible, and a re-detection after a+ # definition bump is a different event from one on the same definitions.+ Write-Host "engine $($s.AMEngineVersion), definitions $($s.AntivirusSignatureVersion) ($($s.AntivirusSignatureLastUpdated)), AMRunningMode $($s.AMRunningMode)"+ }++ $headFlagged = @($flagged | Where-Object { $_ -like 'head/*' })+ $headUnscanned = @($unscanned | Where-Object { $_ -like 'head/*' })+ if ($headFlagged.Count -eq 0 -and $headUnscanned.Count) {+ # Not "nothing was flagged". Nothing was scanned, which is the same sentence a reader+ # would otherwise take as a clean result.+ foreach ($u in $headUnscanned) { Write-Host "::warning::Defender could not scan $u" }+ Write-Host '::warning::COULD NOT MEASURE on head: at least one candidate was never scanned, so there is no clean Defender verdict to report.'+ exit 0+ }+ if ($headFlagged.Count -eq 0) {+ if ($cloudReady) {+ Write-Host 'Defender: nothing on head was flagged, with cloud-delivered protection reachable.'+ } else {+ Write-Host 'Defender: nothing on head was flagged BY LOCAL SIGNATURES. The cloud half was unavailable, so this is a weaker statement than it looks.'+ }+ # A base-only detection is the result this lane is trying to produce, and reporting it as+ # a plain clean head throws away the only positive evidence the change ever gets.+ foreach ($key in $defState.Keys | Where-Object { $_ -like 'base|*' }) {+ if ($defState[$key] -ne 'flagged') { continue }+ $name = ($key -split '\|', 2)[1]+ if ($defState["head|$name"] -eq 'clean') {+ Write-Host "Defender: $name is flagged on the merge base and clean on the candidate. That is an improvement, not a neutral result."+ }+ }+ exit 0+ }+ # Causality is per SCRIPT, the same way the AMSI half assigns it. Reporting only the head+ # side named this change as the cause of a signature that already flags the merge base.+ $introduced = 0+ foreach ($f in $headFlagged) {+ Write-Host "::error::Defender flagged $f"+ $name = ($f -replace '^head/', '') -replace ' \[.*$', ''+ switch ($defState["base|$name"]) {+ 'flagged' { Write-Host "::error:: the merge base is flagged for $name too, so this is pre-existing rather than introduced. It is still a shipped script Defender objects to." }+ 'clean' { Write-Host "::error:: the merge base scanned clean for $name and the candidate did not. This change introduced it."; $introduced++ }+ 'unmeasured' { Write-Host "::error:: the merge base for $name was never validly scanned, so whether this is introduced or pre-existing cannot be said from this run." }+ default { Write-Host "::error:: there is no merge base scan for $name at all, so causality cannot be assigned." }+ }+ }+ Write-Host "Defender: $($headFlagged.Count) detection(s) on head, $introduced of which the merge base does not share."+ exit 1++ - name: Upload the measurements+ if: always()+ uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1+ with:+ name: amsi-defender-differential+ path: ${{ runner.temp }}/amsi/*.json+ if-no-files-found: warn+ retention-days: 30++ - name: Say plainly what was and was not measured+ if: always()+ shell: pwsh+ env:+ AMSI_VERDICT: ${{ steps.amsi.outputs.verdict }}+ IDENTICAL: ${{ steps.sides.outputs.identical }}+ run: |+ # Last, and always. A run whose summary is only "green" teaches the next reader that this+ # lane proves more than it does.+ Write-Host '--- what this run establishes'+ switch ($env:AMSI_VERDICT) {+ 'clean' { Write-Host ' AMSI: a live provider accepted the candidate at compile time under Windows PowerShell 5.1.' }+ 'blocked' { Write-Host ' AMSI: a live provider REFUSED a script. See the errors above.' }+ # Distinct from "did not complete". The probe ran and answered; the answer is that a+ # candidate does not compile, so AMSI never got to judge it and neither verdict applies.+ 'broken' { Write-Host ' AMSI: not measured, because a candidate did not COMPILE. That is a defect in the script, not a scanner verdict.' }+ # Several reasons reach here now, not just a dead provider: a per-row control that stayed+ # silent, a file the probe could not read or find, or an invocation that wrote no result.+ 'unmeasured' { Write-Host ' AMSI: not measured. Either no provider was live in the probe process, or a candidate was never actually put to one. See the warnings above.' }+ default { Write-Host ' AMSI: the probe did not complete.' }+ }+ if ($env:IDENTICAL -eq '1') {+ Write-Host ' the two sides were the same commit, so nothing differential was established.'+ }+ Write-Host ' NOT established: anything about Skyhigh, Bitdefender or Kaspersky, which are'+ Write-Host ' the products that have actually flagged Unsloth. No hosted runner has them, and'+ Write-Host ' one vendor''s verdict is not a proxy for another''s.'diff --git a/tests/security/test_amsi_parse_probe.py b/tests/security/test_amsi_parse_probe.pynew file mode 100644index 00000000000..02816442d8b--- /dev/null+++ b/tests/security/test_amsi_parse_probe.py@@ -0,0 +1,1031 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""The AMSI probe must keep its positive control, and must never actually run what it submits.++The probe answers the question #10805 is about: will a live AMSI provider let install.ps1 compile?+Its entire value rests on the control. A runner where no provider is live returns "not blocked" for+every input, including Microsoft's own test sample, and that outcome is byte-identical to a clean+verdict. release-desktop.yml's comments record three releases that shipped unscanned while their scan+step was green, for exactly this reason.++The second property is that the probe compiles and never invokes. It is handed the real installer, so+an `Invoke-Expression` or a `.Invoke()` creeping in would run a full install inside a measurement+step -- on the base side, from a commit nobody reviewed for that purpose.+"""++from __future__ import annotations++import json+import os+import re+import shutil+import subprocess+import tempfile+import textwrap+from pathlib import Path++import pytest+import yaml++# The shared runner, not a direct subprocess call: a direct one shares a single+# $XDG_CACHE_HOME/powershell startup cache with every other xdist worker, and an interpreter that+# dies at startup then renders as this test failing rather than as the crash it was.+# tests/studio/test_pwsh_calls_use_the_shared_runner.py enforces this.+from unsloth_pwsh_runner import run_pwsh+++REPO = Path(__file__).resolve().parents[2]+PROBE = REPO / ".github" / "scripts" / "Probe-AmsiParse.ps1"+WORKFLOW = REPO / ".github" / "workflows" / "windows-amsi-defender-differential-ci.yml"+++def _probe_text() -> str:+ return PROBE.read_text(encoding = "utf-8")+++def test_the_probe_exists() -> None:+ assert PROBE.is_file(), f"missing {PROBE.relative_to(REPO)}"+++def test_the_probe_parses() -> None:+ """It is invoked by a workflow under Windows PowerShell 5.1, where a syntax error would surface+ as "the probe wrote no result" and be indistinguishable from an absent scanner."""+ pwsh = shutil.which("pwsh")+ if pwsh is None:+ pytest.skip("pwsh is unavailable")+ probe = (+ "$errors = $null; $tokens = $null; "+ "$null = [System.Management.Automation.Language.Parser]::ParseFile("+ "$env:UNSLOTH_TARGET, [ref]$tokens, [ref]$errors); "+ "if ($errors.Count) { $errors | ForEach-Object { $_.Message }; exit 1 }"+ )+ import os++ result = run_pwsh(+ [pwsh, "-NoProfile", "-NonInteractive", "-Command", probe],+ capture_output = True,+ text = True,+ timeout = 120,+ env = {**os.environ, "UNSLOTH_TARGET": str(PROBE)},+ )+ assert result.returncode == 0, result.stdout + result.stderr+++def test_the_probe_compiles_but_never_invokes() -> None:+ """It is handed the real installer. Running it would install, from a commit nobody reviewed."""+ text = _probe_text()+ code = "\n".join(+ line+ for line in text.splitlines()+ if not line.strip().startswith("#") and not line.strip().startswith(".")+ )+ for banned in (+ "Invoke-Expression",+ ".Invoke()",+ "InvokeReturnAsIs",+ "Start-Process",+ "& $sb",+ "iex ",+ ):+ assert banned not in code, (+ f"the probe contains {banned!r}. It must submit text to the compiler and stop there: "+ f"it is handed install.ps1, so invoking would run a full install inside a measurement."+ )+ assert "[scriptblock]::Create" in code, (+ "the probe no longer submits anything to the compiler, which is the only way to ask AMSI "+ "the question. Compilation is the AMSI trigger; execution is not needed and is not wanted."+ )+++def test_the_positive_control_is_present_and_split() -> None:+ """Split so this repository is not itself a sample carrying the signature.++ A single literal here would mean every clone, every source tarball and every scan of this repo+ contains the AMSI test signature, which is the same self-inflicted detection class as the two+ fixture archives that made Panda flag the GitHub zip.+ """+ text = _probe_text()+ assert "$Control" in text, "the probe lost its -Control switch"+ assert "7e72c3ce" in text, (+ "the AMSI test sample is gone. Without it a runner with no live provider returns 'not "+ "blocked' for everything, which is indistinguishable from a clean verdict."+ )+ assert "7e72c3ce-861b-4339-8740-0ac1484c1386" not in text, (+ "the AMSI test sample appears as one literal. Assemble it from fragments, the way "+ "Microsoft's own documentation does, so this repository is not a sample of it."+ )+++def test_a_block_is_matched_on_the_error_id_and_not_on_message_text() -> None:+ """Message text is localised. install.rs already keys on the id, and this follows it."""+ text = _probe_text()+ assert "ScriptContainedMaliciousContent" in text+ assert "ScriptHasAdminBlockedContent" in text, (+ "an administrative policy block is a different cause with a different fix from a signature "+ "match, and collapsing them loses the only distinction that tells a user what to do"+ )+ assert "FullyQualifiedErrorId" in text+++def test_the_probe_never_exits_non_zero_on_a_detection() -> None:+ """Whether a detection is a failure depends on the controls and on which side it appeared on,+ and the caller holds both. A probe that exits non-zero on a detection cannot establish a+ baseline, which is the first thing this lane needs to do."""+ text = _probe_text()+ assert re.search(r"^exit 0\s*$", text, re.M), "the probe must end by exiting zero"+ assert "exit 1" not in text, (+ "the probe decides a verdict. It must report and let the workflow decide, because the "+ "workflow is the only place that knows whether the control fired."+ )+++# ---------------------------------------------------------------------------+# The workflow around it+# ---------------------------------------------------------------------------+++def test_the_workflow_exists_and_is_valid_yaml() -> None:+ assert WORKFLOW.is_file()+ data = yaml.safe_load(WORKFLOW.read_text(encoding = "utf-8"))+ assert list(data["jobs"]) == ["measure"]+++def test_the_lane_is_never_a_required_check_by_schedule_alone() -> None:+ """Definitions change daily and outside our control. A required check that a Microsoft+ definition push can turn red is a release-blocking hazard, so the scheduled run exists to+ notice a new signature, not to gate a merge."""+ body = WORKFLOW.read_text(encoding = "utf-8")+ assert "schedule:" in body+ assert "required check" in body, (+ "the workflow no longer records that it must not be a required check, which is the one "+ "thing a reader needs to know before wiring it into branch protection"+ )+++def test_an_absent_scanner_warns_and_never_reports_clean() -> None:+ body = WORKFLOW.read_text(encoding = "utf-8")+ for needle, why in (+ ("COULD NOT MEASURE", "the unmeasured outcome has to be named, or it reads as clean"),+ ("positive control", "both halves are gated on a control"),+ ("EICAR", "the file-scan half needs its own control"),+ ("did NOT fire", "the control failing has to be reported explicitly"),+ ):+ assert needle in body, f"{why}: {needle!r} is gone"+ # The EICAR literal must be split here too, for the same reason as the AMSI sample.+ assert (+ "X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*" not in body+ ), "EICAR appears as one literal in the workflow; assemble it from fragments"+++def test_the_probe_runs_under_windows_powershell_five_one() -> None:+ """pwsh 7 is a different host with a different AMSI integration, so its answer does not+ transfer, and 5.1 is what install.rs spawns and what the #10805 reporter ran."""+ body = WORKFLOW.read_text(encoding = "utf-8")+ assert "WindowsPowerShell\\v1.0\\powershell.exe" in body+ assert (+ "-ExecutionPolicy RemoteSigned" in body+ ), "the lane that exists to remove relaxed execution policies must not use one itself"+ assert "Bypass" not in body.replace(+ "ScriptHasAdminBlockedContent", ""+ ), "the workflow relaxes an execution policy somewhere"+++def test_the_probe_reports_on_a_host_without_amsi_rather_than_crashing() -> None:+ """Run for real, here, where no AMSI provider exists.++ This is the whole unmeasured path: the control does not fire, nothing is blocked, and the probe+ still has to produce well-formed JSON saying so. If it crashed instead, the workflow would read+ "the probe wrote no result" and a genuinely absent scanner would be reported as a broken lane.+ """+ pwsh = shutil.which("pwsh")+ if pwsh is None:+ pytest.skip("pwsh is unavailable")+ with tempfile.TemporaryDirectory() as tmp:+ out = Path(tmp) / "result.json"+ target = Path(tmp) / "sample.ps1"+ target.write_text("Write-Output 'hello'\n", encoding = "utf-8")+ result = run_pwsh(+ [+ pwsh,+ "-NoProfile",+ "-File",+ str(PROBE),+ "-Control",+ "-Path",+ str(target),+ "-OutFile",+ str(out),+ ],+ capture_output = True,+ text = True,+ timeout = 180,+ )+ assert result.returncode == 0, result.stdout + result.stderr+ assert out.is_file(), f"the probe wrote no result file:\n{result.stdout}{result.stderr}"+ data = json.loads(out.read_text(encoding = "utf-8"))++ labels = [r["label"] for r in data["results"]]+ assert any(l.startswith("control") for l in labels), "the control is missing from the output"+ assert str(target) in labels or any(str(target) in l for l in labels)+ for row in data["results"]:+ assert row["blocked"] is False, (+ "something was reported blocked on a host with no AMSI provider, which means the "+ "'blocked' flag is being set by something other than a scanner verdict"+ )+++def test_the_laid_out_copies_keep_the_bytes_that_ship(tmp_path: Path) -> None:+ """The lane scans the bytes users get, or its verdict does not transfer to them.++ The copies used to be reconstructed rather than copied: `git show` was captured into a+ PowerShell variable, which decodes the blob into lines, and the write back re-encoded them. That+ round trip converts CRLF to LF, collapses every trailing blank line into one, invents a final+ newline where the blob had none, and cannot represent a byte that is not valid UTF-8. The+ scanner was then judging a file this project never serves.++ Driven through the workflow's own extraction snippet against a real blob built to carry all+ three of those properties, rather than by asserting on the text of the snippet, so a future+ rewrite is judged on the bytes it produces.+ """+ pwsh = shutil.which("pwsh")+ if pwsh is None:+ pytest.skip("pwsh is unavailable")++ body = WORKFLOW.read_text(encoding = "utf-8")+ start = body.index("$psi = New-Object System.Diagnostics.ProcessStartInfo")+ end = body.index("$laid++", start)+ snippet = textwrap.dedent(body[start:end])++ repo = tmp_path / "repo"+ repo.mkdir()+ # CRLF, a byte that is not valid UTF-8, two trailing blank lines and no final newline: each one+ # is separately destroyed by the text round trip.+ blob = b"Write-Host 'one'\r\nWrite-Host 'two \xff'\r\n\r\n\r\nWrite-Host 'three'"+ (repo / "install.ps1").write_bytes(blob)+ env = {+ **os.environ,+ "GIT_AUTHOR_NAME": "t",+ "GIT_AUTHOR_EMAIL": "t@t",+ "GIT_COMMITTER_NAME": "t",+ "GIT_COMMITTER_EMAIL": "t@t",+ }+ for argv in (+ ["git", "init", "-q"],+ # -text so the checkout cannot be what normalises the line endings: the question here is+ # only what the extraction does with the blob.+ ["git", "config", "core.autocrlf", "false"],+ ["git", "add", "install.ps1"],+ ["git", "commit", "-qm", "b"],+ ):+ subprocess.run(argv, cwd = repo, check = True, env = env)+ sha = subprocess.run(+ ["git", "rev-parse", "HEAD"], cwd = repo, check = True, capture_output = True, text = True+ ).stdout.strip()++ dest = tmp_path / "copy.ps1"+ script = tmp_path / "lay.ps1"+ script.write_text(+ f"$sha = '{sha}'\n$f = 'install.ps1'\n$dest = '{dest.as_posix()}'\n" + snippet,+ encoding = "utf-8",+ )+ done = run_pwsh(+ [pwsh, "-NoProfile", "-NonInteractive", "-File", str(script)],+ cwd = str(repo),+ capture_output = True,+ text = True,+ timeout = 120,+ )+ assert done.returncode == 0, f"{done.stdout}\n{done.stderr}"+ assert dest.read_bytes() == blob, (+ "the laid-out copy is not the committed blob, so the scanner is judging bytes this project "+ "never serves"+ )++ # The COMMITTED blob, not the working tree. A Windows checkout with the default+ # core.autocrlf=true rewrites install.ps1 to CRLF on disk -- .gitattributes carries no *.ps1+ # rule -- so reading the file here failed on Windows runners while the bytes this project+ # actually serves were unchanged. The lane copies blobs out of git for exactly this reason, so+ # the assertion has to look where the lane looks.+ shipped = subprocess.run(+ ["git", "show", "HEAD:install.ps1"],+ cwd = REPO,+ check = True,+ capture_output = True,+ ).stdout+ assert b"\r\n" not in shipped, (+ "install.ps1 now contains CRLF, so the assumption this lane is built on no longer holds. "+ "Check .gitattributes before changing the workflow: a committed Authenticode signature over "+ "CRLF-rewritten content reads as HashMismatch, which is worse than unsigned."+ )+ assert shipped[:3] != b"\xef\xbb\xbf", "install.ps1 gained a BOM"+++def test_the_mark_of_the_web_is_written_the_documented_way() -> None:+ """`-Stream`, not a stream suffix appended to `-LiteralPath`.++ PowerShell documents exactly one way to address an alternate data stream, and+ `release-desktop.yml:1470` already uses it. The suffix form relies on the path being taken+ "exactly as typed" -- and if it does not bind, the `-ErrorAction SilentlyContinue` next to it+ swallows the error, the mark is never applied, block-at-first-sight never consults the cloud,+ and the step still prints "clean". That is a silent downgrade of the exact condition this lane+ exists to create, which is the worst failure shape available here.+ """+ body = WORKFLOW.read_text(encoding = "utf-8")+ assert "-Stream Zone.Identifier" in body, (+ "the mark-of-the-web stamp no longer uses -Stream, which is the only documented way to "+ "address an alternate data stream"+ )+ # Comment lines stripped before the ban is applied. The comment right above the fixed code+ # explains what the broken form looked like, and naming it is the point of that comment -- a+ # substring search over the raw file matches the explanation and fails. That has now happened+ # three times while writing these guards, which is a good argument for never grepping a file+ # for a string its own prose is obliged to contain.+ code = "\n".join(line for line in body.splitlines() if not line.strip().startswith("#"))+ assert (+ ':Zone.Identifier"' not in code+ ), "the workflow appends a stream name to a path again; use -Stream"+ assert "was scanned WITHOUT mark-of-the-web" in body, (+ "a failed stamp is no longer reported. A scan of a MyComputer-zone file is a weaker test "+ "than this lane claims to run, and a reader has to know which one they got."+ )+++def _amsi_step_script() -> str:+ """The body of the step that turns probe results into a verdict, as CI runs it."""+ workflow = yaml.safe_load(WORKFLOW.read_text(encoding = "utf-8"))+ for step in workflow["jobs"]["measure"]["steps"]:+ if step.get("id") == "amsi":+ return step["run"]+ raise AssertionError(+ "the workflow no longer has a step with id 'amsi'. This test drives the REAL verdict logic "+ "by extracting it, so that it cannot pass against a copy that has drifted from CI."+ )+++def _verdict_logic() -> str:+ """Everything from the per-row control onwards, with the row collection left to the caller."""+ script = _amsi_step_script()+ marker = "# The control decides whether any of this means anything"+ assert (+ marker in script+ ), "the per-row control block is gone from the workflow, so there is nothing to exercise"+ return script[script.index(marker) :]+++def _run_verdict(+ tmp_path: Path,+ rows_ps: str,+ no_result_ps: str = "@()",+) -> tuple[int, str]:+ """Run the extracted verdict logic against synthetic probe rows."""+ pwsh = shutil.which("pwsh")+ if pwsh is None:+ pytest.skip("pwsh is unavailable")+ out = tmp_path / "github_output"+ out.write_text("", encoding = "utf-8")+ script = tmp_path / "verdict.ps1"+ script.write_text(+ f"$env:GITHUB_OUTPUT = '{out.as_posix()}'\n"+ f"$rows = {rows_ps}\n"+ f"$noResult = {no_result_ps}\n" + _verdict_logic(),+ encoding = "utf-8",+ )+ done = run_pwsh(+ [pwsh, "-NoProfile", "-NonInteractive", "-File", str(script)],+ capture_output = True,+ text = True,+ timeout = 120,+ )+ return done.returncode, done.stdout + done.stderr + out.read_text(encoding = "utf-8")+++def _row(side: str, name: str, *, control: bool, result: str) -> str:+ control_ps = (+ f"@{{ label = 'control (AMSI test sample)'; blocked = ${str(control).lower()}; errorId = "+ f"$(if (${str(control).lower()}) {{ 'ScriptContainedMaliciousContent' }} else {{ '' }}) }}"+ )+ return (+ f"[pscustomobject]@{{ side = '{side}'; name = '{name}'; "+ f"data = [pscustomobject]@{{ results = @({control_ps}, {result}) }} }}"+ )+++_COMPILED = "@{ label = 'f.ps1'; blocked = $false; errorId = '' }"+_BLOCKED = "@{ label = 'f.ps1'; blocked = $true; errorId = 'ScriptContainedMaliciousContent' }"+_SYNTAX = "@{ label = 'f.ps1'; blocked = $false; errorId = 'ExpectedExpression' }"+_MISSING = "@{ label = 'f.ps1'; blocked = $false; errorId = ''; missing = $true }"+_UNREADABLE = "@{ label = 'f.ps1'; blocked = $false; errorId = ''; unreadable = $true }"+++ ("label", "head", "head_control", "expect_code", "expect_text"),+ [+ ("both sides compiled", _COMPILED, True, 0, "verdict=clean"),+ ("head refused by a provider", _BLOCKED, True, 1, "AMSI refused head/"),+ ("head did not compile", _SYNTAX, True, 1, "verdict=broken"),+ ("head file missing", _MISSING, True, 0, "verdict=unmeasured"),+ ("head file unreadable", _UNREADABLE, True, 0, "verdict=unmeasured"),+ ("head control silent", _COMPILED, False, 0, "verdict=unmeasured"),+ ],+)+def test_the_head_verdict_is_never_clean_unless_head_was_really_measured(+ tmp_path: Path, label: str, head: str, head_control: bool, expect_code: int, expect_text: str+) -> None:+ """Every way a head measurement can fail must stop short of publishing "clean".++ A lane that reports clean when it did not measure is worse than no lane: it converts an absent+ scanner, an unreadable candidate or a candidate that never compiled into a green check, and this+ repository has already shipped three releases whose scan step was green because nothing scanned.+ The base side is held healthy in every case so that the head side is the only variable.+ """+ rows = (+ f"@({_row('base', 'install.ps1', control = True, result = _COMPILED)}, "+ + _row("head", "install.ps1", control = head_control, result = head)+ + ")"+ )+ code, text = _run_verdict(tmp_path, rows)+ assert code == expect_code, f"{label}: expected exit {expect_code}, got {code}\n{text}"+ assert expect_text in text, f"{label}: expected {expect_text!r} in output\n{text}"+ if expect_text != "verdict=clean":+ assert "verdict=clean" not in text, f"{label}: published a clean verdict anyway\n{text}"+++def test_a_probe_that_wrote_no_result_is_not_silently_dropped(tmp_path: Path) -> None:+ """The row simply vanished before, and a head file that vanishes leaves zero blocked: clean."""+ rows = (+ f"@({_row('base', 'install.ps1', control = True, result = _COMPILED)}, "+ + _row("head", "install.ps1", control = True, result = _COMPILED)+ + ")"+ )+ code, text = _run_verdict(+ tmp_path, rows, no_result_ps = "@('head/setup.ps1 [the probe wrote no result]')"+ )+ assert code == 0+ assert "verdict=unmeasured" in text, text+ assert "verdict=clean" not in text, text+++def test_a_control_firing_elsewhere_does_not_vouch_for_this_process(tmp_path: Path) -> None:+ """The exact borrowing the per-row control exists to stop.++ AMSI initialises per process. A live base invocation followed by a head invocation where no+ provider loaded used to set one job-wide flag to true, and every row was then trusted, so the+ head answer -- taken in a process that would have said "not blocked" to anything at all --+ was published as clean.+ """+ rows = (+ f"@({_row('base', 'install.ps1', control = True, result = _BLOCKED)}, "+ + _row("head", "install.ps1", control = False, result = _COMPILED)+ + ")"+ )+ code, text = _run_verdict(tmp_path, rows)+ assert code == 0+ assert "verdict=unmeasured" in text, text+ assert "verdict=clean" not in text, text+++ ("label", "base_result", "base_control", "expect"),+ [+ ("base compiled the same file", _COMPILED, True, "This change introduced it"),+ (+ "base was refused for the same file",+ _BLOCKED,+ True,+ "pre-existing rather than introduced",+ ),+ ("base row is not validly measured", _COMPILED, False, "cannot be said from this run"),+ ("base did not compile the same file", _SYNTAX, True, "cannot be said from this run"),+ ],+)+def test_causality_is_decided_per_script_and_only_against_a_valid_base(+ tmp_path: Path, label: str, base_result: str, base_control: bool, expect: str+) -> None:+ """Whether a block is introduced or pre-existing is a statement about ONE script.++ Comparing counts across sides said "pre-existing" whenever the base had any block at all, even+ on a different file, and said "introduced" whenever it had none -- including when the base row+ for that script was never validly measured, where the only honest answer is that this run+ cannot tell. Both mistakes point a reader at the wrong commit.+ """+ rows = (+ "@("+ + _row("base", "install.ps1", control = base_control, result = base_result)+ + ", "+ + _row("head", "install.ps1", control = True, result = _BLOCKED)+ + ")"+ )+ code, text = _run_verdict(tmp_path, rows)+ assert code == 1, f"{label}: a refused head script must fail the job\n{text}"+ assert expect in text, f"{label}: expected {expect!r}\n{text}"+++def test_a_block_on_a_different_base_script_is_not_called_pre_existing(tmp_path: Path) -> None:+ """The count-based version's exact failure: base blocked on setup.ps1, head blocked on+ install.ps1, and the run announced the install.ps1 block as pre-existing."""+ rows = (+ "@("+ + _row("base", "setup.ps1", control = True, result = _BLOCKED)+ + ", "+ + _row("base", "install.ps1", control = True, result = _COMPILED)+ + ", "+ + _row("head", "install.ps1", control = True, result = _BLOCKED)+ + ")"+ )+ code, text = _run_verdict(tmp_path, rows)+ assert code == 1+ assert "This change introduced it" in text, text+ assert "pre-existing" not in text.split("install.ps1")[-1], text+++def test_a_real_block_is_reported_even_when_another_row_is_unmeasured(tmp_path: Path) -> None:+ """Incomplete coverage must not swallow a detection that was actually made.++ Returning early on any unmeasured head row meant a script a live provider genuinely REFUSED+ went unreported whenever some other invocation happened to write no result, and the job stayed+ green. A gap in coverage is a warning; a refusal is the finding this lane exists for.+ """+ rows = (+ "@("+ + _row("base", "install.ps1", control = True, result = _COMPILED)+ + ", "+ + _row("head", "install.ps1", control = True, result = _BLOCKED)+ + ")"+ )+ code, text = _run_verdict(+ tmp_path, rows, no_result_ps = "@('head/setup.ps1 [the probe wrote no result]')"+ )+ assert code == 1, f"a refused head script did not fail the job\n{text}"+ assert "AMSI refused head/install.ps1" in text, text+ assert "could not measure head/setup.ps1" in text, "the coverage gap was not reported too"+ assert "verdict=clean" not in text, text+++def test_a_parse_error_is_classified_even_when_the_control_is_silent(tmp_path: Path) -> None:+ """A syntax error is a property of the script, not of the scanner.++ The compiler rejects it whether or not an AMSI provider is listening, so gating the+ classification on the per-row control filed a broken candidate as merely unmeasured and exited+ zero. Only the BLOCKED verdict genuinely depends on a live provider.+ """+ rows = (+ "@("+ + _row("base", "install.ps1", control = True, result = _COMPILED)+ + ", "+ + _row("head", "install.ps1", control = False, result = _SYNTAX)+ + ")"+ )+ code, text = _run_verdict(tmp_path, rows)+ assert code == 1, f"a candidate that does not parse was not reported\n{text}"+ assert "verdict=broken" in text, text+++def test_a_block_claimed_without_a_live_control_is_not_trusted(tmp_path: Path) -> None:+ """The other side of the same reordering: only BLOCKED needs the control, and it still needs it."""+ rows = (+ "@("+ + _row("base", "install.ps1", control = True, result = _COMPILED)+ + ", "+ + _row("head", "install.ps1", control = False, result = _BLOCKED)+ + ")"+ )+ code, text = _run_verdict(tmp_path, rows)+ assert code == 0, text+ assert "verdict=unmeasured" in text, text+ assert "verdict=clean" not in text, text+++def test_a_parse_error_is_reported_even_when_no_control_fired_anywhere(tmp_path: Path) -> None:+ """The per-row ordering was fixed and the global one was not, which left the same hole.++ Whether a script compiles does not depend on AMSI, so the compile verdict has to be decided+ before the gate that reports a scanner-less runner as unmeasured. With the gate first, a+ candidate with a syntax error on a runner where no control fired at all, which is the usual+ state of a hosted image, exited zero and said the probe had not completed.+ """+ rows = (+ "@("+ + _row("base", "install.ps1", control = False, result = _COMPILED)+ + ", "+ + _row("head", "install.ps1", control = False, result = _SYNTAX)+ + ")"+ )+ code, text = _run_verdict(tmp_path, rows)+ assert code == 1, f"a candidate that does not parse was filed as unmeasured\n{text}"+ assert "verdict=broken" in text, text+++def test_cloud_readiness_is_decided_by_maps_and_reports_bafs_separately() -> None:+ """What the on-demand scan needs is MAPS, and block-at-first-sight is a different path.++ This gate used to refuse readiness whenever `DisableBlockAtFirstSeen` was set. That was right+ while the step told the reader block-at-first-sight had acted on the mark of the web; it stopped+ being right once the step was corrected to say what it actually does, which is an explicit+ on-demand scan. BAFS only consults the cloud on an on-access OPEN+ (release-desktop.yml:1301-1305) and this lane never opens the copies, so gating on it labelled a+ runner whose cloud the scan CAN reach as local-signatures-only. The preference is still read and+ still printed, because a reader has to know which configuration produced the verdict.+ """+ body = WORKFLOW.read_text(encoding = "utf-8")+ start = body.index("$cloudReady = $false")+ end = body.index("if ($cloudReady) {", start)+ gate = body[start:end]+ assert "$cloudReady = $true" in gate+ assert "ValidateMapsConnection" in gate, "readiness no longer verifies the MAPS connection"+ assert gate.index("ValidateMapsConnection") < gate.index(+ "$cloudReady = $true"+ ), "readiness is decided before the MAPS connection is validated"+ assert "DisableBlockAtFirstSeen" in gate, (+ "the block-at-first-sight preference is no longer reported at all, so a reader cannot tell "+ "which configuration produced the verdict"+ )+ assert "elseif ($pref.DisableBlockAtFirstSeen)" not in gate, (+ "block-at-first-sight is gating readiness again, which mislabels a MAPS-reachable runner as "+ "local signatures only"+ )+ # And it must still be printed, not merely computed.+ assert "$bafsNote" in gate and "$cloudNote = " in gate+++def test_a_real_block_is_still_reported_when_another_script_fails_to_parse(tmp_path: Path) -> None:+ """The compile-failure exit was taken before the blocks were even derived.++ Moving the compile verdict ahead of the control gate was right, but it then ran before+ `$headBlocked` existed, so a run with one unparseable candidate and one candidate genuinely+ refused by a live provider printed only the parse failure. The refusal is the finding this lane+ exists to surface, and it went unmentioned in the log and in the verdict.+ """+ rows = (+ "@("+ + _row("base", "install.ps1", control = True, result = _COMPILED)+ + ", "+ + _row("head", "install.ps1", control = True, result = _SYNTAX)+ + ", "+ + _row("head", "setup.ps1", control = True, result = _BLOCKED)+ + ")"+ )+ code, text = _run_verdict(tmp_path, rows)+ assert code == 1, text+ assert "verdict=broken" in text, text+ assert "AMSI also refused the candidate" in text, (+ "a genuine AMSI refusal was suppressed because another script failed to parse:\n" + text+ )+++def test_an_unparseable_probe_result_is_not_silently_dropped() -> None:+ """A row with a null payload is neither a block nor an unmeasured candidate.++ The step runs under `$ErrorActionPreference = 'Continue'`, so an existing but truncated JSON+ file made `ConvertFrom-Json` emit a non-terminating error and return nothing, and the row was+ appended with `data = $null`. That row iterates no results at all, so the file quietly left the+ measured set while any other process whose control fired carried the job to a clean verdict.+ """+ body = WORKFLOW.read_text(encoding = "utf-8")+ start = body.index("$json = Join-Path $out")+ end = body.index("if ($rows.Count -eq 0)", start)+ collection = body[start:end]+ assert (+ "ConvertFrom-Json -ErrorAction Stop" in collection+ ), "the probe result is still parsed without erroring, so an invalid file yields a null row"+ assert "could not be parsed" in collection, (+ "a parse failure is not routed into the no-result list, so it shrinks the measured set "+ "instead of being accounted for"+ )+ assert (+ "carries no results" in collection+ ), "a payload that parses but has no results is still accepted as a measured row"+++def test_the_defender_control_is_scanned_the_way_the_candidates_are() -> None:+ """A control that runs a different command than the measurement does not vouch for it.++ `-DisableRemediation` is not only about remediation: this repository's release scanner records+ that it makes the explicit scan ignore file exclusions+ (`.github/workflows/release-desktop.yml:1295` and `:1440-1442`), and the hosted images this lane+ describes ship with both drive roots excluded. The control scan therefore could be skipped+ while the candidate scans worked, leaving the step to exit without measuring anything.+ """+ body = WORKFLOW.read_text(encoding = "utf-8")+ scans = [line for line in body.splitlines() if "-Scan -ScanType 3" in line]+ assert scans, "nothing scans any more"+ for line in scans:+ assert "-DisableRemediation" in line, (+ f"this scan does not use the same flags as the others, so the control and the "+ f"measurement are not comparable:\n{line.strip()}"+ )+++def test_the_laid_out_copies_are_exempt_before_they_are_written() -> None:+ """Excluding the directory only before the SCAN leaves both earlier steps exposed.++ Real-time protection acts on open and on write, and `-DisableRemediation` governs only the+ explicit MpCmdRun scan, so a live provider can take a copy away while the layout step writes it+ or while the AMSI step reads it. The later `Get-ChildItem` loops then simply do not see that+ file, and another valid row can carry both halves to a clean verdict. release-desktop.yml+ establishes its exclusions before copying its inputs for exactly this reason.+ """+ import yaml as _yaml++ workflow = _yaml.safe_load(WORKFLOW.read_text(encoding = "utf-8"))+ steps = workflow["jobs"]["measure"]["steps"]+ names = [str(s.get("name", "")) for s in steps]+ runs = [str(s.get("run", "")) for s in steps]++ exclude_at = next(+ (i for i, r in enumerate(runs) if "Add-MpPreference -ExclusionPath" in r), None+ )+ assert exclude_at is not None, "nothing exempts the comparison directory any more"+ layout_at = next(i for i, n in enumerate(names) if "Lay out both sides" in n)+ amsi_at = next(i for i, n in enumerate(names) if "Ask AMSI" in n)+ assert exclude_at < layout_at, (+ "the exclusion is added after the copies are written, so real-time protection can "+ "quarantine one before it is ever measured"+ )+ assert exclude_at < amsi_at, "the exclusion is added after the copies are opened"+ # A non-terminating Add-MpPreference that was refused looks identical to one that worked.+ assert (+ "Get-MpPreference" in runs[exclude_at]+ ), "the exclusion is never read back, so a refusal is silent"+++def test_both_loops_measure_the_expected_set_not_the_survivors() -> None:+ """Enumerating the directory makes a quarantined copy disappear from the results entirely.++ Both the AMSI and the Defender loop derived their input set from `Get-ChildItem` over the+ laid-out directory. If real-time protection takes a flagged copy away first, which can happen+ whenever `Add-MpPreference` was refused, that candidate enters neither the unmeasured list nor+ the detection list, and a surviving sibling can carry the job to a clean verdict. The layout+ step knows what it wrote, so the loops read that instead and report anything absent.+ """+ body = WORKFLOW.read_text(encoding = "utf-8")+ assert "manifest.json" in body, "the layout step no longer records what it laid out"+ assert (+ body.count("Get-UnslothExpected") >= 3+ ), "not every measurement loop reads the expected set"+ # The bare enumeration must be gone from the measurement loops.+ assert (+ "foreach ($file in (Get-ChildItem -LiteralPath (Join-Path $env:ROOT $side)" not in body+ ), "a measurement loop still enumerates whatever survived on disk"+ # And a vanished file has to reach both verdicts, not just be printed.+ assert '$noResult += "$v' in body, "a vanished copy never reaches the AMSI verdict"+ assert '$unscanned += "$v' in body, "a vanished copy never reaches the Defender verdict"+ assert '$noResult += "$v' in body, "a vanished copy never reaches the AMSI verdict"+ assert '$unscanned += "$v' in body, "a vanished copy never reaches the Defender verdict"+++def test_the_probe_decodes_the_installer_as_utf8(tmp_path: Path) -> None:+ """Windows PowerShell 5.1 reads a BOM-less file as ANSI, and the installers are BOM-less UTF-8.++ That is the host this probe exists to reproduce, so `Get-Content` without an encoding handed+ AMSI a mojibake version of any non-ASCII text in the script. The provider would then be judging+ a string no user ever runs, and it does not match `irm ... | iex` either, where the response is+ decoded as Unicode.+ """+ body = PROBE.read_text(encoding = "utf-8")+ assert (+ "Get-Content -Raw -LiteralPath $file" not in body+ ), "the probe still reads the candidate with Get-Content's default encoding"+ assert "UTF8Encoding" in body, "the probe does not decode the candidate as UTF-8"++ # And the decode really is lossless for the bytes we ship: driven through pwsh rather than+ # asserted about, so a future rewrite is judged on what it produces.+ pwsh = shutil.which("pwsh")+ if pwsh is None:+ pytest.skip("pwsh is unavailable")+ sample = tmp_path / "sample.ps1"+ sample.write_bytes("Write-Host 'café — 中文'\n".encode("utf-8"))+ probe = (+ "$t = [System.IO.File]::ReadAllText($env:UNSLOTH_SAMPLE, "+ "[System.Text.UTF8Encoding]::new($false)); "+ "if ($t -match 'café' -and $t -match '中文') { 'OK' } else { 'MOJIBAKE'; exit 1 }"+ )+ import os as _os++ done = run_pwsh(+ [pwsh, "-NoProfile", "-NonInteractive", "-Command", probe],+ capture_output = True,+ text = True,+ timeout = 120,+ env = {**_os.environ, "UNSLOTH_SAMPLE": str(sample)},+ )+ assert done.returncode == 0 and "OK" in done.stdout, f"{done.stdout}\n{done.stderr}"+++def test_the_defender_lane_does_not_claim_block_at_first_sight() -> None:+ """The exclusion that protects the evidence also removes the on-access path BAFS needs.++ `release-desktop.yml:1301-1305` records that block-at-first-sight only consults the cloud on an+ on-access open. This lane never opens the copies and now exempts them from on-access scanning+ so a detection cannot quarantine the evidence before it is measured, so it cannot exercise that+ path at all. What it does get, and what the low-prevalence verdicts it is aimed at still reach,+ is an on-demand cloud scan through MAPS. The output has to say that and not more.+ """+ body = WORKFLOW.read_text(encoding = "utf-8")+ printed = [+ line+ for line in body.splitlines()+ if "Write-Host" in line and "block-at-first-sight" in line.lower()+ ]+ for line in printed:+ assert (+ "not block-at-first-sight" in line or "does not act" in line+ ), f"this line still tells the reader block-at-first-sight was exercised:\n{line.strip()}"+ assert (+ "ON-DEMAND cloud scan" in body+ ), "the lane no longer says what kind of cloud scan it actually performed"+++def test_the_probe_reports_the_inner_parse_error_id(tmp_path: Path) -> None:+ """The outer record carries only the generic `ParseException`.++ Calling a .NET static method from PowerShell wraps whatever it threw in a+ `MethodInvocationException`, so `$_.FullyQualifiedErrorId` on the outer record is+ `ParseException` no matter why the parse failed. The id that distinguishes a scanner refusal+ (`ScriptContainedMaliciousContent`) from an ordinary syntax error lives on the inner+ `ParseException`'s `Errors` collection. Matching only the outer id meant nothing was ever+ recognised as blocked, including the positive control, so the lane could only report that it+ had not measured.++ Driven through the shipped probe against a real syntax error, because that is the one inner id+ this host can produce without an AMSI provider.+ """+ pwsh = shutil.which("pwsh")+ if pwsh is None:+ pytest.skip("pwsh is unavailable")++ bad = tmp_path / "broken.ps1"+ bad.write_text("if (\n", encoding = "utf-8")+ out = tmp_path / "result.json"+ done = run_pwsh(+ [+ pwsh,+ "-NoProfile",+ "-NonInteractive",+ "-File",+ str(PROBE),+ "-Path",+ str(bad),+ "-OutFile",+ str(out),+ ],+ capture_output = True,+ text = True,+ timeout = 180,+ )+ assert out.exists(), f"the probe wrote no result:\n{done.stdout}\n{done.stderr}"+ data = json.loads(out.read_text(encoding = "utf-8"))+ row = next(r for r in data["results"] if not str(r["label"]).startswith("control"))+ assert row["blocked"] is False, "a syntax error was misreported as a scanner verdict"+ assert "IfStatementMissingCondition" in str(row["errorId"]), (+ f"the probe recorded only the outer generic id, so a real AMSI refusal would be invisible "+ f"too: {row['errorId']!r}"+ )+++def test_the_defender_verdict_assigns_causality_per_script() -> None:+ """A differential lane that reports only the head side is not differential.++ The AMSI half builds per-script base state and says whether a block is introduced or+ pre-existing. The Defender half collected base results and then filtered them out of the+ verdict, so a signature that already flags the merge base was reported exactly like a+ regression this change caused, and a base-only detection -- the result this work is trying to+ produce -- was reported as a plain clean head.+ """+ body = WORKFLOW.read_text(encoding = "utf-8")+ defender = body[body.index("Ask Defender's file scanner") :]+ defender = defender[: defender.index("Upload the measurements")]+ assert (+ "$defState" in defender+ ), "the Defender half records no per-script state to compare against"+ assert '$defState["base|$name"]' in defender, (+ "the Defender verdict does not look up the matching base script, so it cannot tell an "+ "introduced detection from a pre-existing one"+ )+ for phrase in ("pre-existing rather than introduced", "This change introduced it"):+ assert phrase in defender, f"the Defender verdict never says {phrase!r}"+ assert (+ "is flagged on the merge base and clean on the candidate" in defender+ ), "a base-only detection is still reported as a neutral clean head"+++def test_the_defender_control_never_infers_a_block_from_an_exception() -> None:+ """A control that any failure can satisfy is not a control.++ The catch around the EICAR write and scan used to set `$fired = $true` and print that real-time+ protection blocked the file. A permission or I/O failure under `$RUNNER_TEMP`, or MpCmdRun+ failing to launch, took that path too, and every candidate result after it was then trusted --+ so an unavailable scanner reporting no hit, which is the exact condition this control exists to+ catch, could report clean.+ """+ body = WORKFLOW.read_text(encoding = "utf-8")+ defender = body[body.index("Ask Defender's file scanner") :]+ defender = defender[: defender.index("Upload the measurements")]+ control = defender[: defender.index("EICAR proves the LOCAL engine scans")]+ # Every place the control is declared live has to be immediately preceded by an observation,+ # which here means a Test-Path on the control file rather than the fact that something threw.+ lines = control.splitlines()+ for i, line in enumerate(lines):+ if "$fired = $true" not in line:+ continue+ window = "\n".join(lines[max(0, i - 6) : i])+ assert "Test-Path -LiteralPath $controlFile" in window, (+ "this sets the control live without first observing that the control file is gone:\n"+ + window+ + "\n"+ + line+ )+ assert (+ "so this was not a Defender block" in control+ ), "a failed control write no longer reports itself as something other than a block"+ assert (+ "there is no positive control" in control+ ), "a control scan that cannot be launched no longer says the control did not run"+++def test_the_defender_control_writes_a_benign_canary_first() -> None:+ """An absent EICAR file only means Defender if an identical benign write succeeds.++ On its own, absence is equally explained by an ACL, a full disk or a transient I/O error, and+ `$ErrorActionPreference` is `Continue` in this step, so a non-terminating `Set-Content` failure+ does not even raise. The control therefore writes a benign file of the same size the same way+ into the same directory first, and gives up rather than concluding anything if that fails.+ """+ body = WORKFLOW.read_text(encoding = "utf-8")+ defender = body[body.index("Ask Defender's file scanner") :]+ control = defender[: defender.index("EICAR proves the LOCAL engine scans")]+ assert "$canaryFile" in control, "there is no benign canary, so absence is not attributable"+ assert control.index("$canaryOk") < control.index("$controlFile -Encoding ascii"), (+ "the canary is written after the EICAR file, so a failed directory cannot be ruled out "+ "before the EICAR write is interpreted"+ )+ assert (+ "no positive control" in control+ ), "an unwritable control directory no longer reports that there is no positive control"+ # Both writes must be terminating, or Continue carries a failure straight past the check.+ writes = [line for line in control.splitlines() if "Set-Content -LiteralPath $c" in line]+ assert len(writes) == 2, writes+ for line in writes:+ block = control[control.index(line) : control.index(line) + 400]+ assert "-ErrorAction Stop" in block, f"this write is non-terminating:\n{line.strip()}"+++def test_the_control_fires_only_through_the_command_the_candidates_are_read_with() -> None:+ """A live real-time provider does not vouch for the on-demand scan.++ The candidates sit in a directory exempted from on-access scanning and are measured only by the+ explicit `MpCmdRun -Scan ... -DisableRemediation`. A control that fired by being quarantined on+ write therefore proved a provider was live and proved nothing about the command the measurement+ uses, so an on-demand scanner that silently skipped files would still have been trusted. The+ control now lives inside the same exempt root, survives its write, and has to be found by that+ same command.+ """+ body = WORKFLOW.read_text(encoding = "utf-8")+ defender = body[body.index("Ask Defender's file scanner") :]+ control = defender[: defender.index("EICAR proves the LOCAL engine scans")]+ assert "$controlDir = Join-Path $env:ROOT 'defender-control'" in control, (+ "the control is not inside the on-access exclusion, so real-time protection can take it "+ "away before the on-demand scan reads it"+ )+ # Exactly one place may set the control live, and it is the branch that read the scan output.+ lines = control.splitlines()+ fired = [+ i for i, line in enumerate(lines) if "$fired = [bool]" in line or "$fired = $true" in line+ ]+ assert len(fired) == 1, "the control fires from more than one place again:\n" + "\n".join(+ lines[i].strip() for i in fired+ )+ assert "$controlOut -match" in lines[fired[0]], (+ f"the control is set live by something other than the on-demand scan output: "+ f"{lines[fired[0]].strip()}"+ )+ assert (+ "the measurement path is unproven" in control+ ), "a control removed before the scan no longer says the measurement path went unproven"+++def test_a_missing_layout_manifest_cannot_produce_a_clean_verdict() -> None:+ """Enumerating survivors cannot notice that an expected candidate is absent.++ With `manifest.json` gone the fallback lists what is still on disk, so a quarantined copy left+ its siblings to carry the run to `verdict=clean`. The expected set being unknown is itself an+ unmeasured condition and has to reach the verdict, not only the log.+ """+ body = WORKFLOW.read_text(encoding = "utf-8")+ assert (+ body.count("$script:UnslothUnknownExpected") >= 6+ ), "the unknown-expected-set condition is not tracked in both halves"+ for half, feeds in (+ ("Ask AMSI, under Windows PowerShell 5.1", "$noResult +="),+ ("Ask Defender's file scanner", "$unscanned +="),+ ):+ start = body.index(half)+ end = body.index("- name:", start + 10)+ # The step bodies run past the next `- name:` marker for the AMSI half, so take the whole+ # remainder for it and cut at the upload step instead.+ chunk = (+ body[start : body.index("Upload the measurements")] if "AMSI" in half else body[start:]+ )+ loop = chunk[chunk.index("foreach ($side in $script:UnslothUnknownExpected)") :]+ loop = loop[: loop.index("}")]+ assert feeds in loop, f"{half}: the unknown expected set does not reach the verdict"