T16.3: D20 one SOP per project

- A SOP posted with no id lands on sop__<project_id>, the id pushSOP
  has always used. Creating (or moving in) a SOP under any other id on
  a project is refused with 400.
- While a project has a complete SOP, writing another complete one
  needs Project Admin (the existing rule only checked the posted row).
- New project_baseline_sop(): canonical row when complete, else the
  latest complete (pre-D20 rows). Used by /api/sops/latest with
  complete=true&project_id, project_summary, project_sop_team and
  project_qa_group, so a rogue row made before the fix cannot stay
  the baseline or the mail routing.
- Existing rows under other ids stay and keep their old rules.

Security review SEC-02 (a project_user took over the baseline and the
PM/CM/QA mail routing with one POST).

security_check.py: 36/36 with D19-D21. Against da1325c: 7 D20 checks
fail (the findings). Smoke test 27/27; seed_demo writes the canonical
id.

(cherry picked from commit 7bd5ab104d)
This commit is contained in:
2026-10-02 13:55:17 -07:00
parent f4d74879cc
commit c0b736e503
2 changed files with 143 additions and 21 deletions

View File

@@ -168,7 +168,81 @@ def d19():
f"status {r.status_code}")
SECTIONS = [d21, d19]
# ── D20: one SOP per project ─────────────────────────────────────────────────
def d20():
print("\nD20 one SOP per project")
from datetime import datetime, timedelta, timezone
def sop(c, **body):
body.setdefault("project_id", "proj_a")
return c.post("/api/sops", json=body)
def team(qa):
return {"sop": {"project": {"pmId": qa, "cmId": qa, "qaGroupIds": [qa]}}, "state": {}}
r = sop(as_member, name="draft", complete=False, data=team("user_padmin"))
check("D20", "a SOP posted with no id lands on sop__<project_id>",
r.status_code == 200 and r.json().get("id") == "sop__proj_a", f"{r.status_code} {r.json().get('id')}")
r = sop(as_member, id="sop__proj_a", name="done", complete=True, data=team("user_padmin"))
check("D20", "a project_user still completes the first SOP of a project", r.status_code == 200,
f"status {r.status_code}")
r = sop(as_member, id="sop_rogue", name="mine", complete=True, data=team("user_member"))
check("D20", "a project_user cannot create a second SOP (the SEC-02 attack)", r.status_code == 400,
f"status {r.status_code}")
r = sop(as_member, id="sop__proj_a", name="edit", complete=True, data=team("user_member"))
check("D20", "a project_user still cannot change the completed SOP", r.status_code == 403,
f"status {r.status_code}")
r = sop(as_member, id="sop__proj_a", name="undo", complete=False, data=team("user_member"))
check("D20", "a project_user cannot un-complete it either", r.status_code == 403, f"status {r.status_code}")
r = sop(as_padmin, id="sop__proj_a", name="revised", complete=True, data=team("user_padmin"))
check("D20", "a Project Admin can still change the completed SOP", r.status_code == 200,
f"status {r.status_code}")
qa = [u.id for u in A.project_qa_group(SessionLocal(), "proj_a")]
check("D20", "QA mail still goes to the group the real SOP names", qa == ["user_padmin"], str(qa))
# A rogue complete SOP made BEFORE D20 (written straight to the table, newer
# than the real one) must not stay the baseline.
s = SessionLocal()
old, new = datetime.now(timezone.utc) - timedelta(days=2), datetime.now(timezone.utc)
s.add(models.Sop(id="sop__proj_b", project_id="proj_b", name="real", complete=True,
data=team("user_padmin"), updated_at=old))
s.add(models.Sop(id="sop_rogue_b", project_id="proj_b", name="rogue", complete=True,
data=team("user_member"), updated_at=new))
s.commit()
r = as_member.get("/api/sops/latest?complete=true&project_id=proj_b")
check("D20", "the page-load baseline is the canonical SOP, not a newer rogue row",
r.status_code == 200 and r.json().get("id") == "sop__proj_b", f"{r.status_code} {r.json().get('id')}")
qa = [u.id for u in A.project_qa_group(SessionLocal(), "proj_b")]
team_ids = A.project_sop_team(SessionLocal(), "proj_b")
check("D20", "QA and PM/CM mail follow the canonical SOP", qa == ["user_padmin"] and team_ids == ["user_padmin", "user_padmin"],
f"qa={qa} team={team_ids}")
r = as_member.get("/api/projects/proj_b/summary")
check("D20", "the launcher summary reports the canonical SOP", r.json().get("sop_id") == "sop__proj_b",
str(r.json().get("sop_id")))
# Rows made before D20 under other ids keep working under the old rules.
s.add(models.Sop(id="sop_legacy", project_id="proj_c", name="legacy", complete=False, data={}))
db.add(models.ProjectMember(id="pm_member_c", user_id="user_member", project_id="proj_c", role=""))
db.commit(); s.commit()
r = sop(as_member, id="sop_legacy", project_id="proj_c", name="legacy edited", complete=False)
check("D20", "an existing non-canonical SOP can still be edited", r.status_code == 200,
f"status {r.status_code}")
r = sop(as_member, id="sop_legacy", project_id="proj_c", name="legacy done", complete=True)
check("D20", "...and completed the first time", r.status_code == 200, f"status {r.status_code}")
r = sop(as_member, id="sop__proj_c", project_id="proj_c", name="second", complete=True)
check("D20", "while a project has a complete SOP, another complete one needs Project Admin",
r.status_code == 403, f"status {r.status_code}")
r = sop(as_member, id="sop_legacy", project_id="proj_a", name="moved", complete=False)
check("D20", "a SOP cannot be moved into another project under a non-canonical id",
r.status_code in (400, 403), f"status {r.status_code}")
r = client(account("user_stranger")).get("/api/sops/latest?complete=true&project_id=proj_a")
check("D20", "a non-member still gets 404 for the baseline", r.status_code == 404, f"status {r.status_code}")
r = as_admin.post("/api/sops", json={"project_id": "proj_c", "name": "x", "complete": False})
check("D20", "an id-less write (smoke test, seed_demo, admin diagnostics) targets the canonical row",
r.status_code == 200 and r.json().get("id") == "sop__proj_c", f"{r.status_code} {r.json().get('id')}")
SECTIONS = [d21, d19, d20]
def main() -> int: