Skip to content

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

add unit tests

Matheus Nogueira7141e284b814
diff --git a/backend/src/lib/config/env.ts b/backend/src/lib/config/env.ts
index eb9f1337e7..08b44ccbcc 100644
--- a/backend/src/lib/config/env.ts
+++ b/backend/src/lib/config/env.ts
@@ -102,6 +102,40 @@ const zodTimeoutMs = ({
})
);
+export const secretScanningTimeoutsSchema = z.object({
+ SECRET_SCANNING_SCAN_TIMEOUT: zodTimeoutMs({
+ description: "Wall-clock ceiling for a single `infisical scan` invocation before its process group is killed",
+ defaultValue: "10m",
+ legacyMsEnvVar: "SECRET_SCANNING_SCAN_TIMEOUT_MS"
+ }),
+ SECRET_SCANNING_CLONE_TIMEOUT: zodTimeoutMs({
+ description: "Wall-clock ceiling for a single `git clone` invocation before its process group is killed",
+ defaultValue: "10m",
+ legacyMsEnvVar: "SECRET_SCANNING_CLONE_TIMEOUT_MS"
+ }),
+ SECRET_SCANNING_STUCK_SCAN_TIMEOUT: zodTimeoutMs({
+ description:
+ "A scan left in the `scanning` state for longer than this is marked failed by the reaper. Must exceed clone + scan timeouts combined.",
+ defaultValue: "1h",
+ legacyMsEnvVar: "SECRET_SCANNING_STUCK_SCAN_TIMEOUT_MS"
+ })
+});
+
+export const validateSecretScanningTimeouts = (
+ data: z.infer<typeof secretScanningTimeoutsSchema>,
+ ctx: z.RefinementCtx
+) => {
+ const scanBudgetMs =
+ data.SECRET_SCANNING_CLONE_TIMEOUT + data.SECRET_SCANNING_SCAN_TIMEOUT + SECRET_SCANNING_SCAN_OVERHEAD;
+ if (data.SECRET_SCANNING_STUCK_SCAN_TIMEOUT <= scanBudgetMs) {
+ 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.`
+ });
+ }
+};
+
const databaseReadReplicaSchema = z
.object({
DB_CONNECTION_URI: z.string().describe("Postgres read replica database connection string"),
@@ -408,16 +442,7 @@ const envSchema = z
SECRET_SCANNING_PRIVATE_KEY: zpStr(z.string().optional()),
SECRET_SCANNING_ORG_WHITELIST: zpStr(z.string().optional()),
SECRET_SCANNING_GIT_APP_SLUG: zpStr(z.string().default("infisical-radar")),
- SECRET_SCANNING_SCAN_TIMEOUT: zodTimeoutMs({
- description: "Wall-clock ceiling for a single `infisical scan` invocation before its process group is killed",
- defaultValue: "10m",
- legacyMsEnvVar: "SECRET_SCANNING_SCAN_TIMEOUT_MS"
- }),
- SECRET_SCANNING_CLONE_TIMEOUT: zodTimeoutMs({
- description: "Wall-clock ceiling for a single `git clone` invocation before its process group is killed",
- defaultValue: "10m",
- legacyMsEnvVar: "SECRET_SCANNING_CLONE_TIMEOUT_MS"
- }),
+ ...secretScanningTimeoutsSchema.shape,
SECRET_SCANNING_MEMORY_LIMIT_MB: z.coerce
.number()
.int()
@@ -440,12 +465,6 @@ const envSchema = z
.min(0)
.default(5120)
.describe("Repositories larger than this are rejected before/after cloning. Set to 0 to disable."),
- SECRET_SCANNING_STUCK_SCAN_TIMEOUT: zodTimeoutMs({
- description:
- "A scan left in the `scanning` state for longer than this is marked failed by the reaper. Must exceed clone + scan timeouts combined.",
- defaultValue: "1h",
- legacyMsEnvVar: "SECRET_SCANNING_STUCK_SCAN_TIMEOUT_MS"
- }),
// LICENSE
// The License Server host. Serves both the self-hosted token endpoint and the entitlement API.
LICENSE_SERVER_URL: zpStr(z.string().optional().default("https://portal.infisical.com")),
@@ -666,15 +685,7 @@ const envSchema = z
}
});
- const scanBudgetMs =
- data.SECRET_SCANNING_CLONE_TIMEOUT + data.SECRET_SCANNING_SCAN_TIMEOUT + SECRET_SCANNING_SCAN_OVERHEAD;
- if (data.SECRET_SCANNING_STUCK_SCAN_TIMEOUT <= scanBudgetMs) {
- 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.`
- });
- }
+ validateSecretScanningTimeouts(data, ctx);
})
.transform((data) => ({
...data,
diff --git a/backend/src/lib/config/secret-scanning-timeouts.test.ts b/backend/src/lib/config/secret-scanning-timeouts.test.ts
new file mode 100644
index 0000000000..3ad7acc43c
--- /dev/null
+++ b/backend/src/lib/config/secret-scanning-timeouts.test.ts
@@ -0,0 +1,141 @@
+import { afterEach, describe, expect, test, vi } from "vitest";
+
+import { secretScanningTimeoutsSchema, validateSecretScanningTimeouts } from "./env";
+
+const schema = secretScanningTimeoutsSchema.superRefine(validateSecretScanningTimeouts);
+
+const parse = (env: Record<string, string>) => schema.safeParse(env);
+
+const issuesFor = (result: ReturnType<typeof parse>, key: string) =>
+ result.success ? [] : result.error.issues.filter((issue) => issue.path[0] === key).map((issue) => issue.message);
+
+afterEach(() => {
+ vi.unstubAllEnvs();
+});
+
+describe("secret scanning timeouts", () => {
+ test("defaults to ten minute clone and scan timeouts and a one hour stuck threshold", () => {
+ const result = parse({});
+
+ expect(result.success).toBe(true);
+ if (result.success) {
+ expect(result.data).toEqual({
+ SECRET_SCANNING_CLONE_TIMEOUT: 10 * 60 * 1000,
+ SECRET_SCANNING_SCAN_TIMEOUT: 10 * 60 * 1000,
+ SECRET_SCANNING_STUCK_SCAN_TIMEOUT: 60 * 60 * 1000
+ });
+ }
+ });
+
+ test.each([
+ ["30s", 30 * 1000],
+ ["90s", 90 * 1000],
+ ["15m", 15 * 60 * 1000],
+ ["2h", 2 * 60 * 60 * 1000]
+ ])("reads %s as milliseconds", (value, expected) => {
+ const result = parse({ SECRET_SCANNING_SCAN_TIMEOUT: value, SECRET_SCANNING_STUCK_SCAN_TIMEOUT: "6h" });
+
+ expect(result.success).toBe(true);
+ if (result.success) expect(result.data.SECRET_SCANNING_SCAN_TIMEOUT).toBe(expected);
+ });
+
+ test("treats a blank value as unset", () => {
+ const result = parse({ SECRET_SCANNING_SCAN_TIMEOUT: " " });
+
+ expect(result.success).toBe(true);
+ if (result.success) expect(result.data.SECRET_SCANNING_SCAN_TIMEOUT).toBe(10 * 60 * 1000);
+ });
+
+ test("rejects a duration it cannot parse", () => {
+ expect(issuesFor(parse({ SECRET_SCANNING_SCAN_TIMEOUT: "banana" }), "SECRET_SCANNING_SCAN_TIMEOUT")).toEqual([
+ 'Invalid duration "banana". Expected a positive duration string such as "30s", "10m" or "6h".'
+ ]);
+ });
+
+ test("rejects a duration that rounds below a millisecond", () => {
+ expect(parse({ SECRET_SCANNING_SCAN_TIMEOUT: "0s" }).success).toBe(false);
+ });
+
+ describe("legacy `_MS` variables", () => {
+ test("supplies the value when the duration variable is unset", () => {
+ vi.stubEnv("SECRET_SCANNING_CLONE_TIMEOUT_MS", "60000");
+ vi.stubEnv("SECRET_SCANNING_STUCK_SCAN_TIMEOUT_MS", "5400000");
+
+ const result = parse({});
+
+ expect(result.success).toBe(true);
+ if (result.success) {
+ expect(result.data.SECRET_SCANNING_CLONE_TIMEOUT).toBe(60 * 1000);
+ expect(result.data.SECRET_SCANNING_STUCK_SCAN_TIMEOUT).toBe(90 * 60 * 1000);
+ }
+ });
+
+ test("loses to the duration variable when both are set", () => {
+ vi.stubEnv("SECRET_SCANNING_SCAN_TIMEOUT_MS", "60000");
+
+ const result = parse({ SECRET_SCANNING_SCAN_TIMEOUT: "20s" });
+
+ expect(result.success).toBe(true);
+ if (result.success) expect(result.data.SECRET_SCANNING_SCAN_TIMEOUT).toBe(20 * 1000);
+ });
+
+ test("falls through to the default when blank", () => {
+ vi.stubEnv("SECRET_SCANNING_SCAN_TIMEOUT_MS", " ");
+
+ const result = parse({});
+
+ expect(result.success).toBe(true);
+ if (result.success) expect(result.data.SECRET_SCANNING_SCAN_TIMEOUT).toBe(10 * 60 * 1000);
+ });
+
+ test("is rejected by its own name when it holds something other than a number", () => {
+ vi.stubEnv("SECRET_SCANNING_SCAN_TIMEOUT_MS", "nope");
+
+ expect(issuesFor(parse({}), "SECRET_SCANNING_SCAN_TIMEOUT")).toEqual([
+ 'Invalid value "nope" in SECRET_SCANNING_SCAN_TIMEOUT_MS. Expected a positive number of milliseconds.'
+ ]);
+ });
+ });
+
+ describe("stuck scan threshold", () => {
+ test("must leave room for a clone, a scan and the fixed overhead", () => {
+ const result = parse({
+ SECRET_SCANNING_CLONE_TIMEOUT: "10m",
+ SECRET_SCANNING_SCAN_TIMEOUT: "10m",
+ SECRET_SCANNING_STUCK_SCAN_TIMEOUT: "20m"
+ });
+
+ 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."
+ ]);
+ });
+
+ test("is rejected when it exactly equals the scan budget", () => {
+ const result = parse({
+ SECRET_SCANNING_CLONE_TIMEOUT: "1m",
+ SECRET_SCANNING_SCAN_TIMEOUT: "1m",
+ SECRET_SCANNING_STUCK_SCAN_TIMEOUT: "7m"
+ });
+
+ expect(result.success).toBe(false);
+ });
+
+ test("is accepted one millisecond above the scan budget", () => {
+ const result = parse({
+ SECRET_SCANNING_CLONE_TIMEOUT: "1m",
+ SECRET_SCANNING_SCAN_TIMEOUT: "1m",
+ SECRET_SCANNING_STUCK_SCAN_TIMEOUT: "420001"
+ });
+
+ expect(result.success).toBe(true);
+ });
+
+ test("weighs the legacy variables the same way", () => {
+ vi.stubEnv("SECRET_SCANNING_SCAN_TIMEOUT_MS", "3600000");
+
+ const result = parse({ SECRET_SCANNING_STUCK_SCAN_TIMEOUT: "1h" });
+
+ expect(result.success).toBe(false);
+ });
+ });
+});