mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-17 20:09:05 +02:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - App connections must work in both the operator UI and agent tool gateway. > - The first stack layer adds secure remote connections. > - Operators still need clear setup, test, and recovery states. > - This pull request adds the gateway behavior and the workspace connection experience. > - The benefit is a connection flow that is easier to understand and recover. ## Linked Issues or Issue Description Refs #11965 This is stack 2 of 11. It depends on stack 1 and replaces another reviewable part of #11965. ## What Changed - Improve remote tool gateway connection behavior. - Add clearer app setup, test, and recovery states. - Add focused server and UI tests for the new paths. - Keep the diff isolated from later identity and catalog work. - Stabilize DNS-pinned remote HTTP protocol fixtures and the managed-runtime public-origin fixture for this independently tested layer. ## Verification - `pnpm -r typecheck` - `pnpm --filter @paperclipai/server exec vitest run src/__tests__/tool-access-service.test.ts` (150 passed) - `pnpm test:run` - `pnpm check:token-gates` - `pnpm build` ## Risks - Gateway errors now surface through new user-facing states. - A stale connection can require a new setup attempt. - The change does not add a database migration. - The injected HTTP transport and public URL are test-only fixtures; production DNS pinning and runtime behavior are unchanged. > I checked `ROADMAP.md`. This stack continues the existing app connection work from #11965 and does not duplicate another planned item. ## Model Used OpenAI Codex, GPT-5. The runtime model ID and context window were not exposed. The model used reasoning, tool use, and code execution. ## 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 - [x] My branch name describes the change 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 - [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>
245 lines
9.1 KiB
TypeScript
245 lines
9.1 KiB
TypeScript
import { afterEach, describe, expect, it } from "vitest";
|
|
import type { BetterAuthOptions } from "better-auth";
|
|
import { getCookies } from "better-auth/cookies";
|
|
import {
|
|
buildBetterAuthAdvancedOptions,
|
|
buildBetterAuthRateLimitOptions,
|
|
deriveAuthCookiePrefix,
|
|
deriveAuthTrustedOrigins,
|
|
shouldDisableSecureAuthCookies,
|
|
} from "../auth/better-auth.js";
|
|
|
|
const ORIGINAL_INSTANCE_ID = process.env.PAPERCLIP_INSTANCE_ID;
|
|
const ORIGINAL_PUBLIC_URL = process.env.PAPERCLIP_PUBLIC_URL;
|
|
|
|
afterEach(() => {
|
|
if (ORIGINAL_INSTANCE_ID === undefined) delete process.env.PAPERCLIP_INSTANCE_ID;
|
|
else process.env.PAPERCLIP_INSTANCE_ID = ORIGINAL_INSTANCE_ID;
|
|
if (ORIGINAL_PUBLIC_URL === undefined) delete process.env.PAPERCLIP_PUBLIC_URL;
|
|
else process.env.PAPERCLIP_PUBLIC_URL = ORIGINAL_PUBLIC_URL;
|
|
});
|
|
|
|
describe("Better Auth cookie scoping", () => {
|
|
it("derives an instance-scoped cookie prefix", () => {
|
|
expect(deriveAuthCookiePrefix("default")).toBe("paperclip-default");
|
|
expect(deriveAuthCookiePrefix("PAP-1601-worktree")).toBe("paperclip-PAP-1601-worktree");
|
|
});
|
|
|
|
it("uses PAPERCLIP_INSTANCE_ID for the Better Auth cookie prefix", () => {
|
|
process.env.PAPERCLIP_INSTANCE_ID = "sat-worktree";
|
|
|
|
const advanced = buildBetterAuthAdvancedOptions({ disableSecureCookies: false });
|
|
|
|
expect(advanced).toEqual({
|
|
cookiePrefix: "paperclip-sat-worktree",
|
|
});
|
|
expect(getCookies({ advanced } as BetterAuthOptions).sessionToken.name).toMatch(
|
|
/paperclip-sat-worktree\.session_token$/,
|
|
);
|
|
});
|
|
|
|
it("keeps local http auth cookies non-secure while preserving the scoped prefix", () => {
|
|
process.env.PAPERCLIP_INSTANCE_ID = "pap-worktree";
|
|
|
|
expect(buildBetterAuthAdvancedOptions({ disableSecureCookies: true })).toEqual({
|
|
cookiePrefix: "paperclip-pap-worktree",
|
|
useSecureCookies: false,
|
|
});
|
|
expect(getCookies({
|
|
advanced: buildBetterAuthAdvancedOptions({ disableSecureCookies: true }),
|
|
} as BetterAuthOptions).sessionToken.name).toBe("paperclip-pap-worktree.session_token");
|
|
});
|
|
|
|
it("enables Better Auth rate limiting for authenticated private instances by default", () => {
|
|
expect(buildBetterAuthRateLimitOptions({
|
|
deploymentMode: "authenticated",
|
|
deploymentExposure: "private",
|
|
})).toEqual({ enabled: true });
|
|
});
|
|
|
|
it("keeps Better Auth rate limiting enabled for authenticated public instances", () => {
|
|
expect(buildBetterAuthRateLimitOptions({
|
|
deploymentMode: "authenticated",
|
|
deploymentExposure: "public",
|
|
})).toEqual({ enabled: true });
|
|
});
|
|
|
|
it("allows an explicit Better Auth rate-limit override", () => {
|
|
expect(buildBetterAuthRateLimitOptions({
|
|
deploymentMode: "authenticated",
|
|
deploymentExposure: "private",
|
|
override: "true",
|
|
})).toEqual({ enabled: true });
|
|
|
|
expect(buildBetterAuthRateLimitOptions({
|
|
deploymentMode: "authenticated",
|
|
deploymentExposure: "public",
|
|
override: "false",
|
|
})).toEqual({ enabled: false });
|
|
});
|
|
|
|
it("disables secure cookies for authenticated private auto-origin dev servers", () => {
|
|
expect(shouldDisableSecureAuthCookies({
|
|
deploymentMode: "authenticated",
|
|
deploymentExposure: "private",
|
|
authBaseUrlMode: "auto",
|
|
authPublicBaseUrl: undefined,
|
|
publicUrl: undefined,
|
|
})).toBe(true);
|
|
});
|
|
|
|
it("keeps secure cookies for authenticated public auto-origin servers", () => {
|
|
expect(shouldDisableSecureAuthCookies({
|
|
deploymentMode: "authenticated",
|
|
deploymentExposure: "public",
|
|
authBaseUrlMode: "auto",
|
|
authPublicBaseUrl: undefined,
|
|
publicUrl: undefined,
|
|
})).toBe(false);
|
|
});
|
|
|
|
it("uses an explicit public URL when deciding whether secure cookies are required", () => {
|
|
expect(shouldDisableSecureAuthCookies({
|
|
deploymentMode: "authenticated",
|
|
deploymentExposure: "private",
|
|
authBaseUrlMode: "auto",
|
|
authPublicBaseUrl: undefined,
|
|
publicUrl: "https://paperclip.example.test",
|
|
})).toBe(false);
|
|
|
|
expect(shouldDisableSecureAuthCookies({
|
|
deploymentMode: "authenticated",
|
|
deploymentExposure: "public",
|
|
authBaseUrlMode: "explicit",
|
|
authPublicBaseUrl: "http://paperclip.local.test:3100",
|
|
publicUrl: undefined,
|
|
})).toBe(true);
|
|
});
|
|
|
|
it("disables secure cookies when no canonical public auth URL is configured", () => {
|
|
delete process.env.PAPERCLIP_PUBLIC_URL;
|
|
|
|
expect(shouldDisableSecureAuthCookies({
|
|
deploymentMode: "authenticated",
|
|
authBaseUrlMode: "auto",
|
|
authPublicBaseUrl: undefined,
|
|
} as Parameters<typeof shouldDisableSecureAuthCookies>[0])).toBe(true);
|
|
});
|
|
|
|
it("derives secure cookie behavior from the configured public auth URL", () => {
|
|
delete process.env.PAPERCLIP_PUBLIC_URL;
|
|
|
|
expect(shouldDisableSecureAuthCookies({
|
|
deploymentMode: "authenticated",
|
|
authBaseUrlMode: "explicit",
|
|
authPublicBaseUrl: "http://paperclip-dev:46259",
|
|
} as Parameters<typeof shouldDisableSecureAuthCookies>[0])).toBe(true);
|
|
expect(shouldDisableSecureAuthCookies({
|
|
deploymentMode: "authenticated",
|
|
authBaseUrlMode: "explicit",
|
|
authPublicBaseUrl: "https://paperclip.example.test",
|
|
} as Parameters<typeof shouldDisableSecureAuthCookies>[0])).toBe(false);
|
|
});
|
|
|
|
it("uses the caller-resolved public URL for cookie security", () => {
|
|
process.env.PAPERCLIP_PUBLIC_URL = "https://ignored.example.test";
|
|
|
|
expect(shouldDisableSecureAuthCookies({
|
|
deploymentMode: "authenticated",
|
|
authBaseUrlMode: "explicit",
|
|
authPublicBaseUrl: "https://paperclip.example.test",
|
|
publicUrl: "http://paperclip-dev:46259",
|
|
} as Parameters<typeof shouldDisableSecureAuthCookies>[0])).toBe(true);
|
|
});
|
|
|
|
it("disables secure cookies for private authenticated auto mode without a public URL", () => {
|
|
expect(shouldDisableSecureAuthCookies({
|
|
deploymentMode: "authenticated",
|
|
deploymentExposure: "private",
|
|
authBaseUrlMode: "auto",
|
|
authPublicBaseUrl: undefined,
|
|
})).toBe(true);
|
|
});
|
|
|
|
it("disables secure cookies for explicit HTTP public URLs", () => {
|
|
expect(shouldDisableSecureAuthCookies({
|
|
deploymentMode: "authenticated",
|
|
deploymentExposure: "private",
|
|
authBaseUrlMode: "explicit",
|
|
authPublicBaseUrl: "http://board.example.test:3101",
|
|
})).toBe(true);
|
|
});
|
|
|
|
it("keeps secure cookies for explicit HTTPS public URLs", () => {
|
|
expect(shouldDisableSecureAuthCookies({
|
|
deploymentMode: "authenticated",
|
|
deploymentExposure: "public",
|
|
authBaseUrlMode: "explicit",
|
|
authPublicBaseUrl: "https://board.example.test",
|
|
})).toBe(false);
|
|
});
|
|
|
|
it("disables secure cookies only for HTTP loopback requests in a managed HTTPS runtime", () => {
|
|
const managedRuntimeInput = {
|
|
deploymentMode: "authenticated",
|
|
deploymentExposure: "private",
|
|
authBaseUrlMode: "explicit",
|
|
authPublicBaseUrl: "https://worktree.example.test",
|
|
publicUrl: "https://worktree.example.test",
|
|
managedRuntimePublicUrl: "https://worktree.example.test",
|
|
} as const;
|
|
|
|
expect(shouldDisableSecureAuthCookies({
|
|
...managedRuntimeInput,
|
|
requestUrl: "http://127.0.0.1:42013/api/auth/sign-in/email",
|
|
} as Parameters<typeof shouldDisableSecureAuthCookies>[0])).toBe(true);
|
|
expect(shouldDisableSecureAuthCookies({
|
|
...managedRuntimeInput,
|
|
requestUrl: "https://worktree.example.test/api/auth/sign-in/email",
|
|
} as Parameters<typeof shouldDisableSecureAuthCookies>[0])).toBe(false);
|
|
expect(shouldDisableSecureAuthCookies({
|
|
...managedRuntimeInput,
|
|
managedRuntimePublicUrl: undefined,
|
|
requestUrl: "http://127.0.0.1:42013/api/auth/sign-in/email",
|
|
} as Parameters<typeof shouldDisableSecureAuthCookies>[0])).toBe(false);
|
|
expect(shouldDisableSecureAuthCookies({
|
|
...managedRuntimeInput,
|
|
requestUrl: "http://board.example.test:42013/api/auth/sign-in/email",
|
|
} as Parameters<typeof shouldDisableSecureAuthCookies>[0])).toBe(false);
|
|
});
|
|
|
|
it("adds hostname port variants for authenticated mode on non-default ports", () => {
|
|
const trustedOrigins = deriveAuthTrustedOrigins({
|
|
deploymentMode: "authenticated",
|
|
authBaseUrlMode: "auto",
|
|
authPublicBaseUrl: undefined,
|
|
allowedHostnames: ["Board.Example.Test"],
|
|
port: 3101,
|
|
} as Parameters<typeof deriveAuthTrustedOrigins>[0]);
|
|
|
|
expect(trustedOrigins).toEqual(expect.arrayContaining([
|
|
"https://board.example.test",
|
|
"http://board.example.test",
|
|
"https://board.example.test:3101",
|
|
"http://board.example.test:3101",
|
|
]));
|
|
});
|
|
|
|
it("prefers an explicit resolved listen port over the configured port", () => {
|
|
const trustedOrigins = deriveAuthTrustedOrigins({
|
|
deploymentMode: "authenticated",
|
|
authBaseUrlMode: "auto",
|
|
authPublicBaseUrl: undefined,
|
|
allowedHostnames: ["board.example.test"],
|
|
port: 3100,
|
|
} as Parameters<typeof deriveAuthTrustedOrigins>[0], { listenPort: 3101 });
|
|
|
|
expect(trustedOrigins).toEqual(expect.arrayContaining([
|
|
"https://board.example.test:3101",
|
|
"http://board.example.test:3101",
|
|
]));
|
|
expect(trustedOrigins).not.toContain("https://board.example.test:3100");
|
|
expect(trustedOrigins).not.toContain("http://board.example.test:3100");
|
|
});
|
|
});
|