From e163aab3d1143b29827c10304398ba989d6fa01d Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 12 Nov 2026 06:01:01 +0100 Subject: [PATCH] AM-D3 deny: a user's subscribe deny is written, compared and kept MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The grants part composes a subscribe deny where a node's tool runner does not honour the approval mark; the user list must carry it to the bus, and an added deny is a narrowing the user-list guard lets pass as no right lost. Builds: design 46 7 §6 (Permissions.SubscribeDeny, User subscribe_deny, comparison and differences, the guard's narrowing) Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h --- modules/mesh-grants/grants_test.go | 9 +++++++++ .../internal/buscompose/buscompose_test.go | 6 ++++++ .../mesh-grants/internal/buscompose/nats.go | 18 ++++++++++++++++-- modules/mesh-grants/internal/grants/grants.go | 1 + .../mesh-grants/internal/grants/keeploaded.go | 11 ++++++++++- modules/mesh-grants/internal/grants/refused.go | 4 ++-- .../mesh-grants/internal/grants/userlist.go | 18 +++++++++++++++--- 7 files changed, 59 insertions(+), 8 deletions(-) diff --git a/modules/mesh-grants/grants_test.go b/modules/mesh-grants/grants_test.go index 8f722f46..b5c3de58 100644 --- a/modules/mesh-grants/grants_test.go +++ b/modules/mesh-grants/grants_test.go @@ -1265,6 +1265,10 @@ func TestGrants(t *testing.T) { for i := range readable { readable[i].PublishDeny = []string{} } + // A subscribe the shadow withholds (design 46 approval mark §6) where the loaded list withholds none, as on the first + // composition after the release: a narrowing, no right lost. Its reverse, a node gaining approval-mark/1: a right added. + withheld := slices.Clone(shadow) + withheld[x].SubscribeDeny = []string{"mesh.mod.m.tool.execute", "mesh.mod.m.tool.execute.*"} // The live comparison the operator's yes was refused on, 2026-10-27 21:19 UTC (issue 1945): 41 users differ, every // difference an added right. Read whole; a row's mutate makes one of the cases that must still refuse. var live struct { @@ -1309,6 +1313,8 @@ func TestGrants(t *testing.T) { {"only a right added", fewer, nil, nil, nil, []string{"a.mod-x"}, false, nil, nil, 1}, {"a deny added refuses", readable, nil, nil, nil, []string{"a.mod-x"}, false, nil, []string{"a.mod-x"}, 1}, {"a deny the loaded list cannot say refuses a user whose grants differ", fewer, nil, nil, nil, []string{"a.mod-x"}, false, nil, []string{"a.mod-x"}, 1}, + {"a subscribe withheld passes as no right lost", shadow, nil, nil, nil, []string{"a.mod-x"}, false, nil, nil, 0}, + {"a subscribe no longer withheld is a right added", withheld, nil, nil, nil, []string{"a.mod-x"}, false, nil, nil, 1}, {"captured 21:19: only added rights", nil, nil, nil, nil, nil, false, captured(nil), nil, 41}, {"captured, a right removed", nil, nil, nil, nil, nil, false, captured(func(c *grants.Comparison) { nodeAce(c).SubscribeDropped = []string{"mesh.mod.mesh-vault.event.secret-changed"} @@ -1333,6 +1339,9 @@ func TestGrants(t *testing.T) { if strings.Contains(row.name, "deny") { from = denied } + if strings.Contains(row.name, "withheld passes") { + from = withheld + } c = grants.Compare(from, row.loaded, grants.Placed(in)) var differ []string for _, d := range c.Differ { diff --git a/modules/mesh-grants/internal/buscompose/buscompose_test.go b/modules/mesh-grants/internal/buscompose/buscompose_test.go index 53a1bc51..8787267c 100644 --- a/modules/mesh-grants/internal/buscompose/buscompose_test.go +++ b/modules/mesh-grants/internal/buscompose/buscompose_test.go @@ -601,6 +601,12 @@ func TestTheCopiedComposerComposesAsTheControllers(t *testing.T) { if err != nil || perr != nil || !strings.Contains(list, " users = [\n"+AccountsEntry(filled[0].Username(), "$2a$11$h", perms)+" ]\n") { t.Errorf("the accounts entry is not ComposeAccounts' own: %v %v\n%s", err, perr, list) } + // A subscribe deny (design 46 approval mark §6) is written as the server's deny beside its allow, and outranks it. + withheld := Permissions{Publish: []string{"a"}, Subscribe: []string{"mesh.mod.m.tool.>"}, SubscribeDeny: []string{"mesh.mod.m.tool.execute"}} + if entry := AccountsEntry("u", "h", withheld); !strings.Contains(entry, `subscribe: { allow: ["mesh.mod.m.tool.>"], deny: ["mesh.mod.m.tool.execute"] }`) || + MaySubscribe(withheld, "mesh.mod.m.tool.execute") || !MaySubscribe(withheld, "mesh.mod.m.tool.read") { + t.Errorf("a subscribe deny is written as %s", entry) + } c, _ := ConsumerFor(Principal{Kind: KindModule, Node: "a", Module: "m", Consumes: []string{"x.happened"}}) cfg := ConsumerConfig(c) if cfg.Durable != "a_m" || cfg.AckPolicy != jetstream.AckExplicitPolicy || cfg.AckWait != 30*time.Second || diff --git a/modules/mesh-grants/internal/buscompose/nats.go b/modules/mesh-grants/internal/buscompose/nats.go index 12fd9c8d..8aa23802 100644 --- a/modules/mesh-grants/internal/buscompose/nats.go +++ b/modules/mesh-grants/internal/buscompose/nats.go @@ -388,8 +388,14 @@ func MayPublish(perms Permissions, subject string) bool { return false } -// MaySubscribe says whether a principal with these permissions may subscribe to (and so answer) a subject. +// MaySubscribe says whether a principal with these permissions may subscribe to (and so answer) a subject: an allow +// covers it and no deny does. func MaySubscribe(perms Permissions, subject string) bool { + for _, d := range perms.SubscribeDeny { + if SubjectsOverlap(d, subject) { + return false + } + } for _, a := range perms.Subscribe { if SubjectsOverlap(a, subject) { return true @@ -595,6 +601,10 @@ type Permissions struct { // denied to everybody whose grant is wide enough to reach them. The server's deny outranks its allow. PublishDeny []string Subscribe []string + // SubscribeDeny are subjects the principal may not subscribe to although an allow covers them (design 46 approval + // mark §6): every marked subject of a module its runtime carries, where the node's tool runner does not honour the mark. + // The server's deny outranks its allow of `mesh.mod..tool.>`. + SubscribeDeny []string // AllowResponses lets a principal reply to a request it received, on the reply subject that // request carried, once. // @@ -1562,7 +1572,11 @@ func AccountsEntry(user, passwordHash string, perms Permissions) string { } else { fmt.Fprintf(&b, " publish: { allow: [%s] }\n", quoted(perms.Publish)) } - fmt.Fprintf(&b, " subscribe: { allow: [%s] }\n", quoted(perms.Subscribe)) + if len(perms.SubscribeDeny) > 0 { + fmt.Fprintf(&b, " subscribe: { allow: [%s], deny: [%s] }\n", quoted(perms.Subscribe), quoted(perms.SubscribeDeny)) + } else { + fmt.Fprintf(&b, " subscribe: { allow: [%s] }\n", quoted(perms.Subscribe)) + } if perms.AllowResponses { fmt.Fprintf(&b, " allow_responses: { max: 1, ttl: \"%dm\" }\n", int(ResponseTTL/time.Minute)) } diff --git a/modules/mesh-grants/internal/grants/grants.go b/modules/mesh-grants/internal/grants/grants.go index 964e095f..4a29447f 100644 --- a/modules/mesh-grants/internal/grants/grants.go +++ b/modules/mesh-grants/internal/grants/grants.go @@ -75,6 +75,7 @@ func GrantWaits(accounts []string, composed, loaded map[string]User, unknown str if l.PublishDeny != nil { n += len(minus(l.PublishDeny, c.PublishDeny)) } + n += len(minus(l.SubscribeDeny, c.SubscribeDeny)) if n > 0 { adds = append(adds, fmt.Sprintf("%s (%d permissions)", user, n)) } diff --git a/modules/mesh-grants/internal/grants/keeploaded.go b/modules/mesh-grants/internal/grants/keeploaded.go index e65dea25..5cadd5fd 100644 --- a/modules/mesh-grants/internal/grants/keeploaded.go +++ b/modules/mesh-grants/internal/grants/keeploaded.go @@ -60,7 +60,8 @@ func KeepLoaded(composed, loaded []User, inForce map[string]User) map[string]Use // Every deny any list says is kept: a deny the composer drops while it composes part of the mesh would widen the // user (the composer gives every composed user a deny, empty or not, so an intersection would drop the list in // force's). - u := User{User: name, Publish: c.Publish, Subscribe: c.Subscribe, AllowResponses: c.AllowResponses, PublishDeny: c.PublishDeny} + u := User{User: name, Publish: c.Publish, Subscribe: c.Subscribe, AllowResponses: c.AllowResponses, PublishDeny: c.PublishDeny, + SubscribeDeny: c.SubscribeDeny} for _, o := range from { u.Publish = set(append(slices.Clone(u.Publish), o.Publish...)) u.Subscribe = set(append(slices.Clone(u.Subscribe), o.Subscribe...)) @@ -68,6 +69,9 @@ func KeepLoaded(composed, loaded []User, inForce map[string]User) map[string]Use if o.PublishDeny != nil || u.PublishDeny != nil { u.PublishDeny = set(append(slices.Clone(u.PublishDeny), o.PublishDeny...)) } + if o.SubscribeDeny != nil || u.SubscribeDeny != nil { + u.SubscribeDeny = set(append(slices.Clone(u.SubscribeDeny), o.SubscribeDeny...)) + } } if u.Publish == nil { u.Publish = []string{} @@ -94,6 +98,11 @@ func Losses(now, had User) []string { if had.AllowResponses && !now.AllowResponses { out = append(out, "responses") } + // A subscribe deny now adds is a narrowing and no word of had lost (design 46 approval mark §6); one had held that now + // drops widens the user past it. + for _, s := range minus(had.SubscribeDeny, now.SubscribeDeny) { + out = append(out, "subscribe deny dropped "+s) + } if had.PublishDeny != nil { for _, s := range minus(now.PublishDeny, had.PublishDeny) { out = append(out, "deny "+s) diff --git a/modules/mesh-grants/internal/grants/refused.go b/modules/mesh-grants/internal/grants/refused.go index 54b32caa..7ea2c0a9 100644 --- a/modules/mesh-grants/internal/grants/refused.go +++ b/modules/mesh-grants/internal/grants/refused.go @@ -39,7 +39,7 @@ func ComposeEach(r buscompose.Records) ([]User, map[string]string, error) { continue } out = append(out, User{User: p.Username(), Publish: set(perms.Publish), Subscribe: set(perms.Subscribe), - AllowResponses: perms.AllowResponses, PublishDeny: set(perms.PublishDeny)}) + AllowResponses: perms.AllowResponses, PublishDeny: set(perms.PublishDeny), SubscribeDeny: set(perms.SubscribeDeny)}) } sort.Slice(out, func(i, j int) bool { return out[i].User < out[j].User }) return out, refused, nil @@ -81,7 +81,7 @@ func withKept(users []User, kept map[string]User) []User { // (buscompose.AccountsEntry), so the bus reads it as it read the list in force. func keptEntry(u User, hash string) string { return buscompose.AccountsEntry(u.User, hash, buscompose.Permissions{Publish: u.Publish, PublishDeny: u.PublishDeny, - Subscribe: u.Subscribe, AllowResponses: u.AllowResponses}) + Subscribe: u.Subscribe, SubscribeDeny: u.SubscribeDeny, AllowResponses: u.AllowResponses}) } // withEntries puts entries at the end of the accounts block's users, before it closes. diff --git a/modules/mesh-grants/internal/grants/userlist.go b/modules/mesh-grants/internal/grants/userlist.go index 7c2bd8a9..85bf01c7 100644 --- a/modules/mesh-grants/internal/grants/userlist.go +++ b/modules/mesh-grants/internal/grants/userlist.go @@ -24,6 +24,9 @@ type User struct { // PublishDeny is what the user may not publish though an allow reaches it (buscompose's ControllerOnly). The bus // module's nats_users answers none today, so a loaded user's is nil, read as unknown; empty and not nil once it says. PublishDeny []string `json:"publish_deny,omitempty"` + // SubscribeDeny is what the user may not subscribe to though an allow reaches it: the marked subjects withheld from a + // runtime whose tool runner does not honour the mark (design 46 approval mark §6). Nil and empty are both none. + SubscribeDeny []string `json:"subscribe_deny,omitempty"` // Hash is the bcrypt hash the vault holds for the user (WithHashes): in memory alone, never in a digest, a compare // or the part's state, and empty for a loaded user. Hash string `json:"-"` @@ -227,11 +230,17 @@ type UserDiff struct { // none): it cannot be compared, so it is no added right either (ADR 0562 §2). DenyAdded []string `json:"deny_added,omitempty"` DenyUnread []string `json:"deny_unread,omitempty"` + // SubscribeDenyAdded is what the shadow list withholds from the user's subscriptions and the loaded list does not: a + // narrowing, which loses no right and passes the guard at once (design 46 approval mark §6). SubscribeDenyDropped is + // what the loaded list withholds and the shadow list no longer does: a right added. + SubscribeDenyAdded []string `json:"subscribe_deny_added,omitempty"` + SubscribeDenyDropped []string `json:"subscribe_deny_dropped,omitempty"` } // same says the user's grants agree in both lists. func (d UserDiff) same() bool { - return len(d.PublishAdded)+len(d.PublishDropped)+len(d.SubscribeAdded)+len(d.SubscribeDropped)+len(d.DenyAdded) == 0 && d.Responses == "" + return len(d.PublishAdded)+len(d.PublishDropped)+len(d.SubscribeAdded)+len(d.SubscribeDropped)+len(d.DenyAdded)+ + len(d.SubscribeDenyAdded)+len(d.SubscribeDenyDropped) == 0 && d.Responses == "" } // Comparison is the shadow list against the loaded list. @@ -320,11 +329,12 @@ func (c Comparison) Removing() []string { func (c Comparison) AddedRights() []UserDiff { var out []UserDiff for _, d := range c.Differ { - a := UserDiff{User: d.User, PublishAdded: d.PublishAdded, SubscribeAdded: d.SubscribeAdded} + a := UserDiff{User: d.User, PublishAdded: d.PublishAdded, SubscribeAdded: d.SubscribeAdded, + SubscribeDenyDropped: d.SubscribeDenyDropped} if !d.responsesLost() { a.Responses = d.Responses } - if len(a.PublishAdded)+len(a.SubscribeAdded) > 0 || a.Responses != "" { + if len(a.PublishAdded)+len(a.SubscribeAdded)+len(a.SubscribeDenyDropped) > 0 || a.Responses != "" { out = append(out, a) } } @@ -355,6 +365,8 @@ func Compare(shadow, loaded []User, placed func(user string) bool) Comparison { if l.PublishDeny != nil { d.DenyAdded = minus(s.PublishDeny, l.PublishDeny) } + // A loaded list that says no subscribe deny withholds none: the bus holds none it was not given. + d.SubscribeDenyAdded, d.SubscribeDenyDropped = minus(s.SubscribeDeny, l.SubscribeDeny), minus(l.SubscribeDeny, s.SubscribeDeny) if d.same() { // The same allows compose the same deny: one composer derives it from them. c.Same++