xuanwo runs magpie on a server and wants the desktop app to show that
magpie's routing and usage. Browser access in Settings › Network and
sharing goes the other way: it serves this app's page to a browser.
Settings › Network and sharing has a new section, Another magpie. It takes
an address and a key. You can paste the whole link the server printed
(`magpie daemon install --web`, `magpie web`, or another app's Browser
access), and its k= becomes the key. The app then opens that magpie's own
web page in a window of its own. The window signs in through the server's
own webGuard with ?k=, the same way a browser does, and is traded for its
cookie. Nothing new is served here: the page in the window talks to the
server alone and gets no bound calls from this app.
- remoteAddr:
- https:// is used when no scheme is typed.
- Plain http is allowed only to an IP, localhost, a .local name or a
one-word name, so the key never crosses the internet in the clear.
This is also the rule a Mac's webview applies.
- userinfo and other schemes are refused.
- remoteProbe: before the window opens, it asks for the link without
following redirects. Only a 303 that sets a magpie_web_* cookie counts
as success. A 401, a 403 (allowed addresses), an untrusted certificate,
no answer, or any other reply is reported in Settings, translated.
- remote-magpie.json:
- It sits beside settings.json, mode 0600, and the key is never in
settings.json.
- GET /api/remote returns the address and hasKey, never the key.
- POST with no key reuses the key kept for the same address.
- POST /api/remote/forget removes the file.
- The routes are on the app's own windows' API only (!isWeb). A host that
can't open windows refuses.
Run (on mote, at this tree):
- go test -count=1 ./internal/gui/: ok.
- Go tests:
- TestRemoteAddr
- TestRemoteOpensTheOtherMagpie: real webHandler; wrong or missing key
refused, nothing kept; the pasted link opens; 0600; the key is not
returned; a cookie-jar client following the opened link reaches the
page behind the key; the kept key is reused; forget.
- TestRemoteNotMagpie
- TestRemoteNotOnBrowserPages
- GUI test, new: another-magpie.test.cjs. Covers 5 languages, 360px,
and a browser page with no section that never asks /api/remote.
- GUI suite: every GUI file that draws Settings › Network (18, the new one included), plus
i18n-keys, i18n-en-no-cjk and gui-de/ja/zh-tw. Chromium and WebKit:
307/307 pass. Also remote-magpie.test.cjs (the Remote magpie
provider's add sheet), run with the new file at the rebased head.
- gofmt -l: clean.
- go vet: darwin, GOOS=linux -tags nogui, and GOOS=windows on
internal/gui; ok.
Broken by hand:
- A 303 without the magpie cookie taken as signed in:
TestRemoteNotMagpie red.
- remoteRoutes outside !isWeb: TestRemoteNotOnBrowserPages red (200).
- renderRemote drawn on a browser's page, and errors not translated:
another-magpie.test.cjs red in zh/zh-TW/ja/de and the browser case,
in both engines.
Not tried:
- The real Wails window on macOS, Windows or Linux.
- A real server over https.
- Links on the remote page that open a new window (target=_blank,
window.open) inside the remote window.
- Settings › Text size does not rezoom a remote window that is already
open.
182 lines
7.2 KiB
Go
182 lines
7.2 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/cookiejar"
|
|
"net/http/httptest"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// remoteWin is the app's windows, as far as another magpie's window goes:
|
|
// the links it was asked to open.
|
|
type remoteWin struct {
|
|
Windows
|
|
opened []string
|
|
}
|
|
|
|
func (w *remoteWin) OpenRemote(link, title string) { w.opened = append(w.opened, link) }
|
|
|
|
func remotePost(t *testing.T, w Windows, path, body string) (int, string) {
|
|
t.Helper()
|
|
mux := http.NewServeMux()
|
|
remoteRoutes(mux, w)
|
|
r := httptest.NewRequest("POST", path, strings.NewReader(body))
|
|
rec := httptest.NewRecorder()
|
|
mux.ServeHTTP(rec, r)
|
|
return rec.Code, rec.Body.String()
|
|
}
|
|
|
|
func jsonStr(s string) string { b, _ := json.Marshal(s); return string(b) }
|
|
|
|
// What is typed as the other magpie's address: the link it printed, with
|
|
// its key taken out, or a bare address; plain http only where the key
|
|
// doesn't cross the internet.
|
|
func TestRemoteAddr(t *testing.T) {
|
|
for _, c := range []struct{ in, addr, key, err string }{
|
|
{"https://magpie.example.com/?k=0123456789abcdef-key", "https://magpie.example.com/", "0123456789abcdef-key", ""},
|
|
{"magpie.example.com:3430", "https://magpie.example.com:3430/", "", ""},
|
|
{"http://192.168.1.20:3430/?k=abc&view=usage#x", "http://192.168.1.20:3430/?view=usage", "abc", ""},
|
|
{"http://100.101.1.2:3430", "http://100.101.1.2:3430/", "", ""},
|
|
{"http://nas.local:3430", "http://nas.local:3430/", "", ""},
|
|
{"http://server:3430", "http://server:3430/", "", ""},
|
|
{"https://example.com/magpie/", "https://example.com/magpie/", "", ""},
|
|
{"http://magpie.example.com:3430", "", "", "name on the internet"},
|
|
{"https://u:p@magpie.example.com", "", "", "user name"},
|
|
{"ftp://example.com", "", "", "http:// or https://"},
|
|
{"file:///etc/passwd", "", "", "isn't an address"},
|
|
{" ", "", "", "type the other"},
|
|
} {
|
|
addr, key, err := remoteAddr(c.in)
|
|
if c.err != "" {
|
|
if err == nil || !strings.Contains(err.Error(), c.err) {
|
|
t.Errorf("%q: err %v, want %q", c.in, err, c.err)
|
|
}
|
|
continue
|
|
}
|
|
if err != nil || addr != c.addr || key != c.key {
|
|
t.Errorf("%q: %q %q %v, want %q %q", c.in, addr, key, err, c.addr, c.key)
|
|
}
|
|
}
|
|
}
|
|
|
|
// xuanwo on X: the app opens a server's magpie, its routing and usage, in
|
|
// a window. The key is checked against that magpie's own page before the
|
|
// window opens, kept 0600 on this computer, never read back into the page,
|
|
// and the link the window opens signs in to the other magpie's page as a
|
|
// browser does.
|
|
func TestRemoteOpensTheOtherMagpie(t *testing.T) {
|
|
onePortHome(t)
|
|
const key = "0123456789abcdef-remote"
|
|
reached := 0
|
|
other := httptest.NewServer(webHandler("3430", key, webCookieAge, http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
reached++
|
|
rw.WriteHeader(http.StatusTeapot)
|
|
})))
|
|
defer other.Close()
|
|
|
|
w := &remoteWin{}
|
|
// a wrong key: said, nothing opened, nothing kept
|
|
code, body := remotePost(t, w, "/api/remote", `{"url":`+jsonStr(other.URL)+`,"key":"0123456789abcdef-wrong"}`)
|
|
if code != http.StatusBadRequest || !strings.Contains(body, "turned the key away") || len(w.opened) != 0 || reached != 0 {
|
|
t.Fatalf("wrong key: %d %s %v", code, body, w.opened)
|
|
}
|
|
if _, err := os.Stat(remoteFile()); !os.IsNotExist(err) {
|
|
t.Fatalf("a refused key was kept: %v", err)
|
|
}
|
|
// no key at all
|
|
if code, body := remotePost(t, w, "/api/remote", `{"url":`+jsonStr(other.URL)+`}`); code != http.StatusBadRequest || !strings.Contains(body, "type that magpie's key") {
|
|
t.Fatalf("no key: %d %s", code, body)
|
|
}
|
|
|
|
// the link it printed, pasted whole
|
|
code, body = remotePost(t, w, "/api/remote", `{"url":`+jsonStr(other.URL+"/?k="+key)+`}`)
|
|
if code != http.StatusOK || len(w.opened) != 1 || w.opened[0] != other.URL+"/?k="+key {
|
|
t.Fatalf("open: %d %s %v", code, body, w.opened)
|
|
}
|
|
if strings.Contains(body, key) {
|
|
t.Fatalf("the key went back to the page: %s", body)
|
|
}
|
|
if reached != 0 {
|
|
t.Fatal("the check read the page behind the key")
|
|
}
|
|
fi, err := os.Stat(remoteFile())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if runtime.GOOS != "windows" && fi.Mode().Perm() != 0o600 {
|
|
t.Fatalf("remote-magpie.json is %v", fi.Mode().Perm())
|
|
}
|
|
mux := http.NewServeMux()
|
|
remoteRoutes(mux, w)
|
|
rec := httptest.NewRecorder()
|
|
mux.ServeHTTP(rec, httptest.NewRequest("GET", "/api/remote", nil))
|
|
if s := rec.Body.String(); strings.Contains(s, key) || !strings.Contains(s, `"hasKey":true`) || !strings.Contains(s, other.URL) {
|
|
t.Fatalf("GET /api/remote: %s", s)
|
|
}
|
|
|
|
// the window's link signs in to the other magpie's page, as a browser
|
|
jar, _ := cookiejar.New(nil)
|
|
res, err := (&http.Client{Jar: jar, Timeout: 5 * time.Second}).Get(w.opened[0])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
res.Body.Close()
|
|
if res.StatusCode != http.StatusTeapot || reached != 1 || strings.Contains(res.Request.URL.String(), key) {
|
|
t.Fatalf("the window's link: %d at %s, reached %d", res.StatusCode, res.Request.URL, reached)
|
|
}
|
|
|
|
// opened again without the key: the one kept for the address
|
|
if code, body := remotePost(t, w, "/api/remote", `{"url":`+jsonStr(other.URL)+`}`); code != http.StatusOK || len(w.opened) != 2 || w.opened[1] != other.URL+"/?k="+key {
|
|
t.Fatalf("again: %d %s %v", code, body, w.opened)
|
|
}
|
|
// forgotten
|
|
if code, _ := remotePost(t, w, "/api/remote/forget", `{}`); code != http.StatusOK {
|
|
t.Fatalf("forget: %d", code)
|
|
}
|
|
if _, err := os.Stat(remoteFile()); !os.IsNotExist(err) {
|
|
t.Fatalf("forgotten, still there: %v", err)
|
|
}
|
|
}
|
|
|
|
// Something at the address that isn't a magpie page, or none: said, and
|
|
// no window opened.
|
|
func TestRemoteNotMagpie(t *testing.T) {
|
|
onePortHome(t)
|
|
plain := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(rw, r, "/login", http.StatusSeeOther) // a redirect with no magpie cookie
|
|
}))
|
|
defer plain.Close()
|
|
w := &remoteWin{}
|
|
if code, body := remotePost(t, w, "/api/remote", `{"url":`+jsonStr(plain.URL)+`,"key":"0123456789abcdef-key"}`); code != http.StatusBadRequest || !strings.Contains(body, "not as a magpie page") || len(w.opened) != 0 {
|
|
t.Fatalf("not magpie: %d %s", code, body)
|
|
}
|
|
plain.Close()
|
|
if code, body := remotePost(t, w, "/api/remote", `{"url":`+jsonStr(plain.URL)+`,"key":"0123456789abcdef-key"}`); code != http.StatusBadRequest || !strings.Contains(body, "doesn't answer") || len(w.opened) != 0 {
|
|
t.Fatalf("gone: %d %s", code, body)
|
|
}
|
|
}
|
|
|
|
// A page served to a browser (magpie web, the app's Browser access) has no
|
|
// /api/remote: only the app's own windows open another magpie, and a
|
|
// browser can't read or change the key kept for it.
|
|
func TestRemoteNotOnBrowserPages(t *testing.T) {
|
|
onePortHome(t)
|
|
h := webPageHandler(t, webHost{}, nil)
|
|
for _, m := range []string{"GET", "POST"} {
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(m, "/api/remote", strings.NewReader(`{"url":"http://127.0.0.1:1"}`)))
|
|
if rec.Code == http.StatusOK || strings.Contains(rec.Body.String(), "hasKey") {
|
|
t.Fatalf("%s /api/remote on a browser's page: %d %s", m, rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
// and a host that can't open windows refuses rather than keeping it
|
|
if code, body := remotePost(t, webHost{}, "/api/remote", `{"url":"http://127.0.0.1:1/?k=0123456789abcdef-key"}`); code != http.StatusBadRequest || !strings.Contains(body, "only the app") {
|
|
t.Fatalf("browser host: %d %s", code, body)
|
|
}
|
|
}
|