feat(M8a): principals + per-share roles + write enforcement 👥🔐
V2-SPEC §5.2 (write-side, staged before read-side M9). Migration #2 (rides the M8 runner, chains 0→1→2 — verified on real .10 data, version 2, all counts preserved): - principals table + devices.principal_id (every v1 device backfills to one synthetic 'owner' principal — byte-identical to v1 until a share opts in). - members(share, principal_id, role, can_reshare) + shares.acl_mode. A share with zero grants is 'legacy' (every device an implicit owner = v1); the first grant flips it to 'explicit' = deny-by-default. - EffectiveRole(device, share) resolves device→principal→role; legacy⇒owner. - Push write gate: role>=editor AND the access.writable clamp (which can only REMOVE write). Legacy shares reduce to the v1 writable bit exactly. - devbox-hub member set/rm/list + principal <device> <principal> admin CLI — the populate path, so the role layer has a real consumer now (no dead code). Tests: legacy↔explicit lifecycle, deny-by-default, viewer<editor, HTTP push gate (403 unmembered/viewer, opens at editor). 18 pkgs -race green; hub cross-compiles linux/amd64; windows builds.
This commit is contained in:
parent
0d8b2505da
commit
81fbc5c81a
5 changed files with 439 additions and 2 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cmp"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
@ -37,6 +38,8 @@ func main() {
|
||||||
tokenCmd(),
|
tokenCmd(),
|
||||||
readonlyCmd(),
|
readonlyCmd(),
|
||||||
revokeCmd(),
|
revokeCmd(),
|
||||||
|
memberCmd(),
|
||||||
|
principalCmd(),
|
||||||
gcCmd(),
|
gcCmd(),
|
||||||
)
|
)
|
||||||
if err := root.Execute(); err != nil {
|
if err := root.Execute(); err != nil {
|
||||||
|
|
@ -168,6 +171,134 @@ func revokeCmd() *cobra.Command {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// roleNames maps the CLI role words to their numeric levels (and back).
|
||||||
|
var roleNames = map[string]int{"viewer": meta.RoleViewer, "editor": meta.RoleEditor, "admin": meta.RoleAdmin, "owner": meta.RoleOwner}
|
||||||
|
|
||||||
|
func roleName(level int) string {
|
||||||
|
for n, l := range roleNames {
|
||||||
|
if l == level {
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("role%d", level)
|
||||||
|
}
|
||||||
|
|
||||||
|
// memberCmd manages per-share roles (M8a). The FIRST grant on a share flips it
|
||||||
|
// from legacy (every device an implicit owner = v1) to explicit/deny-by-default,
|
||||||
|
// so granting roles is how a share becomes multi-owner — handle with care.
|
||||||
|
func memberCmd() *cobra.Command {
|
||||||
|
var data string
|
||||||
|
cmd := &cobra.Command{Use: "member", Short: "👥 manage per-share roles (M8a)"}
|
||||||
|
|
||||||
|
var reshare bool
|
||||||
|
set := &cobra.Command{
|
||||||
|
Use: "set <share> <principal> <viewer|editor|admin|owner>",
|
||||||
|
Short: "grant/update a principal's role on a share (flips the share to explicit ACLs)",
|
||||||
|
Args: cobra.ExactArgs(3),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
role, ok := roleNames[args[2]]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("unknown role %q (want viewer|editor|admin|owner)", args[2])
|
||||||
|
}
|
||||||
|
db, err := openDB(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
if err := db.SetMember(args[0], args[1], role, reshare); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Fprintf(cmd.OutOrStdout(), "%s is now %s on %s (share now uses explicit ACLs)\n", args[1], args[2], args[0])
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
set.Flags().BoolVar(&reshare, "reshare", false, "allow this member to delegate (the +s bit)")
|
||||||
|
|
||||||
|
rm := &cobra.Command{
|
||||||
|
Use: "rm <share> <principal>",
|
||||||
|
Short: "revoke a principal's role on a share",
|
||||||
|
Args: cobra.ExactArgs(2),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
db, err := openDB(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
if err := db.RemoveMember(args[0], args[1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Fprintf(cmd.OutOrStdout(), "removed %s from %s\n", args[1], args[0])
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
list := &cobra.Command{
|
||||||
|
Use: "list <share>",
|
||||||
|
Short: "list a share's role grants",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
db, err := openDB(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
ms, err := db.Members(args[0])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(ms) == 0 {
|
||||||
|
fmt.Fprintf(cmd.OutOrStdout(), "%s is a legacy share (every device is an implicit owner)\n", args[0])
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, m := range ms {
|
||||||
|
s := ""
|
||||||
|
if m.CanReshare {
|
||||||
|
s = " +s"
|
||||||
|
}
|
||||||
|
fmt.Fprintf(cmd.OutOrStdout(), "%-20s %s%s\n", m.Principal, roleName(m.Role), s)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range []*cobra.Command{set, rm, list} {
|
||||||
|
c.Flags().StringVar(&data, "data", "./devbox-hub-data", "hub data directory")
|
||||||
|
}
|
||||||
|
cmd.AddCommand(set, rm, list)
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
// principalCmd assigns a device to a principal (M8a). Every v1 device belongs to
|
||||||
|
// the synthetic 'owner' principal; point a device at its own principal so per-share
|
||||||
|
// roles can distinguish people, not just devices.
|
||||||
|
func principalCmd() *cobra.Command {
|
||||||
|
var data, name string
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "principal <device> <principal>",
|
||||||
|
Short: "🪪 assign a device to a principal (creates the principal if new)",
|
||||||
|
Args: cobra.ExactArgs(2),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
db, err := openDB(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
pname := cmp.Or(name, args[1])
|
||||||
|
if err := db.EnsurePrincipal(args[1], pname, time.Now().Unix()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := db.SetDevicePrincipal(args[0], args[1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Fprintf(cmd.OutOrStdout(), "device %s now belongs to principal %s\n", args[0], args[1])
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cmd.Flags().StringVar(&data, "data", "./devbox-hub-data", "hub data directory")
|
||||||
|
cmd.Flags().StringVar(&name, "name", "", "human name for the principal (defaults to its id)")
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
func gcCmd() *cobra.Command {
|
func gcCmd() *cobra.Command {
|
||||||
var data string
|
var data string
|
||||||
var keep int
|
var keep int
|
||||||
|
|
|
||||||
|
|
@ -106,9 +106,44 @@ INSERT OR IGNORE INTO snapshots (share, id, parent_id, device_id, created_at, ma
|
||||||
JOIN snapshots src ON src.id = s.head_snapshot
|
JOIN snapshots src ON src.id = s.head_snapshot
|
||||||
WHERE s.head_snapshot IS NOT NULL AND s.head_snapshot != ''
|
WHERE s.head_snapshot IS NOT NULL AND s.head_snapshot != ''
|
||||||
AND NOT EXISTS (SELECT 1 FROM snapshots o WHERE o.share = s.name AND o.id = s.head_snapshot);
|
AND NOT EXISTS (SELECT 1 FROM snapshots o WHERE o.share = s.name AND o.id = s.head_snapshot);
|
||||||
|
`,
|
||||||
|
// 2 (v2, M8a): identity + per-share roles. A *principal* (person/account) sits
|
||||||
|
// above the device: the device stays the auth + revocation unit, the principal
|
||||||
|
// is the authorization subject. Every v1 device backfills to ONE synthetic
|
||||||
|
// `owner` principal, so behavior is byte-identical to v1 until a share opts into
|
||||||
|
// explicit ACLs. shares.acl_mode='legacy' (the default) means every device is an
|
||||||
|
// implicit owner (v1); the first member grant flips it to 'explicit' →
|
||||||
|
// deny-by-default. The v1 `access.writable` bit stays a clamp that can only
|
||||||
|
// REMOVE write, never add it. NO inline FK on the ALTER (SQLite requires a NULL
|
||||||
|
// default for that) — integrity is maintained in the app.
|
||||||
|
`
|
||||||
|
CREATE TABLE principals (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
created_at INTEGER NOT NULL
|
||||||
|
);
|
||||||
|
INSERT INTO principals (id, name, created_at) VALUES ('owner', 'owner', 0);
|
||||||
|
ALTER TABLE devices ADD COLUMN principal_id TEXT NOT NULL DEFAULT 'owner';
|
||||||
|
CREATE TABLE members (
|
||||||
|
share TEXT NOT NULL,
|
||||||
|
principal_id TEXT NOT NULL,
|
||||||
|
role INTEGER NOT NULL,
|
||||||
|
can_reshare INTEGER NOT NULL DEFAULT 0,
|
||||||
|
PRIMARY KEY (share, principal_id)
|
||||||
|
);
|
||||||
|
ALTER TABLE shares ADD COLUMN acl_mode TEXT NOT NULL DEFAULT 'legacy';
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Role levels on a share (higher = more authority). A device's effective write
|
||||||
|
// right is role>=RoleEditor AND its access.writable clamp.
|
||||||
|
const (
|
||||||
|
RoleViewer = 10
|
||||||
|
RoleEditor = 20
|
||||||
|
RoleAdmin = 30
|
||||||
|
RoleOwner = 40
|
||||||
|
)
|
||||||
|
|
||||||
// DB is the hub metadata store.
|
// DB is the hub metadata store.
|
||||||
type DB struct{ sql *sql.DB }
|
type DB struct{ sql *sql.DB }
|
||||||
|
|
||||||
|
|
@ -322,6 +357,120 @@ func (d *DB) SetWritable(deviceID, share string, writable bool) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- principals, members & roles (M8a) -----------------------------------
|
||||||
|
|
||||||
|
// Member is one principal's role on a share.
|
||||||
|
type Member struct {
|
||||||
|
Principal string
|
||||||
|
Role int
|
||||||
|
CanReshare bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// EffectiveRole returns a device's role on a share and whether the share is in
|
||||||
|
// explicit-ACL mode. In legacy mode (the v1 default — no member grants yet) every
|
||||||
|
// known device is an implicit RoleOwner, so v1 behavior is preserved exactly. In
|
||||||
|
// explicit mode the device's principal must hold a member row, else RoleViewer-1
|
||||||
|
// (0) = deny-by-default. An unknown/revoked device gets 0 in either mode.
|
||||||
|
func (d *DB) EffectiveRole(deviceID, share string) (role int, explicit bool, err error) {
|
||||||
|
var pid string
|
||||||
|
err = d.sql.QueryRow(`SELECT principal_id FROM devices WHERE id=? AND revoked=0`, deviceID).Scan(&pid)
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return 0, false, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return 0, false, err
|
||||||
|
}
|
||||||
|
var mode string
|
||||||
|
err = d.sql.QueryRow(`SELECT acl_mode FROM shares WHERE name=?`, share).Scan(&mode)
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
mode = "legacy" // unknown share: stay v1-permissive, push fails later if truly absent
|
||||||
|
} else if err != nil {
|
||||||
|
return 0, false, err
|
||||||
|
}
|
||||||
|
if mode != "explicit" {
|
||||||
|
return RoleOwner, false, nil
|
||||||
|
}
|
||||||
|
err = d.sql.QueryRow(`SELECT role FROM members WHERE share=? AND principal_id=?`, share, pid).Scan(&role)
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return 0, true, nil // deny-by-default
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return 0, true, err
|
||||||
|
}
|
||||||
|
return role, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnsurePrincipal creates a principal if absent (idempotent).
|
||||||
|
func (d *DB) EnsurePrincipal(id, name string, createdAt int64) error {
|
||||||
|
_, err := d.sql.Exec(
|
||||||
|
`INSERT INTO principals (id, name, created_at) VALUES (?,?,?)
|
||||||
|
ON CONFLICT(id) DO NOTHING`, id, name, createdAt)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDevicePrincipal points a device at a principal (the principal must exist).
|
||||||
|
func (d *DB) SetDevicePrincipal(deviceID, principalID string) error {
|
||||||
|
res, err := d.sql.Exec(`UPDATE devices SET principal_id=? WHERE id=?`, principalID, deviceID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n, _ := res.RowsAffected(); n == 0 {
|
||||||
|
return fmt.Errorf("no such device %q", deviceID)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMember grants (or updates) a principal's role on a share and flips the share
|
||||||
|
// into explicit-ACL mode — once a share has any member grant it is deny-by-default.
|
||||||
|
func (d *DB) SetMember(share, principalID string, role int, canReshare bool) error {
|
||||||
|
tx, err := d.sql.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
cr := 0
|
||||||
|
if canReshare {
|
||||||
|
cr = 1
|
||||||
|
}
|
||||||
|
if _, err := tx.Exec(
|
||||||
|
`INSERT INTO members (share, principal_id, role, can_reshare) VALUES (?,?,?,?)
|
||||||
|
ON CONFLICT(share, principal_id) DO UPDATE SET role=excluded.role, can_reshare=excluded.can_reshare`,
|
||||||
|
share, principalID, role, cr); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := tx.Exec(`UPDATE shares SET acl_mode='explicit' WHERE name=?`, share); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return tx.Commit()
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveMember revokes a principal's grant on a share. The share stays in explicit
|
||||||
|
// mode (removing the last member = a locked share, the deliberate deny-all state).
|
||||||
|
func (d *DB) RemoveMember(share, principalID string) error {
|
||||||
|
_, err := d.sql.Exec(`DELETE FROM members WHERE share=? AND principal_id=?`, share, principalID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Members lists a share's grants (empty for a legacy share).
|
||||||
|
func (d *DB) Members(share string) ([]Member, error) {
|
||||||
|
rows, err := d.sql.Query(`SELECT principal_id, role, can_reshare FROM members WHERE share=? ORDER BY role DESC, principal_id`, share)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var out []Member
|
||||||
|
for rows.Next() {
|
||||||
|
var m Member
|
||||||
|
var cr int
|
||||||
|
if err := rows.Scan(&m.Principal, &m.Role, &cr); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
m.CanReshare = cr != 0
|
||||||
|
out = append(out, m)
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
// --- snapshots & chunks --------------------------------------------------
|
// --- snapshots & chunks --------------------------------------------------
|
||||||
|
|
||||||
// Snapshot is one accepted change set on a share.
|
// Snapshot is one accepted change set on a share.
|
||||||
|
|
|
||||||
|
|
@ -377,6 +377,88 @@ func userVersion(t *testing.T, db *sql.DB) int {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestRolesLegacyAndExplicit covers M8a write-side authorization: a fresh share
|
||||||
|
// is legacy (every known device is an implicit owner = v1), and the first member
|
||||||
|
// grant flips it to explicit/deny-by-default. Device->principal->role resolves.
|
||||||
|
func TestRolesLegacyAndExplicit(t *testing.T) {
|
||||||
|
db := open(t)
|
||||||
|
if err := db.AddDevice("devA", "laptop", []byte("k"), 1); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := db.AddDevice("devB", "phone", []byte("k"), 1); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := db.CreateShare("proj", "devA", 1); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Legacy: both devices are implicit owners (v1 behavior preserved).
|
||||||
|
for _, dev := range []string{"devA", "devB"} {
|
||||||
|
role, explicit, err := db.EffectiveRole(dev, "proj")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if explicit || role != RoleOwner {
|
||||||
|
t.Fatalf("legacy %s: role=%d explicit=%v, want owner/false", dev, role, explicit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// An unknown device gets nothing, even in legacy mode.
|
||||||
|
if role, _, _ := db.EffectiveRole("ghost", "proj"); role != 0 {
|
||||||
|
t.Fatalf("unknown device role = %d, want 0", role)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put devB under its own principal, then grant that principal editor.
|
||||||
|
if err := db.EnsurePrincipal("bob", "bob", 2); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := db.SetDevicePrincipal("devB", "bob"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Grant the default 'owner' principal (devA) admin so devA keeps write after
|
||||||
|
// the flip; grant bob editor.
|
||||||
|
if err := db.SetMember("proj", "owner", RoleOwner, true); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := db.SetMember("proj", "bob", RoleEditor, false); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now explicit: devA(owner) and devB(editor) both >= editor.
|
||||||
|
for _, tc := range []struct {
|
||||||
|
dev string
|
||||||
|
want int
|
||||||
|
}{{"devA", RoleOwner}, {"devB", RoleEditor}} {
|
||||||
|
role, explicit, err := db.EffectiveRole(tc.dev, "proj")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !explicit || role != tc.want {
|
||||||
|
t.Fatalf("explicit %s: role=%d explicit=%v, want %d/true", tc.dev, role, explicit, tc.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Demote bob to viewer: below editor -> no write.
|
||||||
|
if err := db.SetMember("proj", "bob", RoleViewer, false); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if role, _, _ := db.EffectiveRole("devB", "proj"); role >= RoleEditor {
|
||||||
|
t.Fatalf("demoted devB role = %d, want < editor(%d)", role, RoleEditor)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove bob entirely: deny-by-default (role 0) in explicit mode.
|
||||||
|
if err := db.RemoveMember("proj", "bob"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if role, explicit, _ := db.EffectiveRole("devB", "proj"); role != 0 || !explicit {
|
||||||
|
t.Fatalf("removed devB role=%d explicit=%v, want 0/true (deny-by-default)", role, explicit)
|
||||||
|
}
|
||||||
|
|
||||||
|
ms, _ := db.Members("proj")
|
||||||
|
if len(ms) != 1 || ms[0].Principal != "owner" {
|
||||||
|
t.Fatalf("members = %+v, want just owner", ms)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestMigrationFromV1WithData is the realistic upgrade path: a populated v1
|
// TestMigrationFromV1WithData is the realistic upgrade path: a populated v1
|
||||||
// database (user_version 0, global snapshot PK) is migrated in place. Data must
|
// database (user_version 0, global snapshot PK) is migrated in place. Data must
|
||||||
// survive, a backup must be written, and the v1 idempotent-push legacy — a share
|
// survive, a backup must be written, and the v1 idempotent-push legacy — a share
|
||||||
|
|
|
||||||
|
|
@ -253,13 +253,22 @@ func (s *Server) handlePush(w http.ResponseWriter, r *http.Request, deviceID str
|
||||||
if !decode(w, r, &req) {
|
if !decode(w, r, &req) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// Write gate (M8a): the device's effective role must be >= editor AND its
|
||||||
|
// access.writable clamp must allow it. In legacy shares (no member grants)
|
||||||
|
// every device is an implicit owner, so this reduces to the v1 writable bit;
|
||||||
|
// an explicit-ACL share is deny-by-default.
|
||||||
writable, err := s.db.Writable(deviceID, req.Share)
|
writable, err := s.db.Writable(deviceID, req.Share)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeErr(w, http.StatusInternalServerError, err.Error())
|
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !writable {
|
role, _, err := s.db.EffectiveRole(deviceID, req.Share)
|
||||||
writeErr(w, http.StatusForbidden, "device is read-only on this share")
|
if err != nil {
|
||||||
|
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !writable || role < meta.RoleEditor {
|
||||||
|
writeErr(w, http.StatusForbidden, "device not permitted to write to this share")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -206,6 +206,72 @@ func TestHubHappyPath(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestPushWriteGate proves the M8a role gate on the HTTP push path: a legacy
|
||||||
|
// share behaves like v1 (the publisher can push), flipping it to explicit ACLs
|
||||||
|
// denies an unmembered device (403), a viewer grant is still too low (403), and
|
||||||
|
// an editor grant opens the gate (the push then fails LATER for a missing blob,
|
||||||
|
// not 403).
|
||||||
|
func TestPushWriteGate(t *testing.T) {
|
||||||
|
base, db := testHub(t)
|
||||||
|
now := time.Now().Unix()
|
||||||
|
|
||||||
|
if err := db.CreateToken(HashToken("tok"), now+3600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
pub, priv, err := ed25519.GenerateKey(rand.Reader)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_, body := do(t, "POST", base+proto.PathJoin, "", mustJSON(t, proto.JoinRequest{
|
||||||
|
Token: "tok", Name: "laptop", Pubkey: pub,
|
||||||
|
Signature: ed25519.Sign(priv, proto.JoinChallenge("tok", pub)),
|
||||||
|
}))
|
||||||
|
var join proto.JoinResponse
|
||||||
|
if err := json.Unmarshal(body, &join); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if status, b := do(t, "POST", base+proto.PathPublish, join.Bearer, mustJSON(t, proto.PublishRequest{Share: "proj"})); status != http.StatusOK {
|
||||||
|
t.Fatalf("publish status = %d, body = %s", status, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A push referencing a blob that was never uploaded — so the only thing under
|
||||||
|
// test is the gate: a pass yields 409 (missing blob), a denial yields 403.
|
||||||
|
push := func() int {
|
||||||
|
st, _ := do(t, "POST", base+proto.PathPush, join.Bearer, mustJSON(t, proto.PushRequest{
|
||||||
|
Share: "proj",
|
||||||
|
ManifestHash: strings.Repeat("a", 64),
|
||||||
|
}))
|
||||||
|
return st
|
||||||
|
}
|
||||||
|
|
||||||
|
// Legacy share: the publisher (principal 'owner') pushes — gate passes (409).
|
||||||
|
if st := push(); st == http.StatusForbidden {
|
||||||
|
t.Fatal("legacy share must allow the owner-device to push (v1 behavior)")
|
||||||
|
}
|
||||||
|
// Flip to explicit by granting a DIFFERENT principal; our device ('owner') is
|
||||||
|
// now unmembered -> deny-by-default.
|
||||||
|
if err := db.SetMember("proj", "stranger", meta.RoleEditor, false); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if st := push(); st != http.StatusForbidden {
|
||||||
|
t.Fatalf("explicit share must deny an unmembered device, got %d", st)
|
||||||
|
}
|
||||||
|
// Viewer is below editor -> still denied.
|
||||||
|
if err := db.SetMember("proj", "owner", meta.RoleViewer, false); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if st := push(); st != http.StatusForbidden {
|
||||||
|
t.Fatalf("viewer must not be able to push, got %d", st)
|
||||||
|
}
|
||||||
|
// Editor opens the gate -> no longer 403 (now 409 for the missing blob).
|
||||||
|
if err := db.SetMember("proj", "owner", meta.RoleEditor, false); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if st := push(); st == http.StatusForbidden {
|
||||||
|
t.Fatal("editor must pass the write gate")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestHubAuthAndHashErrors(t *testing.T) {
|
func TestHubAuthAndHashErrors(t *testing.T) {
|
||||||
base, db := testHub(t)
|
base, db := testHub(t)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue