Skip to content

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

misc: soft delete shared secrets

Sheen Capadngan8e753eda7215
diff --git a/backend/src/services/secret-sharing/secret-sharing-dal.ts b/backend/src/services/secret-sharing/secret-sharing-dal.ts
index 6b5090d664..16b66d871a 100644
--- a/backend/src/services/secret-sharing/secret-sharing-dal.ts
+++ b/backend/src/services/secret-sharing/secret-sharing-dal.ts
@@ -1,9 +1,9 @@
import { Knex } from "knex";
import { TDbClient } from "@app/db";
-import { TableName } from "@app/db/schemas";
+import { TableName, TSecretSharing } from "@app/db/schemas";
import { DatabaseError } from "@app/lib/errors";
-import { ormify } from "@app/lib/knex";
+import { ormify, selectAllTableCols } from "@app/lib/knex";
export type TSecretSharingDALFactory = ReturnType<typeof secretSharingDALFactory>;
@@ -13,15 +13,58 @@ export const secretSharingDALFactory = (db: TDbClient) => {
const pruneExpiredSharedSecrets = async (tx?: Knex) => {
try {
const today = new Date();
- const docs = await (tx || db)(TableName.SecretSharing).where("expiresAt", "<", today).del();
+ const docs = await (tx || db)(TableName.SecretSharing)
+ .where("expiresAt", "<", today)
+ .andWhere("encryptedValue", "<>", "")
+ .update({
+ encryptedValue: "",
+ tag: "",
+ iv: "",
+ hashedHex: ""
+ });
return docs;
} catch (error) {
throw new DatabaseError({ error, name: "pruneExpiredSharedSecrets" });
}
};
+ const findActiveSharedSecrets = async (filters: Partial<TSecretSharing>, tx?: Knex) => {
+ try {
+ const now = new Date();
+ return await (tx || db)(TableName.SecretSharing)
+ .where(filters)
+ .andWhere("expiresAt", ">", now)
+ .andWhere("encryptedValue", "<>", "")
+ .select(selectAllTableCols(TableName.SecretSharing))
+ .orderBy("expiresAt", "asc");
+ } catch (error) {
+ throw new DatabaseError({
+ error,
+ name: "Find Active Shared Secrets"
+ });
+ }
+ };
+
+ const softDeleteById = async (id: string) => {
+ try {
+ await sharedSecretOrm.updateById(id, {
+ encryptedValue: "",
+ iv: "",
+ tag: "",
+ hashedHex: ""
+ });
+ } catch (error) {
+ throw new DatabaseError({
+ error,
+ name: "Soft Delete Shared Secret"
+ });
+ }
+ };
+
return {
...sharedSecretOrm,
- pruneExpiredSharedSecrets
+ pruneExpiredSharedSecrets,
+ softDeleteById,
+ findActiveSharedSecrets
};
};
diff --git a/backend/src/services/secret-sharing/secret-sharing-service.ts b/backend/src/services/secret-sharing/secret-sharing-service.ts
index 012b0f1302..e40b4ef265 100644
--- a/backend/src/services/secret-sharing/secret-sharing-service.ts
+++ b/backend/src/services/secret-sharing/secret-sharing-service.ts
@@ -101,7 +101,7 @@ export const secretSharingServiceFactory = ({
const { actor, actorId, orgId, actorAuthMethod, actorOrgId } = getSharedSecretsInput;
const { permission } = await permissionService.getOrgPermission(actor, actorId, orgId, actorAuthMethod, actorOrgId);
if (!permission) throw new UnauthorizedError({ name: "User not in org" });
- const userSharedSecrets = await secretSharingDAL.find({ userId: actorId, orgId }, { sort: [["expiresAt", "asc"]] });
+ const userSharedSecrets = await secretSharingDAL.findActiveSharedSecrets({ userId: actorId, orgId });
return userSharedSecrets;
};
@@ -113,7 +113,7 @@ export const secretSharingServiceFactory = ({
}
if (sharedSecret.expiresAfterViews != null && sharedSecret.expiresAfterViews >= 0) {
if (sharedSecret.expiresAfterViews === 0) {
- await secretSharingDAL.deleteById(sharedSecretId);
+ await secretSharingDAL.softDeleteById(sharedSecretId);
return;
}
await secretSharingDAL.updateById(sharedSecretId, { $decr: { expiresAfterViews: 1 } });