Skip to content

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

refac validation and add save information about external emails into the database

adilson5d3d033367ca
diff --git a/backend/src/db/migrations/20260324200001_secret-sharing-allow-external-emails.ts b/backend/src/db/migrations/20260324200001_secret-sharing-allow-external-emails.ts
new file mode 100644
index 0000000000..d7761a395a
--- /dev/null
+++ b/backend/src/db/migrations/20260324200001_secret-sharing-allow-external-emails.ts
@@ -0,0 +1,21 @@
+import { Knex } from "knex";
+
+import { TableName } from "../schemas";
+
+export async function up(knex: Knex): Promise<void> {
+ const hasColumn = await knex.schema.hasColumn(TableName.SecretSharing, "allowExternalEmails");
+ if (!hasColumn) {
+ await knex.schema.alterTable(TableName.SecretSharing, (t) => {
+ t.boolean("allowExternalEmails").nullable().defaultTo(false);
+ });
+ }
+}
+
+export async function down(knex: Knex): Promise<void> {
+ const hasColumn = await knex.schema.hasColumn(TableName.SecretSharing, "allowExternalEmails");
+ if (hasColumn) {
+ await knex.schema.alterTable(TableName.SecretSharing, (t) => {
+ t.dropColumn("allowExternalEmails");
+ });
+ }
+}
diff --git a/backend/src/db/schemas/secret-sharing.ts b/backend/src/db/schemas/secret-sharing.ts
index 2f197230af..bce2001db9 100644
--- a/backend/src/db/schemas/secret-sharing.ts
+++ b/backend/src/db/schemas/secret-sharing.ts
@@ -30,6 +30,7 @@ export const SecretSharingSchema = z.object({
type: z.string().default("share"),
authorizedEmails: z.unknown().nullable().optional(),
externalEmails: z.unknown().nullable().optional(),
+ allowExternalEmails: z.boolean().nullable().optional().default(false),
identityId: z.string().uuid().nullable().optional()
});
diff --git a/backend/src/services/secret-sharing/secret-sharing-service.ts b/backend/src/services/secret-sharing/secret-sharing-service.ts
index 2ca22f76c6..e1846eb68f 100644
--- a/backend/src/services/secret-sharing/secret-sharing-service.ts
+++ b/backend/src/services/secret-sharing/secret-sharing-service.ts
@@ -211,7 +211,8 @@ export const secretSharingServiceFactory = ({
orgId,
accessType,
authorizedEmails: authorizedEmails.length > 0 ? JSON.stringify(authorizedEmails) : undefined,
- externalEmails: externalEmails.length > 0 ? JSON.stringify(externalEmails) : undefined
+ externalEmails: externalEmails.length > 0 ? JSON.stringify(externalEmails) : undefined,
+ allowExternalEmails: Boolean(allowExternalEmails)
});
const mappedSharedSecret = mapIdentifierToId(newSharedSecret);
@@ -559,27 +560,35 @@ export const secretSharingServiceFactory = ({
};
};
- // Validates that the current user is authorized to view a secret shared to specific emails.
- const $validateAuthorizedEmails = async (sharedSecret: TSecretSharing, actorId?: string) => {
+ // Validates that an authenticated org member is authorized to view a secret shared to specific emails.
+ const $validateAuthorizedEmailAccess = async (sharedSecret: TSecretSharing, actorId?: string) => {
const hasAuthorizedEmails = sharedSecret.authorizedEmails && (sharedSecret.authorizedEmails as string[]).length > 0;
+
+ if (!hasAuthorizedEmails) return;
+
+ if (!actorId) {
+ throw new UnauthorizedError({ message: "Authentication required to view this secret" });
+ }
+
+ const user = await userDAL.findById(actorId);
+ if (!user || !user.email) {
+ throw new UnauthorizedError({ message: "Authentication required to view this secret" });
+ }
+
+ if (!(sharedSecret.authorizedEmails as string[]).includes(user.email)) {
+ throw new ForbiddenRequestError({ message: "You are not authorized to view this secret" });
+ }
+ };
+
+ // Validates that a secret is properly configured for external (non-org) email access.
+ // External users authenticate via password — actual password verification is handled by the caller.
+ const $validateExternalEmailAccess = (sharedSecret: TSecretSharing) => {
const hasExternalEmails = sharedSecret.externalEmails && (sharedSecret.externalEmails as string[]).length > 0;
- if (hasAuthorizedEmails || hasExternalEmails) {
- if (actorId) {
- // Authenticated user — verify email is in authorized list
- const user = await userDAL.findById(actorId);
- if (!user || !user.email) {
- throw new UnauthorizedError({ message: "Authentication required to view this secret" });
- }
+ if (!hasExternalEmails || !sharedSecret.allowExternalEmails) return;
- if (hasAuthorizedEmails && !(sharedSecret.authorizedEmails as string[]).includes(user.email)) {
- throw new ForbiddenRequestError({ message: "You are not authorized to view this secret" });
- }
- } else if (!sharedSecret.password) {
- // Unauthenticated and no password — must log in
- throw new UnauthorizedError({ message: "Authentication required to view this secret" });
- }
- // Unauthenticated + password-protected: fall through to password check
+ if (!sharedSecret.password) {
+ throw new BadRequestError({ message: "A password is required for secrets shared with external emails" });
}
};
@@ -617,7 +626,8 @@ export const secretSharingServiceFactory = ({
throw new NotFoundError({ message: "The shared secret has reached its view limit" });
}
- await $validateAuthorizedEmails(sharedSecret, actorId);
+ await $validateAuthorizedEmailAccess(sharedSecret, actorId);
+ $validateExternalEmailAccess(sharedSecret);
return {
...mapIdentifierToId(sharedSecret),
@@ -664,7 +674,8 @@ export const secretSharingServiceFactory = ({
throw new ForbiddenRequestError();
}
- await $validateAuthorizedEmails(sharedSecret, actorId);
+ await $validateAuthorizedEmailAccess(sharedSecret, actorId);
+ $validateExternalEmailAccess(sharedSecret);
// all secrets pass through here, meaning we check if its expired first and then check if it needs verification
// or can be safely sent to the client.