Remote magpie and System One addresses got "http://" put in front by several normalisers that each tested for "://" their own way, and one saved "http://http://host" stayed that way until the address was deleted and added again. provider.WithScheme (new, internal/provider/scheme.go) is the one rule: trim, add the default scheme when none is there, lowercase http/https, and collapse a repeated http/https prefix to the innermost one. Path and trailing slash are left as written; "host:3425/x?u=http://y" has no scheme. It is used by provider.normalize (every provider URL, on read), remoteMagpieEndpoints, DetectDecide, parseAddress and gui remoteAddr. app.js has withScheme as its mirror, used by hostOf, localAddress, vertexRoot and respellURL (whose gemini/openai regexes now match any letter case). Old data: an address saved with a doubled scheme is repaired when it is read (provider.normalize for providers and System One addresses, loadRemote for remote-magpie.json). The file is written out correctly on the next save; nobody has to delete and re-add. Reading alone does not rewrite the file. app.js: the core script budget is met without a raise; withScheme was cut down to fit (final package run after the rebase onfd9033cbf: gui and provider ok). Run: - go test ./internal/provider ./internal/gui ./internal/settings ./internal/cli on mote: ok, except TestCoreScriptBudget (284 over), fixed by shortening the JS and re-run alone: ok. - gofmt -l, go vet darwin, GOOS=windows, GOOS=linux -tags nogui: clean. - GUI on mica, chromium and webkit: scheme-once, local-address, custom-systemone-url, decide-endpoint-label, azure-endpoint, remote-*, i18n-keys, connect-address-kept: 158 of 164 pass; the 6 reds are "Remote magpie asks for the other magpie's address", red the same at origin/main5e933e153(#6099). - add-sheet-custom, provider-fetch-again, provider-gemini: 14 of 54 red on mica, the same 14 at origin/mainab98228cc(#5232, #6209). Red on the old code: scheme_test.go, remote_test.go (Go) and scheme-once.test.cjs (withScheme undefined) were run against an origin/main copy and failed there. Not tried: the place that produced "http://http://" was not found by reading (every guard used Contains("://")), so this hardens every normaliser instead of one site; no real remote magpie was used; the full GUI suite was not run.
202 lines
8.0 KiB
Go
202 lines
8.0 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/cookiejar"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"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://192.168.1.20:3430/", "http://192.168.1.20:3430/", "", ""},
|
|
{"HTTPS://Magpie.example.com/?k=abc", "https://Magpie.example.com/", "abc", ""},
|
|
{"http://http://192.168.1.20:3430", "http://192.168.1.20:3430/", "", ""},
|
|
{"https://https://magpie.example.com", "https://magpie.example.com/", "", ""},
|
|
{"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)
|
|
}
|
|
}
|
|
}
|
|
|
|
// An address saved with its scheme twice (eachg0 on X) reads as one.
|
|
func TestRemoteReadsADoubledScheme(t *testing.T) {
|
|
onePortHome(t)
|
|
if err := os.MkdirAll(filepath.Dir(remoteFile()), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(remoteFile(), []byte(`{"url":"http://http://192.168.1.20:3430/","key":"k"}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c, err := loadRemote()
|
|
if err != nil || c.URL != "http://192.168.1.20:3430/" {
|
|
t.Errorf("loadRemote = %+v, %v", c, err)
|
|
}
|
|
}
|
|
|
|
// 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, nil, 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)
|
|
}
|
|
}
|