Skip to content

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

remove unused and check CSFR token

Thiago Araujo da Silva642e27b7bcf4
diff --git a/backend/src/db/migrations/20260529204743_create-github-apps.ts b/backend/src/db/migrations/20260529204743_create-github-apps.ts
index eefdbccf25..098ddd8fc0 100644
--- a/backend/src/db/migrations/20260529204743_create-github-apps.ts
+++ b/backend/src/db/migrations/20260529204743_create-github-apps.ts
@@ -17,8 +17,7 @@ export async function up(knex: Knex): Promise<void> {
t.string("slug").notNullable();
t.string("owner").nullable();
t.timestamps(true, true, true);
- // GitHub app names are globally unique, so enforce uniqueness across all orgs
- t.unique(["name"]);
+ t.unique(["orgId", "name"]);
});
await createOnUpdateTrigger(knex, TableName.GitHubApp);
diff --git a/backend/src/server/routes/v1/github-app-router.ts b/backend/src/server/routes/v1/github-app-router.ts
index b36629400e..f78aa99ec7 100644
--- a/backend/src/server/routes/v1/github-app-router.ts
+++ b/backend/src/server/routes/v1/github-app-router.ts
@@ -76,72 +76,6 @@ export const registerGitHubAppRouter = async (server: FastifyZodProvider) => {
}
});
- server.route({
- method: "POST",
- url: "/manifest/exchange",
- config: {
- rateLimit: writeLimit
- },
- onRequest: verifyAuth([AuthMode.JWT]),
- schema: {
- body: z.object({
- name: z.string().trim().min(1).max(64),
- code: z.string().trim().min(1)
- }),
- response: {
- 200: z.object({
- gitHubApp: SanitizedGitHubAppSchema
- })
- }
- },
- handler: async (req) => {
- const gitHubApp = await server.services.gitHubApp.exchangeManifestCode({
- orgPermission: req.permission,
- name: req.body.name,
- code: req.body.code
- });
-
- return { gitHubApp };
- }
- });
-
- server.route({
- method: "POST",
- url: "/register",
- config: {
- rateLimit: writeLimit
- },
- onRequest: verifyAuth([AuthMode.JWT]),
- schema: {
- body: z.object({
- name: z.string().trim().min(1).max(64),
- appId: z.string().trim().min(1),
- slug: z.string().trim().min(1),
- clientId: z.string().trim().min(1),
- clientSecret: z.string().trim().min(1),
- privateKey: z.string().trim().min(1)
- }),
- response: {
- 200: z.object({
- gitHubApp: SanitizedGitHubAppSchema
- })
- }
- },
- handler: async (req) => {
- const gitHubApp = await server.services.gitHubApp.registerGitHubApp({
- orgPermission: req.permission,
- name: req.body.name,
- appId: req.body.appId,
- slug: req.body.slug,
- clientId: req.body.clientId,
- clientSecret: req.body.clientSecret,
- privateKey: req.body.privateKey
- });
-
- return { gitHubApp };
- }
- });
-
server.route({
method: "GET",
url: "/",
diff --git a/backend/src/services/github-app/github-app-service.ts b/backend/src/services/github-app/github-app-service.ts
index 33ddb861c3..20489f2a46 100644
--- a/backend/src/services/github-app/github-app-service.ts
+++ b/backend/src/services/github-app/github-app-service.ts
@@ -17,12 +17,10 @@ import { KmsDataKey } from "@app/services/kms/kms-types";
import { TGitHubAppDALFactory } from "./github-app-dal";
import {
TDeleteGitHubAppDTO,
- TExchangeGitHubManifestCodeDTO,
TGitHubAppManifestResponse,
TGitHubManifestStatePayload,
TInitiateGitHubManifestDTO,
TListGitHubAppsDTO,
- TRegisterGitHubAppDTO,
TSanitizedGitHubApp
} from "./github-app-types";
@@ -41,80 +39,6 @@ export const gitHubAppServiceFactory = ({
permissionService,
kmsService
}: TGitHubAppServiceFactoryDep) => {
- const exchangeManifestCode = async ({
- name,
- code,
- orgPermission
- }: TExchangeGitHubManifestCodeDTO): Promise<TSanitizedGitHubApp> => {
- const { permission } = await permissionService.getOrgPermission({
- actor: orgPermission.type,
- actorId: orgPermission.id,
- orgId: orgPermission.orgId,
- actorAuthMethod: orgPermission.authMethod,
- actorOrgId: orgPermission.orgId,
- scope: OrganizationActionScope.ParentOrganization
- });
-
- ForbiddenError.from(permission).throwUnlessCan(OrgPermissionActions.Create, OrgPermissionSubjects.Settings);
-
- const existing = await gitHubAppDAL.findOne({ orgId: orgPermission.orgId, name });
- if (existing) {
- throw new BadRequestError({
- message: `A GitHub App with name "${name}" already exists in this organization.`
- });
- }
-
- let manifestResponse: TGitHubAppManifestResponse;
- try {
- const { data } = await request.post<TGitHubAppManifestResponse>(
- `https://api.github.com/app-manifests/${encodeURIComponent(code)}/conversions`,
- {},
- {
- headers: {
- Accept: "application/vnd.github+json",
- "X-GitHub-Api-Version": "2022-11-28"
- }
- }
- );
- manifestResponse = data;
- } catch (err) {
- throw new BadRequestError({
- message:
- "Failed to exchange GitHub App manifest code. The code may be expired or invalid. Please try registering the GitHub App again."
- });
- }
-
- const { encryptor } = await kmsService.createCipherPairWithDataKey({
- type: KmsDataKey.Organization,
- orgId: orgPermission.orgId
- });
-
- const owner = manifestResponse.owner?.login ?? null;
-
- const created = await gitHubAppDAL.create({
- orgId: orgPermission.orgId,
- name,
- appId: String(manifestResponse.id),
- clientId: manifestResponse.client_id,
- encryptedClientSecret: encryptor({ plainText: Buffer.from(manifestResponse.client_secret) }).cipherTextBlob,
- encryptedPrivateKey: encryptor({ plainText: Buffer.from(manifestResponse.pem) }).cipherTextBlob,
- slug: manifestResponse.slug,
- owner
- });
-
- return {
- id: created.id,
- orgId: created.orgId,
- name: created.name,
- appId: String(manifestResponse.id),
- slug: manifestResponse.slug,
- owner,
- connectionCount: 0,
- createdAt: created.createdAt,
- updatedAt: created.updatedAt
- };
- };
-
const listGitHubApps = async ({ orgPermission }: TListGitHubAppsDTO): Promise<TSanitizedGitHubApp[]> => {
const { permission } = await permissionService.getOrgPermission({
actor: orgPermission.type,
@@ -210,61 +134,6 @@ export const gitHubAppServiceFactory = ({
return sanitized;
};
- const registerGitHubApp = async ({
- name,
- appId,
- slug,
- clientId,
- clientSecret,
- privateKey,
- orgPermission
- }: TRegisterGitHubAppDTO): Promise<TSanitizedGitHubApp> => {
- const { permission } = await permissionService.getOrgPermission({
- actor: orgPermission.type,
- actorId: orgPermission.id,
- orgId: orgPermission.orgId,
- actorAuthMethod: orgPermission.authMethod,
- actorOrgId: orgPermission.orgId,
- scope: OrganizationActionScope.ParentOrganization
- });
-
- ForbiddenError.from(permission).throwUnlessCan(OrgPermissionActions.Create, OrgPermissionSubjects.Settings);
-
- const existing = await gitHubAppDAL.findOne({ orgId: orgPermission.orgId, name });
- if (existing) {
- throw new BadRequestError({
- message: `A GitHub App with name "${name}" already exists in this organization.`
- });
- }
-
- const { encryptor } = await kmsService.createCipherPairWithDataKey({
- type: KmsDataKey.Organization,
- orgId: orgPermission.orgId
- });
-
- const created = await gitHubAppDAL.create({
- orgId: orgPermission.orgId,
- name,
- appId,
- clientId,
- encryptedClientSecret: encryptor({ plainText: Buffer.from(clientSecret) }).cipherTextBlob,
- encryptedPrivateKey: encryptor({ plainText: Buffer.from(privateKey) }).cipherTextBlob,
- slug
- });
-
- return {
- id: created.id,
- orgId: created.orgId,
- name: created.name,
- appId,
- slug,
- owner: null,
- connectionCount: 0,
- createdAt: created.createdAt,
- updatedAt: created.updatedAt
- };
- };
-
const initiateManifestCreation = async ({
name,
instanceType,
@@ -445,11 +314,9 @@ export const gitHubAppServiceFactory = ({
};
return {
- exchangeManifestCode,
initiateManifestCreation,
handleManifestCallback,
listGitHubApps,
- deleteGitHubApp,
- registerGitHubApp
+ deleteGitHubApp
};
};
diff --git a/backend/src/services/github-app/github-app-types.ts b/backend/src/services/github-app/github-app-types.ts
index 01434d3e6c..18ba0d780e 100644
--- a/backend/src/services/github-app/github-app-types.ts
+++ b/backend/src/services/github-app/github-app-types.ts
@@ -1,11 +1,5 @@
import { OrgServiceActor } from "@app/lib/types";
-export type TExchangeGitHubManifestCodeDTO = {
- name: string;
- code: string;
- orgPermission: OrgServiceActor;
-};
-
export type TInitiateGitHubManifestDTO = {
name: string;
instanceType: "cloud" | "server";
@@ -36,16 +30,6 @@ export type TDeleteGitHubAppDTO = {
orgPermission: OrgServiceActor;
};
-export type TRegisterGitHubAppDTO = {
- name: string;
- appId: string;
- slug: string;
- clientId: string;
- clientSecret: string;
- privateKey: string;
- orgPermission: OrgServiceActor;
-};
-
export type TGitHubAppManifestResponse = {
id: number;
slug: string;
diff --git a/frontend/src/pages/organization/AppConnections/GitHubManifestCallbackPage/GitHubManifestCallbackPage.tsx b/frontend/src/pages/organization/AppConnections/GitHubManifestCallbackPage/GitHubManifestCallbackPage.tsx
index 23268575a1..9c1ba555f3 100644
--- a/frontend/src/pages/organization/AppConnections/GitHubManifestCallbackPage/GitHubManifestCallbackPage.tsx
+++ b/frontend/src/pages/organization/AppConnections/GitHubManifestCallbackPage/GitHubManifestCallbackPage.tsx
@@ -35,6 +35,17 @@ export const GitHubManifestCallbackPage = () => {
return;
}
+ // Confirm this callback corresponds to a flow this browser initiated. The install state is
+ // round-tripped through the signed manifest token and echoed back here; it must match the CSRF
+ // token stored when the user kicked off app creation. This blocks a crafted callback URL from
+ // pre-seeding the connection form with an attacker-chosen app id.
+ if (installState !== localStorage.getItem("latestCSRFToken")) {
+ createNotification({ type: "error", text: "Invalid GitHub manifest callback state." });
+ navigate({ to: "/organizations/$orgId/app-connections", params: { orgId } });
+ return;
+ }
+ localStorage.removeItem("latestCSRFToken");
+
// The GitHub App is already created at this point. Mark the in-progress connection form so it
// resumes with the new app selected, then send the user back to it to finish the connection.
try {