Files
magpie/internal/access/join_test.go
T
yetone e053c337aa gateway: join a shared magpie with a password (EV_John_Liu on X)
EV_John_Liu asked for quick setup on a LAN: the central magpie serves a
page behind a password where other PCs and Macs get their config and a
new key of their own.

Settings -> Network and sharing -> Join with a password sets the
password (at least 8 characters; only a salted PBKDF2-SHA256 hash is
kept, in join.json, 0600, written through temp-and-rename; a file that
can't be read or parsed means joining is closed). The row then shows the
address to give out, <shared address>/join, and Turn off joining.

/join is the key page (/me) opened to join: the visitor types a name for
the computer and the password. POST /v1/magpie/join makes a gateway key
named after that computer (or "Joined from <ip>"). The key is listed in
Gateway keys like any other, where it can be limited or removed. The
page shows the key once and then the setup for that key: Claude Code,
Codex, a new magpie tab (`magpie provider add remote-magpie '<key>'
url='<address>'`), OpenAI-compatible and curl.

Every route judged:
- lanGuard lets another machine reach GET /join and the join route
  without a key, only while magpie is shared. The route checks the
  password itself.
- A browser's POST must come from the page itself. Host must be one of
  this computer's own names (IP, localhost, MAGPIE_PUBLIC_URL's, its
  hostname), so a DNS-rebound name is refused. Origin must equal Host.
  Missing Origin and Origin "null" are refused, and so is any
  Sec-Fetch-Site but same-origin. corsGuard still refuses null, foreign
  and *.localhost Origins before this. curl and scripts, which send no
  browser marks, are judged on the password alone.
- Throttle: each try from another machine costs a token, a right
  password too. The bucket holds 10 and refills one every 10s, so a
  guesser gets about 360 tries an hour, and a flood of tries makes no
  flood of keys. Over the limit the route answers 429 with Retry-After.
- The password and key never go in a URL or a log line. The key comes
  back only in a no-store 201 body. Logs name the key's id and the
  caller's address.
- The routes are classed in the key-slot and read-only route lists.
  They carry no key.

Run on mote at this head (rebased on 065ca02c2):
- gofmt clean
- go vet: darwin, linux -tags nogui and windows, for access, gateway
  and gui
- go test: access, gui, and gateway's join, route-list, key page and
  LAN tests
- The whole gateway package ran before the rebase, and its only reds
  were the two route-list guards fixed here.
- GUI (node --test), chromium and webkit: lan-join (new), key-page,
  mtls-settings, allow-ips, i18n-keys, lan-docker, lan-lazycat,
  lan-address-height, side-gateway-lan: all ok before the rebase, and
  lan-join, key-page and i18n-keys ok again at this head.
- That GUI run caught a TDZ error: JOINING was read before its const,
  which broke /me too. It is fixed in this commit, and key-page went
  from red to green.

Broken by hand (each red, then put back):
- no throttle: TestJoinHoldsAGuesser red
- no lanGuard bypass: TestJoinMakesARevocableKey, TestJoinHoldsAGuesser
  and TestJoinClosed red
- no ownHost check: TestJoinRefusesOtherPages red
- joinRefusal returning "": rebound remote, cross-site and no-Origin
  browser POSTs each got a 201, red
- Dropping only the Origin comparison stayed green, because corsGuard
  refuses the same Origins first.

Not tried:
- a real two-machine LAN join with the built app
- a Windows or Linux run of the tests (vet only)
- the full suite, which starts after the tag
2026-11-10 22:42:11 +08:00

52 lines
1.4 KiB
Go

package access
import (
"os"
"strings"
"testing"
)
func init() { joinIter = 1000 }
func TestJoinPassword(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if JoinOn() || JoinPasswordOK("") || JoinPasswordOK("anything") {
t.Fatal("open with no password")
}
if err := SetJoinPassword("short"); err == nil {
t.Fatal("a 5-character password was taken")
}
if err := SetJoinPassword("long enough pw"); err != nil {
t.Fatal(err)
}
if !JoinOn() || !JoinPasswordOK("long enough pw") || JoinPasswordOK("long enough pW") || JoinPasswordOK("") {
t.Fatal("password check")
}
b, err := os.ReadFile(JoinPath())
if err != nil {
t.Fatal(err)
}
if string(b) == "" || strings.Contains(string(b), "long enough") {
t.Fatalf("join.json holds the password: %s", b)
}
if fi, _ := os.Stat(JoinPath()); fi.Mode().Perm() != 0o600 && os.PathSeparator == '/' {
t.Errorf("mode %v", fi.Mode())
}
// a file that can't be parsed shuts the door
if err := os.WriteFile(JoinPath(), []byte(`{"salt":"","hash":"","iter":0}`), 0o600); err != nil {
t.Fatal(err)
}
if JoinOn() || JoinPasswordOK("long enough pw") || JoinPasswordOK("") {
t.Fatal("a broken join.json let a password in")
}
if err := SetJoinPassword("long enough pw"); err != nil {
t.Fatal(err)
}
if err := SetJoinPassword(""); err != nil {
t.Fatal(err)
}
if JoinOn() || JoinPasswordOK("long enough pw") {
t.Fatal("still open after clearing")
}
}