Skip to content

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

fix(gateway): check the name before committing the auth method

bernie-gccc8a7994fd1
diff --git a/backend/src/ee/routes/v3/gateway-router.ts b/backend/src/ee/routes/v3/gateway-router.ts
index 0e4d89a9bf..cdee6c2ba6 100644
--- a/backend/src/ee/routes/v3/gateway-router.ts
+++ b/backend/src/ee/routes/v3/gateway-router.ts
@@ -343,6 +343,15 @@ export const registerGatewayV3Router = async (server: FastifyZodProvider) => {
},
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
handler: async (req) => {
+ // Before the auth method is touched, so the usual rejection leaves nothing applied.
+ if (req.body.name) {
+ await server.services.gatewayV2.assertGatewayNameAvailable({
+ orgPermission: req.permission,
+ gatewayId: req.params.gatewayId,
+ name: req.body.name
+ });
+ }
+
// Auth first: it is the half that fails, so a rejection cannot leave behind a rename.
if (req.body.authMethod) {
const result = await server.services.resourceAuthMethod.setMethod({
diff --git a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
index 03020c0f52..0a54ccb641 100644
--- a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
+++ b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
@@ -1317,15 +1317,7 @@ export const gatewayV2ServiceFactory = ({
// --- V3 service methods ---
- const renameGateway = async ({
- orgPermission,
- gatewayId,
- name
- }: {
- orgPermission: OrgServiceActor;
- gatewayId: string;
- name: string;
- }) => {
+ const $assertCanEditGateway = async (orgPermission: OrgServiceActor, gatewayId: string) => {
const gateway = await gatewayV2DAL.findOne({ id: gatewayId, orgId: orgPermission.orgId });
if (!gateway) {
throw new NotFoundError({ message: `Gateway ${gatewayId} not found` });
@@ -1345,6 +1337,40 @@ export const gatewayV2ServiceFactory = ({
OrgPermissionSubjects.Gateway
);
+ return gateway;
+ };
+
+ // Setting an auth method dials the customer's cluster, so the two halves of an update cannot
+ // share a transaction. Checking the name first means the ordinary rejection lands before the
+ // auth method is committed; the unique violation in renameGateway still covers the race.
+ const assertGatewayNameAvailable = async ({
+ orgPermission,
+ gatewayId,
+ name
+ }: {
+ orgPermission: OrgServiceActor;
+ gatewayId: string;
+ name: string;
+ }) => {
+ const gateway = await $assertCanEditGateway(orgPermission, gatewayId);
+ if (gateway.name === name) return;
+
+ const existing = await gatewayV2DAL.findOne({ orgId: orgPermission.orgId, name });
+ if (existing && existing.id !== gatewayId) {
+ throw new BadRequestError({ message: `A gateway named "${name}" already exists` });
+ }
+ };
+
+ const renameGateway = async ({
+ orgPermission,
+ gatewayId,
+ name
+ }: {
+ orgPermission: OrgServiceActor;
+ gatewayId: string;
+ name: string;
+ }) => {
+ const gateway = await $assertCanEditGateway(orgPermission, gatewayId);
if (gateway.name === name) return { gateway, previousName: name };
try {
@@ -1523,6 +1549,7 @@ export const gatewayV2ServiceFactory = ({
// V3
createGateway,
renameGateway,
+ assertGatewayNameAvailable,
connectGateway
};
};