package main
import (
"os"
"path/filepath"
"testing"
"craftdesign.group/quarry/internal/oltp"
)
// A read-only sync token (no i, no y) clones the private repository it
// was minted for, through the sync lane wired as serve wires it: Basic
// counted by syncSignIn, resolved by basicSyncLogin, the read gate
// behind. This is a CI runner's account reading its own repository's
// task names; it must not need push letters to read, its clones must
// never count as failed sign-ins, and it still may not push.
func TestAReadOnlySyncTokenClonesItsPrivateRepository(t *testing.T) {
r := newFirstPushRig(t, "website", "other")
r.commit(t, r.dirs["website"])
r.commit(t, r.dirs["other"])
db := r.rg.odb
if err := db.AddAccount("website-read", "pw-read-12345", false); err != nil {
t.Fatal(err)
}
if err := db.SetCaps("website-read", "website", "gjorz"); err != nil {
t.Fatal(err)
}
tok, minted, err := db.MintToken("website-read", oltp.TokenSpec{
Name: "runner", Scopes: []string{oltp.ScopeSync},
Repos: []string{"website"}, Ceiling: "gjorzh28",
})
if err != nil {
t.Fatal(err)
}
r.front.SetBasicLogin(r.rg.doors.basicSyncLogin)
r.front.SetBasicSignIn(r.rg.doors.syncSignIn)
// More clones than the limiter's free misses: a runner restarting in
// a loop must never hold its own account.
var dir string
for i := 0; i < 8; i++ {
dir = t.TempDir()
out, err := r.zit(t, dir, "clone", "--user", "website-read", "--password", tok, r.base+"website", ".")
if err != nil {
t.Fatalf("FAIL: clone %d with a read-only sync token was refused: %v\n%s", i+1, err, out)
}
}
// The same token may not push.
if err := os.WriteFile(filepath.Join(dir, "readonly.txt"), []byte("x\n"), 0o644); err != nil {
t.Fatal(err)
}
if out, err := r.zit(t, dir, "add", "readonly.txt"); err != nil {
t.Fatalf("add: %v\n%s", err, out)
}
if out, err := r.zit(t, dir, "commit", "-m", "should not land"); err != nil {
t.Fatalf("commit: %v\n%s", err, out)
}
out, err := r.zit(t, dir, "push", "--user", "website-read", "--password", tok, r.base+"website")
if err == nil {
t.Errorf("FAIL: a read-only sync token pushed a check-in\n%s", out)
}
fresh := t.TempDir()
if out, err := r.zit(t, fresh, "clone", "--user", "website-read", "--password", tok, r.base+"website", "."); err != nil {
t.Fatalf("FAIL: clone after the refused push: %v\n%s", err, out)
}
if _, err := os.Stat(filepath.Join(fresh, "readonly.txt")); err == nil {
t.Errorf("FAIL: the refused push's file is in the repository")
}
// A sync with nothing to send is a pull, as Fossil's server answers
// a read-only user's sync, not a refusal.
if out, err := r.zit(t, fresh, "sync", "--user", "website-read", "--password", tok, r.base+"website"); err != nil {
t.Errorf("FAIL: a sync with a read-only token was refused: %v\n%s", err, out)
}
// The token names website only: it does not read another private
// repository, even one its account could read.
if err := db.SetCaps("website-read", "other", "gjorz"); err != nil {
t.Fatal(err)
}
if out, err := r.zit(t, t.TempDir(), "clone", "--user", "website-read", "--password", tok, r.base+"other", "."); err == nil {
t.Errorf("FAIL: a token minted for website cloned another repository\n%s", out)
}
// A revoked token reads nothing.
if err := db.RevokeToken("website-read", minted.ID); err != nil {
t.Fatal(err)
}
if out, err := r.zit(t, t.TempDir(), "clone", "--user", "website-read", "--password", tok, r.base+"website", "."); err == nil {
t.Errorf("FAIL: a revoked read-only token still cloned\n%s", out)
}
if !t.Failed() {
t.Log("ok")
}
}