Files
magpie/internal/update/blocked.go
T
yetone 9ca64d104f update, gui: a new version Windows won't start is never left in place — tried before it goes in, and a restart into one that doesn't say it is up puts the running one back; that version isn't downloaded again by the clock, and Settings › About › Version says why (#894, hisiling) — on Windows 11 with Smart App Control on, the update put the new unsigned exe in place of the running one and restarted into it; CodeIntegrity (3033/3077) refused it, magpie never started again and the working version sat in .old. Now, on Windows, the downloaded exe is first started suspended with no window and ended at once (Windows checks a program when its process is created), and one refused stays out: the running exe isn't moved. At the restart the new process is given MAGPIE_STARTED, a file it writes first thing (AwaitPredecessor); the old one waits up to 20s for it and, if the start fails (ERROR_SYSTEM_INTEGRITY_POLICY_VIOLATION 4551, 1260 or any other), the new process quits before saying so, or it says nothing in time (then it is ended), puts the running exe back over the new one (the new one renamed .blocked if it can't go) and keeps running instead of quitting; nothing notes it as updated in the app. The version is noted in the config folder (update-blocked.json, this machine's only, never synced; gone once a version at least as new runs); the clock's checks leave it alone and show why, a Check clicked downloads and tries it once more. Settings › About › Version: for an application control policy, "0.1.x didn't start on this computer: Windows' Smart App Control (or another app control policy) blocked the new version, which isn't signed. magpie stays on 0.1.y. Download it and run it yourself, or turn Smart App Control off and check again", else the reason, with Download and Check; the error in full on hover. en/zh/ja/de. A user who double-clicks the refused exe is refused by Windows before magpie runs; only signing helps there. TestRelaunchRefusedPutsBack (4551 at the start), TestRelaunchSilentPutsBack (no word in time, killed; quit at once), TestRelaunchReportedStays, TestReportStarted, TestBlockedNote and TestUpdateThatWontStart (the restart doesn't quit, the note, the clock not downloading it again, a Check that does) are new and fail without the change; on Windows TestInstallBinaryRefusedStaysOut, TestTrialStart and TestStartCheckedPutsBackForReal (real processes: not a program, quits at once, runs on silent) pass on the Windows 11 test box with the whole internal/update package (Smart App Control itself isn't on there, so the refusal is simulated). update-blocked.test.cjs (Chromium and WebKit, en and zh: the reason and both versions, full error on hover, Download opens the release, Check downloads, the other reason, no select, no scroll, no left border) fails without the change; update-check, update-mirror, update-auto, update-wait and update-download-percent pass in WebKit; click-scroll's request-list case fails now and then the same on main. go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
2026-10-05 19:43:31 +08:00

219 lines
7.1 KiB
Go

