fix(roles): check the right to save before the revision
A person who may not save the role dataset and sent a stale revision got
409 revision_conflict ("someone else changed it") instead of 403
not_permitted. MayWrite now runs before the revision check; Prepare uses
it too. Found in the R1 S0 stage check.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014aRnCGqELxoix5TfpX8Ejf
This commit is contained in:
1 parent
0f536fc955
commit
1e07f21505
3 files changed
+39
-13
No files matched your search
@@ -516,6 +516,24 @@ type Writer struct {
|
||||
// management. It is the only place a sign-in role counts here.
|
||||
const BootstrapRole = "CXO"
|
||||
|
||||
// MayWrite reports whether w may save the dataset at all, before looking at
|
||||
// what the save changes: B7 management and document owners, or, until
|
||||
// management is recorded, the CXO sign-in role (Q3). The server asks it
|
||||
// before comparing revisions, so a person without the right learns that,
|
||||
// not that someone else changed the dataset.
|
||||
func MayWrite(current *Dataset, w Writer) *Error {
|
||||
if maintains(current, w) || (!current.hasManagementHolder() && slices.Contains(w.OrgRoles, BootstrapRole)) {
|
||||
return nil
|
||||
}
|
||||
return &Error{Status: http.StatusForbidden, Code: "not_permitted",
|
||||
Message: "the dataset is maintained by management and document owners (B7)"}
|
||||
}
|
||||
|
||||
// maintains: B7, management and document owners maintain the dataset.
|
||||
func maintains(current *Dataset, w Writer) bool {
|
||||
return current.Holds(w.Sub, Management) || current.Holds(w.Sub, DocumentOwner)
|
||||
}
|
||||
|
||||
// Prepare checks a save against the current dataset and returns the dataset
|
||||
// to store, with the registry's stamps (S0 spec §5, §6). current is nil when
|
||||
// the company has none yet. authorize lists the ids of deputies and standard
|
||||
@@ -524,18 +542,15 @@ func Prepare(current, incoming *Dataset, authorize []string, w Writer, now time.
|
||||
if err := Validate(incoming); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch {
|
||||
case current.Holds(w.Sub, Management) || current.Holds(w.Sub, DocumentOwner):
|
||||
// B7: management and document owners maintain the dataset.
|
||||
case !current.hasManagementHolder() && slices.Contains(w.OrgRoles, BootstrapRole):
|
||||
if err := MayWrite(current, w); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !maintains(current, w) {
|
||||
// First entry: only management assignments, nothing else.
|
||||
if len(authorize) > 0 || !onlyAddsManagement(current, incoming) {
|
||||
return nil, &Error{Status: http.StatusForbidden, Code: "bootstrap_only",
|
||||
Message: "until management is recorded, the CXO sign-in role may only record management"}
|
||||
}
|
||||
default:
|
||||
return nil, &Error{Status: http.StatusForbidden, Code: "not_permitted",
|
||||
Message: "the dataset is maintained by management and document owners (B7)"}
|
||||
}
|
||||
|
||||
out := *incoming
|
||||
|
||||
@@ -164,19 +164,23 @@ func (s *Server) saveRoleDataset(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
currentRev := 0
|
||||
var prev *roles.Dataset
|
||||
if stored != nil {
|
||||
currentRev = stored.Revision
|
||||
currentRev, prev = stored.Revision, current
|
||||
}
|
||||
writer := roles.Writer{Sub: p.Subject, OrgRoles: p.OrgRoles}
|
||||
// Who may save comes before the revision: someone without the right gets
|
||||
// not_permitted, also from a stale page.
|
||||
if rerr := roles.MayWrite(prev, writer); rerr != nil {
|
||||
writeJSON(w, rerr.Status, rerr)
|
||||
return
|
||||
}
|
||||
if *in.ExpectedRevision != currentRev {
|
||||
s.writeRoleDatasetConflict(w, currentRev)
|
||||
return
|
||||
}
|
||||
var prev *roles.Dataset
|
||||
if stored != nil {
|
||||
prev = current
|
||||
}
|
||||
at := s.now()
|
||||
next, rerr := roles.Prepare(prev, incoming, in.Authorize, roles.Writer{Sub: p.Subject, OrgRoles: p.OrgRoles}, at)
|
||||
next, rerr := roles.Prepare(prev, incoming, in.Authorize, writer, at)
|
||||
if rerr != nil {
|
||||
writeJSON(w, rerr.Status, rerr)
|
||||
return
|
||||
|
||||
@@ -123,6 +123,13 @@ func TestRoleDataset_lifecycle(t *testing.T) {
|
||||
if code, _, raw := f.do(t, "staff", "PUT", f.path(""), saveBody(withDeputyDS, 1)); code != http.StatusForbidden || !strings.Contains(raw, "not_permitted") {
|
||||
t.Fatalf("person without function: %d %s", code, raw)
|
||||
}
|
||||
// From a stale page as well: the right comes before the revision.
|
||||
if code, _, raw := f.do(t, "staff", "PUT", f.path(""), saveBody(withDeputyDS, 0)); code != http.StatusForbidden || !strings.Contains(raw, "not_permitted") {
|
||||
t.Fatalf("person without function, stale revision: %d %s", code, raw)
|
||||
}
|
||||
if code, _, raw := f.do(t, "signup", "PUT", f.path(""), saveBody(mgmtOnly, 0)); code != http.StatusForbidden || !strings.Contains(raw, "not_permitted") {
|
||||
t.Fatalf("CXO bootstrap after management exists: %d %s", code, raw)
|
||||
}
|
||||
// Management records a deputy: the body's authorized_by is ignored, the
|
||||
// save itself is the authorization (B7).
|
||||
code, out, raw = f.do(t, "ceo", "PUT", f.path(""), saveBody(withDeputyDS, 1))
|
||||
|
||||
Reference in new issue
Block a user