Files
wekan/docs/Security/Sandboxes/Windows/sandbox.ps1
T
Lauri Ojansivu 6ac70c4cdd Windows sandbox: VSCodium in Windows Sandbox, seeing only Downloads\repos\wekan.
The Windows counterpart of the Linux Flatpak sandbox and the macOS
sandbox-exec one. Windows Sandbox is a disposable Hyper-V virtual machine
that comes with Windows 10 and 11 Pro, Enterprise and Education; it sees
nothing of the host but the folders mapped into it.

- docs/Security/Sandboxes/Windows/sandbox.ps1 (on the host; sandbox.bat
  starts it) offers to turn the Windows Sandbox feature on, copies
  user.name and user.email into the checkout's .git\config, and writes
  .tools\sandbox\wekan-vscodium.wsb: the checkout as the only mapped
  folder, read-write at the same path under the sandbox's user; network
  on; microphone, camera and printers off; ProtectedClient; half of the
  host's memory, 4 to 16 GiB.
- sandbox-logon.ps1 runs inside at logon: TEMP, TMP and TMPDIR in
  .tools\tmp, git's configuration in .tools\home, PortableGit and the
  VSCodium zip downloaded once into .tools - each verified (VSCodium's
  .sha256, the SHA-256 in Git for Windows' release notes) before it is
  unpacked - and VSCodium opened on the checkout, its settings and
  extensions in .tools\vscodium.
- README.md is the design: what is refused and allowed, how, and the
  trade-offs.

Not run on Windows yet: written and reviewed on macOS, where PowerShell is
not available. tests/windowsSandbox.test.cjs reads the scripts and pins
the single mapping, the .tools locations, verification before unpacking,
and that native commands cannot stop Windows PowerShell 5.1.

Thanks to xet7 !
2026-10-28 00:03:42 +02:00

106 lines
4.6 KiB
PowerShell

# sandbox.ps1 - start VSCodium in Windows Sandbox, which can read and write only
# this WeKan checkout (%USERPROFILE%\Downloads\repos\wekan).
#
# Usage: powershell -ExecutionPolicy Bypass -File docs\Security\Sandboxes\Windows\sandbox.ps1 [-SetupOnly]
# or double-click sandbox.bat beside this file.
#
# The Windows counterpart of the Linux Flatpak sandbox
# (docs/Security/Sandboxes/vscodium) and the macOS one (../macOS). Windows
# Sandbox is a disposable Hyper-V virtual machine: the only host folder it sees
# is the one mapped here. Design and trade-offs: README.md beside this file.
#
# Everything this writes is inside the checkout's .tools\ (ignored by git and by
# Meteor). Turning the Windows Sandbox feature on is the one change to Windows
# itself, and it asks first.
param([switch]$SetupOnly)
$ErrorActionPreference = 'Stop'
$Repo = (Resolve-Path (Join-Path $PSScriptRoot '..\..\..\..')).Path
$Expected = Join-Path $env:USERPROFILE 'Downloads\repos\wekan'
if ($Repo -ne $Expected) {
Write-Host "Note: the documented Windows checkout is $Expected; this one is $Repo."
Write-Host " The sandbox is made for $Repo."
}
$Tools = Join-Path $Repo '.tools'
$SandboxDir = Join-Path $Tools 'sandbox'
foreach ($dir in @('tmp', 'sandbox', 'home', 'vscodium')) {
New-Item -ItemType Directory -Force -Path (Join-Path $Tools $dir) | Out-Null
}
# 1) Windows Sandbox: Windows 10 and 11 Pro, Enterprise and Education. It is a
# Windows feature, off by default; turning it on needs an administrator and
# a restart.
$exe = Join-Path $env:WINDIR 'System32\WindowsSandbox.exe'
if (-not (Test-Path $exe)) {
Write-Host 'Windows Sandbox is not turned on.'
$answer = Read-Host 'Turn it on now? This asks for administrator rights and needs a restart. [y/N]'
if ($answer -notmatch '^[yY]') { exit 1 }
Start-Process -Verb RunAs -Wait powershell -ArgumentList @(
'-NoProfile', '-Command',
'Enable-WindowsOptionalFeature -Online -FeatureName Containers-DisposableClientVM -All -NoRestart')
Write-Host 'Restart Windows, then run this again.'
exit 0
}
# 2) The commit author. The sandbox has no ~\.gitconfig of the host, so the name
# and e-mail are kept in this checkout's own .git\config.
if (Get-Command git -ErrorAction SilentlyContinue) {
# Native commands under 'Stop' fail Windows PowerShell 5.1 on any stderr line.
$ErrorActionPreference = 'Continue'
foreach ($key in @('user.name', 'user.email')) {
$checkoutValue = (& git -C $Repo config --local --get $key 2>$null)
if (-not $checkoutValue) {
$hostValue = (& git config --global --get $key 2>$null)
if ($hostValue) {
& git -C $Repo config --local $key $hostValue
Write-Host "Set $key for this checkout: $hostValue"
} else {
Write-Warning "No $key in the host's git configuration; set it with: git -C `"$Repo`" config $key ..."
}
}
}
$ErrorActionPreference = 'Stop'
}
# 3) The sandbox configuration. The checkout is mapped read-write at the same
# place under the sandbox's own user, WDAGUtilityAccount; nothing else of
# the host is visible. The logon script sets up and starts VSCodium.
$Inside = 'C:\Users\WDAGUtilityAccount\Downloads\repos\wekan'
$Logon = "$Inside\docs\Security\Sandboxes\Windows\sandbox-logon.ps1"
function ConvertTo-XmlText([string]$text) { [System.Security.SecurityElement]::Escape($text) }
$Wsb = Join-Path $SandboxDir 'wekan-vscodium.wsb'
# Half of the host's memory, 4 GiB at least and 16 GiB at most: a Meteor build
# needs room, and the host keeps the other half.
$hostMb = [int]((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory / 1MB)
$MemoryMb = [Math]::Min(16384, [Math]::Max(4096, [int]($hostMb / 2)))
@"
<!-- Generated by docs\Security\Sandboxes\Windows\sandbox.ps1 - edit that, not this. -->
<Configuration>
<MappedFolders>
<MappedFolder>
<HostFolder>$(ConvertTo-XmlText $Repo)</HostFolder>
<SandboxFolder>$Inside</SandboxFolder>
<ReadOnly>false</ReadOnly>
</MappedFolder>
</MappedFolders>
<Networking>Enable</Networking>
<vGPU>Enable</vGPU>
<AudioInput>Disable</AudioInput>
<VideoInput>Disable</VideoInput>
<PrinterRedirection>Disable</PrinterRedirection>
<ProtectedClient>Enable</ProtectedClient>
<MemoryInMB>$MemoryMb</MemoryInMB>
<LogonCommand>
<Command>powershell.exe -NoProfile -ExecutionPolicy Bypass -File "$Logon"</Command>
</LogonCommand>
</Configuration>
"@ | Set-Content -Encoding UTF8 -Path $Wsb
Write-Host "Sandbox ready: $Wsb"
if ($SetupOnly) { exit 0 }
# 4) Start it. Windows Sandbox runs one instance at a time.
Start-Process -FilePath $Wsb
Write-Host 'Windows Sandbox is starting; VSCodium opens in it once its setup has run.'