gui: WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS reaches magpie's WebView2 on Windows again (#2753, LuYquan)

The reporter's Nahimic DLL gets WebView2's renderer killed by code
integrity and the window goes black; the usual workaround,
WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS=--disable-features=RendererCodeIntegrity,
never reached magpie's renderer. The cause is not WebView2 skipping the
variable because magpie passes its own arguments: Wails v3.0.0-beta.24's
loader blanks the variable in its package init
(webviewloader.preventEnvAndRegistryOverrides), before any magpie code
runs. Seen on the Windows box: a debug build's windowsOptions read "".

- internal/webview2env reads the variable at init. It imports only
  syscall, so by Go's init order (first ready package by import path)
  it is initialized before os, and so before Wails' loader, which needs
  os.
- windowsOptions sorts it into Wails' DisabledFeatures, EnabledFeatures
  and AdditionalBrowserArgs. Wails writes msSmartScreenProtection plus
  the user's features as one --disable-features (Chromium keeps only
  the last of two), one --enable-features, and the rest as written.
  A list given twice is merged, each feature once.
- Nothing is added by default: RendererCodeIntegrity stays on unless
  the user asks.

ProcessFailed: not done. Wails' edge.Chromium has a ProcessFailedCallback,
but setupChromium never sets it, the package is under wails/v3/internal
(not importable) and a window's chromium is unexported; Wails has no
public event for a dead renderer. Reloading on renderer death needs a
change in Wails.

Run:
- gofmt -l: clean. go vet darwin, GOOS=linux -tags nogui, GOOS=windows:
  clean. Builds for the three.
- go test ./internal/gui -run 'TestWebView2|TestWindowsOptions' on
  macOS, and the windows/amd64 test binary on the Windows box (sandbox
  dir, my own process name): TestWebView2ArgsSurviveWailsInit passes
  there with os="" (Wails blanked it) and disabled=[RendererCodeIntegrity].
- The real app on the Windows box (portable sandbox, MAGPIE_ADDR :3481,
  USERPROFILE/APPDATA/LOCALAPPDATA redirected) started with
  "--disable-features=RendererCodeIntegrity --enable-features=msTestFoo
  --lang=de": every msedgewebview2.exe of it carries one
  --disable-features with both features, --enable-features=msTestFoo
  and --lang=de. Before, the same launch showed only
  --disable-features=msSmartScreenProtection and --lang=zh-CN.

Broken by hand:
- windowsOptions reading os.Getenv instead of webview2env.Args: on the
  Windows box TestWebView2ArgsSurviveWailsInit goes red
  (os="" disabled=[]).
- webView2Args ignoring the env (""): TestWebView2ArgsMergeTheUsers red
  in "the reporter's" and "twice".
- --disable-features treated as an other argument: red in three cases,
  including the one-switch count.