package update
// A new version that this computer won't run (#894): Windows 11's Smart
// App Control, or another application control policy, refuses magpie's
// unsigned builds it doesn't know yet, and an update that put one in place
// left nothing that starts. The download is tried before it goes in
// (canStart), the restart into it waits for it to say it is up
// (AwaitPredecessor writes MAGPIE_STARTED's file), and a version refused
// either way is put back and noted (NoteBlocked), so the automatic checks
// don't download it again; a check asked for tries it once more.
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"syscall"
"time"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/proc"
)
// BlockedError is a new version that couldn't be started on this computer.
// Nothing of it is left in place: the version running is still the one
// installed.
type BlockedError struct {
Err error
}
func (e *BlockedError) Error() string {
if e.Policy() {
return "Windows didn't let the new version start (" + e.Err.Error() + "): Smart App Control or another application control policy blocks magpie's unsigned builds; download it from " + Site + " and run it yourself, or turn Smart App Control off, then check for updates again"
}
return "the new version couldn't start: " + e.Err.Error() + "; download it from " + Site + " and run it yourself"
}
func (e *BlockedError) Unwrap() error { return e.Err }
// Policy is whether Windows' code integrity refused the program: Smart App
// Control, App Control for Business (WDAC), AppLocker or a software
// restriction policy, which the user can't let through from magpie.
func (e *BlockedError) Policy() bool { return policyRefused(e.Err) }
// errAccessDisabledByPolicy is AppLocker's and software restriction
// policies' refusal; ERROR_SYSTEM_INTEGRITY_* (4550…4599) are code
// integrity's, Smart App Control's among them ("An Application Control
// policy has blocked this file", ERROR_SYSTEM_INTEGRITY_POLICY_VIOLATION).
const (
errAccessDisabledByPolicy syscall.Errno = 1260
errIntegrityPolicy syscall.Errno = 4551
)
func policyRefused(err error) bool {
var n syscall.Errno
if !errors.As(err, &n) {
return false
}
return n == errAccessDisabledByPolicy || n >= 4550 && n < 4600
}
// canStart is whether the program at path can be started here, which is
// tried before it is put in place: Windows starts it suspended and ends
// it at once, so none of it runs; elsewhere nothing refuses a build that
// downloaded whole. A var for tests.
var canStart = trialStart
// startWait is how long a restarted magpie has to say it is up.
var startWait = 20 * time.Second
// startedEnv names the file a relaunched magpie writes once it runs
// (AwaitPredecessor), for the one that started it to know.
const startedEnv = "MAGPIE_STARTED"
// launch starts a program with args and env, detached from this one; the
// channel is closed when it exits, and kill ends it. A var for tests.
var launch = func(exe string, args, env []string) (<-chan struct{}, func(), error) {
cmd := proc.Command(exe, args...)
cmd.Env = env
cmd.Stdin, cmd.Stdout, cmd.Stderr = nil, nil, nil
detach(cmd)
if err := cmd.Start(); err != nil {
return nil, nil, err
}
exited := make(chan struct{})
go func() { cmd.Wait(); close(exited) }()
return exited, func() { cmd.Process.Kill() }, nil
}
// startChecked starts the new version at exe and waits for it to say it
// is up. When it can't be started, quits first, or says nothing in time,
// it is ended and old — this running version, moved aside for it — goes
// back in its place.
func startChecked(exe, old string, args, env []string) error {
marker := filepath.Join(os.TempDir(), "magpie-started-"+strconv.Itoa(os.Getpid()))
os.Remove(marker)
defer os.Remove(marker)
exited, kill, err := launch(exe, args, append(env, startedEnv+"="+marker))
if err == nil {
err = awaitStarted(marker, exited, kill)
}
if err == nil {
return nil
}
blocked := &BlockedError{Err: err}
if rerr := putBack(exe, old); rerr != nil {
return errors.Join(blocked, rerr)
}
return blocked
}
// awaitStarted waits for marker to be written, the started program's
// exit (exited closing) or startWait, whichever comes first.
func awaitStarted(marker string, exited <-chan struct{}, kill func()) error {
tick := time.NewTicker(50 * time.Millisecond)
defer tick.Stop()
deadline := time.After(startWait)
for {
if _, err := os.Stat(marker); err == nil {
return nil
}
select {
case <-exited:
if _, err := os.Stat(marker); err == nil {
return nil // it said so and then went on as another
}
return errors.New("it quit as soon as it started")
case <-deadline:
kill()
// its file let go of, to be put back over
select {
case <-exited:
case <-time.After(5 * time.Second):
}
return fmt.Errorf("it didn't start within %s", startWait)
case <-tick.C:
}
}
}
// putBack puts old in exe's place, the new version that was there out of
// the way (removed, else kept as exe.blocked).
func putBack(exe, old string) error {
var err error
for i := 0; ; i++ {
if err = os.Remove(exe); err == nil || os.IsNotExist(err) {
break
}
if err = os.Rename(exe, exe+".blocked"); err == nil {
break
}
if i == len(asideWaits) {
return fmt.Errorf("couldn't take the new version out of %s: %w", filepath.Base(exe), err)
}
time.Sleep(asideWaits[i])
}
if err := os.Rename(old, exe); err != nil {
return fmt.Errorf("couldn't put %s back from %s: %w", filepath.Base(exe), old, err)
}
return nil
}
// reportStarted writes the file the magpie that started this one waits
// for, once.
func reportStarted() {
p := os.Getenv(startedEnv)
if p == "" {
return
}
os.Unsetenv(startedEnv)
os.WriteFile(p, []byte(strconv.Itoa(os.Getpid())), 0o644)
}
// Blocked is a version this computer refused to start, and why.
type Blocked struct {
Version string `json:"version"`
Error string `json:"error"`
Policy bool `json:"policy,omitempty"` // application control (Smart App Control)
At time.Time `json:"at"`
}
// blockedFile is this computer's own note, never synced.
func blockedFile() string { return filepath.Join(appdir.Config(), "update-blocked.json") }
// NoteBlocked notes that version couldn't start here.
func NoteBlocked(version string, err error) Blocked {
b := Blocked{Version: version, Error: err.Error(), At: time.Now()}
var be *BlockedError
if errors.As(err, &be) {
b.Error, b.Policy = be.Err.Error(), be.Policy()
}
if data, merr := json.Marshal(b); merr == nil {
os.MkdirAll(filepath.Dir(blockedFile()), 0o755)
os.WriteFile(blockedFile(), data, 0o644)
}
return b
}
// ReadBlocked is the version noted as refused here, while it is newer
// than current; once current has caught up (put in by hand), the note
// goes.
func ReadBlocked(current string) *Blocked {
data, err := os.ReadFile(blockedFile())
if err != nil {
return nil
}
var b Blocked
if json.Unmarshal(data, &b) != nil || b.Version == "" || !Newer(b.Version, current) {
os.Remove(blockedFile())
return nil
}
return &b
}
// ClearBlocked forgets the note: the version started after all.
func ClearBlocked() { os.Remove(blockedFile()) }