Skip to content

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.

WSL installer: stop running the C# compiler on the user's Windows machine (#10978)

Daniel Han201790e691b2
diff --git a/.github/workflows/clean-machine-install-ci.yml b/.github/workflows/clean-machine-install-ci.yml
index 7faa05c7644..9b6737f7974 100644
--- a/.github/workflows/clean-machine-install-ci.yml
+++ b/.github/workflows/clean-machine-install-ci.yml
@@ -54,6 +54,9 @@ on:
- 'studio/setup.ps1'
- 'studio/setup.bat'
- 'studio/install_python_stack.py'
+ # The wsl job runs its install inside this watcher, so a change to it changes what that
+ # job can detect.
+ - '.github/scripts/Watch-ForCompiler.ps1'
# setup.sh (727) and setup.ps1 (2343, 3630, 3916) call these directly and the
# overlay makes them THIS ref's code, so they decide whether a clean machine gets
# a native prebuilt or a toolchain-dependent fallback.
@@ -85,6 +88,7 @@ on:
- 'studio/setup.ps1'
- 'studio/setup.bat'
- 'studio/install_python_stack.py'
+ - '.github/scripts/Watch-ForCompiler.ps1'
- 'studio/install_*_prebuilt.py'
- 'studio/prebuilt_core.py'
- 'studio/install_manifest.py'
@@ -936,6 +940,58 @@ jobs:
# A freshly imported rootfs is genuinely bare: the clean machine, not a simulation.
wsl -d unsloth-ci -- sh -c 'for t in curl wget git gcc cmake python3 sudo; do printf "%-8s %s\n" "$t" "$(command -v $t || echo ABSENT)"; done'
+ - name: Enable process creation auditing, and prove the watcher sees a real compile
+ shell: powershell
+ run: |
+ $ErrorActionPreference = 'Stop'
+
+ # Watch-ForCompiler.ps1 unions two detectors, and its 4688 half needs the caller to turn
+ # auditing on -- its no-match path returns an empty compiler list whenever the Security
+ # log merely reads clean. Without this step that half is silently dead here, so a compile
+ # the FileSystemWatcher missed would be reported as "no compiler process" rather than as
+ # "not measured". windows-no-compiler-ci.yml enables the same two settings.
+ auditpol /set /subcategory:"Process Creation" /success:enable | Out-Null
+ $auditKey = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\Audit'
+ New-Item -Path $auditKey -Force | Out-Null
+ New-ItemProperty -Path $auditKey -Name 'ProcessCreationIncludeCmdLine_Enabled' `
+ -Value 1 -PropertyType DWord -Force | Out-Null
+
+ # Large enough that the install's own process noise cannot roll the watched window off
+ # the end of the log.
+ wevtutil sl Security /ms:268435456
+
+ $state = auditpol /get /subcategory:"Process Creation"
+ $state | Tee-Object -FilePath logs/auditpol.txt
+ # Out-String first: auditpol returns an array, and -notmatch on an array filters instead
+ # of testing, so the surviving header lines make any array truthy.
+ if (($state | Out-String) -notmatch 'Process Creation\s+Success') {
+ Write-Host '::error::process creation auditing did not turn on; this lane cannot measure whether a compiler ran'
+ exit 1
+ }
+
+ # A machine-policy override can leave auditpol reporting Success while no 4688 is written,
+ # and a clean run looks identical to an unmeasured one. So compile something on purpose
+ # and require both halves to see it before any clean verdict below is believed.
+ . "$env:GITHUB_WORKSPACE\.github\scripts\Watch-ForCompiler.ps1"
+ $control = Invoke-WithCompilerWatch -Name 'wsl-control' -EvidenceRoot "$PWD/logs" -Action {
+ # The only Add-Type in this lane, here to make the detector fire.
+ Add-Type -TypeDefinition 'public static class UnslothWslCompilerProbe { public static int N() { return 1; } }'
+ }
+ # BOTH halves, because the install below trusts both. Checking only the process half left
+ # a FileSystemWatcher that never attached, or stopped reporting creations, looking exactly
+ # like a run where no library was dropped -- and $seen.TempLibraries is the half the
+ # Bitdefender report in #10540 actually keyed on. windows-no-compiler-ci.yml validates
+ # both for the same reason.
+ if (-not $control.Compilers) {
+ Write-Host '::error::the positive control compiled a type and no compiler process was recorded; a clean result from this lane would mean nothing'
+ exit 1
+ }
+ if (-not $control.TempLibraries) {
+ Write-Host '::error::the positive control compiled a type and no dropped library was recorded, so the filesystem half of the watch is not working; an empty TempLibraries result from the install below would mean nothing'
+ exit 1
+ }
+ Write-Host "positive control: compiler seen ($($control.Compilers -join ', ')) and library seen ($($control.TempLibraries -join ', '))"
+
- name: Install inside WSL, piped exactly as documented
shell: pwsh
run: |
@@ -958,10 +1014,34 @@ jobs:
Write-Host "installer source in WSL: $wslPath"
wsl -d unsloth-ci -u root -- cp "$wslPath" /root/install.sh
}
- # Piped, the `curl ... | sh` shape, so an early exit still exposes the broken pipe.
- wsl -d unsloth-ci -u root -- sh -c 'cd /root && cat install.sh | sh' 2>&1 | Tee-Object -FilePath logs/wsl-install.log
- $installRc = $LASTEXITCODE
+ # Watched LIVE while it runs, not inspected afterwards. The shortcut path runs
+ # powershell.exe on the Windows side, so a compile would happen here, and CodeDom deletes
+ # its whole intermediate directory once the assembly is loaded -- Watch-ForCompiler.ps1
+ # records that a before-and-after listing "saw nothing at all while 4688 recorded
+ # csc.exe". A post-hoc search for .cmdline files therefore cannot fail for the case this
+ # lane exists to catch, and a control that plants persistent files only proves the search
+ # can traverse a directory.
+ . "$env:GITHUB_WORKSPACE\.github\scripts\Watch-ForCompiler.ps1"
+ $seen = Invoke-WithCompilerWatch -Name 'wsl-install' -EvidenceRoot "$PWD/logs" -Action {
+ # Piped, the `curl ... | sh` shape, so an early exit still exposes the broken pipe.
+ wsl -d unsloth-ci -u root -- sh -c 'cd /root && cat install.sh | sh' 2>&1 |
+ Tee-Object -FilePath logs/wsl-install.log
+ $global:installRc = $LASTEXITCODE
+ }
+ $installRc = $global:installRc
Write-Host "installer exit: $installRc"
+ if ($seen.Compilers) {
+ Write-Host "::error::a C# compiler ran during the WSL install: $($seen.Compilers -join ', ')"
+ Write-Host '::error::install.sh must declare SHChangeNotify through reflection emit, never Add-Type -MemberDefinition'
+ exit 1
+ }
+ if ($seen.TempLibraries) {
+ # The other end of the same chain, and the one the Bitdefender report in #10540 keyed
+ # on: Add-Type leaves a freshly built DLL in the temp directory.
+ Write-Host "::error::a compiled library was dropped in the Windows temp during the WSL install: $($seen.TempLibraries -join ', ')"
+ exit 1
+ }
+ Write-Host 'live watch: no compiler process and no compiled library during the install'
if (-not (Test-Path logs/wsl-install.log)) {
Write-Host '::error::the WSL install produced no log'
@@ -1016,6 +1096,99 @@ jobs:
exit 1
}
+ - name: Did the Windows shortcut survive, and did nothing compile to make it?
+ if: always()
+ shell: pwsh
+ run: |
+ # The only lane that runs install.sh's WSL shortcut branch, so the only one that can
+ # answer either half of this. Both halves changed together: the generated script moved
+ # from WSL's /tmp (a \\wsl.localhost UNC path, which is why it needed a relaxed execution
+ # policy) to the Windows %TEMP%, and its icon-refresh declaration stopped compiling.
+ #
+ # Advisory, not fatal. Shortcut creation is best-effort in the installer itself -- it
+ # warns and continues when interop is unavailable -- so failing the job for an absent
+ # .lnk would make this stricter than the code it measures. What must never be advisory is
+ # the compiler check below.
+ $names = @('Unsloth Studio (WSL*.lnk')
+ $roots = @(
+ [Environment]::GetFolderPath('Desktop'),
+ (Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs')
+ )
+ $found = @()
+ foreach ($root in $roots) {
+ if (-not $root -or -not (Test-Path -LiteralPath $root)) { continue }
+ foreach ($pattern in $names) {
+ $found += @(Get-ChildItem -LiteralPath $root -Filter $pattern -ErrorAction SilentlyContinue)
+ }
+ }
+ if ($found.Count -eq 0) {
+ Write-Host '::warning::no WSL shortcut was created; the installer prints its own notice when interop is unavailable, so check logs/wsl-install.log before reading this as a regression'
+ } else {
+ $shell = New-Object -ComObject WScript.Shell
+ foreach ($lnk in $found) {
+ $sc = $shell.CreateShortcut($lnk.FullName)
+ Write-Host "shortcut: $($lnk.Name)"
+ Write-Host " target: $($sc.TargetPath)"
+ Write-Host " args : $($sc.Arguments)"
+ # wsl.exe or wt.exe, never a script engine. #6326 removed the .vbs launcher for
+ # exactly this reason and nothing in the WSL path should reintroduce one.
+ if ($sc.TargetPath -match 'wscript|cscript|mshta') {
+ Write-Host "::error::$($lnk.Name) launches a script host: $($sc.TargetPath)"
+ exit 1
+ }
+ }
+ }
+
+ # The compiler half. A .cmdline or .rsp in the Windows %TEMP% is csc.exe's response file,
+ # and nothing else writes one, so a hit means the icon-refresh declaration is compiling
+ # again.
+ #
+ # Positive control first, because a detector that sees nothing looks exactly like a clean
+ # run, and every reason it could see nothing is silent: an unreadable TEMP, a TEMP that is
+ # not the one the interop process used, a Get-ChildItem that fails on a long path. Plant
+ # one intermediate of each kind, require the search to find both, then remove them. If the
+ # control does not fire the job fails as a broken detector rather than reporting a pass.
+ $controlDir = Join-Path $env:TEMP ('unsloth-compiler-control-' + [guid]::NewGuid().ToString('N').Substring(0, 8))
+ New-Item -ItemType Directory -Path $controlDir -Force | Out-Null
+ $controlFiles = @(
+ (Join-Path $controlDir 'control.cmdline'),
+ (Join-Path $controlDir 'control.rsp')
+ )
+ foreach ($f in $controlFiles) { Set-Content -LiteralPath $f -Value '/noconfig /fullpaths' -Encoding ascii }
+ $controlSeen = @(
+ Get-ChildItem -LiteralPath $env:TEMP -Recurse -Include '*.cmdline', '*.rsp' -ErrorAction SilentlyContinue |
+ Where-Object { $_.FullName -like (Join-Path $controlDir '*') }
+ )
+ Remove-Item -LiteralPath $controlDir -Recurse -Force -ErrorAction SilentlyContinue
+ if ($controlSeen.Count -lt 2) {
+ Write-Host "::error::the compiler-intermediate detector is broken: it found $($controlSeen.Count) of 2 planted files under $env:TEMP."
+ Write-Host '::error::Not reporting this run as clean. A search that cannot see a file it just wrote cannot see one csc.exe wrote either.'
+ exit 1
+ }
+ Write-Host "positive control fired: the detector saw both planted intermediates under $env:TEMP"
+
+ $leftovers = @(
+ Get-ChildItem -LiteralPath $env:TEMP -Filter 'unsloth-shortcut-*.ps1' -ErrorAction SilentlyContinue
+ )
+ if ($leftovers.Count) {
+ Write-Host "::error::the generated shortcut script was not cleaned up: $($leftovers.Name -join ', ')"
+ exit 1
+ }
+ # Leftovers only. The compile itself is judged by the live watch around the install step,
+ # because CodeDom cleans up after itself and this listing would see nothing either way.
+ # Kept because it catches what IS left behind, which is the other half of the union
+ # Watch-ForCompiler.ps1 describes.
+ $compiled = @(
+ Get-ChildItem -LiteralPath $env:TEMP -Recurse -Include '*.cmdline', '*.rsp' -ErrorAction SilentlyContinue |
+ Where-Object { $_.LastWriteTime -gt (Get-Date).AddMinutes(-30) }
+ )
+ if ($compiled.Count) {
+ Write-Host "::error::compiler intermediates were left in the Windows TEMP: $($compiled.FullName -join ', ')"
+ Write-Host '::error::install.sh must declare SHChangeNotify through reflection emit, never Add-Type -MemberDefinition'
+ exit 1
+ }
+ Write-Host 'no compiler intermediates left behind, and no generated script left behind'
+
- name: Tear the distro down
if: always()
shell: pwsh
diff --git a/.github/workflows/windows-no-compiler-ci.yml b/.github/workflows/windows-no-compiler-ci.yml
index 4a268fee500..714e7c028f1 100644
--- a/.github/workflows/windows-no-compiler-ci.yml
+++ b/.github/workflows/windows-no-compiler-ci.yml
@@ -40,6 +40,11 @@ on:
- 'install.ps1'
- 'studio/setup.ps1'
- 'scripts/uninstall.ps1'
+ # install.sh generates PowerShell into a here-string and runs it on the Windows
+ # side for the WSL shortcut, so it can put a compiler on a user's Windows machine
+ # without touching a .ps1. It did: the Add-Type that #10540 removed from
+ # install.ps1 stayed live here, outside this list and outside the text guard.
+ - 'install.sh'
- 'studio/src-tauri/src/install.rs'
- 'studio/src-tauri/src/process.rs'
- 'tests/studio/test_installer_av_shapes.py'
diff --git a/.github/workflows/workflow-trigger-lint.yml b/.github/workflows/workflow-trigger-lint.yml
index 3e645f2d57d..d68bbd30201 100644
--- a/.github/workflows/workflow-trigger-lint.yml
+++ b/.github/workflows/workflow-trigger-lint.yml
@@ -169,6 +169,7 @@ jobs:
tests/studio/test_windows_small_checks_stay_on_their_image.py \
tests/studio/test_windows_ui_lanes_are_isolated.py \
tests/studio/test_workflow_guards_run_unfiltered.py \
+ tests/studio/test_wsl_shortcut_script_is_valid_powershell.py \
tests/test_wheel_smoke_publish_guard.py \
tests/security \
--ignore=tests/security/test_custom_dtype_no_eval.py \
diff --git a/README.md b/README.md
index 47d6603e474..4e18517a2bd 100644
--- a/README.md
+++ b/README.md
@@ -288,7 +288,7 @@ The developer install builds from the `main` branch, which is the latest (nightl
```powershell
git clone https://github.com/unslothai/unsloth.git
cd unsloth
-Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
+Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned
.\install.ps1 --local
unsloth studio -p 8888
```
diff --git a/install.ps1 b/install.ps1
index 1972141560a..a2d774f6d7c 100644
--- a/install.ps1
+++ b/install.ps1
@@ -1,8 +1,9 @@
# Unsloth Studio Installer for Windows PowerShell
#
# Usage, options and the web one-liner: see "Unsloth Studio (web UI)" in the README
-# (https://github.com/unslothai/unsloth#unsloth-studio-web-ui). Not repeated here, because
-# AMSI scans this file in full before a line of it runs and nothing reads the header from inside.
+# (https://github.com/unslothai/unsloth#unsloth-studio-web-ui). Not repeated here: nothing reads
+# this header from inside the script, and the whole file is scanned before any of it runs.
+# Why several things below are written the long way: tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
#
# The web entry point cannot forward arguments, so it takes options as environment variables set
# beforehand (UNSLOTH_NO_TORCH, UNSLOTH_SKIP_AUTOSTART, UNSLOTH_ISOLATE_UV_CACHE,
@@ -835,13 +836,12 @@ function Install-UnslothStudio {
#
# Add-Type on Windows PowerShell 5.1 (the interpreter the desktop app spawns) has
# no in-process compiler: -TypeDefinition and -MemberDefinition alike write C# to
- # %TEMP% and run csc.exe. Bitdefender blocks the resulting DLL, because a
- # windowless PowerShell spawned by a GUI binary, running a compiler and writing
- # executable content to %TEMP%, is a dropper's shape whatever the code says. It
- # also failed with CS2001 when %TEMP% was unusable (issue #9140). Reflection emit
- # builds the same interop stubs in memory: no compiler process, no source, no DLL,
- # empty assembly Location. Available on .NET Framework 4 and .NET 5+, so 5.1 and 7
- # take the same path.
+ # %TEMP% and run csc.exe, which security software blocks and which failed outright
+ # with CS2001 when %TEMP% was unusable (issue #9140). Reflection emit builds the
+ # same interop stubs in memory: no compiler process, no source, no DLL, empty
+ # assembly Location. Available on .NET Framework 4 and .NET 5+, so 5.1 and 7 take
+ # the same path. Which product blocked what:
+ # tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
#
# Throws rather than reporting: each caller wants a different answer to "the native
# side is unavailable", and the two cosmetic ones must not print the resolver's
@@ -3321,13 +3321,12 @@ exit 0
# not suppress a ShouldProcess prompt, and a noninteractive host turns it into
# an error that skips shortcut setup entirely.
Unblock-File -LiteralPath $launcherPs1 -Confirm:$false -ErrorAction SilentlyContinue
- # No .vbs launcher is written. A WScript.Shell .vbs that spawns a hidden
- # ExecutionPolicy-Bypass PowerShell is exactly the shape VBS-dropper
- # heuristics score (e.g. Kaspersky HEUR:Trojan.VBS.Agent.gen). The .lnk
- # shortcuts instead point straight at powershell.exe running
- # launch-studio.ps1 with a hidden window (selected below).
+ # No .vbs launcher is written: the .lnk shortcuts point straight at
+ # powershell.exe running launch-studio.ps1 with a hidden window (selected
+ # below), with no script engine in between.
+ # tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
- # Drop any launch-studio.vbs left by a pre-hardening install (AV-flagged shape).
+ # Drop any launch-studio.vbs left by an install that predates that change.
$legacyLauncherVbs = Join-Path $appDir "launch-studio.vbs"
if (Test-Path -LiteralPath $legacyLauncherVbs) {
Remove-Item -LiteralPath $legacyLauncherVbs -Force -ErrorAction SilentlyContinue
@@ -3400,20 +3399,20 @@ exit 0
return
}
- # Gates the heavy refresh: clearing caches on a no-op reinstall looks like a dropper.
+ # Gates the heavy refresh below: on a reinstall that changed nothing, purging
+ # caches and killing a shell process is wasted work.
+ # tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
$firstInstall = -not (
($desktopLink -and (Test-Path -LiteralPath $desktopLink)) -or
($startMenuLink -and (Test-Path -LiteralPath $startMenuLink))
)
- # Launch transport for the shortcuts: powershell.exe runs
- # launch-studio.ps1 with a hidden window. We deliberately avoid a
- # .vbs/WScript.Shell wrapper -- that script-engine shape is what AV
- # VBS-dropper heuristics score (Kaspersky HEUR:Trojan.VBS.Agent.gen).
+ # Launch transport for the shortcuts: powershell.exe runs launch-studio.ps1
+ # with a hidden window, deliberately without a .vbs/WScript.Shell wrapper.
#
- # RemoteSigned, not Bypass: a hidden window beside a bypassed policy is the pair
- # Microsoft's detections key on, and install.rs makes the same call for the app's own
+ # RemoteSigned, not Bypass, and install.rs makes the same call for the app's own
# launch. This launcher is written locally, so RemoteSigned loads it either way.
+ # tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
$powershellForLnk = Join-Path $env:SystemRoot "System32\WindowsPowerShell\v1.0\powershell.exe"
$shortcutTarget = $powershellForLnk
$shortcutArgs = "-NoProfile -WindowStyle Hidden -ExecutionPolicy RemoteSigned -File `"$launcherPs1`""
@@ -4622,8 +4621,9 @@ exit 0
# Fallback for hosts without winget. Same archive, destination and user-PATH
# prepend as astral's install.ps1, but it fetches a data file with a pinned
- # SHA-256 instead of script text run in-process, which is what AMSI and cloud
- # ML scanners score hardest. Bumping the version means bumping all 3 hashes:
+ # SHA-256 instead of running remote script text in-process.
+ # tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
+ # Bumping the version means bumping all 3 hashes:
# curl -sL https://github.com/astral-sh/uv/releases/download/<ver>/uv-<arch>-pc-windows-msvc.zip.sha256
$UvPinnedVersion = "0.12.1"
$UvPinnedAssets = @{
diff --git a/install.sh b/install.sh
index ac53bbeb917..28bc7be533a 100755
--- a/install.sh
+++ b/install.sh
@@ -2178,10 +2178,103 @@ STUB_EOF
done
_css_wsl_ico_win_ps=$(printf '%s' "$_css_wsl_ico_win" | sed "s/'/''/g")
- # Create shortcuts via a temp PowerShell script to avoid escaping issues
- _css_ps1_tmp=$(mktemp /tmp/unsloth-shortcut-XXXXXX.ps1 2>/dev/null) || true
- if [ -n "$_css_ps1_tmp" ]; then
- cat > "$_css_ps1_tmp" << WSLPS1_EOF
+ # Create shortcuts via a temp PowerShell script to avoid escaping issues.
+ #
+ # On the WINDOWS side of the interop, not in WSL's /tmp. wslpath maps a WSL path to a
+ # \\wsl.localhost\<distro>\... UNC path, and PowerShell treats a script on a UNC path as
+ # remote: RemoteSigned refuses an unsigned one, which is the only reason this launch used to
+ # relax the execution policy. A script under the Windows %TEMP% is on a local volume, so it
+ # is MyComputer-zone and RemoteSigned loads it unsigned, and the relaxed policy stops being
+ # necessary. Behaviour is otherwise identical: same generated script, same launch.
+ _css_win_temp=""
+ if command -v wslpath >/dev/null 2>&1 && command -v cmd.exe >/dev/null 2>&1; then
+ # cmd.exe rather than powershell.exe: one fewer interpreter start, and it cannot be the
+ # thing a policy blocks. The trailing CR is cmd's, not ours.
+ #
+ # /d, because without it cmd runs the AutoRun command out of
+ # HKCU\Software\Microsoft\Command Processor before anything else, on the same stdout.
+ # Clink sets one, so Cmder does, and so do plenty of corporate images; its banner would
+ # be glued to the front of the path, wslpath would reject that and the whole shortcut
+ # would be skipped. Those users got a shortcut before this block existed, so leaving
+ # AutoRun enabled would be a regression that only shows up on their machines.
+ #
+ # Last line rather than the whole stream for whatever still prints (an AutoRun invoked
+ # some other way, a login banner), and trailing blanks go because Win32 strips them
+ # from a path while [ -d ] does not. The || is not dead code: this file runs under
+ # set -e, where a failed substitution would end the install.
+ #
+ # Quoted inside cmd, and the quotes stripped back off here. cmd expands %TEMP% BEFORE
+ # it parses metacharacters, so an unquoted `echo %TEMP%` on a profile holding a valid
+ # path character like & ("C:\Users\A&B\AppData\Local\Temp") turns into two commands:
+ # the echo prints a truncated path and the rest is run as a command. & is legal in a
+ # Windows account name, so this is reachable. Inside double quotes it is literal.
+ # Three candidates, most preferred first, each on its own line. %TEMP% can be
+ # redirected onto a share, and a script there is a REMOTE script: RemoteSigned refuses
+ # an unsigned one, so the shortcut would silently stop being created for exactly the
+ # roaming-profile users who had one before. Rather than relax the policy back to Bypass,
+ # fall through to a directory that is local by construction. install.ps1:3419-3432
+ # solves the same problem for the launcher, which cannot move, by relaxing the policy;
+ # this script CAN move, so it does that instead.
+ #
+ # The & separators are ours and deliberate: each value is quoted, so an & inside a value
+ # stays literal and only these three separators split the line.
+ _css_win_temp_list=$(cmd.exe /d /c 'echo "%TEMP%"&echo "%LOCALAPPDATA%\Temp"&echo "%SystemRoot%\Temp"' 2>/dev/null \
+ | tr -d '\r') || _css_win_temp_list=""
+ # Mapped network drives too, not only UNC spellings. Z:\Temp is the same share and the
+ # same remote zone as \\server\share\Temp -- install.ps1:3419-3431 treats
+ # DriveType.Network as remote for exactly this reason -- and a candidate on one would
+ # otherwise be accepted and then refused by RemoteSigned at launch. `net use` lists the
+ # mapped letters; if it cannot be read the set is empty and only the UNC check applies,
+ # which is the previous behaviour rather than a new failure.
+ _css_net_drives=$(cmd.exe /d /c 'net use' 2>/dev/null | tr -d '\r' \
+ | awk '/\\\\/ { for (i = 1; i <= NF; i++) if ($i ~ /^[A-Za-z]:$/) print substr($i, 1, 1) }' \
+ | tr 'a-z' 'A-Z') || _css_net_drives=""
+
+ _css_old_ifs=$IFS
+ IFS='
+'
+ for _css_cand in $_css_win_temp_list; do
+ IFS=$_css_old_ifs
+ # Quotes off FIRST, blanks second. Win32 strips trailing spaces from a path while
+ # [ -d ] does not, so they have to go; but with the closing quote still the last
+ # character there is no trailing blank to find, and trimming first silently did
+ # nothing for a %TEMP% like "C:\Temp ".
+ _css_cand=${_css_cand#\"}
+ _css_cand=${_css_cand%\"}
+ _css_cand=$(printf '%s' "$_css_cand" | sed 's/[[:space:]]*$//')
+ case "$_css_cand" in
+ # Unexpanded (the variable is unset), or a UNC path. A dotted FQDN, a DFS root
+ # and an IP literal all arrive in this same \\server\share form, and all three
+ # are the remote zone.
+ ""|'%'*'%'*|'\\'*) continue ;;
+ esac
+ # Drive letter against the mapped-network set, before anything else looks at it.
+ _css_cand_letter=$(printf '%s' "$_css_cand" | cut -c1 | tr 'a-z' 'A-Z')
+ _css_is_net=0
+ for _css_nd in $_css_net_drives; do
+ [ "$_css_nd" = "$_css_cand_letter" ] && _css_is_net=1 && break
+ done
+ [ "$_css_is_net" = 1 ] && continue
+ _css_cand_unix=$(wslpath -u "$_css_cand" 2>/dev/null) || continue
+ [ -d "$_css_cand_unix" ] || continue
+ _css_win_temp=$_css_cand_unix
+ break
+ done
+ IFS=$_css_old_ifs
+ if [ -n "$_css_win_temp" ] && [ ! -d "$_css_win_temp" ]; then
+ _css_win_temp=""
+ fi
+ fi
+ # No fallback to WSL's /tmp. That would put the script back on a UNC path and need Bypass
+ # again, and this whole branch is best-effort already: the population where %TEMP% cannot be
+ # read through interop is very nearly the population where interop is broken, which lands on
+ # the same "couldn't create the Windows shortcut" notice below.
+ _css_ps1_tmp=""
+ if [ -n "$_css_win_temp" ]; then
+ _css_ps1_tmp=$(mktemp "$_css_win_temp/unsloth-shortcut-XXXXXX.ps1" 2>/dev/null) || _css_ps1_tmp=""
+ fi
+ if [ -n "$_css_sc_target" ]; then
+ _css_ps1_body=$(cat << WSLPS1_EOF
\$WshShell = New-Object -ComObject WScript.Shell
\$targetExe = (Get-Command '$_css_sc_target' -ErrorAction SilentlyContinue).Source
if (-not \$targetExe) { exit 1 }
@@ -2245,14 +2338,56 @@ if (\$hasIcon) {
# immediately instead of a stale/blank (generic) icon. The reliable fix (no
# explorer restart) is a PER-ITEM SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATHW,
# <lnk>) -- the global SHCNE_ASSOCCHANGED alone does not recover a stale item.
+#
+# Emitted, not compiled. Add-Type -MemberDefinition writes C# to %TEMP% and runs
+# csc.exe on Windows PowerShell 5.1, and security software blocks the DLL that comes
+# out. install.ps1 carries the same reflection-emit form for the same reason; this
+# copy was missed when that one changed. Reflection emit builds the identical stub in
+# memory: no compiler process, no source on disk, no DLL.
+# Which product blocked what: tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
try {
- Add-Type -Namespace UnslothShell -Name IconRefresh -MemberDefinition '[System.Runtime.InteropServices.DllImport("shell32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] public static extern void SHChangeNotify(int e, uint f, string a, System.IntPtr b);' -ErrorAction SilentlyContinue
- foreach (\$p in \$created) { try { [UnslothShell.IconRefresh]::SHChangeNotify(0x00002000, 0x0005, \$p, [System.IntPtr]::Zero) } catch {} }
- [UnslothShell.IconRefresh]::SHChangeNotify(0x08000000, 0, \$null, [System.IntPtr]::Zero)
+ \$refreshType = 'UnslothShellIconRefresh' -as [type]
+ if (-not \$refreshType) {
+ \$asmName = New-Object System.Reflection.AssemblyName 'UnslothShellIconRefreshAsm'
+ # Both spellings, matching New-StudioDynamicAssembly in install.ps1. The static
+ # AssemblyBuilder::DefineDynamicAssembly is documented for .NET Framework 4.5 through
+ # 4.8.1, so the 5.1 host this script is launched under should take the first branch; it
+ # is tried rather than assumed because the outer catch here is empty, so guessing wrong
+ # costs the icon refresh with nothing printed. AppDomain.CurrentDomain is the .NET
+ # Framework spelling and is absent on .NET Core, so it is the fallback and not the lead.
+ \$access = [System.Reflection.Emit.AssemblyBuilderAccess]::Run
+ try {
+ \$asm = [System.Reflection.Emit.AssemblyBuilder]::DefineDynamicAssembly(\$asmName, \$access)
+ } catch [System.Management.Automation.MethodException] {
+ \$asm = [AppDomain]::CurrentDomain.DefineDynamicAssembly(\$asmName, \$access)
+ } catch [System.Management.Automation.RuntimeException] {
+ # Some hosts surface a missing static as RuntimeException rather than
+ # MethodException. Both mean "no such method here", and a real emit failure throws
+ # from the AppDomain call too, so a genuine refusal still reaches the outer catch.
+ \$asm = [AppDomain]::CurrentDomain.DefineDynamicAssembly(\$asmName, \$access)
+ }
+ \$module = \$asm.DefineDynamicModule('UnslothShellIconRefreshMod')
+ \$typeBuilder = \$module.DefineType('UnslothShellIconRefresh',
+ 'Public, Class, AutoClass, AnsiClass, BeforeFieldInit')
+ \$method = \$typeBuilder.DefinePInvokeMethod(
+ 'SHChangeNotify', 'shell32.dll', 'SHChangeNotify',
+ 'Public, Static, PinvokeImpl',
+ [System.Reflection.CallingConventions]::Standard,
+ [System.Void],
+ @([int], [uint32], [string], [IntPtr]),
+ [System.Runtime.InteropServices.CallingConvention]::Winapi,
+ [System.Runtime.InteropServices.CharSet]::Unicode)
+ \$method.SetImplementationFlags(
+ \$method.GetMethodImplementationFlags() -bor [System.Reflection.MethodImplAttributes]::PreserveSig)
+ \$refreshType = \$typeBuilder.CreateType()
+ }
+ foreach (\$p in \$created) { try { \$refreshType::SHChangeNotify(0x00002000, 0x0005, \$p, [System.IntPtr]::Zero) } catch {} }
+ \$refreshType::SHChangeNotify(0x08000000, 0, \$null, [System.IntPtr]::Zero)
} catch {}
# Heavier on-disk icon-cache clear + StartMenuExperienceHost tile rebuild
# (preserve start2.bin) only on first install or a real icon change, so a no-op
-# WSL reinstall does not run a dropper-like clear-cache + kill cluster each time.
+# WSL reinstall does not purge caches and kill a shell process for nothing.
+# See tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
if (\$created.Count -gt 0 -and (\$firstShortcut -or \$iconChanged)) {
try { & "\$env:SystemRoot\System32\ie4uinit.exe" -ClearIconCache } catch {}
try { & "\$env:SystemRoot\System32\ie4uinit.exe" -show } catch {}
@@ -2266,13 +2401,32 @@ if (\$created.Count -gt 0 -and (\$firstShortcut -or \$iconChanged)) {
} catch {}
}
WSLPS1_EOF
+)
- # Convert WSL path to Windows path for powershell.exe
- _css_ps1_win=$(wslpath -w "$_css_ps1_tmp" 2>/dev/null)
- if [ -n "$_css_ps1_win" ]; then
- powershell.exe -NoProfile -ExecutionPolicy Bypass -File "$_css_ps1_win" >/dev/null 2>&1 && _css_created=1
+ if [ -n "$_css_ps1_tmp" ]; then
+ printf '%s\n' "$_css_ps1_body" > "$_css_ps1_tmp"
+ # Convert WSL path to Windows path for powershell.exe
+ _css_ps1_win=$(wslpath -w "$_css_ps1_tmp" 2>/dev/null)
+ if [ -n "$_css_ps1_win" ]; then
+ # RemoteSigned, not Bypass: the script above was written to the Windows %TEMP%
+ # on a local volume, so it is not a remote script and RemoteSigned loads it
+ # unsigned. Pairing a relaxed policy with a PowerShell launch is a scored shape,
+ # and this one was buying nothing once the path stopped being a UNC path.
+ powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File "$_css_ps1_win" >/dev/null 2>&1 && _css_created=1
+ fi
+ rm -f "$_css_ps1_tmp"
+ else
+ # No Windows directory is reachable as a Linux path. That is a real configuration --
+ # [automount] enabled=false leaves interop working while exposing no drive -- and
+ # before the move off /tmp those users still got a shortcut, so losing it here would
+ # be a regression rather than a gap. Hand the script to powershell on STDIN instead:
+ # there is no file, so there is no zone and no execution policy to satisfy (policy
+ # applies to -File, not to -Command), and nothing has to be mounted.
+ #
+ # Our own pipe, not the installer's: `curl | sh` leaves this script's stdin pointing
+ # at the download, and powershell reading that would drink the rest of it (#7548).
+ printf '%s\n' "$_css_ps1_body" | powershell.exe -NoProfile -Command - >/dev/null 2>&1 && _css_created=1
fi
- rm -f "$_css_ps1_tmp"
fi
if [ "$_css_created" -ne 1 ]; then
substep "Couldn't create the Windows shortcut (WSL interop may be disabled)." "$C_WARN"
@@ -3063,7 +3217,7 @@ if ! command -v uv >/dev/null 2>&1 || ! _uv_version_ok uv; then
_uv_refreshed=true
# download() exits the shell outright when neither curl nor wget is present, which an `if` cannot catch, so probe first: a minimal image with uv copied in but no downloader must keep the install it had before the floor moved.
if command -v curl >/dev/null 2>&1 || command -v wget >/dev/null 2>&1; then
- # Pinned release first: a digest-checked data file scores far lower than download-run-delete, which is the literal shape of a dropper.
+ # Pinned release first: fetch a digest-checked data file rather than download-run-delete a remote script. See tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
if _uv_install_pinned; then
:
else
diff --git a/scripts/uninstall.ps1 b/scripts/uninstall.ps1
index f7f06427cd3..fd9c0e41fec 100644
--- a/scripts/uninstall.ps1
+++ b/scripts/uninstall.ps1
@@ -4,8 +4,9 @@
# Unsloth Studio uninstaller for Windows PowerShell. Run -Help for details.
# Custom roots (UNSLOTH_STUDIO_HOME / STUDIO_HOME) come from share\studio.conf.
#
-# Usage: run -Help. The web one-liner is in that help text and is not repeated here, since
-# AMSI scans this file in full before any of it runs and nothing reads the header.
+# Usage: run -Help. The web one-liner is in that help text and is not repeated here: nothing
+# reads this header from inside the script, and the whole file is scanned before any of it runs.
+# Why several things here are written the long way: tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
function Uninstall-UnslothStudio {
$ErrorActionPreference = "Continue"
@@ -29,7 +30,11 @@ Unsloth Studio uninstaller (Windows PowerShell).
Usage:
irm https://raw.githubusercontent.com/unslothai/unsloth/main/scripts/uninstall.ps1 | iex
- Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass; .\scripts\uninstall.ps1
+ Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned; .\scripts\uninstall.ps1
+
+RemoteSigned is enough for the second form: a git clone carries no mark of the
+web, so this script loads unsigned. From a downloaded zip it does carry one, so
+clear it first with: Unblock-File .\scripts\uninstall.ps1
Stops running Unsloth Studio servers, then removes the install dir, launcher
data, CLI shim, desktop and Start Menu shortcuts, the user PATH entry and the
diff --git a/studio/setup.bat b/studio/setup.bat
index 72d1acb141d..56417e56085 100644
--- a/studio/setup.bat
+++ b/studio/setup.bat
@@ -2,4 +2,41 @@
REM SPDX-License-Identifier: AGPL-3.0-only
REM Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-powershell -ExecutionPolicy Bypass -File "%~dp0setup.ps1" %*
+rem RemoteSigned, not Bypass. RemoteSigned refuses an unsigned script only in the Internet or
+rem Untrusted zone, and setup.ps1 normally sits beside this file inside an installed package, so it
+rem is MyComputer-zone and loads unsigned either way. Pairing a relaxed policy with a PowerShell
+rem launch is a token endpoint products score, and it was buying nothing in that case. Which product
+rem scored what: tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD).
+rem
+rem Two populations could regress, and both are handled below rather than assumed away.
+rem
+rem 1. A package unzipped from a download, where setup.ps1 carries a mark of the web that
+rem RemoteSigned honours and Bypass ignored. Unblock-File clears it. Execution policy governs
+rem script FILES and not -Command, so that call runs whatever the machine policy is, and clearing
+rem an absent stream is a no-op that writes nothing.
+rem
+rem 2. setup.ps1 living on a share. Execution policy is judged on the script file's ZONE, and a
+rem dotted-FQDN UNC (\\corp.example.com\...), a DFS path or an IP-literal share lands in the
+rem Internet zone -- where RemoteSigned refuses an unsigned script and Unblock-File cannot save
+rem it, because with no Zone.Identifier stream present the PATH decides and there is nothing to
+rem clear. install.ps1:3405-3421 already steps down to Bypass for exactly this case when it
+rem writes the Start Menu shortcut; the probe below reuses that logic rather than inventing a
+rem second answer. A plain \\server\share and a mapped drive are Intranet and stay RemoteSigned.
+rem
+rem The probe also does the Unblock-File, so this costs one process rather than two.
+rem
+rem The path travels in an environment variable rather than being interpolated into the command
+rem string, so an apostrophe in the install path cannot break the quoting.
+rem
+rem -NoProfile on the probe only, and deliberately NOT on the launch below: setup.ps1 is run with
+rem profiles loaded on purpose, and tests/studio/test_amd_venv_repair_loop.ps1 drives a profile that
+rem sets Set-StrictMode against it.
+setlocal
+set "UNSLOTH_SETUP_SCRIPT=%~dp0setup.ps1"
+set "UNSLOTH_SETUP_POLICY=RemoteSigned"
+for /f "usebackq delims=" %%P in (`powershell -NoProfile -Command "$p = $env:UNSLOTH_SETUP_SCRIPT; Unblock-File -LiteralPath $p -Confirm:$false -ErrorAction SilentlyContinue; $remote = $p -like '\\*'; if (-not $remote) { try { $remote = ([System.IO.DriveInfo]::new([System.IO.Path]::GetPathRoot($p))).DriveType -eq 'Network' } catch {} }; if ($remote) { 'Bypass' } else { 'RemoteSigned' }"`) do set "UNSLOTH_SETUP_POLICY=%%P"
+
+rem Defaulting to RemoteSigned above means a probe that fails to run leaves the tightened policy in
+rem place rather than silently restoring the relaxed one.
+powershell -ExecutionPolicy %UNSLOTH_SETUP_POLICY% -File "%UNSLOTH_SETUP_SCRIPT%" %*
+exit /b %ERRORLEVEL%
diff --git a/studio/setup.ps1 b/studio/setup.ps1
index bf954f5fc45..b7bbb19a7e4 100644
--- a/studio/setup.ps1
+++ b/studio/setup.ps1
@@ -1823,10 +1823,10 @@ $Rule = [string]::new([char]0x2500, 52)
# Native declarations are emitted, never compiled. Add-Type on Windows PowerShell 5.1 has no
# in-process compiler: -TypeDefinition and -MemberDefinition alike write C# to %TEMP% and run
-# csc.exe, and behavioural antivirus blocks the result, because a windowless PowerShell launching
-# a compiler and writing executable content to %TEMP% is a dropper's shape whatever the code says.
-# Reflection emit builds the same stub in memory: no compiler process, no source, no DLL, empty
-# assembly Location. install.ps1 carries the same helper for the same reason.
+# csc.exe, and security software blocks the result. Reflection emit builds the same stub in
+# memory: no compiler process, no source, no DLL, empty assembly Location. install.ps1 carries
+# the same helper for the same reason, and which product blocked what is recorded in
+# tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
# See install.ps1: App Control's Dynamic Code Security always blocks loading unsigned
# System.Reflection.Emit assemblies by usually stopping or crashing the parent rather than
# raising, so this has to be a gate and not a catch.
@@ -4844,8 +4844,9 @@ function Assert-VenvActivated {
# Mirrors install.ps1's Install-UvFromRelease: same archive, destination priority and user-PATH
# prepend as astral's installer, but it fetches a data file with a pinned SHA-256 instead of
-# running remote script text in-process, which is what AMSI scores hardest. Bumping the version
-# means bumping all 3 hashes:
+# running remote script text in-process.
+# tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
+# Bumping the version means bumping all 3 hashes:
# curl -sL https://github.com/astral-sh/uv/releases/download/<ver>/uv-<arch>-pc-windows-msvc.zip.sha256
$UvPinnedVersion = "0.12.1"
$UvPinnedAssets = @{
diff --git a/studio/setup.sh b/studio/setup.sh
index 54d56bd5849..8bfb1a0abfd 100755
--- a/studio/setup.sh
+++ b/studio/setup.sh
@@ -1839,6 +1839,7 @@ _setup_http_get_timed() {
# ── uv from a pinned release ──
# Same archive and destination as astral's installer, but it fetches a data file with a
# pinned SHA-256 instead of piping remote script text into a shell. Mirrors install.sh.
+# See tests/studio/test_installer_av_shapes.py (AV_SHAPES_RECORD)
# Bumping the version means bumping every hash:
# curl -sL https://github.com/astral-sh/uv/releases/download/<ver>/<asset>.sha256
#
diff --git a/tests/sh/test_wsl_shortcut_script_location.sh b/tests/sh/test_wsl_shortcut_script_location.sh
new file mode 100755
index 00000000000..84a1b7ba8a8
--- /dev/null
+++ b/tests/sh/test_wsl_shortcut_script_location.sh
@@ -0,0 +1,385 @@
+#!/bin/bash
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+#
+# install.sh writes a PowerShell script and runs it on the Windows side to create the WSL shortcut.
+# Where that script lands decides whether the launch needs a relaxed execution policy: wslpath maps
+# a WSL path to a \\wsl.localhost\<distro>\... UNC path, which PowerShell treats as a remote script
+# and RemoteSigned refuses unsigned, so the launch used to relax the policy instead. Under the
+# Windows %TEMP% the script is on a local volume, RemoteSigned loads it, and the relaxed policy is
+# unnecessary.
+#
+# Exercises the real resolution block out of install.sh rather than a copy, with cmd.exe and wslpath
+# stubbed, so the three outcomes that matter are covered on any host: resolved, unresolvable, and
+# resolved-but-bogus. The static half then pins what the file must and must not contain.
+set -e
+
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+. "$SCRIPT_DIR/_harness.sh"
+INSTALL_SH="$SCRIPT_DIR/../../install.sh"
+
+# From the first line of the resolution to the mktemp that consumes it. Tracking install.sh this way
+# means a refactor that drops the guard fails here instead of silently going untested.
+BLOCK=$(awk '
+ /^ _css_win_temp=""$/ { grab = 1 }
+ grab { print }
+ grab && /_css_ps1_tmp=\$\(mktemp "\$_css_win_temp\/unsloth-shortcut-XXXXXX\.ps1"/ { print " fi"; exit }
+' "$INSTALL_SH")
+
+echo "=== test_wsl_shortcut_script_location ==="
+
+# Extraction failure is a FAIL, not an early exit, so the static half below still runs and the
+# output names every problem at once instead of only the first. It is still fail-closed: FAIL is
+# non-zero at the end, so a suite that cannot find what it measures never reports a pass.
+HAVE_BLOCK=1
+if ! printf '%s' "$BLOCK" | grep -q '_css_ps1_tmp=\$(mktemp'; then
+ HAVE_BLOCK=0
+ bad "could not extract the Windows-temp resolution block from install.sh.
+ Expected a block starting with ' _css_win_temp=\"\"' and ending at the mktemp of
+ \"\$_css_win_temp/unsloth-shortcut-XXXXXX.ps1\". Either that block was renamed or removed, or
+ install.sh is back to writing the generated shortcut script into WSL's /tmp, which wslpath
+ turns into a \\\\wsl.localhost UNC path that RemoteSigned refuses to load."
+fi
+
+STUBS=$(mktemp -d)
+WINTEMP=$(mktemp -d)
+cleanup() { rm -rf "$STUBS" "$WINTEMP"; }
+trap cleanup EXIT
+
+# wslpath -u <windows path> -> our fake mount point.
+#
+# INPUT-SENSITIVE, and that is the whole point of the stub. A wslpath that returns the fake
+# Windows temp whatever it is handed cannot tell "install.sh read the path correctly" from
+# "install.sh read a banner and a path glued together", so every case below about WHAT cmd.exe
+# printed would pass against a block that got it wrong. Only the exact string maps; anything
+# else maps somewhere that does not exist, which is what the real wslpath does with a string
+# that is not a path.
+# $1 = the only string that maps, $2 = where it maps to.
+make_wslpath_stub() {
+ cat > "$STUBS/wslpath" <<STUB
+#!/bin/sh
+[ "\$1" = "-u" ] || exit 1
+if [ "\$2" = '$1' ]; then printf '%s' '$2'; else printf '%s' '$2/not-what-cmd-printed'; fi
+STUB
+ chmod +x "$STUBS/wslpath"
+}
+
+# $1 is what `echo %TEMP%` prints, and is also the only string wslpath will map. $2, when given,
+# is an AutoRun banner that precedes it on the SAME stdout unless cmd.exe is passed /d.
+make_cmd_stub() {
+ cat > "$STUBS/cmd.exe" <<STUB
+#!/bin/sh
+# Real cmd.exe emits CRLF; the block strips it, so emit one here or the test would not cover that.
+# It also runs the AutoRun command from HKCU\\Software\\Microsoft\\Command Processor first, on this
+# same stdout, unless /d is passed. Clink sets one, so Cmder does, and so do corporate images.
+_autorun=1
+for _a in "\$@"; do [ "\$_a" = "/d" ] && _autorun=0; done
+[ "\$_autorun" = 1 ] && [ -n '${2:-}' ] && printf '%s\r\n' '${2:-}'
+printf '%s\r\n' '$1'
+STUB
+ chmod +x "$STUBS/cmd.exe"
+ # Trailing blanks are not part of the path: Win32 strips them, so the block has to.
+ make_wslpath_stub "$(printf '%s' "$1" | sed 's/[[:space:]]*$//')" "$WINTEMP"
+}
+
+run_block() {
+ _css_win_temp=""
+ _css_ps1_tmp=""
+ ( PATH="$STUBS:$PATH"; eval "$BLOCK"; printf '%s\n%s\n' "$_css_win_temp" "$_css_ps1_tmp" )
+}
+
+if [ "$HAVE_BLOCK" -eq 1 ]; then
+
+# 1. Interop works: the script is created under the Windows temp, not in WSL's /tmp.
+make_cmd_stub 'C:\Users\ci\AppData\Local\Temp'
+OUT=$(run_block)
+RESOLVED=$(printf '%s' "$OUT" | sed -n 1p)
+SCRIPT=$(printf '%s' "$OUT" | sed -n 2p)
+assert_eq "resolved %TEMP% maps through wslpath" "$WINTEMP" "$RESOLVED"
+[ -n "$SCRIPT" ] && ok "a script path was allocated" || bad "no script path was allocated"
+case "$SCRIPT" in
+ "$WINTEMP"/unsloth-shortcut-*.ps1) ok "script lives under the Windows temp" ;;
+ *) bad "script is not under the Windows temp: $SCRIPT" ;;
+esac
+# Structural, not a path-prefix check. The first version of this asserted the script path did not
+# start with /tmp, which is a proxy for "not in WSL's own temp" -- and a broken one: the harness
+# creates its fake Windows temp with mktemp -d, so on any machine with TMPDIR=/tmp (which is every
+# normal CI runner) the stub's own directory matched the pattern and the test failed while install.sh
+# was behaving correctly. It passed locally only because this workspace sets TMPDIR elsewhere.
+#
+# What actually has to hold is that the only place the block can allocate the script is the resolved
+# Windows temp, with no second mktemp anywhere to fall back to. That is true or false regardless of
+# where anyone's temp directory happens to live.
+ALLOC=$(printf '%s' "$BLOCK" | grep -c 'mktemp' || true)
+if [ "$ALLOC" -eq 1 ]; then
+ ok "exactly one mktemp in the block, so there is no second path to fall back to"
+else
+ bad "the block contains $ALLOC mktemp calls; a second one is a fallback that can land in WSL's /tmp, which wslpath turns into a \\\\wsl.localhost UNC path that RemoteSigned refuses"
+fi
+assert_contains "the script is allocated inside the resolved Windows temp" \
+ "$(printf '%s' "$BLOCK" | grep 'mktemp')" '$_css_win_temp/'
+rm -f "$WINTEMP"/unsloth-shortcut-*.ps1
+
+# 2. cmd.exe absent (interop disabled): nothing is allocated, so the caller takes its existing
+# "couldn't create the Windows shortcut" notice. It must NOT fall back to a WSL path, because
+# that is the case that needed the relaxed policy.
+rm -f "$STUBS/cmd.exe"
+OUT=$(run_block)
+assert_eq "no interop: %TEMP% unresolved" "" "$(printf '%s' "$OUT" | sed -n 1p)"
+assert_eq "no interop: no script allocated, so no UNC fallback" "" "$(printf '%s' "$OUT" | sed -n 2p)"
+
+# 3. Interop present but %TEMP% unset: cmd.exe echoes the name back unexpanded. Reading that as a
+# directory would try to mktemp inside a literal "%TEMP%".
+make_cmd_stub '%TEMP%'
+OUT=$(run_block)
+assert_eq "unexpanded %TEMP% is rejected" "" "$(printf '%s' "$OUT" | sed -n 1p)"
+assert_eq "unexpanded %TEMP% allocates nothing" "" "$(printf '%s' "$OUT" | sed -n 2p)"
+
+# 4. cmd.exe prints a path that does not exist: wslpath succeeds, the directory does not.
+make_cmd_stub 'C:\Users\ci\AppData\Local\Temp'
+make_wslpath_stub 'C:\Users\ci\AppData\Local\Temp' "$WINTEMP/definitely-absent"
+OUT=$(run_block)
+assert_eq "a %TEMP% that is not a directory is rejected" "" "$(printf '%s' "$OUT" | sed -n 1p)"
+
+# 5. cmd.exe runs an AutoRun command first, on the same stdout.
+#
+# HKCU\Software\Microsoft\Command Processor\AutoRun runs before anything else unless /d is
+# passed, and `cmd /c` is not exempt. Clink sets one, so Cmder does, and so do plenty of
+# corporate images. Reading the whole stream glues the banner to the front of the path, which
+# wslpath then rejects, and every one of those users silently loses the shortcut they used to
+# get. The fix is /d, not parsing harder, so the stub only suppresses the banner for /d.
+make_cmd_stub 'C:\Users\ci\AppData\Local\Temp' 'clink v1.6.20 is available.'
+OUT=$(run_block)
+assert_eq "an AutoRun banner does not corrupt %TEMP%" "$WINTEMP" "$(printf '%s' "$OUT" | sed -n 1p)"
+[ -n "$(printf '%s' "$OUT" | sed -n 2p)" ] \
+ && ok "an AutoRun banner still allocates the script" \
+ || bad "an AutoRun banner cost the user their shortcut"
+rm -f "$WINTEMP"/unsloth-shortcut-*.ps1
+
+# 6. Trailing blanks on the value. Win32 strips them from a path, [ -d ] does not, so a %TEMP%
+# set with one would otherwise resolve to a directory that appears not to exist.
+make_cmd_stub 'C:\Users\ci\AppData\Local\Temp '
+OUT=$(run_block)
+assert_eq "trailing blanks are trimmed off %TEMP%" "$WINTEMP" "$(printf '%s' "$OUT" | sed -n 1p)"
+rm -f "$WINTEMP"/unsloth-shortcut-*.ps1
+
+else
+ echo " SKIP: the four resolution cases need the block above; see the failure printed there"
+fi
+
+# --- Static half: what the shipped file may and may not contain. -------------------------------
+
+# /d on the cmd.exe that reads %TEMP%, so no AutoRun command gets to print onto the stdout this
+# block is reading. Static as well as behavioural: the case above proves the block survives a
+# banner, this proves it survives it the cheap way rather than by out-parsing whatever prints.
+assert_contains "the %TEMP% probe disables AutoRun" \
+ "$(grep 'cmd.exe .*echo .*%TEMP%' "$INSTALL_SH")" "cmd.exe /d /c"
+
+# The value is expanded inside quotes. cmd expands before it parses, so an unquoted %TEMP% holding
+# an & is split into two commands; the behavioural case above proves the block survives that, and
+# this pins the mechanism so a rewrite cannot quietly drop the quoting and still pass on paths that
+# happen to contain no metacharacter.
+assert_contains "the %TEMP% probe quotes the value inside cmd" \
+ "$(grep 'cmd.exe .*echo .*%TEMP%' "$INSTALL_SH")" '"%TEMP%"'
+
+LAUNCH=$(grep -n 'powershell.exe -NoProfile -ExecutionPolicy .* -File "\$_css_ps1_win"' "$INSTALL_SH" || true)
+assert_contains "the shortcut launch uses RemoteSigned" "$LAUNCH" "-ExecutionPolicy RemoteSigned"
+assert_not_contains "the shortcut launch does not relax the policy" "$LAUNCH" "-ExecutionPolicy Bypass"
+
+# Anywhere in the file, not just this launch: a relaxed policy is a token a classifier matches
+# whether or not the line runs.
+POLICY_HITS=$(grep -c -- '-ExecutionPolicy Bypass' "$INSTALL_SH" || true)
+assert_eq "install.sh relaxes the execution policy nowhere" "0" "$POLICY_HITS"
+
+# The generated script declares SHChangeNotify by emitting it. Add-Type -MemberDefinition writes C#
+# to %TEMP% and runs csc.exe on PowerShell 5.1, and behavioural antivirus blocks the DLL that comes
+# out; #10540 removed that from install.ps1 and this copy was missed for a release.
+ADDTYPE_HITS=$(grep -c '^[[:space:]]*Add-Type' "$INSTALL_SH" || true)
+assert_eq "install.sh compiles no C#" "0" "$ADDTYPE_HITS"
+assert_contains "SHChangeNotify is emitted instead" "$(cat "$INSTALL_SH")" "DefinePInvokeMethod"
+
+# Both notifications survive. The per-item one is the only thing that recovers a rewritten
+# same-name .lnk; the global broadcast alone does not, so losing it would show as a stale icon.
+BODY=$(cat "$INSTALL_SH")
+assert_contains "per-item SHCNE_UPDATEITEM refresh kept" "$BODY" "SHChangeNotify(0x00002000, 0x0005"
+assert_contains "global SHCNE_ASSOCCHANGED refresh kept" "$BODY" "SHChangeNotify(0x08000000, 0"
+
+# A cmd.exe stub that reproduces the ORDER cmd works in: %VAR% is expanded first, and the result is
+# then parsed for metacharacters. That is what makes an & inside the value dangerous, and the
+# earlier stub could not show it because it echoed a fixed string without ever parsing a command.
+# & is legal in a Windows account name, so "C:\Users\A&B\AppData\Local\Temp" is a real path.
+make_parsing_cmd_stub() {
+ cat > "$STUBS/cmd.exe" <<STUB
+#!/bin/sh
+# Everything after /c, joined with spaces. cmd receives a command LINE, so the old unquoted
+# `echo %TEMP%` form arrives as one line too; joining is what lets this stub judge both forms.
+_cmd=""
+_take=0
+for _a in "\$@"; do
+ if [ "\$_take" = 1 ]; then
+ if [ -z "\$_cmd" ]; then _cmd="\$_a"; else _cmd="\$_cmd \$_a"; fi
+ fi
+ [ "\$_a" = "/c" ] && _take=1
+done
+# Expansion happens BEFORE parsing, exactly as cmd does it.
+_cmd=\$(printf '%s' "\$_cmd" | sed "s/%TEMP%/\$(printf '%s' '$1' | sed 's/[&/\\\\]/\\\\&/g')/g")
+# Now parse. An & inside double quotes is literal; outside, it separates commands.
+_q=0; _first=""; _rest=""; _i=1
+while [ "\$_i" -le "\${#_cmd}" ]; do
+ _ch=\$(printf '%s' "\$_cmd" | cut -c"\$_i")
+ if [ "\$_ch" = '"' ]; then _q=\$((1-_q))
+ elif [ "\$_ch" = "&" ] && [ "\$_q" = 0 ]; then
+ _rest=\$(printf '%s' "\$_cmd" | cut -c\$((_i+1))-); break
+ fi
+ _first="\$_first\$_ch"; _i=\$((_i+1))
+done
+# cmd's echo prints its argument VERBATIM, quotes included: `echo "x"` outputs "x" with the
+# quotes. Stripping them here would hide whether install.sh strips them itself, which is the whole
+# point of the quoting, and it hid the order in which install.sh trims trailing blanks.
+_out=\$(printf '%s' "\$_first" | sed 's/^echo //')
+printf '%s\r\n' "\$_out"
+[ -n "\$_rest" ] && printf "'%s' is not recognized as an internal or external command\r\n" "\$_rest"
+exit 0
+STUB
+ chmod +x "$STUBS/cmd.exe"
+ # Trimmed, like the other stub: Win32 strips trailing blanks from a path, so the block must hand
+ # wslpath the trimmed value and this has to agree or the case could never pass.
+ make_wslpath_stub "$(printf '%s' "$1" | sed 's/[[:space:]]*$//')" "$WINTEMP"
+}
+
+# A plain path through the faithful stub: the quoting must not corrupt the ordinary case.
+make_parsing_cmd_stub 'C:\Users\ci\AppData\Local\Temp'
+OUT=$(run_block)
+assert_eq "quoted expansion still resolves an ordinary path" "$WINTEMP" "$(printf '%s' "$OUT" | sed -n 1p)"
+rm -f "$WINTEMP"/unsloth-shortcut-*.ps1
+
+# The case this guards: an & in the Windows temp path. Unquoted, cmd splits the line, the echo
+# prints a truncated path, wslpath is handed something that is not the temp directory and the whole
+# shortcut is silently skipped.
+make_parsing_cmd_stub 'C:\Users\A&B\AppData\Local\Temp'
+OUT=$(run_block)
+assert_eq "an ampersand in %TEMP% survives expansion" "$WINTEMP" "$(printf '%s' "$OUT" | sed -n 1p)"
+SCRIPT=$(printf '%s' "$OUT" | sed -n 2p)
+[ -n "$SCRIPT" ] && ok "a script path was allocated despite the ampersand" || bad "the ampersand killed shortcut creation"
+rm -f "$WINTEMP"/unsloth-shortcut-*.ps1
+
+
+# Trailing blanks THROUGH the quoting path. The older trailing-blank case used the fixed-string
+# stub, which emits the value unquoted, so it never saw that the trim now runs against a string
+# whose last character is a closing quote. Win32 strips trailing spaces from a path and [ -d ] does
+# not, so a %TEMP% of "C:\Temp " has to come back as C:\Temp or the directory check clears it and
+# shortcut creation is skipped.
+make_parsing_cmd_stub 'C:\Users\ci\AppData\Local\Temp '
+OUT=$(run_block)
+assert_eq "trailing blanks are trimmed after the quotes come off" "$WINTEMP" "$(printf '%s' "$OUT" | sed -n 1p)"
+rm -f "$WINTEMP"/unsloth-shortcut-*.ps1
+
+# %TEMP% redirected onto a share. A script written there is a REMOTE script and RemoteSigned
+# refuses an unsigned one, so before the fallback these users silently stopped getting a shortcut
+# they used to get. $1 is what %TEMP% expands to, $2 what %LOCALAPPDATA% does.
+make_three_var_cmd_stub() {
+ # Prints the three candidates in the order the block asks for them, each wrapped in quotes the
+ # way cmd's echo emits them. The expand-then-parse behaviour is covered by the other stub; what
+ # this one exercises is which candidate install.sh SELECTS.
+ cat > "$STUBS/cmd.exe" <<STUB
+#!/bin/sh
+printf '%s\r\n' '"$1"'
+printf '%s\r\n' '"$2\\Temp"'
+printf '%s\r\n' '"C:\\Windows\\Temp"'
+exit 0
+STUB
+ chmod +x "$STUBS/cmd.exe"
+ make_wslpath_stub "$2\\Temp" "$WINTEMP"
+}
+
+# The regression this guards: %TEMP% on a UNC share must not kill shortcut creation. The fallback
+# has to land on the local %LOCALAPPDATA%\Temp instead, and it must still be RemoteSigned.
+make_three_var_cmd_stub '\\fileserver.corp.example.com\profiles$\ci\Temp' 'C:\Users\ci\AppData\Local'
+OUT=$(run_block)
+assert_eq "a UNC %TEMP% falls through to a local directory" "$WINTEMP" "$(printf '%s' "$OUT" | sed -n 1p)"
+SCRIPT=$(printf '%s' "$OUT" | sed -n 2p)
+[ -n "$SCRIPT" ] && ok "a script path was allocated despite a redirected %TEMP%" || bad "a UNC %TEMP% silently killed shortcut creation"
+rm -f "$WINTEMP"/unsloth-shortcut-*.ps1
+
+# And an IP-literal UNC, which is the same zone and a different spelling.
+make_three_var_cmd_stub '\\10.1.2.3\profiles\ci\Temp' 'C:\Users\ci\AppData\Local'
+OUT=$(run_block)
+assert_eq "an IP-literal UNC %TEMP% also falls through" "$WINTEMP" "$(printf '%s' "$OUT" | sed -n 1p)"
+rm -f "$WINTEMP"/unsloth-shortcut-*.ps1
+
+# A mapped network drive is the same share and the same remote zone as its UNC spelling, so a
+# %TEMP% on Z: has to fall through too. $1 = %TEMP%, $2 = %LOCALAPPDATA%, $3 = the `net use` output.
+make_mapped_drive_stub() {
+ cat > "$STUBS/cmd.exe" <<STUB
+#!/bin/sh
+for _a in "\$@"; do
+ case "\$_a" in
+ *"net use"*) printf '%s\r\n' '$3'; exit 0 ;;
+ esac
+done
+printf '%s\r\n' '"$1"'
+printf '%s\r\n' '"$2\\Temp"'
+printf '%s\r\n' '"C:\\Windows\\Temp"'
+exit 0
+STUB
+ chmod +x "$STUBS/cmd.exe"
+ # Maps BOTH the mapped-drive path and the local one to real directories, so the mapped drive is
+ # genuinely usable. Otherwise the old code would fall through for the wrong reason -- an
+ # unmappable path -- and the case could never show the difference.
+ cat > "$STUBS/wslpath" <<STUB
+#!/bin/sh
+[ "\$1" = "-u" ] || exit 1
+case "\$2" in
+ '$1') printf '%s' '$NETTEMP' ;;
+ "$2\\Temp") printf '%s' '$WINTEMP' ;;
+ *) printf '%s' '$WINTEMP/not-what-cmd-printed' ;;
+esac
+STUB
+ chmod +x "$STUBS/wslpath"
+}
+
+NETTEMP=$(mktemp -d)
+trap 'rm -rf "$STUBS" "$WINTEMP" "$NETTEMP"' EXIT
+make_mapped_drive_stub 'Z:\Temp' 'C:\Users\ci\AppData\Local' 'OK Z: \\fileserver\profiles Microsoft Windows Network'
+OUT=$(run_block)
+assert_eq "a mapped network drive %TEMP% falls through to a local directory" "$WINTEMP" "$(printf '%s' "$OUT" | sed -n 1p)"
+rm -f "$WINTEMP"/unsloth-shortcut-*.ps1
+
+# And a LOCAL drive letter must not be rejected just because some other letter is mapped.
+make_mapped_drive_stub 'C:\Users\ci\AppData\Local\Temp' 'C:\Users\ci\AppData\Local' 'OK Z: \\fileserver\profiles Microsoft Windows Network'
+make_wslpath_stub 'C:\Users\ci\AppData\Local\Temp' "$WINTEMP"
+OUT=$(run_block)
+assert_eq "an unmapped local %TEMP% is still accepted" "$WINTEMP" "$(printf '%s' "$OUT" | sed -n 1p)"
+rm -f "$WINTEMP"/unsloth-shortcut-*.ps1
+
+# ---------------------------------------------------------------------------
+# The launch path, statically. [automount] enabled=false leaves interop working while exposing no
+# Windows drive as a Linux directory, so no candidate can be allocated and the file-based launch
+# cannot run at all. Before the move off /tmp those users still got a shortcut, so silently losing
+# it is a regression rather than a pre-existing gap.
+# ---------------------------------------------------------------------------
+BODY_ALL=$(cat "$INSTALL_SH")
+assert_contains "the generated script is captured once, not written straight to a file" \
+ "$BODY_ALL" '_css_ps1_body=$(cat << WSLPS1_EOF'
+assert_contains "there is a stdin launch for when no Windows directory is reachable" \
+ "$BODY_ALL" 'powershell.exe -NoProfile -Command -'
+# Execution policy applies to -File and not to -Command, so the fallback needs no relaxation and
+# must not acquire one.
+STDIN_LINE=$(printf '%s' "$BODY_ALL" | grep -- 'powershell.exe -NoProfile -Command -')
+case "$STDIN_LINE" in
+ *ExecutionPolicy*) bad "the stdin launch carries an execution policy flag it does not need: $STDIN_LINE" ;;
+ *) ok "the stdin launch relaxes no execution policy" ;;
+esac
+# It must be fed from our own pipe, never from the installer's stdin, which under `curl | sh` is
+# still the download (#7548).
+case "$STDIN_LINE" in
+ *printf*"|"*powershell.exe*) ok "the stdin launch is fed from its own pipe" ;;
+ *) bad "the stdin launch does not pipe the body in: $STDIN_LINE" ;;
+esac
+
+# The one place the counters are read, and it has to be the LAST thing the file does. `bad` records
+# a failure and returns success, so any assertion that runs after this check reports its failure and
+# still leaves the script exiting 0.
+echo ""
+echo " $PASS passed, $FAIL failed"
+[ "$FAIL" -eq 0 ] || exit 1
diff --git a/tests/studio/test_installer_av_shapes.py b/tests/studio/test_installer_av_shapes.py
index a2701301c7f..1e4181425fe 100644
--- a/tests/studio/test_installer_av_shapes.py
+++ b/tests/studio/test_installer_av_shapes.py
@@ -24,8 +24,183 @@ from unsloth_pwsh_runner import pwsh_env
REPO = Path(__file__).resolve().parents[2]
PS_SCRIPTS = ("install.ps1", "studio/setup.ps1", "scripts/uninstall.ps1")
-SH_SCRIPTS = ("install.sh", "studio/setup.sh")
-ALL_SCRIPTS = PS_SCRIPTS + SH_SCRIPTS
+SH_SCRIPTS = ("install.sh", "studio/setup.sh", "scripts/uninstall.sh")
+# A shipped .bat is scanned like any other file, and studio/setup.bat launches PowerShell, so the
+# same rules apply. Neither it nor scripts/uninstall.sh was in any list here, which is how
+# setup.bat's `-ExecutionPolicy Bypass` survived the passes in #7822 and #8586: every guard below
+# was reading a set of files that did not include it.
+BAT_SCRIPTS = ("studio/setup.bat",)
+ALL_SCRIPTS = PS_SCRIPTS + SH_SCRIPTS + BAT_SCRIPTS
+
+
+# ---------------------------------------------------------------------------
+# Why the installers are written the way they are, and which product flagged what
+# ---------------------------------------------------------------------------
+#
+# This record lives here, in a test, rather than in the shipped scripts or in a doc.
+#
+# Not in the scripts, because PowerShell hands the entire top-level script block to AMSI at compile
+# time, before the first statement runs -- so every byte of install.ps1 is classifier input, comments
+# included. VirusTotal's analysis of the file quoted one of our own comments back as grounds for
+# suspicion: "The presence of comments that suggest the script is designed to evade detection by
+# security tools (e.g. 'AMSI scans this file in full before a line of it runs') further adds to the
+# suspicion." Prose naming vendors and detection families raises the score of the very file it is
+# trying to explain.
+#
+# Here, because this file already owns the guards that keep those shapes out, it ships to nobody, it
+# is not packaged, and nothing scans it. The rule the tests below enforce is:
+#
+# a comment in a shipped script says what the code does and what breaks if you change it;
+# this record says which scanner flagged which shape, and when.
+#
+# Nothing below is an evasion. Every decision makes the installer do strictly LESS than the shape it
+# replaced: no compiler, no script engine, no remote script text, no cache purge on a no-op. The
+# heuristics are not wrong about the shapes; they are wrong that we are an instance of them, and the
+# fix is to stop having the shape.
+AV_SHAPES_RECORD = r"""
+## Measured detections
+
+| Sample | Verdict |
+|---|---|
+| install.ps1 at 1ad44677d (the revision reported in #10805) | 1 malicious / 58 undetected: Skyhigh
+ (Trellix / McAfee Enterprise) BehavesLike.PS.Suspicious.gr, engine v2021.2.0+4045, definitions
+ 20260912 |
+| the same file uploaded elsewhere as unsloth_install2.ps1 | same single engine, same label |
+| tests/security/fixtures/malicious_wheel.whl, malicious_sdist.tar.gz | 2/65 and 2/60 (Tencent,
+ Rising), and the cause of Panda flagging the GitHub repo zip as Exploit/CVE-2014-6271. Both are now
+ generated at test time rather than committed |
+
+Beyond the one engine verdict, VirusTotal runs 17 Sigma rules against install.ps1 (1 high, 11 medium,
+5 low) plus two crowdsourced YARA rules. That rule count, not an engine detection, is what drives the
+high scores seen on third-party analysis sites.
+
+## What the hardening has actually bought so far
+
+Measured by scripts/virustotal_delta.py, not asserted:
+
+| | baseline (1ad44677d) | main (2c70592a) |
+|------------------|-------------------------------|-------------------|
+| engines flagging | 1 / 60 | 1 / 61 |
+| which | Skyhigh BehavesLike.PS.Suspicious.gr | the same |
+| Sigma | 17 (1 high, 11 medium, 5 low) | 14 (10 medium, 4 low) |
+| YARA | 2 | 0 |
+
+The high-severity rule is gone, and so are both YARA hits --
+INDICATOR_SUSPICIOUS_PWSH_B64Encoded_Concatenated_FileEXEC and Windows_API_Function, the second of
+which is the P/Invoke cluster. The Skyhigh verdict has not moved, and that is recorded as unchanged
+rather than dressed up: a cloud behavioural verdict is not recomputed because we deleted some code,
+and the honest expectation is that the static axis moves first and the engine axis may never move.
+
+This is the first before-and-after this work has had. Six passes shipped without one, which is why
+none of them could be shown to have achieved anything.
+
+## Reflection emit instead of Add-Type
+
+Add-Type on Windows PowerShell 5.1 -- the interpreter the desktop app spawns, and the one a clean
+Windows box ships -- has no in-process compiler. -TypeDefinition and -MemberDefinition alike write C#
+to %TEMP% and run csc.exe, leaving source, a response file and a DLL behind.
+
+Bitdefender blocked the resulting DLL as Gen:Variant.MSILHeracles.272113 (#10540). The shape it scores
+is real: a windowless PowerShell spawned by a GUI binary, running a compiler, writing executable
+content to %TEMP%. The same call also failed outright with CS2001 where %TEMP% was unusable (#9140),
+so this is a robustness fix as much as a detection one.
+
+System.Reflection.Emit builds the identical interop stubs in memory: no compiler process, no source
+file, no DLL, and an empty Assembly.Location. windows-no-compiler-ci.yml is the standing proof --
+4688 process auditing unioned with a live FileSystemWatcher over every temp root, with a positive
+control, requiring zero compiler launches across a full install.
+
+## No .vbs launcher
+
+A WScript.Shell .vbs that spawns a hidden PowerShell with a bypassed execution policy is close to the
+canonical shape VBS-dropper heuristics are written for -- Kaspersky HEUR:Trojan.VBS.Agent.gen is the
+family. The .lnk files therefore point straight at powershell.exe running launch-studio.ps1, with no
+script engine in between, and the installer removes an older launch-studio.vbs if it finds one,
+because leaving it behind means a machine that was cleaned still has the flagged file.
+
+## RemoteSigned rather than Bypass, next to a hidden window
+
+A hidden window paired with a bypassed execution policy is a pair Microsoft's own detections key on,
+and it appears in Sigma's "Suspicious PowerShell WindowStyle Option" too. The hidden window is a real
+requirement -- a console flashing up on every launch is a visible regression, and
+tests/studio/install/test_launch_studio_launcher.py pins the flag -- so the half that goes is the
+policy.
+
+RemoteSigned is not a weaker guarantee here, it is the same one: it refuses unsigned scripts only in
+the Internet and Untrusted zones, and every script involved is written locally by the installer. Where
+a mark of the web can exist (a downloaded zip rather than a clone), Unblock-File clears it first
+instead of the policy being relaxed to tolerate it.
+
+Three things to know before simplifying any of this:
+
+ - UNC paths are a remote zone. \\wsl.localhost\<distro>\tmp\x.ps1 is refused under RemoteSigned,
+ which is why install.sh resolves the Windows %TEMP% for its generated script and skips shortcut
+ creation rather than falling back to Bypass when it cannot.
+ - Execution policy is evaluated against the script FILE's zone, not against who launched
+ PowerShell, and it does not apply to -Command at all. A .cmd or .bat shim therefore cannot
+ launder a remote-zone script, and Sigma's "Powershell Execute Batch Script" scores the shim
+ itself.
+ - The Windows CLIENT default is Restricted, which blocks every script file. Documentation telling a
+ user to set a process-scoped policy is load-bearing; it just does not need to say Bypass.
+
+## The icon-cache refresh is gated on a first install or a real icon change
+
+Clearing icon caches and killing StartMenuExperienceHost is the only way to make Explorer pick up a
+rewritten .lnk icon, and ie4uinit's global broadcast alone does not do it. But "clear caches, kill a
+shell process, repeat on every run" is a cluster behavioural engines score, and on a reinstall that
+changed nothing it is also pure waste. So both install.ps1 and install.sh snapshot the icon and run
+the heavy path only on a first install or an actual change, preserving start2.bin.
+
+SHChangeNotify stays, and with it one shell32 import: a permanently wrong desktop icon is a worse
+outcome than one import.
+
+## uv comes from a pinned archive, not from a remote install script
+
+The upstream one-liner pipes a remote script into the interpreter. Running remote script text
+in-process is the highest-scoring thing in this problem space, and download-run-delete is the literal
+definition of a dropper. Our fallback reaches the same end state (same archive, same destination, same
+user-PATH prepend as astral's installer) by fetching a DATA file with a pinned SHA-256.
+
+Cost: bumping the uv version means bumping all three hashes, one per architecture, in all three
+scripts. That is deliberate friction and the comment at each site says so.
+
+There is a floor no shape work removes: while the supported install route is
+`irm https://unsloth.ai/install.ps1 | iex`, the documented entry point is itself the top-scoring token
+sequence in this space.
+
+## Why the script headers do not repeat the usage text
+
+install.ps1 and scripts/uninstall.ps1 are scanned in full at compile time, before a line of either
+runs, and nothing reads a header comment from inside the script. Duplicating the README's option list
+into a header adds bytes to a classifier's input and reaches no user.
+
+## Shapes we are keeping, on purpose
+
+Each fires a rule and each is load-bearing. Listed so nobody spends a second pass rediscovering them.
+
+ - -WindowStyle Hidden on the shortcuts. Removing it makes a console window appear on every launch;
+ the flag is a pinned contract in test_launch_studio_launcher.py.
+ - Authenticode publisher checks on the python.org and vc_redist downloads. Weakening a real security
+ control to lower a heuristic score is backwards.
+ - New-Object -ComObject WScript.Shell to write the .lnk files. The only shortcut mechanism available
+ to PowerShell 5.1 without IShellLink interop; hand-writing the shell-link binary format or
+ emitting COM interop are both more suspicious and more fragile.
+ - Unblock-File rather than deleting the Zone.Identifier stream directly. The direct delete trades
+ one medium indicator ("Suspicious Unblock-File") for another ("Hidden Executable In NTFS Alternate
+ Data Stream"), which already fires.
+ - ie4uinit, Get-Process, python -X utf8 -c, Invoke-WebRequest. Each is scored; each has no
+ equivalent that does the job.
+
+## Reporting a detection
+
+Use the "Windows: antivirus or security software blocked the installer" issue form. It requires the
+product, the exact detection name, the full error including its FullyQualifiedErrorId, and the output
+of a read-only collection script naming the AMSI providers actually loaded.
+
+Those fields are required because clearance is granted per file hash and every vendor submission form
+asks for a detection name. Six previous reports (#8523, #6326, #6588, #6648, #10540, #10805) named
+none of them, which is why none could be submitted to a vendor or proven fixed.
+"""
def _text(name: str) -> str:
@@ -107,19 +282,103 @@ def test_no_encoded_or_base64_command_payloads(name: str) -> None:
assert banned not in text, f"{name} contains {banned}"
+_HIDDEN = re.compile(r"-WindowStyle\s+Hidden", re.IGNORECASE)
+_BYPASS = re.compile(
+ r"-ExecutionPolicy\s+Bypass|Set-ExecutionPolicy[^\r\n]*?\bBypass\b", re.IGNORECASE
+)
+_ASSIGNMENT = re.compile(r"\$(?:script:|env:)?(\w+)\s*(?:=|\+=)\s*(.*)")
+
+# Every relaxed execution policy left in a shipped script, why it is still there, and what removes
+# it. A ratchet: these counts may go down, never up, and the test fails BOTH ways -- too many is a
+# new site, too few is a stale entry that has stopped guarding anything.
+KNOWN_BYPASS_SITES = {
+ # install.ps1:3433, the roaming-profile fallback for a launcher on a share. The last one left,
+ # and the only one that is genuinely load-bearing: %LOCALAPPDATA% can be folder-redirected to a
+ # UNC path, RemoteSigned refuses an unsigned script there, and a desktop shortcut that silently
+ # does nothing is worse than the token. Removing it needs launch-studio.ps1 written to a
+ # guaranteed-local directory first.
+ "install.ps1": 1,
+}
+
+# Known (script, variable) pairs where one assignment carries a hidden window and another a relaxed
+# policy. Empty is the goal. install.ps1's $shortcutArgs is recorded rather than failed on, so this
+# guard can land without also forcing the launcher relocation above.
+# Keyed on the CASEFOLDED variable name. PowerShell variable names are not case-sensitive
+# (about_Variables: "Variable names aren't case-sensitive"), so `$shortcutArgs` and `$ShortcutArgs`
+# are one variable. Grouping on the captured spelling instead would file them as two, each holding
+# only one of the two flags, and layer 3 below would wave the pair through on a capitalisation edit.
+KNOWN_SPLIT_PAIR_VARIABLES = {("install.ps1", "shortcutargs")}
+
+
@pytest.mark.parametrize("name", ALL_SCRIPTS)
def test_a_hidden_window_never_pairs_with_a_bypassed_policy(name: str) -> None:
- # Microsoft's detections key on this pair;
- # install.rs already refuses it for the app's own launch.
- # Python setup/refresh argv is exercised at the subprocess boundary by
- # unsloth_cli/tests/test_studio_runtime_gate_powershell.py::
- # test_windows_launch_uses_process_flags_without_windowstyle.
- for number, line in enumerate(_text(name).splitlines(), start = 1):
- if re.search(r"-WindowStyle\s+Hidden", line, re.IGNORECASE):
- assert not re.search(
- r"-ExecutionPolicy\s+Bypass", line, re.IGNORECASE
+ """Three layers, because the same-line check alone never saw the pair we actually shipped.
+
+ Microsoft's detections key on the pair, and install.rs already refuses it for the app's own
+ launch. Python setup/refresh argv is exercised at the subprocess boundary by
+ unsloth_cli/tests/test_studio_runtime_gate_powershell.py::
+ test_windows_launch_uses_process_flags_without_windowstyle.
+
+ The original check compared the two flags only within one physical line. install.ps1 assigns
+ `$shortcutArgs` a hidden window at :3419 and then overwrites it with a relaxed policy at :3433,
+ fourteen lines apart in one function, and that passed for as long as it existed. A scanner reads
+ the file, not the line.
+ """
+ text = _text(name)
+ lines = text.splitlines()
+
+ # 1. Same line. The cheapest check and the one with the clearest message.
+ for number, line in enumerate(lines, start = 1):
+ if _HIDDEN.search(line):
+ assert not _BYPASS.search(
+ line
), f"{name}:{number} pairs a hidden window with a bypassed policy: {line.strip()}"
+ # 2. A ratchet on how many relaxed policies the file contains at all, wherever they sit and
+ # whatever they are near. This is what catches a new one arriving somewhere the other two
+ # layers do not model.
+ found = [
+ (number, line.strip()) for number, line in enumerate(lines, start = 1) if _BYPASS.search(line)
+ ]
+ allowed = KNOWN_BYPASS_SITES.get(name, 0)
+ assert len(found) <= allowed, (
+ f"{name} relaxes the execution policy in {len(found)} place(s) but {allowed} are recorded: "
+ f"{found}. RemoteSigned loads any locally written, unmarked script, which covers almost "
+ f"every case; if this one genuinely cannot, add it to KNOWN_BYPASS_SITES with the reason "
+ f"and what would remove it."
+ )
+ assert len(found) >= allowed, (
+ f"{name} now has {len(found)} relaxed policies but KNOWN_BYPASS_SITES records {allowed}. "
+ f"Lower the count in the same commit that removed one, so the ratchet keeps its grip "
+ f"instead of leaving slack for the next regression to fit into."
+ )
+
+ # 3. Same variable, any distance: the union of everything assigned to one name must not contain
+ # both flags. This is the layer that catches the install.ps1 3419/3433 shape.
+ contributions: dict[str, set] = {}
+ spellings: dict[str, set] = {}
+ for line in lines:
+ match = _ASSIGNMENT.match(line.strip())
+ if not match:
+ continue
+ # Casefolded, because PowerShell resolves $shortcutArgs and $ShortcutArgs to one variable.
+ key = match.group(1).casefold()
+ spellings.setdefault(key, set()).add(match.group(1))
+ seen = contributions.setdefault(key, set())
+ if _HIDDEN.search(match.group(2)):
+ seen.add("hidden")
+ if _BYPASS.search(match.group(2)):
+ seen.add("bypass")
+ for variable, seen in sorted(contributions.items()):
+ if seen != {"hidden", "bypass"}:
+ continue
+ written = " / ".join(sorted(spellings[variable]))
+ assert (name, variable) in KNOWN_SPLIT_PAIR_VARIABLES, (
+ f"{name}: ${written} is assigned a hidden window in one place and a relaxed policy in "
+ f"another. Only one of them reaches the command line, so whichever is dead weight "
+ f"should go rather than be recorded here."
+ )
+
# Every native import left in the installers, however it is declared. Both scripts define theirs through reflection
# emit now, which costs no compile: install.ps1 the path resolver, console thunk, icon refresh and process-image
@@ -184,6 +443,62 @@ def _native_imports(text: str) -> set:
return imported
+def test_setup_bat_clears_the_mark_before_loading_under_remotesigned() -> None:
+ """The batch launcher's two calls, in order, and the one flag it must not grow.
+
+ `setup.bat` used to run `powershell -ExecutionPolicy Bypass -File setup.ps1`. RemoteSigned is
+ enough, because setup.ps1 ships beside it inside an installed package and is MyComputer-zone.
+ The exception is a package unzipped from a download, where setup.ps1 carries a mark of the web
+ that RemoteSigned honours and Bypass ignored, so the mark is cleared first. Execution policy
+ governs script FILES and not -Command, so that first call runs under any machine policy.
+
+ The launch must keep loading profiles. That is not an oversight: tests/studio/
+ test_amd_venv_repair_loop.ps1 drives a profile that sets `Set-StrictMode -Version Latest`
+ against setup.ps1, and adding -NoProfile here would silently retire that coverage.
+ """
+ text = _text("studio/setup.bat")
+ lines = [
+ line
+ for line in text.splitlines()
+ if line.strip() and not line.strip().lower().startswith(("rem ", "@echo", "rem\t"))
+ ]
+ # The path and the policy both travel in variables now, so match on the flags rather than on a
+ # literal filename: the path is an environment variable so an apostrophe in the install
+ # directory cannot break the quoting, and the policy is chosen by a probe that steps down to
+ # Bypass only for a script on a remote share (see
+ # test_setup_bat_steps_down_to_bypass_only_for_a_remote_script).
+ launches = [line for line in lines if "-File" in line and "-ExecutionPolicy" in line]
+ assert len(launches) == 1, f"expected exactly one setup.ps1 launch, found {launches}"
+ launch = launches[0]
+
+ assert "-ExecutionPolicy %UNSLOTH_SETUP_POLICY%" in launch, (
+ f"studio/setup.bat must load setup.ps1 under the probed policy, not a hardcoded relaxed "
+ f"one: {launch}"
+ )
+ assert 'set "UNSLOTH_SETUP_POLICY=RemoteSigned"' in text, (
+ "the probed policy no longer DEFAULTS to RemoteSigned. That default is what makes a probe "
+ "which fails to run leave the tightened policy in place instead of restoring the relaxed one."
+ )
+ assert "-NoProfile" not in launch, (
+ "studio/setup.bat must keep loading profiles for setup.ps1. "
+ "tests/studio/test_amd_venv_repair_loop.ps1 drives a profile that sets Set-StrictMode "
+ "against it, and -NoProfile here would retire that coverage without anything failing."
+ )
+
+ unblock = [line for line in lines if "Unblock-File" in line]
+ assert len(unblock) == 1, f"expected exactly one Unblock-File call, found {unblock}"
+ assert text.index(unblock[0]) < text.index(launch), (
+ "the mark of the web has to be cleared before the launch that RemoteSigned would refuse, "
+ "not after it"
+ )
+ # Interpolating the path into the command string breaks on an apostrophe in the install
+ # directory, which is a real Windows user name.
+ assert "$env:" in unblock[0], (
+ f"pass the script path to Unblock-File through an environment variable rather than "
+ f"interpolating it into the command string: {unblock[0]}"
+ )
+
+
@pytest.mark.parametrize("name", ALL_SCRIPTS)
def test_no_new_native_imports(name: str) -> None:
text = _text(name)
@@ -259,7 +574,7 @@ if __name__ == "__main__":
raise SystemExit(pytest.main([__file__, "-v"]))
[email protected]("name", ("install.ps1", "studio/setup.ps1"))
[email protected]("name", ALL_SCRIPTS)
def test_the_installer_never_runs_the_c_sharp_compiler(name: str) -> None:
"""The desktop app spawns Windows PowerShell 5.1, which compiles Add-Type by writing C# to
%TEMP% and running csc.exe. A GUI binary launching a windowless PowerShell that launches a
@@ -269,20 +584,32 @@ def test_the_installer_never_runs_the_c_sharp_compiler(name: str) -> None:
Add-Type in full, not only -TypeDefinition: -MemberDefinition wraps its argument in a class
and compiles that too. -AssemblyName is the only exception, since it loads an assembly that
- already exists on disk. Both scripts, because a compile left anywhere makes "does this run a
- compiler" depend on which entrypoint ran and whether an early return came first, and a guard
- that holds only conditionally is what let this reach the field.
+ already exists on disk. Every shipped script, because a compile left anywhere makes "does this
+ run a compiler" depend on which entrypoint ran and whether an early return came first, and a
+ guard that holds only conditionally is what let this reach the field.
+
+ The shell scripts are covered for a concrete reason, not for symmetry. install.sh writes
+ PowerShell into a here-string and runs it on the Windows side to create the WSL shortcut, and
+ that generated script still carried the `Add-Type -MemberDefinition` this test exists to ban:
+ #10540 replaced it in install.ps1 and the install.sh copy was missed, because the parametrise
+ list here stopped at the two .ps1 files. The `^[ \\t]*Add-Type` anchor matches inside a
+ here-string exactly as it does outside one, so seeing it needs no here-string parsing.
"""
text = _text(name)
hits = re.findall(r"(?m)^[ \t]*Add-Type\b(?![^\r\n]*-AssemblyName).*", text)
assert not hits, (
f"{name} compiles C# again ({len(hits)} Add-Type call(s), first: {hits[0].strip()!r}). "
- "Declare native methods with New-StudioEmittedNativeType instead; -MemberDefinition runs "
- "csc.exe just as -TypeDefinition does."
+ "Declare native methods with New-StudioEmittedNativeType instead, or with an inline "
+ "DefinePInvokeMethod block where the script is generated and cannot call it; "
+ "-MemberDefinition runs csc.exe just as -TypeDefinition does."
)
- assert (
- "DefinePInvokeMethod" in text
- ), f"{name} no longer emits its native imports; update this guard"
+ # Conditional: only a script that declares a native import has an emit to still be doing.
+ # scripts/uninstall.ps1 and studio/setup.sh declare none, and demanding the token of them would
+ # be a guard that fails for being satisfied.
+ if _native_imports(text):
+ assert (
+ "DefinePInvokeMethod" in text
+ ), f"{name} declares native imports without emitting them; update this guard"
# The private-%TEMP% retry is gone with it: redirecting TEMP to compile again cannot beat a
# filter driver, and "blocked writing an executable to TEMP, change TEMP, write it again" is
# itself an evasion heuristic. Scoped to the resolver, since Initialize-StudioTempEnvironment
@@ -700,3 +1027,231 @@ def test_the_native_resolver_still_has_a_lexical_fallback() -> None:
assert "Get-StudioLexicalPath" in text
# Constrained Language Mode forbids defining types at all, by emit as by Add-Type.
assert '$languageMode -ne "FullLanguage"' in text
+
+
+# ---------------------------------------------------------------------------
+# The shipped scripts must not name detections. The document must.
+# ---------------------------------------------------------------------------
+
+
+# Every file that ships and is scanned, including the two no other check in this file reads.
+DOCUMENTED_SCRIPTS = tuple(sorted(set(ALL_SCRIPTS) | {"studio/setup.bat", "scripts/uninstall.sh"}))
+
+# Vendor names, detection families and analyst vocabulary. Not a style rule: PowerShell hands the
+# entire top-level script block to AMSI at compile time, so comments are classifier input, and
+# VirusTotal's analysis of install.ps1 quoted one of our own comments as grounds for suspicion.
+BANNED_TOKENS = (
+ "bitdefender",
+ "kaspersky",
+ "skyhigh",
+ "trellix",
+ "mcafee",
+ "avast",
+ "sophos",
+ "malwarebytes",
+ "tencent",
+ # "rising" is deliberately absent. It is a real engine, and one of the two that flagged the
+ # fixture archives, but the word is also ordinary English: "rising memory use" is a sentence
+ # someone will write, and boundary matching cannot tell it from the vendor. A guard that fails
+ # on valid prose gets deleted by the next person, so it is worth less than nothing. "tencent",
+ # the other engine that flagged those archives, has no such problem and stays.
+ "panda",
+ "wacatac",
+ "heur:",
+ "heracles",
+ "gen:variant",
+ "behaveslike",
+ "trojan",
+ "dropper",
+ "amsi",
+ "smartscreen",
+ "virustotal",
+ "sigma rule",
+ "malware",
+)
+
+# Generic words describing a runtime hazard the code actually handles, one of which reaches the
+# user. Banning these would delete real operational meaning, so they are deliberately allowed:
+# antivirus, quarantine, scanner, security software, blocked.
+#
+# "false positive" is also deliberately absent, and for a more interesting reason: this test caught
+# it at install.ps1 and studio/setup.ps1, where it means a *statistical* false positive in a
+# registry probe and has nothing to do with a scanner. A token list is only as good as the words
+# having one meaning.
+
+
+def _banned_pattern(token: str) -> re.Pattern:
+ """`token`, matched on word boundaries where the token's own edges are word characters.
+
+ A raw substring search makes several of these unusable. `rising` is inside `surprising`,
+ `arising` and `comprising`; `panda` is inside `pandas`, which is a real dependency name. The
+ failure message would then accuse an ordinary sentence of naming an antivirus vendor, and the
+ fix a reader would reach for is to delete the guard. Boundaries are conditional because
+ `heur:` and `gen:variant` end or begin on a colon, where `\b` asserts the opposite of what is
+ wanted.
+ """
+ left = r"\b" if token[:1].isalnum() else ""
+ right = r"\b" if token[-1:].isalnum() else ""
+ return re.compile(left + re.escape(token) + right, re.IGNORECASE)
+
+
[email protected]("name", DOCUMENTED_SCRIPTS)
[email protected]("token", BANNED_TOKENS)
+def test_no_shipped_script_names_a_detection(name: str, token: str) -> None:
+ path = REPO / name
+ if not path.is_file():
+ pytest.skip(f"{name} is not present")
+ found = _banned_pattern(token).search(path.read_text(encoding = "utf-8"))
+ assert not found, (
+ f"{name} contains {token!r}. Vendor names, detection families and analyst vocabulary "
+ f"belong in tests/studio/test_installer_av_shapes.py, not in a file that is itself handed to "
+ f"a classifier in full before it runs. Say what the code does and what breaks if it "
+ f"changes; link the anchor for which product flagged what."
+ )
+
+
+def test_the_record_survives_and_keeps_its_evidence() -> None:
+ """Without this, the ban above is satisfiable by deleting the knowledge instead of moving it.
+
+ Six hardening passes shipped without recording which engine flagged what, which is why none of
+ them could be shown to have fixed anything. AV_SHAPES_RECORD is where that record lives now --
+ in this file rather than a doc, because a test ships to nobody and nothing scans it, and because
+ the guards that enforce the split are right here beside it.
+ """
+ for section in (
+ "## Measured detections",
+ "## Reflection emit instead of Add-Type",
+ "## No .vbs launcher",
+ "## RemoteSigned rather than Bypass, next to a hidden window",
+ "## The icon-cache refresh is gated on a first install or a real icon change",
+ "## uv comes from a pinned archive, not from a remote install script",
+ "## Why the script headers do not repeat the usage text",
+ "## Shapes we are keeping, on purpose",
+ ):
+ assert section in AV_SHAPES_RECORD, f"the record lost its {section!r} section"
+
+ # The sections are the skeleton; these are the point. A record with headings and no evidence is
+ # the same loss with extra steps.
+ for evidence in (
+ "Gen:Variant.MSILHeracles.272113",
+ "HEUR:Trojan.VBS.Agent.gen",
+ "BehavesLike.PS.Suspicious.gr",
+ "#10540",
+ "#10805",
+ "#9140",
+ ):
+ assert evidence in AV_SHAPES_RECORD, f"the record no longer names {evidence}"
+
+
[email protected]("name", DOCUMENTED_SCRIPTS)
+def test_every_script_that_dropped_its_explanation_points_at_the_record(name: str) -> None:
+ """A comment reduced to "security software blocks this" with no forward reference is worse
+ than the prose it replaced: the next maintainer cannot tell whether it is still true.
+ """
+ path = REPO / name
+ if not path.is_file():
+ pytest.skip(f"{name} is not present")
+ text = path.read_text(encoding = "utf-8")
+ hints = ("Add-Type", "RemoteSigned", "ClearIconCache", "Sha256", "SHA-256")
+ if not any(hint in text for hint in hints):
+ pytest.skip(f"{name} carries none of the documented shapes")
+ assert "test_installer_av_shapes.py" in text, (
+ f"{name} implements one of the recorded shapes but references nothing. Point at "
+ f"tests/studio/test_installer_av_shapes.py, where AV_SHAPES_RECORD says which product "
+ f"flagged what, so the reasoning is one grep away rather than lost."
+ )
+ # Assembled, so that a later blanket rename of the doc path cannot silently rewrite this check
+ # into asserting the opposite of what it means. That happened once while writing it.
+ stale = "docs/windows-installer-" + "av-shapes.md"
+ assert stale not in text, (
+ f"{name} points at {stale}, which does not exist. The record lives in "
+ f"tests/studio/test_installer_av_shapes.py as AV_SHAPES_RECORD."
+ )
+
+
+# -------------------------------------------------------------------------
+# studio/setup.bat
+# ---------------------------------------------------------------------------
+
+
+def _setup_bat_probe() -> str:
+ """The PowerShell that setup.bat embeds to clear the mark and choose a policy."""
+ for line in _text("studio/setup.bat").splitlines():
+ stripped = line.strip().lower()
+ if stripped.startswith("rem") or stripped.startswith("@rem"):
+ continue
+ if "-Command" not in line:
+ continue
+ body = line.split('-Command "', 1)[1]
+ return body[: body.rindex('"`)')]
+ raise AssertionError(
+ "studio/setup.bat no longer embeds a -Command probe. It needs one: Unblock-File has to run "
+ "before setup.ps1 is loaded under RemoteSigned, and the remote-path check has to happen "
+ "before a policy is chosen."
+ )
+
+
+def test_the_setup_bat_probe_parses() -> None:
+ """It is one long line inside a batch `for /f` backquote block, which is a quoting minefield.
+
+ A syntax error here does not fail loudly: the `for /f` captures nothing, the batch default of
+ RemoteSigned stands, and the mark of the web is never cleared -- so a user who unzipped a
+ download gets a refusal with no hint that the probe was the thing that broke.
+ """
+ pwsh = shutil.which("pwsh")
+ if pwsh is None:
+ pytest.skip("pwsh is unavailable")
+ probe = _setup_bat_probe()
+ import tempfile
+
+ with tempfile.TemporaryDirectory() as tmp:
+ path = Path(tmp) / "probe.ps1"
+ path.write_text(probe, encoding = "utf-8")
+ result = _run_pwsh_parse(pwsh, path)
+ assert (
+ result.returncode == 0
+ ), f"the probe embedded in studio/setup.bat does not parse:\n{result.stdout}\n{result.stderr}"
+
+
+def _run_pwsh_parse(pwsh: str, path: Path):
+ import os
+ from unsloth_pwsh_runner import run_pwsh
+
+ script = (
+ "$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 }"
+ )
+ return run_pwsh(
+ [pwsh, "-NoProfile", "-NonInteractive", "-Command", script],
+ capture_output = True,
+ text = True,
+ timeout = 120,
+ env = {**os.environ, "UNSLOTH_TARGET": str(path)},
+ )
+
+
+def test_setup_bat_steps_down_to_bypass_only_for_a_remote_script() -> None:
+ """The one case where RemoteSigned is a real regression, handled the way install.ps1 handles it.
+
+ Execution policy is judged on the script file's ZONE. A dotted-FQDN UNC, a DFS path or an
+ IP-literal share is the Internet zone, where RemoteSigned refuses an unsigned script and
+ `Unblock-File` cannot help: with no `Zone.Identifier` stream present the path decides, and there
+ is nothing to clear. `install.ps1` already steps down to Bypass for exactly this when it writes
+ the shortcut; reusing that logic beats inventing a second answer.
+ """
+ probe = _setup_bat_probe()
+ assert (
+ "DriveInfo" in probe and "Network" in probe
+ ), "the probe no longer detects a mapped network drive, so a script on H:/Z: would be refused"
+ assert "-like '\\\\*'" in probe, "the probe no longer detects a UNC path"
+ assert (
+ "'Bypass'" in probe and "'RemoteSigned'" in probe
+ ), "the probe no longer chooses between the two policies"
+ assert (
+ "Unblock-File" in probe
+ ), "the probe no longer clears the mark of the web, so an unzipped download is refused"
+
+ # The launch line, the -NoProfile asymmetry and the Unblock-File ordering are asserted by
+ # test_setup_bat_clears_the_mark_before_loading_under_remotesigned above; not repeated here.
diff --git a/tests/studio/test_wsl_shortcut_script_is_valid_powershell.py b/tests/studio/test_wsl_shortcut_script_is_valid_powershell.py
new file mode 100644
index 00000000000..edbcba17f76
--- /dev/null
+++ b/tests/studio/test_wsl_shortcut_script_is_valid_powershell.py
@@ -0,0 +1,297 @@
+# 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 PowerShell install.sh generates for the WSL shortcut has to be valid PowerShell.
+
+Nothing else checks. install.sh is shell, so `bash -n` covers the here-string as a here-string
+and says nothing about its contents; the contents only ever run on a Windows machine, through
+`powershell.exe -File`, with stdout and stderr both sent to /dev/null and the exit status folded
+into a best-effort `&& _css_created=1`. A parse error there is not a crash and not a message: the
+whole script fails to load, no shortcut appears, and the user gets the "WSL interop may be
+disabled" notice, which points at the wrong thing entirely.
+
+That path is one edit away at all times. The block this guards was just rewritten from a
+one-line `Add-Type -MemberDefinition` into a twenty-line reflection-emit sequence with nested
+calls, an array literal and a here-string escaping every `$` it means to keep -- and it sits
+inside `try { } catch { }`, so even at RUNTIME on the right machine a mistake inside it is
+swallowed.
+
+The emit test is the other half: syntax is not enough, the API sequence has to be real. shell32
+is Windows-only, but DefineDynamicAssembly / DefineDynamicModule / DefineType /
+DefinePInvokeMethod / CreateType is not, so the same sequence is pointed at a symbol this host
+does export and the resulting method is called. A wrong call order or a wrong argument count
+fails here, on Linux, instead of silently on a user's desktop.
+
+What that is worth, measured rather than asserted. Mutating the shipped file five ways: a dropped
+closing paren in DefinePInvokeMethod, an unterminated string literal in DefineType and a stray
+closing brace all fail both tests or the parse one; giving the P/Invoke the wrong number of
+parameters fails the emit one. Dropping SetImplementationFlags does NOT fail, and cannot: it sets
+PreserveSig, whose only effect is on how a failing HRESULT is surfaced, and the retargeted symbol
+here returns a plain int. That line is unguarded and is the one place a reviewer still has to
+read for themselves.
+"""
+
+from __future__ import annotations
+
+import re
+import shlex
+import sys
+import shutil
+import subprocess
+from pathlib import Path
+
+import pytest
+
+from unsloth_pwsh_runner import run_pwsh
+
+
+REPO = Path(__file__).resolve().parents[2]
+INSTALL_SH = REPO / "install.sh"
+
+needs_pwsh = pytest.mark.skipif(shutil.which("pwsh") is None, reason = "needs PowerShell")
+
+# What `_render` produces is a pure function of install.sh's bytes and the four values below, so the
+# Linux and macOS legs cover the subject completely and a Windows leg adds no coverage of it. What a
+# Windows leg does add is a different shell: `bash` there is Git Bash or the WSL stub in System32,
+# neither of which install.sh is ever run by -- its WSL arm is gated on /proc/version naming
+# microsoft, so the shell that reads this here-string is always a POSIX one inside the distro. The
+# repository already draws this line the same way for its other bash-driven installer tests
+# (tests/python/test_install_uv_override_space.py:21,
+# tests/studio/install/test_selection_logic.py:4185).
+pytestmark = pytest.mark.skipif(
+ sys.platform == "win32", reason = "renders install.sh with bash; POSIX shell installer test",
+)
+
+# Values install.sh would interpolate, chosen to be awkward: a distro name with a space, and the
+# apostrophe the surrounding single-quoted PowerShell literals have to be doubling.
+_RENDER_VARS = {
+ "_css_sc_target": "wt.exe",
+ "_css_sc_args_ps": 'wsl.exe -d "O\'\'Brien 24.04" -- bash -l -c "exec /home/u/launch.sh"',
+ "_css_lnk_name_ps": "Unsloth Studio (WSL - O''Brien 24.04).lnk",
+ "_css_wsl_ico_win_ps": "C:\\Users\\ci\\unsloth.ico",
+}
+
+
+def _render() -> str:
+ """The here-string as bash would actually write it, not a copy of it.
+
+ Extracted from the shipped file and expanded by a real shell, so every backslash escape in
+ it is resolved the way install.sh resolves it. A test holding its own copy of this script
+ would keep passing after install.sh broke.
+ """
+ text = INSTALL_SH.read_text(encoding = "utf-8")
+ body = re.search(
+ # The body is captured into a variable now, because it is either written to a file or piped
+ # to powershell on stdin depending on whether a Windows directory is reachable.
+ r"(?ms)^ _css_ps1_body=\$\(cat << WSLPS1_EOF\n(.*?)^WSLPS1_EOF$",
+ text,
+ )
+ assert body, (
+ "could not find the WSLPS1_EOF here-string in install.sh. Either the WSL shortcut script "
+ "moved or it is no longer generated, and until this locates it again nothing checks that "
+ "what install.sh writes is PowerShell at all."
+ )
+ # shlex.quote, not repr: these values deliberately contain the apostrophes and double quotes
+ # the real ones do, and Python's repr quotes for Python.
+ assigns = "".join(f"{k}={shlex.quote(v)}\n" for k, v in _RENDER_VARS.items())
+ script = assigns + "cat << WSLPS1_EOF\n" + body.group(1) + "WSLPS1_EOF\n"
+ # check = False, then asserted. CalledProcessError carries the command and the return code and
+ # drops the shell's own diagnostic, so a rendering that fails to render reported a 6 KB repr of
+ # the script and not the one line saying what was wrong with it.
+ done = subprocess.run(["bash", "-c", script], capture_output = True, text = True)
+ assert done.returncode == 0, (
+ f"bash could not render the here-string (exit {done.returncode}):\n"
+ f"{done.stderr.strip()}\n{done.stdout.strip()[:2000]}"
+ )
+ assert "SHChangeNotify" in done.stdout, done.stdout
+ return done.stdout
+
+
+@needs_pwsh
+def test_the_generated_wsl_shortcut_script_parses() -> None:
+ rendered = _render()
+ probe = (
+ "$errors = $null; $tokens = $null; "
+ "$text = [Console]::In.ReadToEnd(); "
+ "$null = [System.Management.Automation.Language.Parser]::ParseInput("
+ "$text, [ref]$tokens, [ref]$errors); "
+ "if ($errors -and $errors.Count) { "
+ " $errors | ForEach-Object { Write-Output ("
+ " 'PARSE_ERROR line ' + $_.Extent.StartLineNumber + ': ' + $_.Message) }; "
+ "} else { Write-Output 'PARSE_OK' }"
+ )
+ done = run_pwsh(
+ ["pwsh", "-NoProfile", "-NonInteractive", "-Command", probe],
+ input = rendered,
+ capture_output = True,
+ text = True,
+ verdict = "PARSE_OK",
+ )
+ assert "PARSE_OK" in done.stdout, (
+ "the PowerShell install.sh generates for the WSL shortcut does not parse, so on a real "
+ "machine it would load nothing and the user would be told WSL interop is disabled:\n"
+ f"{done.stdout.strip()}\n{done.stderr.strip()}"
+ )
+
+
+@needs_pwsh
+def test_the_icon_refresh_emit_sequence_builds_a_callable_type() -> None:
+ rendered = _render()
+ emit = re.search(r"(?ms)^try \{\n( \$refreshType = .*?)\n\} catch \{\}", rendered)
+ assert emit, (
+ "could not find the icon-refresh emit block in the generated script; if SHChangeNotify "
+ "is now declared some other way, update this test rather than deleting it"
+ )
+ # Retargeted, not rewritten: only the library, the symbol and its signature change, so the
+ # call sequence under test is the shipped one character for character.
+ body = emit.group(1)
+ body = body.replace(
+ "'SHChangeNotify', 'shell32.dll', 'SHChangeNotify'", "'getpid', 'libc', 'getpid'"
+ )
+ body = re.sub(r"\[System\.Void\],", "[int],", body)
+ body = re.sub(r"@\(\[int\], \[uint32\], \[string\], \[IntPtr\]\),", "@(),", body)
+ body = body.replace(
+ "[System.Runtime.InteropServices.CharSet]::Unicode)",
+ "[System.Runtime.InteropServices.CharSet]::Ansi)",
+ )
+ body = "\n".join(l for l in body.splitlines() if "SHChangeNotify(" not in l)
+ body = body.replace("UnslothShellIconRefresh", "UnslothEmitProbe")
+ assert "DefinePInvokeMethod" in body and "getpid" in body, body
+
+ # Twice, because the block caches on `-as [type]`: a second pass must reuse the type rather
+ # than throw on a duplicate name, which is what makes the guard safe to leave in.
+ script = body + "\nWrite-Output ('EMIT_A ' + [string]$refreshType::getpid())\n"
+ script += body + "\nWrite-Output ('EMIT_B ' + [string]$refreshType::getpid())\n"
+ done = run_pwsh(
+ ["pwsh", "-NoProfile", "-NonInteractive", "-Command", script],
+ capture_output = True,
+ text = True,
+ verdict = "EMIT_B",
+ )
+ pids = re.findall(r"EMIT_[AB] (\d+)", done.stdout)
+ assert len(pids) == 2, (
+ "the reflection-emit sequence in the generated script does not build a callable P/Invoke "
+ "type. shell32 is Windows-only but this sequence is not, so this is the sequence itself "
+ f"being wrong:\n{done.stdout.strip()}\n{done.stderr.strip()}"
+ )
+ assert pids[0] == pids[1], f"the cached type was rebuilt instead of reused: {pids}"
+
+
+if __name__ == "__main__":
+ raise SystemExit(pytest.main([__file__, "-v"]))
+
+
+def test_the_wsl_install_is_watched_live_for_a_compiler() -> None:
+ """A search run after the install cannot see the compile it is looking for.
+
+ CodeDom deletes its whole intermediate directory once the assembly is loaded, which
+ `.github/scripts/Watch-ForCompiler.ps1` records as a before-and-after diff seeing "nothing at
+ all while 4688 recorded csc.exe". So a post-hoc listing of `*.cmdline` cannot fail for the case
+ this lane exists to catch, and a positive control that plants persistent files only proves the
+ search can traverse a directory. The install has to run INSIDE the watcher.
+ """
+ import yaml
+
+ workflow = yaml.safe_load(
+ (REPO / ".github" / "workflows" / "clean-machine-install-ci.yml").read_text(
+ encoding = "utf-8"
+ )
+ )
+ steps = [
+ step
+ for job in workflow["jobs"].values()
+ for step in (job.get("steps") or [])
+ # The WSL leg specifically. The Linux container legs run the same piped install and
+ # have no Windows side to watch.
+ if "cat install.sh | sh" in str(step.get("run", ""))
+ and "wsl -d unsloth-ci" in str(step.get("run", ""))
+ ]
+ assert steps, "nothing runs the piped WSL install any more"
+ for step in steps:
+ run = str(step["run"])
+ assert "Invoke-WithCompilerWatch" in run, (
+ "the WSL install is not wrapped in the live compiler watch, so a compile that cleans up "
+ "after itself would go unseen and the lane would report it clean"
+ )
+ assert "$seen.Compilers" in run, "the watch result is never inspected"
+ assert "$seen.TempLibraries" in run, (
+ "the dropped-library half is never inspected, and that is the half the Bitdefender "
+ "report in #10540 keyed on"
+ )
+
+
+def test_the_wsl_lane_arms_the_4688_half_of_the_watch() -> None:
+ """The watcher's process half was dead in this lane, and a dead half reads as a clean one.
+
+ `Watch-ForCompiler.ps1` says outright that 4688 "needs auditing enabled by the caller". Its
+ no-match path returns an empty compiler list whenever the Security log is merely readable, so
+ with auditing off `$seen.Compilers` is empty no matter what ran, and the step still prints that
+ no compiler process was seen. That leaves the FileSystemWatcher carrying the whole result alone
+ while the output claims two detectors. Require the audit policy to be turned on, verified, and
+ shown to produce a real detection before the install is judged.
+ """
+ import yaml
+
+ workflow = yaml.safe_load(
+ (REPO / ".github" / "workflows" / "clean-machine-install-ci.yml").read_text(
+ encoding = "utf-8"
+ )
+ )
+ job = next(
+ job
+ for job in workflow["jobs"].values()
+ if any(
+ "cat install.sh | sh" in str(step.get("run", ""))
+ and "wsl -d unsloth-ci" in str(step.get("run", ""))
+ for step in (job.get("steps") or [])
+ )
+ )
+ runs = [str(step.get("run", "")) for step in (job.get("steps") or [])]
+ index = next(i for i, run in enumerate(runs) if "cat install.sh | sh" in run)
+ # Only the steps BEFORE the install count: auditing turned on afterwards measures nothing.
+ earlier = "\n".join(runs[:index])
+ assert 'auditpol /set /subcategory:"Process Creation" /success:enable' in earlier, (
+ "the WSL lane never enables process creation auditing, so the 4688 half of the watch is "
+ "dead and an empty compiler list means unmeasured rather than clean"
+ )
+ assert (
+ "ProcessCreationIncludeCmdLine_Enabled" in earlier
+ ), "without the command line, 4688 cannot tell csc.exe ran for us from csc.exe ran"
+ assert (
+ "Process Creation\\s+Success" in earlier
+ ), "the audit policy is set but never verified, and machine policy can silently override it"
+ assert "$control.TempLibraries" in earlier, (
+ "the control validates only the process half, so a FileSystemWatcher that never attached "
+ "looks exactly like a run that dropped no library -- and that is the half the Bitdefender "
+ "report in #10540 keyed on"
+ )
+ assert "$control.Compilers" in earlier, (
+ "nothing proves the detector fires on this runner, so a clean verdict is indistinguishable "
+ "from a detector that never attached"
+ )
+
+
+def test_the_generated_emit_carries_both_spellings_of_define_dynamic_assembly() -> None:
+ """The outer catch is empty, so guessing the wrong spelling costs the icon refresh silently.
+
+ `install.ps1`'s `New-StudioDynamicAssembly` tries the static
+ `AssemblyBuilder::DefineDynamicAssembly` and falls back to
+ `AppDomain.CurrentDomain.DefineDynamicAssembly`, and says why: the static form is documented for
+ .NET Framework 4.5 through 4.8.1, so the Windows PowerShell 5.1 host this script is launched
+ under should take the first branch, but nothing in this repository can run a .NET Framework host
+ to confirm it. The generated WSL script had only the static form under a bare `catch {}`, which
+ is the combination that fails invisibly. Carry the same fallback.
+ """
+ script = _render()
+ assert (
+ "[System.Reflection.Emit.AssemblyBuilder]::DefineDynamicAssembly" in script
+ ), "the generated script no longer tries the documented static spelling first"
+ assert "[AppDomain]::CurrentDomain.DefineDynamicAssembly" in script, (
+ "the generated script has no AppDomain fallback, so on a host without the static overload "
+ "the empty outer catch swallows the failure and both icon refreshes stop happening"
+ )
+ static_at = script.index("[System.Reflection.Emit.AssemblyBuilder]::DefineDynamicAssembly")
+ appdomain_at = script.index("[AppDomain]::CurrentDomain.DefineDynamicAssembly")
+ assert (
+ static_at < appdomain_at
+ ), "AppDomain.CurrentDomain is absent on .NET Core, so leading with it would break pwsh"