smoke.ps1 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <#
  2. srt-win install/uninstall lifecycle smoke test.
  3. Exercises `srt-win install` (provisions the `srt-sandbox` user +
  4. user-SID-keyed WFP filter set) and `srt-win uninstall` against a
  5. built srt-win.exe. Throws on any assertion failure. Requires
  6. elevation (NetUserAdd / Fwpm* both need admin).
  7. Usage (local dev machine):
  8. pwsh vendor/srt-win-src/ci/smoke.ps1 .\target\release\srt-win.exe
  9. Usage (CI — workflow passes the path):
  10. pwsh vendor/srt-win-src/ci/smoke.ps1 vendor\srt-win-src\target\release\srt-win.exe
  11. All WFP operations target $InstallSublayer (a fixed test GUID), NOT
  12. the production default sublayer — safe to run on a dev machine
  13. that has real sandbox-runtime filters installed.
  14. #>
  15. param(
  16. [Parameter(Mandatory = $true)]
  17. [string]$Exe,
  18. # Distinct from wfp::DEFAULT_SUBLAYER_GUID so local runs never
  19. # touch a production install.
  20. [string]$InstallSublayer = 'b2e8a6c4-1f73-4d09-9e25-c7b0d3a48f61'
  21. )
  22. $ErrorActionPreference = 'Stop'
  23. if (-not (Test-Path $Exe)) {
  24. throw "srt-win.exe not found at '$Exe'"
  25. }
  26. Write-Host "srt-win smoke: exe=$Exe sublayer=$InstallSublayer"
  27. $isl = @('--sublayer-guid', $InstallSublayer)
  28. # Explicit so the assertions below are deterministic even if the
  29. # compiled-in default changes.
  30. $pr = @('--proxy-port-range', '60080-60089')
  31. function Run([string[]]$argv) {
  32. & $Exe @argv
  33. if ($LASTEXITCODE -ne 0) {
  34. throw "srt-win $($argv -join ' ') exited $LASTEXITCODE"
  35. }
  36. }
  37. function J([string[]]$argv) { Run $argv | ConvertFrom-Json }
  38. function MustFail([string[]]$argv, [string]$why) {
  39. & $Exe @argv 2>$null
  40. if ($LASTEXITCODE -eq 0) {
  41. throw "expected non-zero ($why): srt-win $($argv -join ' ')"
  42. }
  43. }
  44. # ── negative input ──────────────────────────────────────────────────
  45. MustFail (@('install', '--proxy-port-range', '100-50') + $isl) 'low>high'
  46. MustFail (@('install', '--proxy-port-range', '1-1000') + $isl) 'range too wide'
  47. MustFail (@('install', '--proxy-port-range', '60080') + $isl) 'missing dash'
  48. # ── pre-install absent ───────────────────────────────────────────────
  49. # CI runners are elevated so BFE enum succeeds; a non-elevated caller
  50. # would get `cannot-read` here — that path can't be exercised on the
  51. # elevated runner.
  52. $pre = J (@('wfp', 'status') + $isl)
  53. if ($pre.state -ne 'absent') {
  54. throw "pre-install wfp status expected absent, got $($pre.state)"
  55. }
  56. # ═════════════════════════════════════════════════════════════════════
  57. # Single-step `install` / `uninstall`.
  58. # ═════════════════════════════════════════════════════════════════════
  59. Run (@('install') + $isl + $pr)
  60. $iw = J (@('wfp', 'status') + $isl)
  61. if ($iw.state -ne 'installed' -or $iw.filters -lt 4) {
  62. throw "install: wfp expected installed/>=4, got $($iw.state)/$($iw.filters)"
  63. }
  64. if ($iw.port_range[0] -ne 60080 -or $iw.port_range[1] -ne 60089) {
  65. throw "install: expected port_range [60080,60089], got [$($iw.port_range -join ',')]"
  66. }
  67. # ── sandbox user provisioned by install ────────────────────────────
  68. $us = J @('user', 'status')
  69. Write-Host "user status: $($us | ConvertTo-Json -Compress)"
  70. if (-not $us.user.exists) { throw "install: sandbox user not provisioned" }
  71. if (-not $us.user.sid -or -not $us.user.sid.StartsWith('S-1-5-21-')) {
  72. throw "install: sandbox user SID missing or malformed: '$($us.user.sid)'"
  73. }
  74. if (-not $us.user.group_exists) { throw "install: sandbox-runtime-users group missing" }
  75. if (-not $us.user.in_builtin_users) { throw "install: sandbox user not in BUILTIN\Users" }
  76. if (-not $us.user.in_sandbox_group) { throw "install: sandbox user not in sandbox-runtime-users" }
  77. if (-not $us.user.hidden_from_logon) { throw "install: sandbox user not hidden from Winlogon" }
  78. if (-not $us.cred_present) { throw "install: credential not present (HKLM Cred\Blob)" }
  79. # Keep in sync with SETUP_VERSION (vendor/srt-win-src/src/install.rs).
  80. if ($us.marker_version -ne 2) { throw "install: setup marker version expected 2, got $($us.marker_version)" }
  81. if ($us.marker_user_sid -ne $us.user.sid) {
  82. throw "install: marker SID '$($us.marker_user_sid)' != live SID '$($us.user.sid)'"
  83. }
  84. if ($iw.user_sid -ne $us.user.sid) {
  85. throw "install: WFP user_sid '$($iw.user_sid)' != provisioned '$($us.user.sid)'"
  86. }
  87. # read-cred returns the cleartext password — 32 chars from the
  88. # documented alphabet. Run as the real user (the sandbox user is
  89. # DENY'd on the directory).
  90. $pw = & $Exe user read-cred
  91. if ($LASTEXITCODE -ne 0) { throw "user read-cred exited $LASTEXITCODE" }
  92. if ($pw.Length -ne 32) { throw "user read-cred: expected 32 chars, got $($pw.Length)" }
  93. if ($pw -match '["\s\\`&|<>^]') {
  94. throw "user read-cred: password contains an excluded char: '$pw'"
  95. }
  96. # State-dir DACL: explicit DENY for sandbox-runtime-users — the CA
  97. # key material lives under this dir and must be unreadable from
  98. # inside the sandbox. (Credential + marker live in
  99. # HKLM\SOFTWARE\sandbox-runtime now, asserted below.)
  100. $stateDir = Join-Path $env:ProgramData 'sandbox-runtime'
  101. $acl = Get-Acl $stateDir
  102. $deny = $acl.Access | Where-Object {
  103. $_.AccessControlType -eq 'Deny' -and
  104. $_.IdentityReference.Value -match 'sandbox-runtime-users$'
  105. }
  106. if (-not $deny) {
  107. throw "install: state-dir DACL has no DENY for sandbox-runtime-users; got:`n$($acl.Access | Out-String)"
  108. }
  109. # Logon-type denials: the sandbox group must carry the four LSA
  110. # deny rights (everything except interactive, which the
  111. # CreateProcessWithLogonW launch needs). secedit is the only stock
  112. # read path for user-right assignments.
  113. $grpSid = $us.user.group_sid
  114. if (-not $grpSid) { throw 'install: user status carries no group_sid' }
  115. $seceditOut = Join-Path $env:TEMP 'srt-secedit.inf'
  116. Remove-Item $seceditOut -ea SilentlyContinue # never read a stale export
  117. secedit /export /cfg $seceditOut /areas USER_RIGHTS | Out-Null
  118. if ($LASTEXITCODE -ne 0 -or -not (Test-Path $seceditOut)) {
  119. throw "install: secedit /export failed (exit $LASTEXITCODE)"
  120. }
  121. $rights = Get-Content $seceditOut -Raw
  122. foreach ($r in @('SeDenyNetworkLogonRight','SeDenyBatchLogonRight','SeDenyServiceLogonRight','SeDenyRemoteInteractiveLogonRight')) {
  123. $line = ($rights -split "`n" | Where-Object { $_ -match "^$r" })
  124. # secedit exports resolvable trustees by NAME, unresolvable as *SID.
  125. if (-not $line -or ($line -notmatch 'sandbox-runtime-users' -and $line -notmatch [regex]::Escape($grpSid))) {
  126. throw "install: $r does not include the sandbox group ($grpSid); got: $line"
  127. }
  128. }
  129. $interactive = ($rights -split "`n" | Where-Object { $_ -match '^SeDenyInteractiveLogonRight' })
  130. if ($interactive -match 'sandbox-runtime-users' -or $interactive -match [regex]::Escape($grpSid)) {
  131. throw "install: SeDenyInteractiveLogonRight includes the sandbox group - the launch path would be broken"
  132. }
  133. Remove-Item $seceditOut -ea SilentlyContinue
  134. Write-Host 'install ok: logon-type denials stamped on the sandbox group (interactive untouched)'
  135. # Registry store: base key present with marker + identity; Cred
  136. # subkey deny for the sandbox group and no BU write; Ca subkey BU
  137. # KEY_SET_VALUE (unelevated trust-ca) but sandbox-group DENY.
  138. $reg = Get-ItemProperty 'HKLM:\SOFTWARE\sandbox-runtime' -ea Stop
  139. if (-not $reg.MarkerVersion) { throw 'install: MarkerVersion missing from HKLM store' }
  140. $credAcl = Get-Acl 'HKLM:\SOFTWARE\sandbox-runtime\Cred'
  141. if (-not ($credAcl.Access | Where-Object { $_.AccessControlType -eq 'Deny' -and $_.IdentityReference.Value -match 'sandbox-runtime-users$' })) {
  142. throw "install: Cred key has no sandbox-group DENY:`n$($credAcl.Access | Out-String)"
  143. }
  144. if ($credAcl.Access | Where-Object { $_.AccessControlType -eq 'Allow' -and $_.IdentityReference.Value -match '\\Users$' -and ($_.RegistryRights -band [System.Security.AccessControl.RegistryRights]::SetValue) }) {
  145. throw "install: Cred key is writable by BUILTIN\Users:`n$($credAcl.Access | Out-String)"
  146. }
  147. $caAcl = Get-Acl 'HKLM:\SOFTWARE\sandbox-runtime\Ca'
  148. if (-not ($caAcl.Access | Where-Object { $_.AccessControlType -eq 'Deny' -and $_.IdentityReference.Value -match 'sandbox-runtime-users$' })) {
  149. throw "install: Ca key has no sandbox-group DENY:`n$($caAcl.Access | Out-String)"
  150. }
  151. if (-not ($caAcl.Access | Where-Object { $_.AccessControlType -eq 'Allow' -and $_.IdentityReference.Value -match '\\Users$' -and ($_.RegistryRights -band [System.Security.AccessControl.RegistryRights]::SetValue) })) {
  152. throw "install: Ca key lacks BUILTIN\Users SetValue (unelevated trust-ca broken):`n$($caAcl.Access | Out-String)"
  153. }
  154. # ── M1: stale/foreign marker → plain install completes ────────────
  155. # An admin (or an older build) leaves MarkerVersion at a value the
  156. # broker doesn't expect: read_cred refuses with "re-run install",
  157. # status still exits 0, and a plain (no --force) install falls
  158. # through the early-out and heals the marker.
  159. Set-ItemProperty 'HKLM:\SOFTWARE\sandbox-runtime' -Name MarkerVersion -Value 99
  160. $m1b = & $Exe user read-cred 2>&1 | Out-String
  161. if ($LASTEXITCODE -eq 0) { throw "M1b: read-cred succeeded on marker v99. out: $m1b" }
  162. if ($m1b -notmatch '(?i)marker version mismatch.*re-run') {
  163. throw "M1b: expected marker-mismatch refusal. out: $m1b"
  164. }
  165. $m1c = & $Exe user status 2>&1 | Out-String
  166. if ($LASTEXITCODE -ne 0) { throw "M1c: user status on stale marker expected exit 0; got $LASTEXITCODE" }
  167. Write-Host "M1b ok: read-cred refuses + status survives a stale marker"
  168. $m1out = & $Exe @(@('install') + $isl + $pr) 2>&1 | Out-String
  169. if ($LASTEXITCODE -ne 0) { throw "M1: install exited $LASTEXITCODE. out: $m1out" }
  170. if ($m1out -match '(?i)already installed.*no changes') {
  171. throw "M1: install short-circuited on a stale marker. out: $m1out"
  172. }
  173. $mv = (Get-ItemProperty 'HKLM:\SOFTWARE\sandbox-runtime').MarkerVersion
  174. if ($mv -eq 99) { throw "M1: marker not healed (still 99)" }
  175. Write-Host "M1 ok: stale marker healed by plain install (now v$mv)"
  176. # Re-install above re-wrote the cred; downstream rows (read-cred
  177. # etc.) already ran and don't depend on it.
  178. # block-user must NOT match the REAL user — its SD allows only the
  179. # sandbox user's SID.
  180. $r = curl.exe -s -m 10 -o NUL -w "%{http_code}" https://example.com
  181. if ($LASTEXITCODE -ne 0 -or $r -ne '200') {
  182. throw "install: real-user egress past block-user expected 200, got exit=$LASTEXITCODE code='$r'"
  183. }
  184. Write-Host "install: real-user egress past block-user OK ($r)"
  185. # Idempotency, same range: second install with identical flags is
  186. # a no-op (exit 0, "already installed").
  187. $out = & $Exe @(@('install') + $isl + $pr) 2>&1 | Out-String
  188. if ($LASTEXITCODE -ne 0) {
  189. throw "install idempotency: same-range expected exit 0, got $LASTEXITCODE"
  190. }
  191. if ($out -notmatch 'already installed') {
  192. throw "install idempotency: expected 'already installed' in output, got '$out'"
  193. }
  194. $iw2 = J (@('wfp', 'status') + $isl)
  195. if ($iw2.filters -ne $iw.filters) {
  196. throw "install idempotency: filters $($iw.filters) -> $($iw2.filters)"
  197. }
  198. # Conflict, different range without --force: exits 13.
  199. & $Exe @(@('install', '--proxy-port-range', '50000-50001') + $isl) 2>$null
  200. if ($LASTEXITCODE -ne 13) {
  201. throw "install conflict: different-range without --force expected exit 13, got $LASTEXITCODE"
  202. }
  203. # Original range still in place.
  204. $iw3 = J (@('wfp', 'status') + $isl)
  205. if ($iw3.port_range[0] -ne 60080) {
  206. throw "install conflict: range was overwritten without --force"
  207. }
  208. # --force replaces.
  209. Run (@('install', '--proxy-port-range', '50000-50001', '--force') + $isl)
  210. $iwF = J (@('wfp', 'status') + $isl)
  211. if ($iwF.port_range[0] -ne 50000 -or $iwF.port_range[1] -ne 50001) {
  212. throw "install --force: expected port_range [50000,50001], got [$($iwF.port_range -join ',')]"
  213. }
  214. # `wfp verify` and `user trust-ca` route through
  215. # CreateProcessWithLogonW (Secondary Logon). GHA runners have it;
  216. # ensure it's running (idempotent).
  217. try { Start-Service seclogon -ea Stop } catch {
  218. Write-Host "smoke: WARNING: Start-Service seclogon: $_"
  219. }
  220. # ── wfp verify: behavioral egress-block probe ───────────────────────
  221. # Spawns the runner as the sandbox user and direct-connects to a
  222. # target. The block-user filter fires at ALE_AUTH_CONNECT (before
  223. # any packet leaves) → WSAEACCES → exit 0 + "blocked". This is the
  224. # non-elevated readiness check (BFE enum is admin-gated; this
  225. # isn't). stderr (the runner's BLOCKED/UNREACHABLE line) flows to
  226. # the host so it's in the CI log; stdout is JUST the JSON line.
  227. #
  228. # `--target 127.0.0.1:49999` (a local listener bound below) — OUT of
  229. # the WFP loopback permit range, so block-user fires when the fence
  230. # is active and the connect succeeds when it isn't. Deterministic;
  231. # no internet. This is the same shape as the product path
  232. # (`verifyWindowsWfpEgress` binds an ephemeral out-of-range loopback
  233. # listener and passes it as `--target`).
  234. $probePort = 49999
  235. $probeTgt = "127.0.0.1:$probePort"
  236. $probeLsn = [System.Net.Sockets.TcpListener]::new(
  237. [System.Net.IPAddress]::Loopback, $probePort)
  238. $probeLsn.Start()
  239. function WfpVerify([string]$tgt) {
  240. $out = & $Exe wfp verify --target $tgt
  241. $ec = $LASTEXITCODE
  242. Write-Host "wfp verify --target ${tgt}: exit=$ec stdout='$out'"
  243. return [pscustomobject]@{ exit = $ec; json = $out | ConvertFrom-Json }
  244. }
  245. $v = WfpVerify $probeTgt
  246. if ($v.exit -ne 0 -or $v.json.egress_probe -ne 'blocked') {
  247. throw ("wfp verify (fence-active): expected exit 0 + blocked, " +
  248. "got exit=$($v.exit) probe='$($v.json.egress_probe)'")
  249. }
  250. if ($v.json.target -ne $probeTgt) {
  251. throw "wfp verify: --target not honoured; got '$($v.json.target)'"
  252. }
  253. Write-Host "wfp verify ok: fence-active → blocked (target=$probeTgt)"
  254. # ── user trust-ca: cert recorded + written ─────────────────────────
  255. # Cert lifecycle = sandbox-user lifecycle (set via `user trust-ca`,
  256. # persistent until uninstall); `srt-win install` and `srt-win exec`
  257. # never touch it. Mint a throwaway self-signed cert (PEM), pass it
  258. # via `user trust-ca`, and assert `user status` surfaces the thumb +
  259. # a PEM that round-trips back to the same DER.
  260. $caDir = Join-Path $env:TEMP "srt-ca-$(Get-Random)"
  261. $null = mkdir $caDir
  262. try {
  263. $ca = New-SelfSignedCertificate -Subject 'CN=srt-smoke-ca DO NOT TRUST' `
  264. -CertStoreLocation Cert:\CurrentUser\My -NotAfter (Get-Date).AddDays(1)
  265. $caPem = Join-Path $caDir 'ca.pem'
  266. $b64 = [Convert]::ToBase64String($ca.RawData, 'InsertLineBreaks')
  267. "-----BEGIN CERTIFICATE-----`n$b64`n-----END CERTIFICATE-----" |
  268. Set-Content -Path $caPem -Encoding ascii
  269. $thumb = $ca.Thumbprint
  270. Remove-Item "Cert:\CurrentUser\My\$thumb" -ea SilentlyContinue
  271. # `user trust-ca` is the FIRST `spawn_runner` call (CPWLW + the
  272. # WinSta0/BNO grants). A hang here is the broker waiting on a
  273. # runner that never finished init — e.g. an under-granted
  274. # WinSta0 mask. Marker so a hung CI log shows which step it is
  275. # (the captured `$out` never prints on a hang).
  276. Write-Host 'ca-trust: spawning runner (CPWLW + station/BNO grants)'
  277. $out = & $Exe user trust-ca $caPem 2>&1 | Out-String
  278. if ($LASTEXITCODE -ne 0) {
  279. throw "user trust-ca exited ${LASTEXITCODE}: $out"
  280. }
  281. if ($out -notmatch '(?i)CA installed.*thumb=') {
  282. throw "user trust-ca: runner did not log CA install. out: $out"
  283. }
  284. $usCa = J @('user', 'status')
  285. if ($usCa.ca_cert_thumb -ne $thumb) {
  286. throw "user status: ca_cert_thumb '$($usCa.ca_cert_thumb)' != '$thumb'"
  287. }
  288. if (-not $usCa.ca_cert_pem -or $usCa.ca_cert_pem -notmatch 'BEGIN CERTIFICATE') {
  289. throw "user status: ca_cert_pem missing or malformed"
  290. }
  291. # PEM round-trips back to the same DER (so der_to_pem and the
  292. # state-DB blob agree).
  293. $pemBody = ($usCa.ca_cert_pem -replace '-----[^-]+-----', '' -replace '\s', '')
  294. if ([Convert]::ToBase64String($ca.RawData) -ne $pemBody) {
  295. throw 'user status: ca_cert_pem does not round-trip to original DER'
  296. }
  297. # Real user's Root must NOT have it — the write is scoped to the
  298. # sandbox user's HKU\<SID>.
  299. if (Get-ChildItem Cert:\CurrentUser\Root |
  300. Where-Object Subject -match 'srt-smoke-ca') {
  301. throw 'user trust-ca: CA leaked into REAL user CurrentUser\Root'
  302. }
  303. Write-Host "ca-trust ok: thumb=$thumb recorded + written into sandbox-user Root"
  304. # `install --force` re-provisions the account but must preserve
  305. # the recorded CA — write_setup_info's ON CONFLICT DO UPDATE
  306. # excludes ca_cert (owned by set_ca_cert), so install never
  307. # touches it.
  308. Run (@('install', '--force') + $isl + $pr)
  309. $usCa3 = J @('user', 'status')
  310. if ($usCa3.ca_cert_thumb -ne $thumb) {
  311. throw ("install --force: ca_cert wiped — got " +
  312. "'$($usCa3.ca_cert_thumb)', expected '$thumb'")
  313. }
  314. Write-Host 'ca-trust ok: install --force preserves ca_cert'
  315. } finally {
  316. Remove-Item $caDir -Recurse -Force -ea SilentlyContinue
  317. }
  318. # --keep-user: filters removed, sandbox user + cred file kept.
  319. Run (@('uninstall', '--keep-user') + $isl)
  320. $uw0 = J (@('wfp', 'status') + $isl)
  321. if ($uw0.state -ne 'absent') {
  322. throw "uninstall --keep-user: wfp expected absent, got $($uw0.state)"
  323. }
  324. $us0 = J @('user', 'status')
  325. if (-not $us0.user.exists -or -not $us0.cred_present) {
  326. throw "uninstall --keep-user: sandbox user/cred should be kept"
  327. }
  328. # ── wfp verify (fence-INACTIVE): connect succeeds ───────────────────
  329. # Filters were just removed (`uninstall --keep-user`) but the
  330. # sandbox user is kept → the probe runs and the connect to the
  331. # local listener SUCCEEDS (no WFP block) → exit 3 + "connected".
  332. # This is the security-boundary proof that exit 0 above wasn't a
  333. # false positive.
  334. $v = WfpVerify $probeTgt
  335. if ($v.exit -ne 3 -or $v.json.egress_probe -ne 'connected') {
  336. throw ("wfp verify (fence-inactive): expected exit 3 + " +
  337. "connected, got exit=$($v.exit) probe=" +
  338. "'$($v.json.egress_probe)' — probe cannot distinguish " +
  339. "fence-active from fence-missing")
  340. }
  341. Write-Host 'wfp verify ok: fence-inactive → exit 3 + connected'
  342. $probeLsn.Stop()
  343. # Re-install to set up for the full uninstall below.
  344. Run (@('install') + $isl + $pr)
  345. # Uninstall removes filters AND the sandbox user.
  346. Run (@('uninstall') + $isl)
  347. $uw = J (@('wfp', 'status') + $isl)
  348. if ($uw.state -ne 'absent') {
  349. throw "uninstall: wfp expected absent, got $($uw.state)"
  350. }
  351. $usGone = J @('user', 'status')
  352. if ($usGone.user.exists) {
  353. throw "uninstall: sandbox user should be removed, got $($usGone | ConvertTo-Json -Compress)"
  354. }
  355. if ($usGone.cred_present) {
  356. throw "uninstall: credential row should be cleared"
  357. }
  358. if ($null -ne $usGone.marker_version) {
  359. throw "uninstall: setup marker row should be cleared"
  360. }
  361. if ($null -ne $usGone.ca_cert_thumb) {
  362. throw "uninstall: ca_cert row should be cleared"
  363. }
  364. # Idempotent no-op: second uninstall must also exit 0.
  365. Run (@('uninstall') + $isl)
  366. # ── U1: --sandbox-user provisions under a custom name ──────────────
  367. # Not pre-created — install creates it. `user status` reads the name
  368. # from the setup marker, so it reports the custom name.
  369. Run (@('install', '--sandbox-user', 'srt-sb-custom') + $isl + $pr)
  370. $usc = J @('user', 'status')
  371. if ($usc.user.name -ne 'srt-sb-custom') {
  372. throw "U1: expected user.name='srt-sb-custom', got '$($usc.user.name)'"
  373. }
  374. if (-not $usc.user.exists -or -not $usc.user.sid.StartsWith('S-1-5-21-')) {
  375. throw "U1: srt-sb-custom not provisioned; got $($usc | ConvertTo-Json -Compress)"
  376. }
  377. Write-Host "U1 ok: --sandbox-user srt-sb-custom provisioned (sid=$($usc.user.sid))"
  378. # Re-install with the DEFAULT name and no --force → exit 13
  379. # (recorded name differs).
  380. & $Exe @(@('install') + $isl + $pr) 2>$null
  381. if ($LASTEXITCODE -ne 13) {
  382. throw "U1: install with default name over srt-sb-custom expected exit 13, got $LASTEXITCODE"
  383. }
  384. # Uninstall reads the recorded name and removes srt-sb-custom.
  385. Run (@('uninstall') + $isl)
  386. net user srt-sb-custom 2>$null
  387. if ($LASTEXITCODE -eq 0) {
  388. throw "U1: srt-sb-custom account still exists after uninstall"
  389. }
  390. Write-Host 'U1 ok: uninstall removes the recorded custom-name account'
  391. # ── U1c: --sandbox-user rejects a name we didn't create ────────────
  392. # `Administrators` resolves (as an alias) → the ownership guard at
  393. # ensure_user() entry fires (no HKLM setup record) → provision() bails
  394. # → exit 14.
  395. $u1c = & $Exe @(@('install', '--sandbox-user', 'Administrators') + $isl + $pr) 2>&1 | Out-String
  396. if ($LASTEXITCODE -ne 14) {
  397. throw "U1c: expected exit 14 for --sandbox-user Administrators, got $LASTEXITCODE. out: $u1c"
  398. }
  399. if ($u1c -notmatch 'already exists and was not created by srt-win') {
  400. throw "U1c: expected 'already exists and was not created by srt-win' in output. out: $u1c"
  401. }
  402. Write-Host 'U1c ok: --sandbox-user Administrators refused (exit 14)'
  403. # ── U1d: --sandbox-user rejects a foreign local user ───────────────
  404. # Pre-create a local user srt-win has no record of → same ownership
  405. # guard fires (never rotate a password we didn't set). Random suffix
  406. # so a leftover from a prior run can't false-positive.
  407. $foreign = "srt-sb-f$(Get-Random -Max 99999)"
  408. net user $foreign ('Aa1!' + [guid]::NewGuid().ToString('N').Substring(0,16)) /add /Y | Out-Null
  409. if ($LASTEXITCODE -ne 0) { throw "U1d: net user $foreign /add failed" }
  410. try {
  411. $u1d = & $Exe @(@('install', '--sandbox-user', $foreign) + $isl + $pr) 2>&1 | Out-String
  412. if ($LASTEXITCODE -ne 14) {
  413. throw "U1d: expected exit 14 for foreign local user, got $LASTEXITCODE. out: $u1d"
  414. }
  415. if ($u1d -notmatch 'already exists and was not created by srt-win') {
  416. throw "U1d: expected ownership-guard message. out: $u1d"
  417. }
  418. Write-Host "U1d ok: foreign local user '$foreign' refused (exit 14)"
  419. } finally {
  420. net user $foreign /delete 2>$null
  421. }
  422. # U1c/U1d may have created SANDBOX_GROUP before bailing — clean up.
  423. Run (@('uninstall') + $isl)
  424. Write-Host 'srt-win smoke: OK'