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
add audit log to domain verification errors
Thiago Araujo da Silva0ff2744a045a
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.mdindex cf1fb8e1b0..d03ec1116a 100644--- a/backend/CLAUDE.md+++ b/backend/CLAUDE.md@@ -438,7 +438,7 @@ catch, and reading that as a rename would let a stale alias rewrite somebody elsconsequence to know: a legacy unverified alias whose email changed before its next login is stale,so it gets the email-verification fallback (a code sent to the dead mailbox) rather than a sync.-Two invariants:+Three invariants:- **It never fails a login.** A rename that could not be applied leaves stale data, which is what wealready had; a throw locks the person out of an org that has no other way in. Every failure path@@ -449,12 +449,15 @@ Two invariants:its aliases and rewrite the identity every other org of that user sees. Every login pathestablishes both already (`verifyEmailDomainOwnership` is a positive check: the domain must beverified *for this org*, and the alias branch runs it against the existing account's username),- so the helper's copy is there to keep the rename from outliving those checks.+ so the helper's copy is there to keep the rename from outliving those checks. It is unreachable+ today, and audits the skip anyway (`SSO_USER_EMAIL_SYNC_SKIPPED` with+ `reason: "domain-not-owned"`) so a refactor that removes a caller's check leaves a trail rather+ than a silently stale account.- **It never renames onto an occupied address.** `users.username` is globally unique and the row isglobal rather than org-scoped, so the asserted address may already be a personal signup or an- unmerged duplicate. The conflict is recorded (`SSO_USER_PROFILE_SYNC_CONFLICT`) and the email is- left alone; the name still syncs. The preceding read is not a lock, so a unique violation lands on- the same path.+ unmerged duplicate. The conflict is recorded (`SSO_USER_EMAIL_SYNC_SKIPPED` with+ `reason: "address-taken"`) and the email is left alone; the name still syncs. The preceding read+ is not a lock, so a unique violation lands on the same path.SCIM is the other half of the same story and moves with it. `updateScimUser` / `replaceScimUser`rejected every email change outright, which left the data stale *and* put the provisioning job in adiff --git a/backend/src/ee/services/audit-log/audit-log-types.ts b/backend/src/ee/services/audit-log/audit-log-types.tsindex 01ab022621..cf8ba50ce1 100644--- a/backend/src/ee/services/audit-log/audit-log-types.ts+++ b/backend/src/ee/services/audit-log/audit-log-types.ts@@ -592,7 +592,7 @@ export enum EventType {OIDC_GROUP_MEMBERSHIP_MAPPING_REMOVE_USER = "oidc-group-membership-mapping-remove-user",OIDC_PROVISIONED_PLACEHOLDER_ADOPTED = "oidc-provisioned-placeholder-adopted",SSO_USER_PROFILE_SYNCED = "sso-user-profile-synced",- SSO_USER_PROFILE_SYNC_CONFLICT = "sso-user-profile-sync-conflict",+ SSO_USER_EMAIL_SYNC_SKIPPED = "sso-user-email-sync-skipped",CREATE_KMIP_CLIENT = "create-kmip-client",UPDATE_KMIP_CLIENT = "update-kmip-client",DELETE_KMIP_CLIENT = "delete-kmip-client",@@ -4970,14 +4970,17 @@ interface SsoUserProfileSyncedEvent {};}-interface SsoUserProfileSyncConflictEvent {- type: EventType.SSO_USER_PROFILE_SYNC_CONFLICT;+export type TSsoUserEmailSyncSkipReason = "address-taken" | "domain-not-owned";++interface SsoUserEmailSyncSkippedEvent {+ type: EventType.SSO_USER_EMAIL_SYNC_SKIPPED;metadata: {userId: string;aliasType: string;externalId: string;currentEmail: string;assertedEmail: string;+ reason: TSsoUserEmailSyncSkipReason;conflictingUserId?: string;};}@@ -7660,7 +7663,7 @@ export type Event =| OidcGroupMembershipMappingRemoveUserEvent| OidcProvisionedPlaceholderAdoptedEvent| SsoUserProfileSyncedEvent- | SsoUserProfileSyncConflictEvent+ | SsoUserEmailSyncSkippedEvent| CreateKmipClientEvent| UpdateKmipClientEvent| DeleteKmipClientEventdiff --git a/backend/src/services/user-alias/user-alias-fns.test.ts b/backend/src/services/user-alias/user-alias-fns.test.tsindex 07bd250123..f7027dcfdb 100644--- a/backend/src/services/user-alias/user-alias-fns.test.ts+++ b/backend/src/services/user-alias/user-alias-fns.test.ts@@ -443,7 +443,8 @@ describe("syncSsoUserProfile", () => {expect(deps.updatedRows[0]).toEqual({ firstName: "Bob" });expect(deps.userDAL.findOne).toHaveBeenCalledWith({ username: "[email protected]" });const [audited] = deps.auditLogService.createAuditLog.mock.calls[0];- expect(audited.event.type).toBe("sso-user-profile-sync-conflict");+ expect(audited.event.type).toBe("sso-user-email-sync-skipped");+ expect(audited.event.metadata.reason).toBe("address-taken");expect(audited.event.metadata.conflictingUserId).toBe("user-2");});@@ -463,7 +464,8 @@ describe("syncSsoUserProfile", () => {expect(user.username).toBe("[email protected]");const [audited] = deps.auditLogService.createAuditLog.mock.calls[0];- expect(audited.event.type).toBe("sso-user-profile-sync-conflict");+ expect(audited.event.type).toBe("sso-user-email-sync-skipped");+ expect(audited.event.metadata.reason).toBe("address-taken");});test("keeps the name when the address is taken between the check and the write", async () => {@@ -477,7 +479,8 @@ describe("syncSsoUserProfile", () => {expect(deps.updatedRows).toEqual([{ firstName: "Bob" }]);const [conflict] = deps.auditLogService.createAuditLog.mock.calls[0];- expect(conflict.event.type).toBe("sso-user-profile-sync-conflict");+ expect(conflict.event.type).toBe("sso-user-email-sync-skipped");+ expect(conflict.event.metadata.reason).toBe("address-taken");const [synced] = deps.auditLogService.createAuditLog.mock.calls[1];expect(synced.event.type).toBe("sso-user-profile-synced");expect(synced.event.metadata.newEmail).toBeUndefined();@@ -499,6 +502,11 @@ describe("syncSsoUserProfile", () => {expect(user.username).toBe("[email protected]");expect(user.firstName).toBe("Bob");expect(deps.updatedRows[0]).toEqual({ firstName: "Bob" });++ const [skipped] = deps.auditLogService.createAuditLog.mock.calls[0];+ expect(skipped.event.type).toBe("sso-user-email-sync-skipped");+ expect(skipped.event.metadata.reason).toBe("domain-not-owned");+ expect(skipped.event.metadata.conflictingUserId).toBeUndefined();});test("skips the email when the org does not own the address being written", async () => {@@ -508,5 +516,9 @@ describe("syncSsoUserProfile", () => {expect(user.username).toBe("[email protected]");expect(deps.userDAL.updateById).not.toHaveBeenCalled();expect(deps.userDAL.findOne).not.toHaveBeenCalled();++ const [skipped] = deps.auditLogService.createAuditLog.mock.calls[0];+ expect(skipped.event.type).toBe("sso-user-email-sync-skipped");+ expect(skipped.event.metadata.reason).toBe("domain-not-owned");});});diff --git a/backend/src/services/user-alias/user-alias-fns.ts b/backend/src/services/user-alias/user-alias-fns.tsindex 056118c191..b31cd4b5a6 100644--- a/backend/src/services/user-alias/user-alias-fns.ts+++ b/backend/src/services/user-alias/user-alias-fns.ts@@ -1,7 +1,11 @@import { Knex } from "knex";import { AccessScope, TableName, TUserAliases, TUsers } from "@app/db/schemas";-import { EventType, TAuditLogServiceFactory } from "@app/ee/services/audit-log/audit-log-types";+import {+ EventType,+ TAuditLogServiceFactory,+ TSsoUserEmailSyncSkipReason+} from "@app/ee/services/audit-log/audit-log-types";import { TEmailDomainDALFactory } from "@app/ee/services/email-domain/email-domain-dal";import { verifyEmailDomainOwnership } from "@app/ee/services/email-domain/email-domain-fns";import { DatabaseErrorCode } from "@app/lib/error-codes";@@ -158,29 +162,40 @@ export const syncSsoUserProfile = async ({if (!isEmailChanged && !isNameChanged) return user;- const logConflict = async (conflictingUserId?: string) => {+ const logSkippedEmailSync = async ({+ reason,+ conflictingUserId+ }: {+ reason: TSsoUserEmailSyncSkipReason;+ conflictingUserId?: string;+ }) => {+ const detail =+ reason === "domain-not-owned"+ ? "the organization does not own both addresses"+ : "the asserted address belongs to another account";logger.warn({ userId: user.id, orgId, externalId: userAlias.externalId, assertedEmail: assertedUsername, conflictingUserId },- `Skipped SSO email sync, the asserted address belongs to another account [userId=${user.id}] [orgId=${orgId}]`+ `Skipped SSO email sync, ${detail} [userId=${user.id}] [orgId=${orgId}]`);await auditLogService.createAuditLog({actor: { type: ActorType.PLATFORM, metadata: {} },orgId,event: {- type: EventType.SSO_USER_PROFILE_SYNC_CONFLICT,+ type: EventType.SSO_USER_EMAIL_SYNC_SKIPPED,metadata: {userId: user.id,aliasType: userAlias.aliasType,externalId: userAlias.externalId,currentEmail: user.username,assertedEmail: assertedUsername,+ reason,conflictingUserId}}}).catch((err) => {- logger.error(err, `Failed to audit SSO profile sync conflict for user ${user.id} in org ${orgId}`);+ logger.error(err, `Failed to audit the skipped SSO email sync for user ${user.id} in org ${orgId}`);});};@@ -195,10 +210,7 @@ export const syncSsoUserProfile = async ({if (!isOrgOwnedRename) {isEmailApplied = false;- logger.warn(- { userId: user.id, orgId, externalId: userAlias.externalId },- `Skipped SSO email sync, the organization does not own both addresses [userId=${user.id}] [orgId=${orgId}]`- );+ await logSkippedEmailSync({ reason: "domain-not-owned" });if (!isNameChanged) return user;}}@@ -207,7 +219,7 @@ export const syncSsoUserProfile = async ({const conflictingUser = await userDAL.findOne({ username: assertedUsername });if (conflictingUser && conflictingUser.id !== user.id) {isEmailApplied = false;- await logConflict(conflictingUser.id);+ await logSkippedEmailSync({ reason: "address-taken", conflictingUserId: conflictingUser.id });if (!isNameChanged) return user;}}@@ -246,7 +258,7 @@ export const syncSsoUserProfile = async ({return user;}- await logConflict();+ await logSkippedEmailSync({ reason: "address-taken" });isEmailApplied = false;if (!isNameChanged) return user;diff --git a/frontend/src/hooks/api/auditLogs/constants.tsx b/frontend/src/hooks/api/auditLogs/constants.tsxindex d29a7d0fc4..b6f735eefc 100644--- a/frontend/src/hooks/api/auditLogs/constants.tsx+++ b/frontend/src/hooks/api/auditLogs/constants.tsx@@ -200,8 +200,7 @@ export const eventToNameMap: { [K in EventType]: string } = {[EventType.OIDC_PROVISIONED_PLACEHOLDER_ADOPTED]:"OIDC login adopted provisioned placeholder account",[EventType.SSO_USER_PROFILE_SYNCED]: "Synced user profile from identity provider",- [EventType.SSO_USER_PROFILE_SYNC_CONFLICT]:- "Skipped user email sync from identity provider due to conflict",+ [EventType.SSO_USER_EMAIL_SYNC_SKIPPED]: "Skipped user email sync from identity provider",[EventType.SECRET_APPROVAL_REQUEST_REVIEW]: "Review Secret Approval Request",[EventType.CREATE_KMIP_CLIENT]: "Create KMIP client",[EventType.UPDATE_KMIP_CLIENT]: "Update KMIP client",diff --git a/frontend/src/hooks/api/auditLogs/enums.tsx b/frontend/src/hooks/api/auditLogs/enums.tsxindex 93bdf31aa1..472f193c7f 100644--- a/frontend/src/hooks/api/auditLogs/enums.tsx+++ b/frontend/src/hooks/api/auditLogs/enums.tsx@@ -207,7 +207,7 @@ export enum EventType {OIDC_GROUP_MEMBERSHIP_MAPPING_REMOVE_USER = "oidc-group-membership-mapping-remove-user",OIDC_PROVISIONED_PLACEHOLDER_ADOPTED = "oidc-provisioned-placeholder-adopted",SSO_USER_PROFILE_SYNCED = "sso-user-profile-synced",- SSO_USER_PROFILE_SYNC_CONFLICT = "sso-user-profile-sync-conflict",+ SSO_USER_EMAIL_SYNC_SKIPPED = "sso-user-email-sync-skipped",CREATE_KMIP_CLIENT = "create-kmip-client",UPDATE_KMIP_CLIENT = "update-kmip-client",DELETE_KMIP_CLIENT = "delete-kmip-client",