Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

PR comments and read cookie in resolver

Matheus Nogueira5979aca1218f
diff --git a/backend/src/server/plugins/inject-secret-scanning-project-id.test.ts b/backend/src/server/plugins/inject-secret-scanning-project-id.test.ts
index f486f7d346..f63e70258c 100644
--- a/backend/src/server/plugins/inject-secret-scanning-project-id.test.ts
+++ b/backend/src/server/plugins/inject-secret-scanning-project-id.test.ts
@@ -1,3 +1,4 @@
+import cookie from "@fastify/cookie";
import Fastify, { FastifyInstance } from "fastify";
import { afterEach, describe, expect, test, vi } from "vitest";
import { z } from "zod";
@@ -11,6 +12,7 @@ const ORG_ID = "org-1";
const ACTIVE_PROJECT_ID = "11111111-1111-4111-8111-111111111111";
const OLDER_PROJECT_ID = "22222222-2222-4222-8222-222222222222";
const UNKNOWN_PROJECT_ID = "33333333-3333-4333-8333-333333333333";
+const COOKIE_NAME = `infisical-ss-active-project-${ORG_ID}`;
const buildServer = async ({
activeProjectId = ACTIVE_PROJECT_ID as string | null,
@@ -32,6 +34,7 @@ const buildServer = async ({
return reply.status(statusCode).send({ message: error.message });
});
+ await app.register(cookie);
app.decorate("services", {
secretScanningV2ProjectResolver: { findActiveProjectId, isSecretScanningProject }
} as never);
@@ -129,6 +132,63 @@ describe("injectSecretScanningProjectId", () => {
expect(res.json()).toEqual({ projectId: OLDER_PROJECT_ID });
});
+ test("uses the cookie's project when no projectId is passed", async () => {
+ const server = await buildServer();
+ app = server.app;
+
+ const res = await app.inject({
+ method: "GET",
+ url: "/api/v2/secret-scanning/data-sources",
+ cookies: { [COOKIE_NAME]: OLDER_PROJECT_ID }
+ });
+
+ expect(res.json()).toEqual({ projectId: OLDER_PROJECT_ID });
+ expect(server.isSecretScanningProject).toHaveBeenCalledWith(OLDER_PROJECT_ID, ORG_ID);
+ expect(server.findActiveProjectId).not.toHaveBeenCalled();
+ });
+
+ test("prefers an explicit projectId over the cookie", async () => {
+ const server = await buildServer();
+ app = server.app;
+
+ const res = await app.inject({
+ method: "GET",
+ url: `/api/v2/secret-scanning/data-sources?projectId=${ACTIVE_PROJECT_ID}`,
+ cookies: { [COOKIE_NAME]: OLDER_PROJECT_ID }
+ });
+
+ expect(res.json()).toEqual({ projectId: ACTIVE_PROJECT_ID });
+ expect(server.isSecretScanningProject).toHaveBeenCalledTimes(1);
+ });
+
+ test("falls back to the active project when the cookie's project is not in the org", async () => {
+ const server = await buildServer();
+ app = server.app;
+
+ const res = await app.inject({
+ method: "GET",
+ url: "/api/v2/secret-scanning/data-sources",
+ cookies: { [COOKIE_NAME]: UNKNOWN_PROJECT_ID }
+ });
+
+ expect(res.statusCode).toBe(200);
+ expect(res.json()).toEqual({ projectId: ACTIVE_PROJECT_ID });
+ });
+
+ test("ignores a cookie that is not a UUID or belongs to another org", async () => {
+ const server = await buildServer();
+ app = server.app;
+
+ const res = await app.inject({
+ method: "GET",
+ url: "/api/v2/secret-scanning/data-sources",
+ cookies: { [COOKIE_NAME]: "not-a-uuid", "infisical-ss-active-project-org-2": OLDER_PROJECT_ID }
+ });
+
+ expect(res.json()).toEqual({ projectId: ACTIVE_PROJECT_ID });
+ expect(server.isSecretScanningProject).not.toHaveBeenCalled();
+ });
+
test("rejects a projectId that is not a Secret Scanning project in the org", async () => {
const server = await buildServer();
app = server.app;
diff --git a/backend/src/server/plugins/inject-secret-scanning-project-id.ts b/backend/src/server/plugins/inject-secret-scanning-project-id.ts
index dc8794c069..5b5f98447d 100644
--- a/backend/src/server/plugins/inject-secret-scanning-project-id.ts
+++ b/backend/src/server/plugins/inject-secret-scanning-project-id.ts
@@ -1,10 +1,17 @@
import { FastifyPluginAsync } from "fastify";
import fp from "fastify-plugin";
+import RE2 from "re2";
import { BadRequestError, NotFoundError } from "@app/lib/errors";
const SECRET_SCANNING_PREFIX = "/api/v2/secret-scanning/";
+const COOKIE_PREFIX = "infisical-ss-active-project-";
+
+const secretScanningActiveProjectCookieName = (orgId: string) => `${COOKIE_PREFIX}${orgId}`;
+
+const UUID_REGEX = new RE2("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", "i");
+
type TSchemaWithShape = { shape?: Record<string, unknown> } | undefined;
// Most Secret Scanning routes are keyed by a data source or finding ID; only routes that declare a
@@ -28,6 +35,15 @@ const readProjectIdFromRequest = (
return null;
};
+const readProjectIdFromCookie = (
+ cookies: Record<string, string | undefined> | undefined,
+ orgId: string
+): string | null => {
+ const value = cookies?.[secretScanningActiveProjectCookieName(orgId)];
+ if (!value || !UUID_REGEX.test(value)) return null;
+ return value;
+};
+
export const injectSecretScanningProjectId: FastifyPluginAsync = fp(async (server) => {
server.decorateRequest("internalSecretScanningProjectId", "");
@@ -54,6 +70,20 @@ export const injectSecretScanningProjectId: FastifyPluginAsync = fp(async (serve
return;
}
+ // The cookie is the project the user last opened in the UI. A stale one is ignored rather than
+ // rejected, since the user never typed it.
+ const fromCookie = readProjectIdFromCookie(req.cookies, req.permission.orgId);
+ if (fromCookie) {
+ const isValidForOrg = await server.services.secretScanningV2ProjectResolver.isSecretScanningProject(
+ fromCookie,
+ req.permission.orgId
+ );
+ if (isValidForOrg) {
+ req.internalSecretScanningProjectId = fromCookie;
+ return;
+ }
+ }
+
// Never creates the project: these routes accept tokens whose scopes are only checked later, against it.
const activeProjectId = await server.services.secretScanningV2ProjectResolver.findActiveProjectId(
req.permission.orgId
diff --git a/frontend/src/helpers/secretScanningActiveProject.ts b/frontend/src/helpers/secretScanningActiveProject.ts
index 74297200a8..0c5db13528 100644
--- a/frontend/src/helpers/secretScanningActiveProject.ts
+++ b/frontend/src/helpers/secretScanningActiveProject.ts
@@ -18,10 +18,10 @@ export const setSecretScanningActiveProjectCookie = (orgId: string, projectId: s
};
// Which Secret Scanning project a page in this org shows, now that the URL carries none: the one the
-// user last opened, while they are still a member of it, otherwise the org's active project. Unlike
-// Certificate Manager's, the backend never reads this cookie: every project-scoped Secret Scanning
-// request names its projectId, so only the frontend needs to agree with itself. Do not add a backend
-// fallback to it, since the cookie is shared by every tab and would redirect another tab's writes.
+// user last opened, while they are still a member of it, otherwise the org's active project. The
+// backend falls back to this cookie for requests that name no project, but it is shared by every tab,
+// so a tab that switched projects would redirect another tab's writes. Keep naming the projectId on
+// every project-scoped request.
export const resolveSecretScanningProjectId = ({
orgId,
activeProjectId,
diff --git a/frontend/src/pages/organization/SettingsPage/components/OrgProductSettingsTab/OrgProductSettingsTab.tsx b/frontend/src/pages/organization/SettingsPage/components/OrgProductSettingsTab/OrgProductSettingsTab.tsx
index c5333dd372..ab8d49e97c 100644
--- a/frontend/src/pages/organization/SettingsPage/components/OrgProductSettingsTab/OrgProductSettingsTab.tsx
+++ b/frontend/src/pages/organization/SettingsPage/components/OrgProductSettingsTab/OrgProductSettingsTab.tsx
@@ -20,7 +20,7 @@ import { ProjectType } from "@app/hooks/api/projects/types";
import { OrgCertManagerTab } from "../OrgCertManagerTab";
import { OrgSecretScanningTab } from "../OrgSecretScanningTab";
-const TEMPLATE_PRODUCTS = [ProjectType.KMS, ProjectType.SecretScanning, ProjectType.PAM];
+const TEMPLATE_PRODUCTS = [ProjectType.KMS, ProjectType.PAM];
export const OrgProductSettingsTab = () => {
const { currentOrg } = useOrganization();