Skip to content

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

fix race condition and use new protection added

Thiago Araujo da Silva19475556f76c
diff --git a/backend/src/services/github-app/github-app-dal.ts b/backend/src/services/github-app/github-app-dal.ts
index c7435b3808..f49c94c02a 100644
--- a/backend/src/services/github-app/github-app-dal.ts
+++ b/backend/src/services/github-app/github-app-dal.ts
@@ -1,10 +1,21 @@
+import { Knex } from "knex";
+
import { TDbClient } from "@app/db";
-import { TableName } from "@app/db/schemas";
+import { TableName, TGithubApps } from "@app/db/schemas";
import { ormify } from "@app/lib/knex";
export type TGitHubAppDALFactory = ReturnType<typeof gitHubAppDALFactory>;
export const gitHubAppDALFactory = (db: TDbClient) => {
const orm = ormify(db, TableName.GitHubApp);
- return orm;
+
+ const findByIdWithLock = async (id: string, orgId: string, tx: Knex): Promise<TGithubApps | undefined> => {
+ const app = await tx(TableName.GitHubApp).where({ id, orgId }).forUpdate().first();
+ return app;
+ };
+
+ return {
+ ...orm,
+ findByIdWithLock
+ };
};
diff --git a/backend/src/services/github-app/github-app-service.ts b/backend/src/services/github-app/github-app-service.ts
index 8c79e3a151..6a59dc4459 100644
--- a/backend/src/services/github-app/github-app-service.ts
+++ b/backend/src/services/github-app/github-app-service.ts
@@ -13,6 +13,7 @@ import { logger } from "@app/lib/logger";
import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator";
import { TAppConnectionDALFactory } from "@app/services/app-connection/app-connection-dal";
import {
+ assertPlatformGitHubHostAllowed,
getGitHubInstanceApiUrl,
resolveGitHubAppCredentials
} from "@app/services/app-connection/github/github-connection-fns";
@@ -108,37 +109,39 @@ export const gitHubAppServiceFactory = ({
ForbiddenError.from(permission).throwUnlessCan(OrgPermissionActions.Delete, OrgPermissionSubjects.Settings);
- const existing = await gitHubAppDAL.findOne({ id, orgId: orgPermission.orgId });
- if (!existing) {
- throw new NotFoundError({ message: `GitHub App with id ${id} not found in this organization.` });
- }
-
- const connectionCounts = await appConnectionDAL.countByGitHubApp(orgPermission.orgId);
- const connectionCount = connectionCounts.find(({ gitHubAppId }) => gitHubAppId === existing.id)?.count ?? 0;
-
- if (connectionCount > 0) {
- throw new BadRequestError({
- message: `Cannot delete GitHub App "${existing.name}" while it is in use by ${connectionCount} connection${
- connectionCount === 1 ? "" : "s"
- }. Remove those connections first.`
- });
- }
+ return gitHubAppDAL.transaction(async (tx) => {
+ const existing = await gitHubAppDAL.findByIdWithLock(id, orgPermission.orgId, tx);
+ if (!existing) {
+ throw new NotFoundError({ message: `GitHub App with id ${id} not found in this organization.` });
+ }
- const sanitized: TSanitizedGitHubApp = {
- id: existing.id,
- orgId: existing.orgId,
- name: existing.name,
- appId: existing.appId,
- slug: existing.slug,
- owner: existing.owner ?? null,
- connectionCount: 0,
- createdAt: existing.createdAt,
- updatedAt: existing.updatedAt
- };
+ const connectionCounts = await appConnectionDAL.countByGitHubApp(orgPermission.orgId, tx);
+ const connectionCount = connectionCounts.find(({ gitHubAppId }) => gitHubAppId === existing.id)?.count ?? 0;
- await gitHubAppDAL.deleteById(existing.id);
+ if (connectionCount > 0) {
+ throw new BadRequestError({
+ message: `Cannot delete GitHub App "${existing.name}" while it is in use by ${connectionCount} connection${
+ connectionCount === 1 ? "" : "s"
+ }. Remove those connections first.`
+ });
+ }
- return sanitized;
+ const sanitized: TSanitizedGitHubApp = {
+ id: existing.id,
+ orgId: existing.orgId,
+ name: existing.name,
+ appId: existing.appId,
+ slug: existing.slug,
+ owner: existing.owner ?? null,
+ connectionCount: 0,
+ createdAt: existing.createdAt,
+ updatedAt: existing.updatedAt
+ };
+
+ await gitHubAppDAL.deleteById(existing.id, tx);
+
+ return sanitized;
+ });
};
const initiateManifestCreation = async ({
@@ -160,6 +163,8 @@ export const gitHubAppServiceFactory = ({
ForbiddenError.from(permission).throwUnlessCan(OrgPermissionActions.Create, OrgPermissionSubjects.Settings);
+ assertPlatformGitHubHostAllowed(githubHost);
+
const existing = await gitHubAppDAL.findOne({ orgId: orgPermission.orgId, name });
if (existing) {
throw new BadRequestError({
@@ -255,6 +260,8 @@ export const gitHubAppServiceFactory = ({
});
}
+ assertPlatformGitHubHostAllowed(githubHost);
+
if (githubHost) {
await blockLocalAndPrivateIpAddresses(`https://${githubHost}`);
}
@@ -341,6 +348,8 @@ export const gitHubAppServiceFactory = ({
ForbiddenError.from(permission).throwUnlessCan(OrgPermissionActions.Read, OrgPermissionSubjects.Settings);
+ assertPlatformGitHubHostAllowed(host);
+
const { appId, clientId, privateKey } = await resolveGitHubAppCredentials(
{ gitHubAppId: gitHubAppId ?? null, orgId: orgPermission.orgId },
{ gitHubAppDAL, kmsService }