YouWhat on Discord (macOS): adding a custom provider failed with "mkdir ~/.config/magpie: permission denied". ~/.config wasn't the user's (a command run under sudo makes it root's), so magpie couldn't make its folder. The app still ran because reads of a missing file mean "none", but every save failed. The GUI saves showed the raw error; the background writes (usage, quotas, caches) dropped theirs. Now: - appdir.Blocked() names the folder that keeps magpie from writing its config or cache folder: the folder itself, or the first one above it when it doesn't exist yet. Only folders inside the home folder are named. Unix only; on Windows it is "". - /api/state carries configBlocked. A failed save whose error is a permission refusal comes back with code config_blocked. The window asks once a load, and again over the editor whose save failed. "Not now" returns to that editor as it was left. The tray panel doesn't ask. - Repair (POST /api/config/repair): a folder the user owns gets u+rwx with no prompt. Another user's folder is chowned to the user, through the system's password prompt (update.AsAdmin, the updater's: osascript on a Mac, pkexec on Linux). That chown is not recursive for ~/.config, so the other apps' folders inside it stay as they are; magpie's own folder is chowned -R, since everything in it is magpie's. magpie web shows no prompt; it names the sudo command instead. Data never moves: every reader keeps the one path, so nothing splits. - Words in en, zh, zh-TW, ja and de. - The core-script budget goes up by 1,112 bytes (app.js stays under its own), with no room added. Run (on mote via mote-run, at this tree, based on17114b505): - gofmt -l: empty. go vet ./... (darwin), GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean. - go test -timeout 45m ./internal/appdir ./internal/update ./internal/gui ./internal/proc: appdir, update and proc ok. gui fails only TestMainWindowCorners/macOS_26_SDK, which fails the same on origin/main17114b505on mote (open: #3949). TestCoreScriptBudget's first red led to the raise above, and it passes after. - Red on old code: TestBlockedConfigIsRepairedInOneClick at17114b505fails with "failed save = 400 {"error":"mkdir …/.config/magpie: permission denied"}; want code config_blocked". Its sandbox HOME has .config at chmod 555, which is the reporter's error. With the change it passes: the save names ~/.config, Repair returns 204, the same save goes through into ~/.config/magpie/providers.json, and state clears. TestRepairOfAnotherUsersFolderAsksAnAdmin fakes another owner and checks that the script is "chown <uid>:<gid> '<~/.config>' && chmod u+rwx", with no -R, that magpie's own folder gets -R, and that web shows no prompt. - GUI: config-blocked.test.cjs, cli-behind.test.cjs and i18n-keys.test.cjs in Chromium and WebKit (en, zh), 12/12 ok. The new file checks the keys in zh, zh-TW, ja and de. It is red on the old assets (2/2 not ok in Chromium). Not tried: a real root-owned ~/.config and the real osascript or pkexec prompt (no sudo on the test boxes). The script runs through the updater's AsAdmin, which already runs a privileged script. The TUI and CLI don't offer the repair yet: #4287.
134 lines
5.0 KiB
Go
134 lines
5.0 KiB
Go
//go:build !windows
|
|
|
|
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// YouWhat on Discord: ~/.config wasn't the user's (sudo had made it), so
|
|
// adding a custom provider failed with "mkdir ~/.config/magpie: permission
|
|
// denied" and nothing said what to do. The failed save now names the
|
|
// folder (config_blocked), /api/state names it for the window's Repair,
|
|
// and Repair makes it writable again, after which the same save goes
|
|
// through, into the folder every reader looks in.
|
|
func TestBlockedConfigIsRepairedInOneClick(t *testing.T) {
|
|
if os.Getuid() == 0 {
|
|
t.Skip("root writes any folder")
|
|
}
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
dotConfig := filepath.Join(home, ".config")
|
|
if err := os.Mkdir(dotConfig, 0o555); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { os.Chmod(dotConfig, 0o755) })
|
|
h := Handler(nil, nil)
|
|
call := func(method, path, body string) (int, string) {
|
|
t.Helper()
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(method, path, strings.NewReader(body)))
|
|
return rec.Code, rec.Body.String()
|
|
}
|
|
const add = `{"id":"mine","name":"Mine","chat":"http://127.0.0.1:1/v1","key":"sk-a","models":["m1"],"new":true}`
|
|
|
|
code, body := call("POST", "/api/provider/save", add)
|
|
var failed struct{ Error, Code, Blocked string }
|
|
json.Unmarshal([]byte(body), &failed)
|
|
if code == 200 {
|
|
t.Fatalf("save into a folder the user can't write went through: %s", body)
|
|
}
|
|
if failed.Code != "config_blocked" || failed.Blocked != "~/.config" {
|
|
t.Fatalf("failed save = %d %s; want code config_blocked naming ~/.config, so the window can offer Repair", code, body)
|
|
}
|
|
_, body = call("GET", "/api/state", "")
|
|
var s struct {
|
|
ConfigBlocked string `json:"configBlocked"`
|
|
}
|
|
json.Unmarshal([]byte(body), &s)
|
|
if s.ConfigBlocked != "~/.config" {
|
|
t.Fatalf("state configBlocked = %q, want ~/.config", s.ConfigBlocked)
|
|
}
|
|
|
|
if code, body := call("POST", "/api/config/repair", "{}"); code != 204 {
|
|
t.Fatalf("repair: %d %s", code, body)
|
|
}
|
|
if code, body := call("POST", "/api/provider/save", add); code != 200 {
|
|
t.Fatalf("save after repair: %d %s", code, body)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dotConfig, "magpie", "providers.json")); err != nil {
|
|
t.Fatalf("the provider isn't in ~/.config/magpie, where every reader looks: %v", err)
|
|
}
|
|
_, body = call("GET", "/api/state", "")
|
|
s.ConfigBlocked = ""
|
|
json.Unmarshal([]byte(body), &s)
|
|
if s.ConfigBlocked != "" {
|
|
t.Fatalf("state configBlocked = %q after repair, want empty", s.ConfigBlocked)
|
|
}
|
|
}
|
|
|
|
// A folder another user owns (root, after sudo) can't be chmodded by the
|
|
// user: Repair asks the system's password prompt to give that folder alone
|
|
// back, not everything under it, while magpie's own folder is given back
|
|
// whole. Magpie web's page, on another device, never shows the prompt.
|
|
func TestRepairOfAnotherUsersFolderAsksAnAdmin(t *testing.T) {
|
|
if os.Getuid() == 0 {
|
|
t.Skip("root writes any folder")
|
|
}
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
dotConfig := filepath.Join(home, ".config")
|
|
if err := os.Mkdir(dotConfig, 0o555); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { os.Chmod(dotConfig, 0o755) })
|
|
wasAdmin, wasElevate, wasMine := asAdmin, canElevate, mine
|
|
t.Cleanup(func() { asAdmin, canElevate, mine = wasAdmin, wasElevate, wasMine })
|
|
mine = func(string) bool { return false } // as if root's
|
|
canElevate = func() bool { return true }
|
|
var scripts []string
|
|
asAdmin = func(why, script string) error {
|
|
scripts = append(scripts, script)
|
|
return os.Chmod(dotConfig, 0o755) // what chown + chmod u+rwx would leave
|
|
}
|
|
if err := repairConfig(true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(scripts) != 1 || strings.Contains(scripts[0], "-R") || !strings.Contains(scripts[0], "'"+dotConfig+"'") {
|
|
t.Fatalf("scripts = %q; want one chown of %s alone, not -R over other apps' folders", scripts, dotConfig)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dotConfig, "magpie")); err != nil {
|
|
t.Fatalf("magpie's folder not made after repair: %v", err)
|
|
}
|
|
|
|
// magpie's own folder taken: given back whole
|
|
own := filepath.Join(dotConfig, "magpie")
|
|
os.Chmod(own, 0o555)
|
|
t.Cleanup(func() { os.Chmod(own, 0o755) })
|
|
scripts = nil
|
|
asAdmin = func(why, script string) error { scripts = append(scripts, script); return os.Chmod(own, 0o755) }
|
|
if err := repairConfig(true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(scripts) != 1 || !strings.Contains(scripts[0], "chown -R ") {
|
|
t.Fatalf("scripts = %q; want magpie's own folder given back whole", scripts)
|
|
}
|
|
|
|
// magpie web: no prompt, the command to run instead
|
|
os.Chmod(own, 0o555)
|
|
scripts = nil
|
|
err := repairConfig(false)
|
|
if err == nil || len(scripts) != 0 || !strings.Contains(err.Error(), "sudo chown") {
|
|
t.Fatalf("web repair = %v, scripts %q; want no prompt and the command named", err, scripts)
|
|
}
|
|
}
|