mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-04 04:29:01 +01:00
fix(trust): treat denied historical author policy as quarantined during restore
When restoring an agent-written revision whose original author's trust boundary later changed and now conflicts with the saved run policy (403), do not block the restore — fail closed by treating the restored content as quarantined.
This commit is contained in:
1 parent
da08af3789
commit
09de82f54d
2 files changed
+134
-15
No files matched your search
@@ -754,4 +754,109 @@ describeEmbeddedPostgres("resolveActorSourceTrustForIssue", () => {
|
||||
|
||||
expect(result.document.sourceTrust).toBeNull();
|
||||
});
|
||||
|
||||
it("allows a user to restore an agent-written revision even after the original author's trust boundary changes", async () => {
|
||||
const company = await createCompany();
|
||||
const [project] = await db
|
||||
.insert(projects)
|
||||
.values({ companyId: company.id, name: "Restore Policy Project", status: "in_progress" })
|
||||
.returning();
|
||||
const agent = await createAgent(company.id, {
|
||||
trustPreset: LOW_TRUST_REVIEW_PRESET,
|
||||
authorizationPolicy: {
|
||||
trustPreset: LOW_TRUST_REVIEW_PRESET,
|
||||
trustBoundary: {
|
||||
mode: LOW_TRUST_REVIEW_PRESET,
|
||||
companyId: company.id,
|
||||
projectIds: [project!.id],
|
||||
},
|
||||
},
|
||||
});
|
||||
const [issue] = await db
|
||||
.insert(issues)
|
||||
.values({
|
||||
companyId: company.id,
|
||||
projectId: project!.id,
|
||||
title: "Restore after boundary change",
|
||||
status: "in_progress",
|
||||
priority: "medium",
|
||||
assigneeAgentId: agent.id,
|
||||
})
|
||||
.returning();
|
||||
const [run] = await db
|
||||
.insert(heartbeatRuns)
|
||||
.values({
|
||||
companyId: company.id,
|
||||
agentId: agent.id,
|
||||
status: "running",
|
||||
nativeIssueId: issue!.id,
|
||||
contextSnapshot: {
|
||||
issueId: issue!.id,
|
||||
executionPolicy: {
|
||||
trustPreset: LOW_TRUST_REVIEW_PRESET,
|
||||
authorizationPolicy: {
|
||||
trustPreset: LOW_TRUST_REVIEW_PRESET,
|
||||
trustBoundary: {
|
||||
mode: LOW_TRUST_REVIEW_PRESET,
|
||||
companyId: company.id,
|
||||
projectIds: [project!.id],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
.returning();
|
||||
|
||||
const rev1 = await documentService(db).upsertIssueDocument({
|
||||
issueId: issue!.id,
|
||||
key: "report",
|
||||
title: "Initial Report",
|
||||
format: "markdown",
|
||||
body: "Original low-trust body",
|
||||
createdByAgentId: agent.id,
|
||||
createdByRunId: run!.id,
|
||||
});
|
||||
await documentService(db).upsertIssueDocument({
|
||||
issueId: issue!.id,
|
||||
key: "report",
|
||||
title: "Updated Report",
|
||||
format: "markdown",
|
||||
body: "User sanitized body",
|
||||
baseRevisionId: rev1.document.latestRevisionId,
|
||||
createdByUserId: "board-user",
|
||||
});
|
||||
|
||||
// Mutate the original author's trust boundary so it conflicts with the saved run policy.
|
||||
await db
|
||||
.update(agents)
|
||||
.set({
|
||||
permissions: {
|
||||
trustPreset: LOW_TRUST_REVIEW_PRESET,
|
||||
authorizationPolicy: {
|
||||
trustPreset: LOW_TRUST_REVIEW_PRESET,
|
||||
trustBoundary: {
|
||||
mode: LOW_TRUST_REVIEW_PRESET,
|
||||
companyId: randomUUID(),
|
||||
projectIds: [randomUUID()],
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
.where(eq(agents.id, agent.id));
|
||||
|
||||
const restored = await documentService(db).restoreIssueDocumentRevision({
|
||||
issueId: issue!.id,
|
||||
key: "report",
|
||||
revisionId: rev1.document.latestRevisionId,
|
||||
createdByUserId: "board-user",
|
||||
});
|
||||
|
||||
expect(restored.document.body).toBe("Original low-trust body");
|
||||
expect(restored.document.sourceTrust).toMatchObject({
|
||||
preset: LOW_TRUST_REVIEW_PRESET,
|
||||
disposition: "quarantined",
|
||||
sourceIssueId: issue!.id,
|
||||
sourceAgentId: agent.id,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -6,12 +6,12 @@ import { and, asc, desc, eq, inArray, sql } from "drizzle-orm";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import { documentRevisions, documents, heartbeatRuns, issueDocuments, issues } from "@paperclipai/db";
|
||||
import { ISSUE_DOCUMENT_BATCH_MAX_IDS, isSystemIssueDocumentKey, isUuidLike, issueDocumentKeySchema } from "@paperclipai/shared";
|
||||
import { conflict, notFound, payloadTooLarge, unprocessable } from "../errors.js";
|
||||
import { HttpError, conflict, notFound, payloadTooLarge, unprocessable } from "../errors.js";
|
||||
import { isUniqueViolation } from "../db-errors.js";
|
||||
import { insertRowsInChunks } from "./batch-insert.js";
|
||||
import type { ImportIssueDocumentRow } from "./import-write-types.js";
|
||||
import { emitPlanCreatedAfterCommit } from "./product-telemetry.js";
|
||||
import { resolveActorSourceTrustForIssue } from "./source-trust.js";
|
||||
import { buildLowTrustSourceTrust, resolveActorSourceTrustForIssue } from "./source-trust.js";
|
||||
|
||||
// Stay below download_artifact's 10 MiB limit, including JSON escaping and metadata.
|
||||
export const ISSUE_DOCUMENT_BATCH_MAX_BYTES = 8 * 1024 * 1024;
|
||||
@@ -798,19 +798,33 @@ export function documentService(db: Db) {
|
||||
const originalRevisionRunId = revision.createdByAgentId
|
||||
? await resolveDocumentCreatedByRunId(tx, existing.companyId, revision.createdByRunId)
|
||||
: null;
|
||||
const restoredRevisionSourceTrust =
|
||||
!actorSourceTrust && revision.createdByAgentId
|
||||
? await resolveActorSourceTrustForIssue({
|
||||
db: tx as unknown as Db,
|
||||
issue,
|
||||
actor: {
|
||||
actorType: "agent",
|
||||
actorId: revision.createdByAgentId,
|
||||
agentId: revision.createdByAgentId,
|
||||
runId: revision.createdByRunId ?? null,
|
||||
},
|
||||
})
|
||||
: null;
|
||||
let restoredRevisionSourceTrust: typeof documents.$inferInsert.sourceTrust = null;
|
||||
if (!actorSourceTrust && revision.createdByAgentId) {
|
||||
try {
|
||||
restoredRevisionSourceTrust = await resolveActorSourceTrustForIssue({
|
||||
db: tx as unknown as Db,
|
||||
issue,
|
||||
actor: {
|
||||
actorType: "agent",
|
||||
actorId: revision.createdByAgentId,
|
||||
agentId: revision.createdByAgentId,
|
||||
runId: revision.createdByRunId ?? null,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
// If the original revision author's policy changed and now conflicts (403),
|
||||
// do not block the restore — fail closed by treating the restored content as quarantined.
|
||||
if (error instanceof HttpError && error.status === 403) {
|
||||
restoredRevisionSourceTrust = buildLowTrustSourceTrust({
|
||||
issueId: issue.id,
|
||||
runId: originalRevisionRunId,
|
||||
agentId: revision.createdByAgentId,
|
||||
});
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
const resolvedSourceTrust = actorSourceTrust ?? restoredRevisionSourceTrust ?? null;
|
||||
const preserveQuarantinedRevisionProvenance = Boolean(!actorSourceTrust && restoredRevisionSourceTrust);
|
||||
|
||||
|
||||
Reference in new issue
Block a user