Skip to content

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

prevent race condition triggering 2 full scans

Matheus Nogueira32f11219847a
diff --git a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-dal.ts b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-dal.ts
index 1e60c6b474..bc8c761d7f 100644
--- a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-dal.ts
+++ b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-dal.ts
@@ -487,6 +487,16 @@ export const secretScanningV2DALFactory = (db: TDbClient) => {
}
};
+ // Locks the data source's resources until the transaction ends, so concurrent scan requests check for in-flight
+ // scans one at a time. Ordered so two lockers take the rows in the same order and can't deadlock.
+ const findResourcesByDataSourceIdForUpdate = async (dataSourceId: string, tx: Knex) => {
+ try {
+ return await tx(TableName.SecretScanningResource).where({ dataSourceId }).orderBy("id").forUpdate();
+ } catch (error) {
+ throw new DatabaseError({ error, name: "Find For Update - Secret Scanning Resource" });
+ }
+ };
+
// Reads the primary: the caller writes these rows straight after, and a replica that still shows
// a just-completed scan as in-progress would have it reaped.
const findStuckScans = async (startedBefore: Date, limit: number, tx?: Knex) => {
@@ -708,7 +718,8 @@ export const secretScanningV2DALFactory = (db: TDbClient) => {
},
resources: {
...resourceOrm,
- findWithDetails: findResourcesWithDetails
+ findWithDetails: findResourcesWithDetails,
+ findByDataSourceIdForUpdate: findResourcesByDataSourceIdForUpdate
},
scans: {
...scanOrm,
diff --git a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-queue.ts b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-queue.ts
index 7ba2ebde95..2c3c255a7e 100644
--- a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-queue.ts
+++ b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-queue.ts
@@ -194,9 +194,9 @@ export const secretScanningV2QueueServiceFactory = ({
const rawResources = await factory.listRawResources(dataSource);
await secretScanningV2DAL.resources.transaction(async (tx) => {
- // Read through the transaction so it hits the primary: a resource added just before this call may not have
- // reached the replica yet, and would be skipped.
- const existingResources = await secretScanningV2DAL.resources.find({ dataSourceId: dataSource.id }, { tx });
+ // Read on the primary, since a resource added just before this call may not have reached the replica, and
+ // locked, so two requests can't both see no in-flight scan and queue the same resource twice.
+ const existingResources = await secretScanningV2DAL.resources.findByDataSourceIdForUpdate(dataSource.id, tx);
const existingByExternalId = new Map(existingResources.map((resource) => [resource.externalId, resource]));
let filteredRawResources = rawResources.filter((resource) => existingByExternalId.has(resource.externalId));