Skip to content

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

fix PR comments

Matheus Nogueira1079dff0353f
diff --git a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-fns.ts b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-fns.ts
index 5c27702c09..493a1efa59 100644
--- a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-fns.ts
+++ b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-fns.ts
@@ -22,7 +22,7 @@ import { SecretMatch } from "@app/ee/services/secret-scanning/secret-scanning-qu
import { BITBUCKET_SECRET_SCANNING_DATA_SOURCE_LIST_OPTION } from "@app/ee/services/secret-scanning-v2/bitbucket";
import { GITHUB_SECRET_SCANNING_DATA_SOURCE_LIST_OPTION } from "@app/ee/services/secret-scanning-v2/github";
import { GITLAB_SECRET_SCANNING_DATA_SOURCE_LIST_OPTION } from "@app/ee/services/secret-scanning-v2/gitlab";
-import { getConfig } from "@app/lib/config/env";
+import { getConfig, SECRET_SCANNING_COMMIT_ENUMERATION_TIMEOUT } from "@app/lib/config/env";
import { crypto } from "@app/lib/crypto";
import { BadRequestError } from "@app/lib/errors";
import { titleCaseToCamelCase } from "@app/lib/fn";
@@ -165,10 +165,6 @@ export const cloneRepository = async ({ cloneUrl, repoPath }: TCloneRepository):
*/
const COMMIT_LIST_ARGS = ["rev-list", "--full-history", "--all"];
-// Enumeration is a traversal with no patch generation, so it is bounded separately from the scan
-// rather than spending any of the customer-tunable scan budget.
-const COMMIT_ENUMERATION_TIMEOUT = 5 * 60 * 1000;
-
export type TCommitBatch = {
/** Commits to skip in `git log`'s newest-first order before this batch begins. */
skip: number;
@@ -205,7 +201,7 @@ export const planCommitBatches = async ({
await execFileBounded("git", [...COMMIT_LIST_ARGS, "--reverse"], {
phase: SecretScanningExecPhase.Enumerate,
cwd: repoPath,
- timeoutMs: COMMIT_ENUMERATION_TIMEOUT,
+ timeoutMs: SECRET_SCANNING_COMMIT_ENUMERATION_TIMEOUT,
env: GIT_PROCESS_ENV,
onStdoutLine: (line) => {
const commit = line.trim();
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 d8d390ec78..ba81030685 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
@@ -103,7 +103,8 @@ export const secretScanningV2QueueServiceFactory = ({
*/
const acquireFullScanLease = async (resourceId: string, scanId: string) => {
const key = KeyStorePrefixes.SecretScanningFullScanLease(resourceId);
- const ttlSeconds = Math.ceil(getSecretScanningScanBudgetMs(getConfig()) / 1000);
+ const scanBudget = getSecretScanningScanBudgetMs(getConfig());
+ const ttlSeconds = Math.ceil(scanBudget / 1000);
const acquired = await keyStore.setItemWithExpiryNX(key, ttlSeconds, scanId);
@@ -266,8 +267,15 @@ export const secretScanningV2QueueServiceFactory = ({
return;
}
- await secretScanningV2DAL.scans.update(
- { id: scanId },
+ // Guarded so a retry cannot reopen a scan that already reached a terminal status. The work
+ // itself succeeded and only a side effect after it (a notification enqueue, an audit log)
+ // threw, so BullMQ hands the whole handler back; without this an already-Completed scan would
+ // be reset to `scanning` and, on the last attempt, closed out as Failed.
+ const startedScans = await secretScanningV2DAL.scans.update(
+ {
+ id: scanId,
+ $in: { status: [SecretScanningScanStatus.Queued, SecretScanningScanStatus.Scanning] }
+ },
{
status: SecretScanningScanStatus.Scanning,
scanningStartedAt: new Date(),
@@ -275,6 +283,13 @@ export const secretScanningV2QueueServiceFactory = ({
}
);
+ if (!startedScans.length) {
+ logger.warn(
+ `secretScanningV2Queue: Full Scan skipped, scan was already closed out ${logDetails} [scanType=${SecretScanningScanType.FullScan}]`
+ );
+ return;
+ }
+
let connection: TAppConnection | null = null;
if (dataSource.connection) connection = await decryptAppConnection(dataSource.connection, kmsService);
@@ -642,14 +657,26 @@ export const secretScanningV2QueueServiceFactory = ({
);
try {
- await secretScanningV2DAL.scans.update(
- { id: scanId },
+ // Same guard as the full scan: a retry after a post-completion side effect failed must not
+ // reopen a scan that already reached a terminal status.
+ const startedScans = await secretScanningV2DAL.scans.update(
+ {
+ id: scanId,
+ $in: { status: [SecretScanningScanStatus.Queued, SecretScanningScanStatus.Scanning] }
+ },
{
status: SecretScanningScanStatus.Scanning,
scanningStartedAt: new Date()
}
);
+ if (!startedScans.length) {
+ logger.warn(
+ `secretScanningV2Queue: Diff Scan skipped, scan was already closed out ${logDetails} [scanType=${SecretScanningScanType.DiffScan}]`
+ );
+ return;
+ }
+
let connection: TAppConnection | null = null;
if (dataSource.connection) connection = await decryptAppConnection(dataSource.connection, kmsService);
diff --git a/backend/src/lib/config/env.ts b/backend/src/lib/config/env.ts
index 81ba11db76..af9f9926e2 100644
--- a/backend/src/lib/config/env.ts
+++ b/backend/src/lib/config/env.ts
@@ -66,14 +66,29 @@ export const runModesSchema = zpStr(z.string().optional())
export const SECRET_SCANNING_SCAN_OVERHEAD = ms("5m");
/**
- * Clone plus scan plus that overhead: the longest a healthy scan can go without making externally
- * visible progress. The stuck-scan validation and the full-scan lease TTL both key off it, so they
- * cannot drift apart.
+ * Ceiling for the `git rev-list` pass that plans a full scan's commit batches. Enumeration is a
+ * traversal with no patch generation, so it is bounded separately from the scan rather than
+ * spending any of the customer-tunable scan budget — but it runs between the clone and the first
+ * progress update, so the stuck-scan budget has to carry it.
+ */
+export const SECRET_SCANNING_COMMIT_ENUMERATION_TIMEOUT = ms("5m");
+
+/**
+ * Everything a scan can spend between two progress updates that is not the customer's clone and
+ * scan timeouts: enumerating the commits to batch, and the measurement and bookkeeping around them.
+ */
+export const SECRET_SCANNING_FIXED_SCAN_HEADROOM =
+ SECRET_SCANNING_COMMIT_ENUMERATION_TIMEOUT + SECRET_SCANNING_SCAN_OVERHEAD;
+
+/**
+ * The longest a healthy scan can go without recording progress: the clone, the commit enumeration
+ * and measurement that follow it, one `infisical scan` invocation, and the bookkeeping around them.
+ * The stuck-scan validation and the full-scan lease TTL both key off it, so they cannot drift apart.
*/
export const getSecretScanningScanBudgetMs = (timeouts: {
SECRET_SCANNING_CLONE_TIMEOUT: number;
SECRET_SCANNING_SCAN_TIMEOUT: number;
-}) => timeouts.SECRET_SCANNING_CLONE_TIMEOUT + timeouts.SECRET_SCANNING_SCAN_TIMEOUT + SECRET_SCANNING_SCAN_OVERHEAD;
+}) => timeouts.SECRET_SCANNING_CLONE_TIMEOUT + timeouts.SECRET_SCANNING_SCAN_TIMEOUT + SECRET_SCANNING_FIXED_SCAN_HEADROOM;
const zodTimeoutMs = ({
envVar,
@@ -150,7 +165,7 @@ export const validateSecretScanningTimeouts = (
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ["SECRET_SCANNING_STUCK_SCAN_TIMEOUT"],
- message: `SECRET_SCANNING_STUCK_SCAN_TIMEOUT (${data.SECRET_SCANNING_STUCK_SCAN_TIMEOUT}ms) must exceed SECRET_SCANNING_CLONE_TIMEOUT + SECRET_SCANNING_SCAN_TIMEOUT plus ${SECRET_SCANNING_SCAN_OVERHEAD}ms of measurement and bookkeeping (${scanBudgetMs}ms), otherwise healthy in-flight scans are reaped as stuck.`
+ message: `SECRET_SCANNING_STUCK_SCAN_TIMEOUT (${data.SECRET_SCANNING_STUCK_SCAN_TIMEOUT}ms) must exceed SECRET_SCANNING_CLONE_TIMEOUT + SECRET_SCANNING_SCAN_TIMEOUT plus ${SECRET_SCANNING_FIXED_SCAN_HEADROOM}ms of commit enumeration, measurement and bookkeeping (${scanBudgetMs}ms), otherwise healthy in-flight scans are reaped as stuck.`
});
}
};
diff --git a/backend/src/lib/config/secret-scanning-timeouts.test.ts b/backend/src/lib/config/secret-scanning-timeouts.test.ts
index 539cf760a3..c1a139aec4 100644
--- a/backend/src/lib/config/secret-scanning-timeouts.test.ts
+++ b/backend/src/lib/config/secret-scanning-timeouts.test.ts
@@ -106,7 +106,7 @@ describe("secret scanning timeouts", () => {
});
expect(issuesFor(result, "SECRET_SCANNING_STUCK_SCAN_TIMEOUT")).toEqual([
- "SECRET_SCANNING_STUCK_SCAN_TIMEOUT (1200000ms) must exceed SECRET_SCANNING_CLONE_TIMEOUT + SECRET_SCANNING_SCAN_TIMEOUT plus 300000ms of measurement and bookkeeping (1500000ms), otherwise healthy in-flight scans are reaped as stuck."
+ "SECRET_SCANNING_STUCK_SCAN_TIMEOUT (1200000ms) must exceed SECRET_SCANNING_CLONE_TIMEOUT + SECRET_SCANNING_SCAN_TIMEOUT plus 600000ms of commit enumeration, measurement and bookkeeping (1800000ms), otherwise healthy in-flight scans are reaped as stuck."
]);
});
@@ -114,7 +114,7 @@ describe("secret scanning timeouts", () => {
const result = parse({
SECRET_SCANNING_CLONE_TIMEOUT: "1m",
SECRET_SCANNING_SCAN_TIMEOUT: "1m",
- SECRET_SCANNING_STUCK_SCAN_TIMEOUT: "7m"
+ SECRET_SCANNING_STUCK_SCAN_TIMEOUT: "12m"
});
expect(result.success).toBe(false);
@@ -124,7 +124,7 @@ describe("secret scanning timeouts", () => {
const result = parse({
SECRET_SCANNING_CLONE_TIMEOUT: "1m",
SECRET_SCANNING_SCAN_TIMEOUT: "1m",
- SECRET_SCANNING_STUCK_SCAN_TIMEOUT: "420001"
+ SECRET_SCANNING_STUCK_SCAN_TIMEOUT: "720001"
});
expect(result.success).toBe(true);
diff --git a/docs/self-hosting/configuration/envars.mdx b/docs/self-hosting/configuration/envars.mdx
index 2a970d4f1d..287c1f9fe2 100644
--- a/docs/self-hosting/configuration/envars.mdx
+++ b/docs/self-hosting/configuration/envars.mdx
@@ -1065,8 +1065,8 @@ You can configure third-party app connections for re-use across Infisical Projec
<ParamField query="SECRET_SCANNING_STUCK_SCAN_TIMEOUT" type="string" default="1h" optional>
How long a scan may stay in progress before it is assumed dead, meaning its worker was killed,
and marked failed. Takes a duration string such as `90s`, `10m`, or `2h`. Defaults to `1h`. Must
- exceed the clone and scan timeouts plus the time a scan spends measuring the repository and
- writing its results; the server refuses to start otherwise.
+ exceed the clone and scan timeouts plus the time a scan spends measuring the repository,
+ enumerating its commits and writing its results; the server refuses to start otherwise.
</ParamField>
</Accordion>