fix(comments): keep saved authorization cards from blocking replies (#21744)

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - People use task comments to ask for help with pending requests.
> - Saving a comment also checks whether it replaces any pending
requests.
> - A stored request that fails today's payload schema can abort that
comment write.
> - The connection gateway could write such a request without validating
its generated target.
> - This change validates new authorization cards, reads older custom
identities, and keeps malformed requests from blocking comments.
> - Comments remain available while authorization and review gates
retain their authority.

## Linked Issues or Issue Description

**What happened?**

A connection authorization card can contain a generated custom target
key longer than 120 characters. The gateway writes the card directly.
Later, the interaction reader rejects it. Both the task interaction list
and comment submission then return a validation error. Other malformed
saved requests can also abort a comment write.

**Expected behavior**

People can ask questions on a task while authorization is pending. New
cards meet the shared schema. Older generated identities remain
readable. A malformed request never becomes approval because a person
posted a comment.

**Steps to reproduce**

1. Use a personal connection with a long UID and no grant for the
current user.
2. Invoke a tool. The gateway saves an authorization request with the
long UID in its target key.
3. Open the task or post a comment. Reading the saved payload fails the
key length limit.

Related PR searches found no existing repair for this defect. This is a
focused bug fix, with no roadmap feature overlap.

## What Changed

- Validate generated connection authorization payloads before either
insert or update. Use a stable SHA-256 target identity from the existing
connection/user idempotency key. Bound its display label.
- Read older confirmation custom keys and labels without the new-input
length limits. Preserve their exact identities. Keep target types, URLs,
document revisions, and resolver checks strict. New requests still use
the strict shared schema.
- During current and historical comment supersession, keep an invalid
saved request pending, report its ID and schema locations, and continue
with valid siblings. Database and other unexpected errors still
propagate.
- Keep this behavior in the gateway writer and interaction owner. The
two existing source files exceed 1,000 lines; the additions extend their
existing write/hydration boundaries rather than introduce another
ownership path.

## Verification

- Reproduced the deployed failure through the browser: comment POST and
interaction GET both returned HTTP 400 for `target.key` exceeding 120
characters. The person's draft was restored.
- Ran the new regressions before the repair: four expected failures and
one short-identity control pass. Failures came from real payload parsing
and real PostgreSQL comment writes, not fixture setup.
- After repair, all 257 tests passed in the complete affected suites:
`issue-thread-interactions-service.test.ts`, `tool-gateway.test.ts`, and
`issue-thread-interactions.test.ts`.
- Safety cases cover fresh and reused authorization cards, repeated
missing-grant calls, refusing another user's credentials, two persisted
follow-ups, strict new-input limits, wrong-company resolution, unsafe
URLs, invalid document revisions, malformed question and confirmation
payloads, and valid sibling supersession.
- Local browser acceptance used an isolated, credential-free source
instance on macOS arm64 with embedded PostgreSQL and Chrome. A card
shaped like an older saved long-key authorization request remained
visible. Two comments saved through Send, including a second after
reload. The original comment persisted and the authorization request
remained pending. No model execution was configured.
- Browser acceptance was repeated on final commit
`58b57dfe104b2ab421c7f24c89db4c29fcfda8ec` after rebase. The server
health and ownership check confirmed that exact serving revision. Both
comments persisted and the authorization stayed pending. Both owned
instances were then stopped. All 257 affected tests passed again after
rebase. The final fixture typing correction passed its six affected
PostgreSQL cases. Greptile reviewed final head
`58b57dfe104b2ab421c7f24c89db4c29fcfda8ec` at 5/5, with zero review
threads or open findings. Required CI is green on that head: aggregate
`ci / verify` and `ci / e2e` both passed. The repository CI run is
https://github.com/paperclipai/paperclip/actions/runs/43710675824 . All
55 completed checks passed; four inapplicable checks were skipped. The
non-required Superagent Security Scan remains in progress at 35%, with
no annotations or reported findings. This external scan is still
unverified; it was not retried or marked as passed.
- `git diff --check` and the module boundary check pass. All seven local
server typecheck layers pass on the final fixture revision. An earlier
run found fixture typing errors; those were corrected and rerun
successfully. Full repo tests, release qualification, and deployment
were not run.

## Risks

- Stored custom keys and labels may exceed current input limits. Only
those length limits change for saved confirmation payloads; unsafe links
and invalid document targets still fail validation.
- A malformed request stays pending and still needs repair. It does not
receive consent, disappear, or block a comment. Its read or resolution
can still return a validation error.
- This PR does not deploy the fix or recover a remote task. The original
environment remains affected until rollout.

## Model Used

OpenAI GPT-6-based Codex, with code editing, shell execution, browser
tools, and PostgreSQL-backed tests. The host does not expose a more
specific model version or context size.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
(behavior and limits are recorded in this PR)
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip authored and GitHub committed 2026-11-15 06:10:35 -06:00
1 parent 689d62661d
commit c17ef9249d
4 files changed
+144 -17

No files matched your search

@@ -3478,6 +3478,88 @@ describeEmbeddedPostgres("issueThreadInteractionService", () => {
expect(listed[0]?.status).toBe("cancelled");
});
it("reads an old authorization target and saves follow-up comments without resolving the request", async () => {
const { companyId, issueId } = await seedConfirmationIssue("Saved authorization target");
const key = `connection:https://mcp.example.test/${"connection/".repeat(25)}:user:local-board`;
const [card] = await db.insert(issueThreadInteractions).values({
companyId, issueId, kind: "request_confirmation", status: "pending",
continuationPolicy: "wake_assignee", requestedResolverPolicy: "human_only",
effectiveResolverPolicy: "human_only", createdByUserId: "local-board",
payload: { version: 1, prompt: "Connect your account to continue",
target: { type: "custom", key, label: "Personal connection ".repeat(10), href: "/apps/example/permissions" } },
}).returning();
expect((await interactionsSvc.listForIssue(issueId))[0]).toMatchObject({
id: card.id, status: "pending", effectiveResolverPolicy: "human_only",
payload: { target: { key } },
});
for (const body of ["How do I connect it?", "Please explain the next step."]) {
const saved = await issuesSvc.addComment(issueId, body, { userId: "local-board" }, {
clientRequestId: randomUUID(),
});
expect(await db.select().from(issueComments).where(eq(issueComments.id, saved.id)))
.toMatchObject([{ body }]);
}
// Reconstruct the reader as a reload would. A comment never supplies consent.
expect((await issueThreadInteractionService(db).listForIssue(issueId))[0]).toMatchObject({
id: card.id, status: "pending", result: null, payload: { target: { key } },
});
await expect(interactionsSvc.create({ id: issueId, companyId }, {
kind: "request_confirmation", payload: { version: 1, prompt: "New request",
target: { type: "custom", key } },
}, { userId: "local-board" })).rejects.toThrow();
await expect(interactionsSvc.acceptInteraction({ id: issueId, companyId: randomUUID(), projectId: null, goalId: null }, card.id, {}, { userId: "local-board" }))
.rejects.toMatchObject({ status: 404 });
});
it.each([
{ type: "custom", key: "" },
{ type: "custom", key: "authorization", href: "javascript:alert(1)" },
{ type: "issue_document", key: "plan", revisionId: "invalid" },
] as const)("keeps invalid stored target structure protected: %j", async target => {
const { companyId, issueId } = await seedConfirmationIssue("Invalid target structure");
const [card] = await db.insert(issueThreadInteractions).values({
companyId, issueId, kind: "request_confirmation", status: "pending",
continuationPolicy: "none", createdByUserId: "local-board",
payload: { version: 1, prompt: "Review this request", target },
}).returning();
await expect(interactionsSvc.listForIssue(issueId)).rejects.toThrow();
await expect(interactionsSvc.acceptInteraction({ id: issueId, companyId, projectId: null, goalId: null }, card.id, {}, { userId: "local-board" }))
.rejects.toThrow();
await expect(issuesSvc.addComment(issueId, "This request needs repair", { userId: "local-board" }))
.resolves.toMatchObject({ body: "This request needs repair" });
expect((await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.id, card.id)))[0])
.toMatchObject({ status: "pending", result: null });
});
it.each(["request_confirmation", "ask_user_questions"] as const)(
"keeps a malformed %s pending while saving a comment and superseding a valid sibling", async kind => {
const { companyId, issueId } = await seedConfirmationIssue("Malformed saved request");
const [malformed] = await db.insert(issueThreadInteractions).values({
companyId, issueId, kind, status: "pending", continuationPolicy: "wake_assignee",
createdByUserId: "local-board",
payload: sql`${JSON.stringify({ version: 1, prompt: null, supersedeOnUserComment: true })}::jsonb`,
}).returning();
const [valid] = await db.insert(issueThreadInteractions).values({
companyId, issueId, kind: "request_confirmation", status: "pending",
continuationPolicy: "none", createdByUserId: "local-board",
payload: { version: 1, prompt: "An optional question", supersedeOnUserComment: true },
createdAt: new Date(Date.now() - 1000),
}).returning();
const saved = await issuesSvc.addComment(issueId, "I need help with this request", { userId: "local-board" });
expect((await db.select().from(issueComments).where(eq(issueComments.id, saved.id)))[0].body)
.toBe("I need help with this request");
expect((await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.id, malformed.id)))[0])
.toMatchObject({ status: "pending", result: null });
expect((await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.id, valid.id)))[0])
.toMatchObject({ status: "expired", result: { outcome: "superseded_by_comment", commentId: saved.id } });
await expect(interactionsSvc.expireRequestConfirmationsSupersededByHistoricalComments({ id: issueId, companyId }))
.resolves.toEqual([]);
await expect(interactionsSvc.acceptInteraction({ id: issueId, companyId, projectId: null, goalId: null }, malformed.id, {}, { userId: "local-board" }))
.rejects.toThrow();
},
);
it("derives legacy pending interactions as expired on closed issues without mutating the GET", async () => {
const { companyId, issueId } = await seedConfirmationIssue("Legacy pending interaction on closed issue");
const created = await interactionsSvc.create({ id: issueId, companyId }, {
+16 -6
View File
@@ -7,6 +7,7 @@ import { join } from "node:path";
import express from "express";
import { and, eq, sql } from "drizzle-orm";
import type { LiveEvent } from "@paperclipai/shared";
import { requestConfirmationPayloadSchema } from "@paperclipai/shared";
import { subscribeCompanyLiveEvents } from "../services/live-events.js";
import { assignedToolName as nativeAssignedToolName } from "../services/native-runtime/assigned-mcp-tools.js";
import request from "supertest";
@@ -2824,7 +2825,11 @@ rl.on("line", (line) => {
}
});
it("starts with another user's personal app and requests authorization only when used", async () => {
it.each([
{ longIdentity: false, existing: false },
{ longIdentity: true, existing: false },
{ longIdentity: true, existing: true },
])("requests another user's personal authorization only when used (long: $longIdentity, existing: $existing)", async ({ longIdentity, existing }) => {
const originalApiUrl = process.env.PAPERCLIP_API_URL;
process.env.PAPERCLIP_API_URL = "http://paperclip.example.test";
const company = await createCompany(db);
@@ -2849,6 +2854,7 @@ rl.on("line", (line) => {
// may be unable to authenticate even though this user's grant is valid.
// The cached catalog must remain visible so grant resolution can happen.
await db.update(toolConnections).set({
...(longIdentity ? { uid: `https://mcp.example.test/${"connection/".repeat(25)}`, name: "Personal connection ".repeat(10) } : {}),
credentialPolicy: "per_user",
healthStatus: "error",
healthMessage: "This app needs you to sign in.",
@@ -2882,7 +2888,7 @@ rl.on("line", (line) => {
const call = () => request(app).post(endpoint).set("authorization", `Bearer ${runtime[0]!.token}`)
.send({ jsonrpc: "2.0", id: "call", method: "tools/call", params: { name: tool.name, arguments: {} } });
await db.insert(issueThreadInteractions).values({
if (existing) await db.insert(issueThreadInteractions).values({
companyId: company.id,
issueId: issue.id,
kind: "request_confirmation",
@@ -2913,10 +2919,14 @@ rl.on("line", (line) => {
effectiveResolverPolicy: "human_only",
addresseeUserId: "carol",
});
expect(interaction!.payload).toMatchObject({
prompt: `Connect your ${connection.name} account to continue`,
target: { key: `connection:${connection.uid}:user:carol` },
});
const payload = requestConfirmationPayloadSchema.parse(interaction!.payload);
expect(payload.target?.type).toBe("custom");
expect(payload.target && "key" in payload.target && payload.target.key.length).toBeLessThanOrEqual(120);
// A repeated refused call refreshes the same request, with a stable target.
await call().expect(409);
const cards = await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.issueId, issue.id));
expect(cards).toHaveLength(1);
expect(cards[0].payload).toEqual(interaction!.payload);
await db.insert(connectionGrants).values({
companyId: company.id,
@@ -107,6 +107,9 @@ import {
requestCheckboxConfirmationPayloadSchema,
requestCheckboxConfirmationResultSchema,
requestConfirmationPayloadSchema,
requestConfirmationCustomTargetSchema,
requestConfirmationIssueAttachmentTargetSchema,
requestConfirmationIssueDocumentTargetSchema,
requestConfirmationResultSchema,
requestItemVerdictsPayloadSchema,
requestItemVerdictsResultSchema,
@@ -117,6 +120,7 @@ import {
withdrawIssueThreadInteractionSchema,
} from "@paperclipai/shared";
import { z } from "zod";
import { logger } from "../middleware/logger.js";
import { conflict, forbidden, notFound, unprocessable } from "../errors.js";
import { getTelemetryClient } from "../telemetry.js";
import { authorizationService } from "./authorization.js";
@@ -703,6 +707,21 @@ function parseStoredInteractionResult<S extends z.ZodTypeAny>(
return null;
}
// Older internal writers persisted custom identity and display text without the
// input length limits. Read those exact values across upgrades; never truncate
// an identity or relax target types, URLs, revisions, or resolution authority.
// New writes still use the strict shared payload schema.
const storedRequestConfirmationPayloadSchema = requestConfirmationPayloadSchema.extend({
target: z.discriminatedUnion("type", [
requestConfirmationIssueDocumentTargetSchema,
requestConfirmationIssueAttachmentTargetSchema,
requestConfirmationCustomTargetSchema.extend({
key: z.string().trim().min(1),
label: z.string().trim().min(1).nullable().optional(),
}),
]).nullable().optional(),
});
function hydrateInteraction(
row: IssueThreadInteractionRow,
): IssueThreadInteraction {
@@ -774,7 +793,7 @@ function hydrateInteraction(
),
} satisfies AskUserQuestionsInteraction;
case "request_confirmation": {
const payload = requestConfirmationPayloadSchema.parse(row.payload);
const payload = storedRequestConfirmationPayloadSchema.parse(row.payload);
return {
...base,
kind: "request_confirmation",
@@ -824,6 +843,23 @@ function hydrateInteraction(
}
}
function interactionForCommentSupersession(row: IssueThreadInteractionRow) {
try {
return hydrateInteraction(row) as UserCommentSupersedableInteraction;
} catch (error) {
if (!(error instanceof z.ZodError)) throw error;
// The comment is useful even when a saved request cannot be decoded. Keep
// that gate pending, refuse its normal resolution, and report only schema
// locations, never payload/credential values. Valid siblings still expire.
logger.warn({
companyId: row.companyId, issueId: row.issueId, interactionId: row.id,
kind: row.kind,
validationIssues: error.issues.map(issue => ({ code: issue.code, path: issue.path })),
}, "Saved interaction is invalid; leaving it pending while saving a comment");
return null;
}
}
async function touchIssue(db: IssueTouchDb, issueId: string, companyId: string) {
await db.transaction(async tx => {
await notifyStatusCardIssueChange(tx, companyId);
@@ -4962,10 +4998,9 @@ export function issueThreadInteractionService(
const superseded = rows.filter((row) => {
if (row.kind === "ask_user_questions" && scope?.conversationAgentId && scope.conversationUserId) return false;
if (!isUserCommentSupersedableKind(row.kind)) return false;
const interaction = hydrateInteraction(
row,
) as UserCommentSupersedableInteraction;
const interaction = interactionForCommentSupersession(row);
return (
interaction !== null &&
shouldSupersedeInteractionOnUserComment(interaction) &&
isCommentAtOrAfterInteraction({
commentCreatedAt: comment.createdAt,
@@ -5071,9 +5106,8 @@ export function issueThreadInteractionService(
>();
for (const row of rows) {
if (!isUserCommentSupersedableKind(row.kind)) continue;
const interaction = hydrateInteraction(
row,
) as UserCommentSupersedableInteraction;
const interaction = interactionForCommentSupersession(row);
if (!interaction) continue;
if (!shouldSupersedeInteractionOnUserComment(interaction)) continue;
const supersedingComment = comments.find((comment) =>
+5 -4
View File
@@ -75,6 +75,7 @@ import {
} from "./mcp-tasks.js";
import { logger } from "../middleware/logger.js";
import { getAppDefinitionForApplicationKey, isConnectorFeatureEnabled } from "@paperclipai/shared";
import { requestConfirmationPayloadSchema } from "@paperclipai/shared";
import { instanceSettingsService } from "./instance-settings.js";
import { redactSensitiveText } from "../redaction.js";
import { spawn } from "node:child_process";
@@ -4797,7 +4798,7 @@ export function createToolGatewayService(
.limit(1);
const href = `/${company?.issuePrefix ?? ""}/apps/${connection.id}/permissions`;
const idempotencyKey = `connection-authorization:${connection.id}:${userId}`;
const payload = {
const payload = requestConfirmationPayloadSchema.parse({
version: 1 as const,
prompt:
grantKind === "organization"
@@ -4814,12 +4815,12 @@ export function createToolGatewayService(
: "This run needs your personal authorization. Paperclip will not use another user's identity.",
target: {
type: "custom" as const,
key: `connection:${connection.uid}:user:${userId}`,
key: `connection-authorization:${createHash("sha256").update(idempotencyKey).digest("hex")}`,
revisionId: connection.updatedAt.toISOString(),
label: `Connect ${connection.name}`,
label: `Connect ${connection.name}`.slice(0, 120),
href,
},
};
});
const [existing] = await db
.select({ id: issueThreadInteractions.id })
.from(issueThreadInteractions)