Files
magpie/internal/davsync/setuptext.go
T
yetone d21cb563e9 sync: Copy setup gives the sync setup as text, sealed with the passphrase (WJaunita45604 on X)
Setting sync up on a second computer meant typing the server, user,
password (or S3 keys, or GitHub token) and every tick again. Settings ›
Sync's form, with sync on, now has Copy setup. It copies one line,
`magpie-sync:…`, to paste into the same form on the other computer.

- The line is not base64 of the settings. It is the setup (server,
  password or token, endpoint, region, path style, branch, the ticks)
  sealed with the sync passphrase by backup.SealData, the AES-GCM and
  PBKDF2 envelope the server's file already uses. The passphrase is never
  in it. Whoever has the line and not the passphrase has nothing. The
  copied note says what it holds and that the same passphrase opens it.
- Pasted into any field of the form, the line is not typed in; the form
  asks for the passphrase, opens it (POST /api/davsync/setup-open), picks
  WebDAV, S3 or GitHub from its address and fills every field and tick.
  Nothing is saved until Turn on, and Turn on opens a pasted line first. A
  wrong passphrase, or text that isn't a setup, is said in the reader's
  language. A line a chat wrapped, or text around it, still opens.
- Not built: GitLab or MySQL sync backends.

Run (mica):
- At the pushed tree (run as 8fadc2ef8, the same tree before these bodies): go test ./internal/gui ./internal/davsync
  ok; sync-setup-text, i18n-keys and i18n-en-no-cjk in the 110 of 110 the
  commit before names. At the base 5b25d7e9e: ./internal/backup ok too.
  New TestSetupTextOpensWithItsPassphrase (WebDAV, S3, GitHub; no secret
  readable in the text or its decoded bytes; wrapped line; wrong
  passphrase), TestSetupTextNeedsSyncAndItsOwnText and
  TestDavsyncSetupTextRoutes (through the page's routes: off, copy, open on
  a computer with sync off, wrong passphrase, nothing saved).
- New sync-setup-text.test.cjs, Chromium and WebKit, en, zh, zh-TW, ja, de
  at 900px and en, zh at 440px: 42 of 42. The sync GUI files and
  i18n-keys / i18n-en-no-cjk: green but s3-sync webkit en, red the same
  alone on origin/main e4ea31321 (#3413).
- GOOS=linux go vet -tags nogui, GOOS=windows go vet: clean. gofmt clean.
- Broken by hand: SetupText unsealed (base64 of the JSON) made
  TestSetupTextOpensWithItsPassphrase red ("dav-app-password-9z is
  readable"); Turn on without opening the pasted line made all seven
  "pasted setup fills" GUI tests red. Both restored green.

Not tried: a real WebDAV, S3 or GitHub server between two computers, a
paste in the real app, WebKitGTK.
2026-11-13 18:39:28 +08:00

102 lines
3.5 KiB
Go

package davsync
import (
"encoding/base64"
"encoding/json"
"errors"
"regexp"
"strings"
"github.com/yetone/magpie/internal/backup"
)
// SetupPrefix begins a sync setup copied as text (WJaunita45604 on X: one
// copy, pasted on another computer, sets sync up there the same).
const SetupPrefix = "magpie-sync:"
// Setup is what a setup copied as text holds: the server, its password or
// token, and what is synced. Never the passphrase: the text is sealed with
// it, so it is typed on the other computer to open the text, as it is
// typed there to open the server's file.
type Setup struct {
URL string `json:"url"`
User string `json:"user,omitempty"`
Password string `json:"password,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
Region string `json:"region,omitempty"`
PathStyle bool `json:"pathStyle,omitempty"`
Branch string `json:"branch,omitempty"`
Keys bool `json:"keys"`
Agents bool `json:"agents"`
Library bool `json:"library"`
Memory bool `json:"memory"`
Usage bool `json:"usage,omitempty"`
SignIns bool `json:"signIns,omitempty"`
}
// ErrSetupOff is SetupText with sync off: there is nothing to copy.
var ErrSetupOff = errors.New("sync isn't set up on this computer")
// ErrNotSetup is OpenSetupText given text that isn't a copied setup.
var ErrNotSetup = errors.New("this isn't a sync setup copied from magpie")
// ErrSetupPassphrase is OpenSetupText with a passphrase that doesn't open it.
var ErrSetupPassphrase = errors.New("the passphrase doesn't open this setup: type the one sync uses on the other computer")
// SetupText is this computer's sync setup as one line of text, sealed with
// its passphrase (AES-GCM under a PBKDF2 key, as the server's file is), so
// the text alone gives no one the server's password.
func SetupText() (string, error) {
c, ok := Load()
if !ok {
return "", ErrSetupOff
}
if c.Passphrase == "" {
return "", ErrSetupOff
}
b, err := json.Marshal(Setup{URL: c.URL, User: c.User, Password: c.Password, Endpoint: c.Endpoint, Region: c.Region,
PathStyle: c.PathStyle, Branch: c.Branch, Keys: c.Keys, Agents: c.Agents, Library: c.library(), Memory: c.memoryOn(),
Usage: c.Usage, SignIns: c.SignIns})
if err != nil {
return "", err
}
sealed, err := backup.SealData(b, c.Passphrase)
if err != nil {
return "", err
}
return SetupPrefix + base64.RawURLEncoding.EncodeToString(sealed), nil
}
// setupBody is the setup's text after its prefix, to the first character
// that can't be in it: line breaks and spaces a chat or a mail wrapped it
// with are in it, and taken out after.
var setupBody = regexp.MustCompile(`^[A-Za-z0-9_\-+/=\s]*`)
// OpenSetupText opens what SetupText gave, with the passphrase it was sealed
// with. Text before the setup, and line breaks a chat put in it, are skipped.
func OpenSetupText(text, pass string) (Setup, error) {
i := strings.Index(text, SetupPrefix)
if i < 0 {
return Setup{}, ErrNotSetup
}
body := text[i+len(SetupPrefix):]
body = strings.Join(strings.Fields(setupBody.FindString(body)), "")
body = strings.NewReplacer("+", "-", "/", "_", "=", "").Replace(body)
sealed, err := base64.RawURLEncoding.DecodeString(body)
if err != nil {
return Setup{}, ErrNotSetup
}
plain, err := backup.OpenData(sealed, pass)
if errors.Is(err, backup.ErrPassphrase) {
return Setup{}, ErrSetupPassphrase
}
if err != nil {
return Setup{}, ErrNotSetup
}
var s Setup
if json.Unmarshal(plain, &s) != nil || s.URL == "" {
return Setup{}, ErrNotSetup
}
return s, nil
}