Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix(github-org-sync): require linked identities before sync
Scott Wilson6ab33bd6b12b
diff --git a/backend/src/ee/services/github-org-sync/github-org-sync-service.test.ts b/backend/src/ee/services/github-org-sync/github-org-sync-service.test.tsindex f58ecdb28d..9e37073a85 100644--- a/backend/src/ee/services/github-org-sync/github-org-sync-service.test.ts+++ b/backend/src/ee/services/github-org-sync/github-org-sync-service.test.ts@@ -3,7 +3,11 @@ import { describe, expect, test, vi } from "vitest";import { BadRequestError } from "@app/lib/errors";-import { buildGithubMemberMatcher, fetchGithubOrgTeams } from "./github-org-sync-service";+import {+ assertGithubGroupMembersLinked,+ buildGithubMemberMatcher,+ fetchGithubOrgTeams+} from "./github-org-sync-service";type TVariables = { cursor: string | null; slug?: string; teamsPageSize?: number; membersPageSize?: number };@@ -141,3 +145,31 @@ describe("buildGithubMemberMatcher", () => {expect(match(githubMember(null))).toBeUndefined();});});++describe("assertGithubGroupMembersLinked", () => {+ const activeUsers = [+ { id: "user-a", email: "[email protected]" },+ { id: "user-b", email: "[email protected]" }+ ];++ test("allows reconciliation when every existing member has a verified GitHub link", () => {+ expect(() =>+ assertGithubGroupMembersLinked({+ currentUserIds: new Set(["user-a", "user-b"]),+ linkedUserIds: new Set(["user-a", "user-b"]),+ activeUsers+ })+ ).not.toThrow();+ });++ test("stops reconciliation before changes when an existing member has no verified GitHub link", () => {+ expect(() =>+ assertGithubGroupMembersLinked({+ currentUserIds: new Set(["user-a", "user-b"]),+ linkedUserIds: new Set(["user-a"]),+ activeUsers,+ noChangesApplied: true+ })+ ).toThrow(/bob@example\.com.*No changes were applied/);+ });+});diff --git a/backend/src/ee/services/github-org-sync/github-org-sync-service.ts b/backend/src/ee/services/github-org-sync/github-org-sync-service.tsindex afbd3f9019..ec817c1201 100644--- a/backend/src/ee/services/github-org-sync/github-org-sync-service.ts+++ b/backend/src/ee/services/github-org-sync/github-org-sync-service.ts@@ -267,13 +267,39 @@ interface GroupMembership {lastName: string | null;}+export const assertGithubGroupMembersLinked = ({+ currentUserIds,+ linkedUserIds,+ activeUsers,+ noChangesApplied = false+}: {+ currentUserIds: Set<string>;+ linkedUserIds: Set<string>;+ activeUsers: { id: string; email: string }[];+ noChangesApplied?: boolean;+}) => {+ const unlinkedUserIds = [...currentUserIds].filter((userId) => !linkedUserIds.has(userId));+ if (!unlinkedUserIds.length) return;++ const emailsByUserId = new Map(activeUsers.map((user) => [user.id, user.email]));+ const reportLimit = 10;+ const listedMembers = unlinkedUserIds.slice(0, reportLimit).map((userId) => emailsByUserId.get(userId) ?? userId);+ const remainingCount = unlinkedUserIds.length - listedMembers.length;+ const remainingMessage = remainingCount > 0 ? ` and ${remainingCount} more` : "";+ const completionMessage = noChangesApplied ? " No changes were applied." : "";++ throw new BadRequestError({+ message: `GitHub team sync cannot safely reconcile ${unlinkedUserIds.length} existing group member${unlinkedUserIds.length === 1 ? "" : "s"} without a verified GitHub sign-in (${listedMembers.join(", ")}${remainingMessage}). Ask them to sign in with GitHub, or remove them from the corresponding Infisical groups, then run the sync again.${completionMessage}`+ });+};+type TGithubOrgSyncServiceFactoryDep = {githubOrgSyncDAL: TGithubOrgSyncDALFactory;permissionService: Pick<TPermissionServiceFactory, "getOrgPermission">;kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;userGroupMembershipDAL: Pick<TUserGroupMembershipDALFactory,- "findGroupMembershipsByUserIdInOrg" | "findGroupMembershipsByGroupIdInOrg" | "insertMany" | "delete"+ "find" | "findGroupMembershipsByUserIdInOrg" | "findGroupMembershipsByGroupIdInOrg" | "insertMany" | "delete">;groupDAL: Pick<TGroupDALFactory, "insertMany" | "transaction" | "find">;membershipRoleDAL: Pick<TMembershipRoleDALFactory, "insertMany">;@@ -796,11 +822,11 @@ export const githubOrgSyncServiceFactory = ({: [];const matchGithubMember = buildGithubMemberMatcher(githubAliases, activeUserIds);const linkedActiveUserIds = new Set(githubAliases.map((alias) => alias.userId));+ const activeUsers = activeMembers.flatMap((member) => (member.user ? [member.user] : []));const startTime = Date.now();const syncErrors: string[] = [];const unmatchedGithubUsers = new Set<string>();- const preservedUnlinkedUserIds = new Set<string>();const octokit = new Octokit({ auth: orgAccessToken });@@ -856,10 +882,24 @@ export const githubOrgSyncServiceFactory = ({});const existingTeamsMap = groupBy(existingTeamsOnInfisical, (i) => i.name);+ if (existingTeamsOnInfisical.length) {+ const existingGroupMemberships = await userGroupMembershipDAL.find({+ $in: { groupId: existingTeamsOnInfisical.map((team) => team.id) }+ });+ assertGithubGroupMembersLinked({+ currentUserIds: new Set(+ existingGroupMemberships.map((membership) => membership.userId).filter((userId) => activeUserIds.has(userId))+ ),+ linkedUserIds: linkedActiveUserIds,+ activeUsers,+ noChangesApplied: true+ });+ }+const teamsToCreate = allGithubTeamNames.filter((teamName) => !(teamName in existingTeamsMap));const createdTeams = new Set<string>();const updatedTeams = new Set<string>();- const totalRemovedMemberships = 0;+ let totalRemovedMemberships = 0;if (teamsToCreate.length > 0) {await groupDAL.transaction(async (tx) => {@@ -918,7 +958,7 @@ export const githubOrgSyncServiceFactory = ({}});- await groupDAL.transaction(async (tx) => {+ const removedMembershipsForTeam = await groupDAL.transaction(async (tx) => {const currentMemberships = (await userGroupMembershipDAL.findGroupMembershipsByGroupIdInOrg(team.id,orgPermission.orgId,@@ -932,18 +972,17 @@ export const githubOrgSyncServiceFactory = ({currentUserIds.add(activeMember.user.id);}});+ assertGithubGroupMembersLinked({+ currentUserIds,+ linkedUserIds: linkedActiveUserIds,+ activeUsers+ });const usersToAdd = Array.from(expectedUserIds).filter((userId) => !currentUserIds.has(userId));- // A member without a verified GitHub alias has no trustworthy identity to reconcile yet.- // Keep their current access until a GitHub login creates that link.const membershipsToRemove = currentMemberships.filter((membership) => {const userId = activeMembersById.get(membership.orgMembershipId)?.user?.id;if (!userId) return false;- if (!linkedActiveUserIds.has(userId)) {- preservedUnlinkedUserIds.add(userId);- return false;- }return !expectedUserIds.has(userId);});@@ -975,7 +1014,10 @@ export const githubOrgSyncServiceFactory = ({updatedTeams.add(teamName);}++ return membershipsToRemove.length;});+ totalRemovedMemberships += removedMembershipsForTeam;}if (createdTeams.size || updatedTeams.size) {@@ -984,12 +1026,6 @@ export const githubOrgSyncServiceFactory = ({usageMeteringService.emit(orgPermission.orgId, PamIdentities.key);}- if (preservedUnlinkedUserIds.size) {- syncErrors.push(- `Infisical left existing group access unchanged for ${preservedUnlinkedUserIds.size} organization member${preservedUnlinkedUserIds.size === 1 ? "" : "s"} without a verified GitHub login. Ask them to sign in with GitHub, then run the sync again.`- );- }-if (unmatchedGithubUsers.size) {logger.info({ orgId: orgPermission.orgId, count: unmatchedGithubUsers.size },diff --git a/docs/documentation/platform/github-org-sync.mdx b/docs/documentation/platform/github-org-sync.mdxindex 86b4bbcf86..59b7abd520 100644--- a/docs/documentation/platform/github-org-sync.mdx+++ b/docs/documentation/platform/github-org-sync.mdx@@ -110,8 +110,13 @@ Infisical account. Manual sync uses that account ID to match each GitHub team memember.Infisical doesn't infer identity from a GitHub username or public profile email. If a member hasn't-signed in with GitHub, manual sync doesn't add group access and leaves any existing group access-unchanged. Ask the member to sign in with GitHub, then run **Sync Now** again.+signed in with GitHub, manual sync doesn't add group access. If that member already belongs to a+group that corresponds to a GitHub team, the sync stops before making changes and identifies the+members who need to sign in. Ask each listed member to sign in with GitHub, or remove them from the+corresponding Infisical groups, then run **Sync Now** again.++Before the first manual sync, ask each Infisical member who needs GitHub-controlled group access to+sign in with GitHub at least once.## Troubleshooting