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

@@ -1331,6 +1331,32 @@ def archive_project(project_id: str, body: ArchiveIn, user: models.User = Depend
# ── SOPs ─────────────────────────────────────────────────────────────────────
def canonical_sop_id(project_id: Optional[str]) -> Optional[str]:
"""D20: the one id a project's SOP lives at - the id pushSOP has always used."""
return f"sop__{project_id}" if project_id else None
def project_baseline_sop(db: Session, project_id: Optional[str]) -> Optional["models.Sop"]:
"""D20: THE completed SOP of a project, for every reader that treats it as the
baseline (page load, PM/CM mail, QA mail, the launcher summary).
The canonical row wins when it is complete. "Most recent complete" is only the
fallback, for projects whose SOP predates the fixed id (seed data, API
clients). Before D20 "most recent" was the whole rule, which is what let a
second complete SOP take the baseline over (security review SEC-02)."""
if not project_id:
return None
row = db.get(models.Sop, canonical_sop_id(project_id))
if row is not None and row.complete and row.project_id == project_id:
return row
return db.scalars(
select(models.Sop)
.where((models.Sop.project_id == project_id) & (models.Sop.complete.is_(True)))
.order_by(models.Sop.updated_at.desc())
.limit(1)
).first()
@app.post("/api/sops")
def upsert_sop(body: SopIn, user: models.User = Depends(auth.get_current_user), db: Session = Depends(get_db)):
check_id(body.id)
@@ -1339,6 +1365,11 @@ def upsert_sop(body: SopIn, user: models.User = Depends(auth.get_current_user),
# one it currently sits on below — so an archived job can be neither edited nor
# used as a source to move a SOP out of.
require_project_writable(db, user, body.project_id, "Saving a SOP")
# D20: one SOP per project, at a fixed id. The browser has always written
# `sop__<project_id>` (pushSOP); a request with no id now gets it too.
canonical = canonical_sop_id(body.project_id)
if not body.id and canonical:
body.id = canonical
sop = db.get(models.Sop, body.id) if body.id else None
if sop is not None:
require_project_access(db, user, sop.project_id)
@@ -1348,6 +1379,28 @@ def upsert_sop(body: SopIn, user: models.User = Depends(auth.get_current_user),
# project member, including marking it complete the first time.
if sop.complete:
require_project_admin(db, user, sop.project_id, "Changing a completed SOP")
# D20 (security review SEC-02): the rule above checked only the row being
# posted, so a project_user could post a NEW id with complete=true and every
# "latest complete SOP" reader - the page baseline and the PM/CM/QA mail
# routing - followed it. A project gets no second SOP row (creating one, or
# moving one in, under any id but the canonical one is refused), and while
# it has a complete SOP, writing another complete one is a Project Admin act.
# Rows made before D20 under other ids stay and keep their old rules.
if canonical and body.id != canonical and (sop is None or sop.project_id != body.project_id):
raise HTTPException(
status_code=400,
detail=f"A project has one SOP. Save it as {canonical}.",
)
if body.project_id and body.complete:
other_complete = db.scalar(
select(models.Sop.id).where(
(models.Sop.project_id == body.project_id)
& (models.Sop.complete.is_(True))
& (models.Sop.id != body.id)
).limit(1)
)
if other_complete:
require_project_admin(db, user, body.project_id, "Changing a completed SOP")
is_new = sop is None
if sop is None:
sop = models.Sop(id=body.id or gen_id("sop"))
@@ -1380,6 +1433,15 @@ def list_sops(project_id: Optional[str] = Query(None), full: bool = Query(False)
@app.get("/api/sops/latest")
def latest_sop(complete: Optional[bool] = None, project_id: Optional[str] = Query(None), user: models.User = Depends(auth.get_current_user), db: Session = Depends(get_db)):
# D20: "the project's completed SOP" is what pullProject() asks for on every
# page load, so it is answered by the baseline rule, not by "most recent".
# Same 404 as before for a project the caller cannot reach.
if complete is True and project_id:
ids = accessible_project_ids(db, user)
sop = project_baseline_sop(db, project_id) if (ids is None or project_id in ids) else None
if not sop:
raise HTTPException(status_code=404, detail="No SOP found")
return sop.to_dict()
stmt = select(models.Sop)
if complete is not None:
stmt = stmt.where(models.Sop.complete == complete)
@@ -1560,15 +1622,10 @@ def reopened_critical(old_data: Optional[dict], new_data: Optional[dict]) -> lis
def project_sop_team(db: Session, project_id: Optional[str]) -> list[str]:
"""PM + CM user ids from the project's most recent complete SOP."""
"""PM + CM user ids from the project's baseline SOP (D20: project_baseline_sop)."""
if not project_id:
return []
sop = db.scalars(
select(models.Sop)
.where((models.Sop.project_id == project_id) & (models.Sop.complete.is_(True)))
.order_by(models.Sop.updated_at.desc())
.limit(1)
).first()
sop = project_baseline_sop(db, project_id)
if not sop:
return []
data = sop.data or {}
@@ -1586,15 +1643,11 @@ def project_qa_group(db: Session, project_id: Optional[str]) -> list["models.Use
"""D2: the QA group named on the project's latest complete SOP. pushSOP writes
the row as data={sop, state}, so the project block is data['sop']['project'] -
project_sop_team above read the flat shape until BL-021 was fixed
(2026-08-20); both now read nested-first, exactly alike."""
(2026-08-20); both now read nested-first, exactly alike. D20: both also read
the same row, project_baseline_sop."""
if not project_id:
return []
sop = db.scalars(
select(models.Sop)
.where((models.Sop.project_id == project_id) & (models.Sop.complete.is_(True)))
.order_by(models.Sop.updated_at.desc())
.limit(1)
).first()
sop = project_baseline_sop(db, project_id)
if not sop:
return []
data = sop.data or {}
@@ -2661,12 +2714,7 @@ def project_summary(project_id: str, user: models.User = Depends(auth.get_curren
raise HTTPException(status_code=404, detail="Project not found")
require_project_access(db, user, project_id)
sop = db.scalars(
select(models.Sop)
.where(models.Sop.project_id == project_id, models.Sop.complete.is_(True))
.order_by(models.Sop.updated_at.desc())
.limit(1)
).first()
sop = project_baseline_sop(db, project_id) # D20: the same row every page loads
wp_total = db.scalar(
select(func.count()).select_from(models.WorkPackage).where(