2026-03-11 17:23:33 -05:00
|
|
|
import express from "express";
|
|
|
|
|
import request from "supertest";
|
|
|
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
|
|
|
|
|
|
const mockActivityService = vi.hoisted(() => ({
|
|
|
|
|
list: vi.fn(),
|
|
|
|
|
forIssue: vi.fn(),
|
|
|
|
|
runsForIssue: vi.fn(),
|
|
|
|
|
issuesForRun: vi.fn(),
|
|
|
|
|
create: vi.fn(),
|
|
|
|
|
}));
|
|
|
|
|
|
fix(authz): scope import, approvals, activity, and heartbeat routes (#3315)
## Thinking Path
> - Paperclip orchestrates AI agents and company-scoped control-plane
actions for zero-human companies.
> - This change touches the server authz boundary around company
portability, approvals, activity, and heartbeat-run operations.
> - The vulnerability was that board-authenticated callers could cross
company boundaries or create new companies through import paths without
the same authorization checks enforced elsewhere.
> - Once that gap existed, an attacker could chain it into higher-impact
behavior through agent execution paths.
> - The fix needed to harden every confirmed authorization gap in the
reported chain, not just the first route that exposed it.
> - This pull request adds the missing instance-admin and company-access
checks and adds regression tests for each affected route.
> - The benefit is that cross-company actions and new-company import
flows now follow the same control-plane authorization rules as the rest
of the product.
## What Changed
- Required instance-admin access for `new_company` import preview/apply
flows in `server/src/routes/companies.ts`.
- Required company access before approval decision routes in
`server/src/routes/approvals.ts`.
- Required company access for activity creation and heartbeat-run issue
listing in `server/src/routes/activity.ts`.
- Required company access before heartbeat cancellation in
`server/src/routes/agents.ts`.
- Added regression coverage in the corresponding server route tests.
## Verification
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/company-portability-routes.test.ts
src/__tests__/approval-routes-idempotency.test.ts
src/__tests__/activity-routes.test.ts
src/__tests__/agent-permissions-routes.test.ts`
- `pnpm --filter @paperclipai/server typecheck`
- Prior verification on the original security patch branch also included
`pnpm build`.
## Risks
- Low code risk: the change is narrow and only adds missing
authorization gates to existing routes.
- Operational risk: the advisory is already public, so this PR should be
merged quickly to minimize the public unpatched window.
- Residual product risk remains around open signup / bootstrap defaults,
which was intentionally left out of this patch because the current
first-user onboarding flow depends on it.
## Model Used
- OpenAI GPT-5 Codex coding agent with tool use and local code execution
in the Codex CLI environment.
## 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 run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] If this change affects the UI, I have included before/after
screenshots
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] I will address all Greptile and reviewer comments before
requesting merge
Co-authored-by: Forgotten <forgottenrunes@protonmail.com>
2026-04-10 11:55:27 -05:00
|
|
|
const mockHeartbeatService = vi.hoisted(() => ({
|
|
|
|
|
getRun: vi.fn(),
|
|
|
|
|
}));
|
|
|
|
|
|
2026-03-11 17:23:33 -05:00
|
|
|
const mockIssueService = vi.hoisted(() => ({
|
|
|
|
|
getById: vi.fn(),
|
|
|
|
|
getByIdentifier: vi.fn(),
|
|
|
|
|
}));
|
|
|
|
|
|
2026-04-09 06:12:39 -05:00
|
|
|
function registerRouteMocks() {
|
|
|
|
|
vi.doMock("../services/activity.js", () => ({
|
|
|
|
|
activityService: () => mockActivityService,
|
|
|
|
|
}));
|
2026-03-11 17:23:33 -05:00
|
|
|
|
2026-04-09 06:12:39 -05:00
|
|
|
vi.doMock("../services/index.js", () => ({
|
|
|
|
|
issueService: () => mockIssueService,
|
fix(authz): scope import, approvals, activity, and heartbeat routes (#3315)
## Thinking Path
> - Paperclip orchestrates AI agents and company-scoped control-plane
actions for zero-human companies.
> - This change touches the server authz boundary around company
portability, approvals, activity, and heartbeat-run operations.
> - The vulnerability was that board-authenticated callers could cross
company boundaries or create new companies through import paths without
the same authorization checks enforced elsewhere.
> - Once that gap existed, an attacker could chain it into higher-impact
behavior through agent execution paths.
> - The fix needed to harden every confirmed authorization gap in the
reported chain, not just the first route that exposed it.
> - This pull request adds the missing instance-admin and company-access
checks and adds regression tests for each affected route.
> - The benefit is that cross-company actions and new-company import
flows now follow the same control-plane authorization rules as the rest
of the product.
## What Changed
- Required instance-admin access for `new_company` import preview/apply
flows in `server/src/routes/companies.ts`.
- Required company access before approval decision routes in
`server/src/routes/approvals.ts`.
- Required company access for activity creation and heartbeat-run issue
listing in `server/src/routes/activity.ts`.
- Required company access before heartbeat cancellation in
`server/src/routes/agents.ts`.
- Added regression coverage in the corresponding server route tests.
## Verification
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/company-portability-routes.test.ts
src/__tests__/approval-routes-idempotency.test.ts
src/__tests__/activity-routes.test.ts
src/__tests__/agent-permissions-routes.test.ts`
- `pnpm --filter @paperclipai/server typecheck`
- Prior verification on the original security patch branch also included
`pnpm build`.
## Risks
- Low code risk: the change is narrow and only adds missing
authorization gates to existing routes.
- Operational risk: the advisory is already public, so this PR should be
merged quickly to minimize the public unpatched window.
- Residual product risk remains around open signup / bootstrap defaults,
which was intentionally left out of this patch because the current
first-user onboarding flow depends on it.
## Model Used
- OpenAI GPT-5 Codex coding agent with tool use and local code execution
in the Codex CLI environment.
## 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 run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] If this change affects the UI, I have included before/after
screenshots
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] I will address all Greptile and reviewer comments before
requesting merge
Co-authored-by: Forgotten <forgottenrunes@protonmail.com>
2026-04-10 11:55:27 -05:00
|
|
|
heartbeatService: () => mockHeartbeatService,
|
2026-04-09 06:12:39 -05:00
|
|
|
}));
|
|
|
|
|
}
|
2026-03-11 17:23:33 -05:00
|
|
|
|
2026-04-09 06:12:39 -05:00
|
|
|
async function createApp() {
|
|
|
|
|
const [{ errorHandler }, { activityRoutes }] = await Promise.all([
|
|
|
|
|
import("../middleware/index.js"),
|
|
|
|
|
import("../routes/activity.js"),
|
|
|
|
|
]);
|
2026-03-11 17:23:33 -05:00
|
|
|
const app = express();
|
|
|
|
|
app.use(express.json());
|
|
|
|
|
app.use((req, _res, next) => {
|
|
|
|
|
(req as any).actor = {
|
|
|
|
|
type: "board",
|
|
|
|
|
userId: "user-1",
|
|
|
|
|
companyIds: ["company-1"],
|
|
|
|
|
source: "session",
|
|
|
|
|
isInstanceAdmin: false,
|
|
|
|
|
};
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
app.use("/api", activityRoutes({} as any));
|
|
|
|
|
app.use(errorHandler);
|
|
|
|
|
return app;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe("activity routes", () => {
|
|
|
|
|
beforeEach(() => {
|
2026-04-09 06:12:39 -05:00
|
|
|
vi.resetModules();
|
|
|
|
|
registerRouteMocks();
|
2026-03-11 17:23:33 -05:00
|
|
|
vi.clearAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("resolves issue identifiers before loading runs", async () => {
|
|
|
|
|
mockIssueService.getByIdentifier.mockResolvedValue({
|
|
|
|
|
id: "issue-uuid-1",
|
|
|
|
|
companyId: "company-1",
|
|
|
|
|
});
|
|
|
|
|
mockActivityService.runsForIssue.mockResolvedValue([
|
|
|
|
|
{
|
|
|
|
|
runId: "run-1",
|
2026-04-09 10:26:17 -05:00
|
|
|
adapterType: "codex_local",
|
2026-03-11 17:23:33 -05:00
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
|
2026-04-09 06:12:39 -05:00
|
|
|
const app = await createApp();
|
|
|
|
|
const res = await request(app).get("/api/issues/PAP-475/runs");
|
2026-03-11 17:23:33 -05:00
|
|
|
|
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
|
expect(mockIssueService.getByIdentifier).toHaveBeenCalledWith("PAP-475");
|
|
|
|
|
expect(mockIssueService.getById).not.toHaveBeenCalled();
|
|
|
|
|
expect(mockActivityService.runsForIssue).toHaveBeenCalledWith("company-1", "issue-uuid-1");
|
2026-04-09 10:26:17 -05:00
|
|
|
expect(res.body).toEqual([{ runId: "run-1", adapterType: "codex_local" }]);
|
2026-03-11 17:23:33 -05:00
|
|
|
});
|
fix(authz): scope import, approvals, activity, and heartbeat routes (#3315)
## Thinking Path
> - Paperclip orchestrates AI agents and company-scoped control-plane
actions for zero-human companies.
> - This change touches the server authz boundary around company
portability, approvals, activity, and heartbeat-run operations.
> - The vulnerability was that board-authenticated callers could cross
company boundaries or create new companies through import paths without
the same authorization checks enforced elsewhere.
> - Once that gap existed, an attacker could chain it into higher-impact
behavior through agent execution paths.
> - The fix needed to harden every confirmed authorization gap in the
reported chain, not just the first route that exposed it.
> - This pull request adds the missing instance-admin and company-access
checks and adds regression tests for each affected route.
> - The benefit is that cross-company actions and new-company import
flows now follow the same control-plane authorization rules as the rest
of the product.
## What Changed
- Required instance-admin access for `new_company` import preview/apply
flows in `server/src/routes/companies.ts`.
- Required company access before approval decision routes in
`server/src/routes/approvals.ts`.
- Required company access for activity creation and heartbeat-run issue
listing in `server/src/routes/activity.ts`.
- Required company access before heartbeat cancellation in
`server/src/routes/agents.ts`.
- Added regression coverage in the corresponding server route tests.
## Verification
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/company-portability-routes.test.ts
src/__tests__/approval-routes-idempotency.test.ts
src/__tests__/activity-routes.test.ts
src/__tests__/agent-permissions-routes.test.ts`
- `pnpm --filter @paperclipai/server typecheck`
- Prior verification on the original security patch branch also included
`pnpm build`.
## Risks
- Low code risk: the change is narrow and only adds missing
authorization gates to existing routes.
- Operational risk: the advisory is already public, so this PR should be
merged quickly to minimize the public unpatched window.
- Residual product risk remains around open signup / bootstrap defaults,
which was intentionally left out of this patch because the current
first-user onboarding flow depends on it.
## Model Used
- OpenAI GPT-5 Codex coding agent with tool use and local code execution
in the Codex CLI environment.
## 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 run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] If this change affects the UI, I have included before/after
screenshots
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] I will address all Greptile and reviewer comments before
requesting merge
Co-authored-by: Forgotten <forgottenrunes@protonmail.com>
2026-04-10 11:55:27 -05:00
|
|
|
|
|
|
|
|
it("requires company access before creating activity events", async () => {
|
|
|
|
|
const app = await createApp();
|
|
|
|
|
const res = await request(app)
|
|
|
|
|
.post("/api/companies/company-2/activity")
|
|
|
|
|
.send({
|
|
|
|
|
actorId: "user-1",
|
|
|
|
|
action: "test.event",
|
|
|
|
|
entityType: "issue",
|
|
|
|
|
entityId: "issue-1",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(res.status).toBe(403);
|
|
|
|
|
expect(mockActivityService.create).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("requires company access before listing issues for another company's run", async () => {
|
|
|
|
|
mockHeartbeatService.getRun.mockResolvedValue({
|
|
|
|
|
id: "run-2",
|
|
|
|
|
companyId: "company-2",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const app = await createApp();
|
|
|
|
|
const res = await request(app).get("/api/heartbeat-runs/run-2/issues");
|
|
|
|
|
|
|
|
|
|
expect(res.status).toBe(403);
|
|
|
|
|
expect(mockActivityService.issuesForRun).not.toHaveBeenCalled();
|
|
|
|
|
});
|
2026-03-11 17:23:33 -05:00
|
|
|
});
|