diff --git a/server/app/auth/mcp_keys.py b/server/app/auth/mcp_keys.py index 54c2c69..9d98d26 100644 --- a/server/app/auth/mcp_keys.py +++ b/server/app/auth/mcp_keys.py @@ -268,6 +268,7 @@ def _audit(session, actor: Actor, action: str, row: McpToken) -> None: session.add( AuditLog( + actor_user_id=actor.user_id, actor_kind=actor.kind, actor_name=actor.name, via=actor.via, diff --git a/server/app/storage/pending_changes_db.py b/server/app/storage/pending_changes_db.py index a479bf7..00b4435 100644 --- a/server/app/storage/pending_changes_db.py +++ b/server/app/storage/pending_changes_db.py @@ -122,6 +122,7 @@ summary=request.summary, reason=request.reason, status="draft", + author_user_id=actor.user_id, actor_kind=actor.kind, actor_name=actor.name, via=actor.via, @@ -141,6 +142,7 @@ session.add( AuditLog( + actor_user_id=actor.user_id, actor_kind=actor.kind, actor_name=actor.name, via=actor.via, @@ -162,7 +164,7 @@ actor = self._resolve_actor(actor) with self.database.session() as session: with session.begin(): - changeset = self._changeset(session, change_id) + changeset = self._changeset(session, change_id, for_update=True) if changeset.status != "draft": raise PendingChangeError("Only pending changes can be applied") @@ -199,6 +201,7 @@ changeset.validation = validation session.add( AuditLog( + actor_user_id=actor.user_id, actor_kind=actor.kind, actor_name=actor.name, via=actor.via, @@ -369,12 +372,17 @@ ) @staticmethod - def _changeset(session: Session, change_id: str) -> Changeset: + def _changeset( + session: Session, change_id: str, for_update: bool = False + ) -> Changeset: try: identifier = UUID(str(change_id)) except ValueError as exc: raise PendingChangeError("Pending change not found") from exc - changeset = session.get(Changeset, identifier) + query = select(Changeset).where(Changeset.id == identifier) + if for_update: + query = query.with_for_update() + changeset = session.scalars(query).first() if changeset is None: raise PendingChangeError("Pending change not found") return changeset diff --git a/server/app/storage/validation.py b/server/app/storage/validation.py index 3e5822f..35c3b2a 100644 --- a/server/app/storage/validation.py +++ b/server/app/storage/validation.py @@ -61,25 +61,27 @@ issues: list[dict[str, str]] = [] for document, version in session.execute(latest_doc_rows(session)).all(): slug = document.slug - if slug == "README.md" or slug.startswith("server/"): - continue - - frontmatter = version.frontmatter or {} - if not frontmatter: - issues.append( - _issue(slug, "missing-frontmatter", "Markdown document is missing frontmatter") - ) - continue - - missing = sorted(REQUIRED_FRONTMATTER - set(frontmatter)) - if missing: - issues.append( - _issue( - slug, - "missing-frontmatter-fields", - "Missing frontmatter fields: " + ", ".join(missing), + frontmatter_required = slug != "README.md" and not slug.startswith("server/") + if frontmatter_required: + frontmatter = version.frontmatter or {} + if not frontmatter: + issues.append( + _issue( + slug, + "missing-frontmatter", + "Markdown document is missing frontmatter", + ) ) - ) + else: + missing = sorted(REQUIRED_FRONTMATTER - set(frontmatter)) + if missing: + issues.append( + _issue( + slug, + "missing-frontmatter-fields", + "Missing frontmatter fields: " + ", ".join(missing), + ) + ) _scan_text(slug, version.raw, issues) return issues diff --git a/server/tests/test_db_changesets.py b/server/tests/test_db_changesets.py index 69860e1..521e59b 100644 --- a/server/tests/test_db_changesets.py +++ b/server/tests/test_db_changesets.py @@ -1,9 +1,13 @@ from __future__ import annotations +import threading +import uuid + import pytest from fastapi.testclient import TestClient from sqlalchemy import func, select +from app.auth.actor import Actor from app.config import Settings, get_settings from app.main import app from app.pending_changes import PendingChangeError, ProposedChangeRequest @@ -16,7 +20,15 @@ list_document_versions, restore_document_version, ) -from app.storage.models import InventoryItem, InventoryItemVersion +from app.storage.models import ( + AuditLog, + Changeset, + DocVersion, + Document, + InventoryItem, + InventoryItemVersion, + User, +) from app.storage.pending_changes_db import PostgresPendingChangeRepository from app.storage.validation import validate_database @@ -235,6 +247,116 @@ assert PostgresInventoryRepository(database).read_item("services", "gitbucket") == before +@pytest.mark.parametrize("target", ["README.md", "server/leak.md"]) +def test_secret_scan_covers_all_document_paths( + imported: tuple[Settings, Database], + repository: PostgresPendingChangeRepository, + target: str, +) -> None: + change = repository.create( + ProposedChangeRequest( + kind="doc", + target=target, + summary="Leak a secret", + payload={"content": "password: hunter2\n"}, + ) + ) + + with pytest.raises(PendingChangeError, match="possible-secret"): + repository.apply(change["id"]) + + +def test_actor_user_id_is_recorded( + imported: tuple[Settings, Database], + repository: PostgresPendingChangeRepository, +) -> None: + settings, database = imported + with database.session() as session: + with session.begin(): + user = User( + sub="actor-user", + email="actor@example.test", + display_name="Actor User", + profile={}, + ) + session.add(user) + session.flush() + user_id = user.id + + actor = Actor( + kind="user", name="Actor User", via="ui", user_id=user_id + ) + change = repository.create( + ProposedChangeRequest( + kind="doc", + target="10-systems/actor.md", + summary="Add actor document", + payload={"content": _document("Actor")}, + ), + actor=actor, + ) + repository.apply(change["id"], actor=actor) + + with database.session() as session: + changeset = session.get(Changeset, uuid.UUID(change["id"])) + assert changeset is not None + assert changeset.author_user_id == user_id + + audits = session.scalars( + select(AuditLog) + .where(AuditLog.entity_id == change["id"]) + .order_by(AuditLog.created_at) + ).all() + assert audits + assert all(audit.actor_user_id == user_id for audit in audits) + + version = session.scalars( + select(DocVersion) + .join(Document, Document.id == DocVersion.document_id) + .where(Document.slug == "10-systems/actor.md") + .order_by(DocVersion.version_no.desc()) + ).first() + assert version is not None + assert version.author_user_id == user_id + + +def test_concurrent_apply_is_serialized( + imported: tuple[Settings, Database], + repository: PostgresPendingChangeRepository, +) -> None: + settings, database = imported + change = repository.create( + ProposedChangeRequest( + kind="doc", + target="10-systems/concurrent.md", + summary="Add concurrent document", + payload={"content": _document("Concurrent")}, + ) + ) + barrier = threading.Barrier(2) + results: list[str] = [] + + def run() -> None: + worker = PostgresPendingChangeRepository(database, settings, via="api") + barrier.wait() + try: + worker.apply(change["id"]) + results.append("ok") + except Exception as exc: # noqa: BLE001 - asserted below + results.append(str(exc)) + + threads = [threading.Thread(target=run) for _ in range(2)] + for thread in threads: + thread.start() + for thread in threads: + thread.join(timeout=10) + assert not thread.is_alive() + + assert results.count("ok") == 1 + assert any("Only pending changes can be applied" in result for result in results) + assert len(list_document_versions(database, "10-systems/concurrent.md")) == 1 + + def test_database_validation_is_clean(imported: tuple[Settings, Database]) -> None: _, database = imported