Full suite at origin/main 1a3b92688 + this and the #2757/#2755 GUI
commits, go test -timeout 45m ./..., sandbox HOME: gofmt -l clean, vet
darwin/linux(nogui)/windows clean, 57 packages ok. At load 475-548 from
other sessions' suites, agent (#2544, #2723), gateway (#2108, #2191;
TestClaudeAccountsSpentFallBackToCodex #2858), library (#2853), davsync,
provider and plugin (#2869; TestGatewayHookMixedPiPackage and
TestHostOnBunLackingLibsSaysWhich #2865) ran past 45m, and internal/gui
had TestSetAnswersWhileDevinAndProvidersHang red (#1375). Rerun alone:
internal/gui and internal/webview2env ok. An earlier full run at
d783df46c + this alone: 61 ok, the same load reds. No asset changes, so
no GUI run.
This commit is contained in:
yetone
2026-10-19 07:23:04 +08:00
parent 41f5107b91
commit 3608ed782e
4 changed files with 206 additions and 2 deletions
+8 -1
View File
@@ -29,6 +29,7 @@ import (
"github.com/yetone/magpie/internal/shortcut"
"github.com/yetone/magpie/internal/stats"
"github.com/yetone/magpie/internal/update"
"github.com/yetone/magpie/internal/webview2env"
)
//go:embed tray.png
@@ -281,11 +282,17 @@ func (h *host) zoomWindows() {
// windowsOptions are the app's Windows options. Portable, WebView2 keeps
// its profile in data\webview2: left to Wails it is %APPDATA%\magpie.exe,
// a folder a portable magpie must not leave behind (#508). Installed, it
// stays there, so what the pages kept isn't lost.
// stays there, so what the pages kept isn't lost. The browser arguments
// the user set in WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS, which Wails' loader
// blanks before WebView2 can read them, are merged into Wails' own (#2753).
func windowsOptions() application.WindowsOptions {
disabled, enabled, other := webView2Args(webview2env.Args)
return application.WindowsOptions{
DisableQuitOnLastWindowClosed: true,
WebviewUserDataPath: appdir.WebView(),
DisabledFeatures: disabled,
EnabledFeatures: enabled,
AdditionalBrowserArgs: other,
}
}
+68 -1
View File
@@ -1,6 +1,10 @@
package gui
import "fmt"
import (
"fmt"
"slices"
"strings"
)
// magpie's windows are WebView2 on Windows, a runtime Windows 10 doesn't
// always have. Without it Wails hands the failure to ErrorHandler and exits,
@@ -60,3 +64,66 @@ var webView2Words = map[string][2]string{
"Bis dahin öffnet \"magpie web\" magpie im Browser und \"magpie tui\" im Terminal.\r\n\r\n" +
"Die Download-Seite jetzt öffnen?"},
}
// webView2Args sorts what the user put in WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS
// (webview2env.Args) into Wails' three options, which it passes as the
// app's own arguments: the features to disable, the ones to enable, and
// every other argument as written. Wails' loader blanks the variable
// itself, so without this a user's --disable-features=RendererCodeIntegrity
// never reached magpie's renderer (#2753, LuYquan: an injected DLL got it
// killed and the window went black). Wails puts its own
// msSmartScreenProtection first in DisabledFeatures and writes the list as
// one --disable-features (and EnabledFeatures as one --enable-features):
// Chromium keeps only the last of a repeated switch, so two would drop one
// of them. A list given more than once is merged, each feature once, in
// the order given. Arguments are split as Windows splits a command line on
// spaces, a double-quoted run kept whole.
func webView2Args(env string) (disabled, enabled, other []string) {
for _, a := range splitArgs(env) {
switch k, v, _ := strings.Cut(a, "="); k {
case "--disable-features":
disabled = addFeatures(disabled, v)
case "--enable-features":
enabled = addFeatures(enabled, v)
default:
other = append(other, a)
}
}
return disabled, enabled, other
}
// addFeatures adds a comma list's features to have, each once.
func addFeatures(have []string, list string) []string {
for _, f := range strings.Split(strings.Trim(list, `"`), ",") {
if f = strings.TrimSpace(f); f != "" && !slices.Contains(have, f) {
have = append(have, f)
}
}
return have
}
// splitArgs splits a command line on spaces and tabs outside double quotes,
// keeping each argument as written, its quotes too.
func splitArgs(s string) []string {
var out []string
var cur strings.Builder
quoted := false
for _, r := range s {
switch {
case r == '"':
quoted = !quoted
cur.WriteRune(r)
case (r == ' ' || r == '\t' || r == '\n' || r == '\r') && !quoted:
if cur.Len() > 0 {
out = append(out, cur.String())
cur.Reset()
}
default:
cur.WriteRune(r)
}
}
if cur.Len() > 0 {
out = append(out, cur.String())
}
return out
}
+111
View File
@@ -0,0 +1,111 @@
//go:build !nogui
package gui
import (
"fmt"
"os"
"os/exec"
"slices"
"strings"
"testing"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/yetone/magpie/internal/webview2env"
)
// wailsBrowserArgs is the command line Wails v3.0.0-beta.24 makes from the
// app's Windows options (setupChromium, webview_window_windows.go): its own
// msSmartScreenProtection first among the disabled features, each list
// once as one switch, then the other arguments as given.
func wailsBrowserArgs(o application.WindowsOptions) []string {
var disabled []string
for _, f := range append([]string{"msSmartScreenProtection"}, o.DisabledFeatures...) {
if !slices.Contains(disabled, f) {
disabled = append(disabled, f)
}
}
args := []string{"--disable-features=" + strings.Join(disabled, ",")}
if len(o.EnabledFeatures) > 0 {
args = append(args, "--enable-features="+strings.Join(o.EnabledFeatures, ","))
}
return append(args, o.AdditionalBrowserArgs...)
}
// #2753 (LuYquan): magpie's own --disable-features made WebView2 skip the
// user's WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS, so RendererCodeIntegrity
// stayed on and an injected DLL got the renderer killed. Now the user's
// arguments are merged into magpie's: one --disable-features holding both
// lists (Chromium keeps only the last of two), one --enable-features, and
// every other argument as written.
func TestWebView2ArgsMergeTheUsers(t *testing.T) {
for _, c := range []struct {
name, env string
want []string
}{
{"none set", "", []string{"--disable-features=msSmartScreenProtection"}},
{"the reporter's", "--disable-features=RendererCodeIntegrity",
[]string{"--disable-features=msSmartScreenProtection,RendererCodeIntegrity"}},
{"twice, with magpie's own and a repeat", `--disable-features=A,msSmartScreenProtection --disable-features="B, A"`,
[]string{"--disable-features=msSmartScreenProtection,A,B"}},
{"enable and others kept", " --enable-features=X,Y\t--remote-debugging-port=9333 --proxy-server=\"http://h:1 x\" --enable-features=Y,Z --lang=de ",
[]string{"--disable-features=msSmartScreenProtection", "--enable-features=X,Y,Z", "--remote-debugging-port=9333", `--proxy-server="http://h:1 x"`, "--lang=de"}},
{"a bare switch", "--disable-features", []string{"--disable-features=msSmartScreenProtection"}},
} {
t.Run(c.name, func(t *testing.T) {
was := webview2env.Args
t.Cleanup(func() { webview2env.Args = was })
webview2env.Args = c.env
got := wailsBrowserArgs(windowsOptions())
if fmt.Sprint(got) != fmt.Sprint(c.want) {
t.Fatalf("WebView2 is started with %q, want %q", got, c.want)
}
n := 0
for _, a := range got {
if strings.HasPrefix(a, "--disable-features") {
n++
}
}
if n != 1 {
t.Fatalf("%d --disable-features in %q, Chromium keeps only the last", n, got)
}
})
}
}
// Nothing in the defaults turns renderer code integrity off: only a user
// who asks for it gets it (#2753).
func TestWebView2KeepsCodeIntegrityByDefault(t *testing.T) {
was := webview2env.Args
t.Cleanup(func() { webview2env.Args = was })
webview2env.Args = ""
if got := strings.Join(wailsBrowserArgs(windowsOptions()), " "); strings.Contains(got, "RendererCodeIntegrity") {
t.Fatalf("default arguments %q turn RendererCodeIntegrity off", got)
}
}
// On Windows Wails' WebView2 loader blanks the variable in its init, which
// runs before magpie's code (#2753: the reporter's flag never arrived).
// The value magpie was started with still reaches the options: this test
// starts its own binary with the variable set, where the loader's init has
// run as in the app.
func TestWebView2ArgsSurviveWailsInit(t *testing.T) {
if os.Getenv("MAGPIE_TEST_WEBVIEW2_CHILD") == "1" {
fmt.Printf("os=%q disabled=%q\n", os.Getenv(webview2env.Name), windowsOptions().DisabledFeatures)
return
}
exe, err := os.Executable()
if err != nil {
t.Fatal(err)
}
cmd := exec.Command(exe, "-test.run=^TestWebView2ArgsSurviveWailsInit$", "-test.count=1")
cmd.Env = append(os.Environ(), "MAGPIE_TEST_WEBVIEW2_CHILD=1", webview2env.Name+"=--disable-features=RendererCodeIntegrity")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("child: %v\n%s", err, out)
}
if !strings.Contains(string(out), `disabled=["RendererCodeIntegrity"]`) {
t.Fatalf("the started value didn't reach the options:\n%s", out)
}
t.Logf("%s", strings.SplitN(string(out), "\n", 2)[0])
}
+19
View File
@@ -0,0 +1,19 @@
// Package webview2env keeps WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS as magpie
// was started with. Wails' WebView2 loader blanks the variable in its own
// init (webviewloader.preventEnvAndRegistryOverrides, so neither it nor the
// registry's policy can change the browser's arguments), before magpie's
// code runs, and a user's --disable-features=RendererCodeIntegrity never
// reached the renderer (#2753). This package reads it first. Go
// initializes, at each step, the first package by import path whose
// imports are all done. This one imports only syscall, so it is ready as
// soon as syscall is, and it sorts before "os"; the loader needs os, so
// os, and the loader after it, can't start while this one waits.
package webview2env
import "syscall"
// Name is the variable WebView2 reads extra browser arguments from.
const Name = "WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS"
// Args is the variable's value when magpie started, "" when it wasn't set.
var Args, _ = syscall.Getenv(Name)