Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

fix(installer): register the update task without PowerShell

Beeba5af47f1fc6
diff --git a/desktop/installer/nectarlink.nsi b/desktop/installer/nectarlink.nsi
index edd47e2..53cd015 100644
--- a/desktop/installer/nectarlink.nsi
+++ b/desktop/installer/nectarlink.nsi
@@ -195,12 +195,37 @@ Section "Install"
nsExec::Exec 'netsh advfirewall firewall add rule name="${FIREWALL_RULE}" dir=in action=allow program="$INSTDIR\${EXE}" enable=yes profile=private,domain description="Lets paired phones connect to NectarLink."'
; Updates without an administrator prompt: a task that runs this app as
- ; SYSTEM to install a newer release, which signed-in users may start but
- ; not change (update-task.ps1, desktop/app/src/updater.rs).
+ ; SYSTEM to install a newer release (desktop/app/src/updater.rs). Only
+ ; SYSTEM and administrators may change it; signed-in users may read and
+ ; start it. Registered with schtasks from a task definition rather than
+ ; PowerShell, which security software can hold up without a word, leaving
+ ; setup stuck at this step.
!insertmacro Progress "step updates"
InitPluginsDir
- File "/oname=$PLUGINSDIR\update-task.ps1" "update-task.ps1"
- nsExec::Exec 'powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "$PLUGINSDIR\update-task.ps1" -Exe "$INSTDIR\${EXE}"'
+ FileOpen $0 "$PLUGINSDIR\update-task.xml" w
+ FileWriteUTF16LE /BOM $0 '<?xml version="1.0" encoding="UTF-16"?>$\r$\n'
+ FileWriteUTF16LE $0 '<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">$\r$\n'
+ FileWriteUTF16LE $0 ' <RegistrationInfo>$\r$\n'
+ FileWriteUTF16LE $0 ' <Description>Installs new versions of NectarLink from its GitHub releases.</Description>$\r$\n'
+ FileWriteUTF16LE $0 ' <SecurityDescriptor>D:(A;;FA;;;SY)(A;;FA;;;BA)(A;;GRGX;;;AU)</SecurityDescriptor>$\r$\n'
+ FileWriteUTF16LE $0 ' </RegistrationInfo>$\r$\n'
+ FileWriteUTF16LE $0 ' <Principals>$\r$\n'
+ FileWriteUTF16LE $0 ' <Principal id="Author"><UserId>S-1-5-18</UserId><RunLevel>HighestAvailable</RunLevel></Principal>$\r$\n'
+ FileWriteUTF16LE $0 ' </Principals>$\r$\n'
+ FileWriteUTF16LE $0 ' <Settings>$\r$\n'
+ FileWriteUTF16LE $0 ' <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>$\r$\n'
+ FileWriteUTF16LE $0 ' <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>$\r$\n'
+ FileWriteUTF16LE $0 ' <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>$\r$\n'
+ FileWriteUTF16LE $0 ' <AllowStartOnDemand>true</AllowStartOnDemand>$\r$\n'
+ FileWriteUTF16LE $0 ' <Enabled>true</Enabled>$\r$\n'
+ FileWriteUTF16LE $0 ' <ExecutionTimeLimit>PT30M</ExecutionTimeLimit>$\r$\n'
+ FileWriteUTF16LE $0 ' </Settings>$\r$\n'
+ FileWriteUTF16LE $0 ' <Actions Context="Author">$\r$\n'
+ FileWriteUTF16LE $0 ' <Exec><Command>"$INSTDIR\${EXE}"</Command><Arguments>--update-service</Arguments></Exec>$\r$\n'
+ FileWriteUTF16LE $0 ' </Actions>$\r$\n'
+ FileWriteUTF16LE $0 '</Task>$\r$\n'
+ FileClose $0
+ nsExec::Exec 'schtasks /Create /TN "Nectarlink Update" /XML "$PLUGINSDIR\update-task.xml" /F'
!insertmacro Progress "step shortcuts"
CreateShortCut "$SMPROGRAMS\${DISPLAY}.lnk" "$INSTDIR\${EXE}" "" "$INSTDIR\${EXE}" 0 "" "" "Your phone, on your PC"
diff --git a/desktop/installer/update-task.ps1 b/desktop/installer/update-task.ps1
deleted file mode 100644
index d5a75d1..0000000
--- a/desktop/installer/update-task.ps1
+++ /dev/null
@@ -1,25 +0,0 @@
-# SPDX-License-Identifier: GPL-3.0-or-later
-# Registers the "Nectarlink Update" task (run by the installer, as an
-# administrator). The task runs the app as SYSTEM with --update-service,
-# which installs the latest GitHub release when it's newer, after checking
-# it against the release's SHA-256 sums (desktop/app/src/updater.rs). It
-# takes no input from whoever starts it. Signed-in users may start it, so
-# updates need no administrator prompt; only SYSTEM and administrators may
-# change or delete it.
-param(
- [Parameter(Mandatory)][string]$Exe
-)
-$ErrorActionPreference = 'Stop'
-$name = 'Nectarlink Update'
-
-$action = New-ScheduledTaskAction -Execute $Exe -Argument '--update-service'
-$principal = New-ScheduledTaskPrincipal -UserId 'S-1-5-18' -LogonType ServiceAccount -RunLevel Highest
-$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries `
- -MultipleInstances IgnoreNew -ExecutionTimeLimit (New-TimeSpan -Minutes 30)
-Register-ScheduledTask -TaskName $name -Action $action -Principal $principal -Settings $settings `
- -Description 'Installs new versions of NectarLink from its GitHub releases.' -Force | Out-Null
-
-# SYSTEM and administrators: full control. Authenticated users: read and run.
-$service = New-Object -ComObject Schedule.Service
-$service.Connect()
-$service.GetFolder('\').GetTask($name).SetSecurityDescriptor('D:(A;;FA;;;SY)(A;;FA;;;BA)(A;;GRGX;;;AU)', 0)
diff --git a/docs/technical/releasing.md b/docs/technical/releasing.md
index 226d7ea..929c01f 100644
--- a/docs/technical/releasing.md
+++ b/docs/technical/releasing.md
@@ -64,7 +64,7 @@ Signing. Either plugs in after `package.ps1` builds the installer, before
seconds after start and daily. With **Install updates automatically** on,
it waits until nobody has used the PC for 10 minutes and nothing is in
progress, then starts the **Nectarlink Update** task that the installer
- registers (`desktop/installer/update-task.ps1`). The task runs as SYSTEM,
+ registers (`desktop/installer/nectarlink.nsi`). The task runs as SYSTEM,
downloads and checks the installer itself, and runs it silently; a hidden
relauncher reopens the app afterwards. The task logs to `update.log` in the
install folder. Details and the security reasoning:
diff --git a/docs/technical/security-model.md b/docs/technical/security-model.md
index eb050ec..622d6ba 100644
--- a/docs/technical/security-model.md
+++ b/docs/technical/security-model.md
@@ -117,7 +117,7 @@ Even when a capability toggle is enabled, sensitive operations require interacti
## Automatic Updates and Release Verification
-Implemented in `desktop/app/src/updater.rs` (`win/http.rs`), `desktop/installer/update-task.ps1` and `android/app/src/main/kotlin/app/nectarlink/android/update/AppUpdater.kt`.
+Implemented in `desktop/app/src/updater.rs` (`win/http.rs`), `desktop/installer/nectarlink.nsi` (the task's definition) and `android/app/src/main/kotlin/app/nectarlink/android/update/AppUpdater.kt`.
- **Endpoint**: Both apps query `https://api.github.com/repos/thtbee/nectarlink/releases/latest` over HTTPS with the OS TLS stack (`WinHTTP`, `HttpURLConnection`). Only `https://` URLs are fetched, redirects stay on HTTPS, and responses are size-capped while they are read. No device identifiers or telemetry are sent.
- **What is accepted**: a published release (not a draft or pre-release) whose version is newer than the running one, with an installer named `Nectarlink-*-x64-setup.exe` (Windows, a plain file name) or `*-android.apk`, and a `SHA256SUMS.txt` that lists it. The download is rejected unless its SHA-256 matches. Android additionally refuses an APK not signed with the app's own key.
diff --git a/scripts/smoke-installer.ps1 b/scripts/smoke-installer.ps1
index 9236750..83da570 100644
--- a/scripts/smoke-installer.ps1
+++ b/scripts/smoke-installer.ps1
@@ -31,6 +31,11 @@ function Check($what, [bool]$ok) {
Write-Host "ok: $what"
}
function FirewallRule { (netsh advfirewall firewall show rule name=Nectarlink) -match 'Nectarlink' }
+function UpdateTask {
+ $scheduler = New-Object -ComObject Schedule.Service
+ $scheduler.Connect()
+ try { $scheduler.GetFolder('\').GetTask('Nectarlink Update') } catch { $null }
+}
Write-Host "Installing $Setup"
Start-Process $Setup -ArgumentList '/S' -Wait
@@ -40,6 +45,16 @@ Check "it's in Apps (uninstall entry)" (Test-Path $uninstallKey)
Check "it's in the Start menu" (Test-Path $shortcut)
Check "the firewall lets phones in" ([bool](FirewallRule))
Check "license notices are included" ((Get-Item (Join-Path $dir 'THIRD-PARTY-NOTICES.txt')).Length -gt 100KB)
+$task = UpdateTask
+Check "the update task is registered" ($null -ne $task)
+Check "the update task runs the installed app as SYSTEM" (
+ $task.Definition.Principal.UserId -match '^(SYSTEM|S-1-5-18|NT AUTHORITY\\SYSTEM)$' -and
+ $task.Definition.Actions.Item(1).Path.Trim('"') -eq $exe -and
+ $task.Definition.Actions.Item(1).Arguments -eq '--update-service')
+# 4: the DACL. Signed-in users may read and run it, not change it.
+$dacl = $task.GetSecurityDescriptor(4)
+Check "signed-in users may only read and start the update task" (
+ $dacl -match '\(A;;GRGX;;;AU\)' -and $dacl -match '\(A;;FA;;;SY\)' -and $dacl -match '\(A;;FA;;;BA\)')
# The installed copy starts on its own files and quits when asked.
$app = Start-Process $exe -ArgumentList '--minimized', '--data-dir', $data -PassThru
@@ -58,6 +73,7 @@ Check "the app folder is gone" (-not (Test-Path $dir))
Check "the uninstall entry is gone" (-not (Test-Path $uninstallKey))
Check "the Start menu shortcut is gone" (-not (Test-Path $shortcut))
Check "the firewall rule is gone" (-not (FirewallRule))
+Check "the update task is gone" ($null -eq (UpdateTask))
Write-Host "Installer smoke test passed."
# netsh reports "no rules match" with a failing exit code; that was the point.
exit 0