Skip to content

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

fix: recheck cached unused access against current secrets and bound read activity inputs

Dimitris Staratzis82c548cfb407
diff --git a/backend/src/ee/routes/v1/insights-router.ts b/backend/src/ee/routes/v1/insights-router.ts
index 4891e85b20..cbfda9db6b 100644
--- a/backend/src/ee/routes/v1/insights-router.ts
+++ b/backend/src/ee/routes/v1/insights-router.ts
@@ -479,7 +479,7 @@ export const registerInsightsRouter = async (server: FastifyZodProvider) => {
"List the users and machine identities that can read a secret's value but have not read it in the last 30 days",
security: [{ bearerAuth: [] }],
params: z.object({
- projectId: z.string().trim().describe(INSIGHTS.GET_UNUSED_SECRET_ACCESS.projectId)
+ projectId: z.string().trim().max(36).describe(INSIGHTS.GET_UNUSED_SECRET_ACCESS.projectId)
}),
querystring: z.object({
offset: z.coerce.number().int().min(0).max(10000).default(0).describe(INSIGHTS.GET_UNUSED_SECRET_ACCESS.offset),
@@ -497,11 +497,21 @@ export const registerInsightsRouter = async (server: FastifyZodProvider) => {
onRequest: verifyAuth([AuthMode.JWT, AuthMode.OAUTH]),
handler: async (req, reply) => {
const { projectId } = req.params;
- const { unusedAccess, totalCount, unlistedCount, trackingStartedAt, unusedAccessFrom, remainingTTL } =
- await server.services.secretReadActivity.listUnusedSecretAccess({ projectId, ...req.query }, req.permission);
+ const {
+ unusedAccess,
+ totalCount,
+ unlistedCount,
+ trackingStartedAt,
+ unusedAccessFrom,
+ remainingTTL,
+ projectName
+ } = await server.services.secretReadActivity.listUnusedSecretAccess({ projectId, ...req.query }, req.permission);
await server.services.auditLog.createAuditLog({
projectId,
- event: { type: EventType.VIEW_INSIGHTS_SECRETS_MANAGEMENT_UNUSED_ACCESS, metadata: { projectId } },
+ event: {
+ type: EventType.VIEW_INSIGHTS_SECRETS_MANAGEMENT_UNUSED_ACCESS,
+ metadata: { projectId, projectName }
+ },
...req.auditLogInfo
});
diff --git a/backend/src/ee/routes/v1/secret-router.ts b/backend/src/ee/routes/v1/secret-router.ts
index d0d4a9e4b5..a18b5ba6eb 100644
--- a/backend/src/ee/routes/v1/secret-router.ts
+++ b/backend/src/ee/routes/v1/secret-router.ts
@@ -4,6 +4,7 @@ import { ProjectPermissionSecretActions } from "@app/ee/services/permission/proj
import { RAW_SECRETS } from "@app/lib/api-docs";
import { prefixWithSlash, removeTrailingSlash } from "@app/lib/fn";
import { readLimit } from "@app/server/config/rateLimiter";
+import { BaseSecretNameSchema } from "@app/server/lib/schemas";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { ActorType, AuthMode } from "@app/services/auth/auth-type";
@@ -91,14 +92,15 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => {
}
],
params: z.object({
- secretName: z.string().trim().min(1).describe(RAW_SECRETS.GET_READ_ACTIVITY.secretName)
+ secretName: BaseSecretNameSchema.max(500).describe(RAW_SECRETS.GET_READ_ACTIVITY.secretName)
}),
querystring: z.object({
- projectId: z.string().trim().describe(RAW_SECRETS.GET_READ_ACTIVITY.projectId),
+ projectId: z.string().trim().max(36).describe(RAW_SECRETS.GET_READ_ACTIVITY.projectId),
environment: z.string().trim().min(1).max(255).describe(RAW_SECRETS.GET_READ_ACTIVITY.environment),
secretPath: z
.string()
.trim()
+ .max(1024)
.default("/")
.transform(prefixWithSlash)
.transform(removeTrailingSlash)
diff --git a/backend/src/ee/services/audit-log/audit-log-types.ts b/backend/src/ee/services/audit-log/audit-log-types.ts
index 2e12a83ca0..5dacf934bc 100644
--- a/backend/src/ee/services/audit-log/audit-log-types.ts
+++ b/backend/src/ee/services/audit-log/audit-log-types.ts
@@ -5935,6 +5935,7 @@ interface ViewSecretManagementInsightsUnusedAccessEvent {
type: EventType.VIEW_INSIGHTS_SECRETS_MANAGEMENT_UNUSED_ACCESS;
metadata: {
projectId: string;
+ projectName: string;
};
}
diff --git a/backend/src/ee/services/secret-read-activity/secret-read-activity-service.ts b/backend/src/ee/services/secret-read-activity/secret-read-activity-service.ts
index 22c6c23d59..7ee4a737bc 100644
--- a/backend/src/ee/services/secret-read-activity/secret-read-activity-service.ts
+++ b/backend/src/ee/services/secret-read-activity/secret-read-activity-service.ts
@@ -52,6 +52,8 @@ const REACH_YIELD_INTERVAL = 100;
// cached and listed, about 200 KB per project.
const UNUSED_SECRET_ACCESS_MAX_LISTED = 1000;
+type TCachedUnusedSecretAccess = TUnusedSecretAccess & { secretId: string };
+
type TSecretReadActivityServiceFactoryDep = {
permissionService: Pick<TPermissionServiceFactory, "getProjectPermission" | "getProjectPermissions">;
licenseService: Pick<TLicenseServiceFactory, "getPlan">;
@@ -305,46 +307,68 @@ export const secretReadActivityServiceFactory = ({
)
);
- const rowsBySecret: (TUnusedSecretAccess & { secretTags: string[] })[][] = [];
- for (const { secret, environment, secretPath, trackedSince, reads } of activities) {
+ const unreadActorsOf = ({ secret, environment, secretPath, trackedSince, reads }: (typeof activities)[number]) =>
+ actorGroups
+ .filter(([{ permission }]) =>
+ hasSecretReadValueOrDescribePermission(permission, ProjectPermissionSecretActions.ReadValue, {
+ environment,
+ secretPath,
+ secretName: secret.key,
+ secretTags: secret.tags?.map((tag) => tag.slug)
+ })
+ )
+ .flat()
+ .filter(
+ (actor) =>
+ !reads.has(`${actor.actorType}:${actor.id}`) &&
+ coversWholeWindow(trackedSince, accessChangedAtByActorId.get(actor.id), windowStart)
+ );
+
+ // Only counts are kept for every secret: a large project can have millions of unread pairs, and only the
+ // highest-ranked ones are listed, so their rows are built in a second pass.
+ const unreadCounts: { activityIndex: number; count: number }[] = [];
+ for (let activityIndex = 0; activityIndex < activities.length; activityIndex += 1) {
// Permission checks are synchronous, so yield between secrets to keep a large project from blocking other requests.
// eslint-disable-next-line no-await-in-loop
await new Promise<void>((resolve) => {
setImmediate(resolve);
});
- const subjectFields = {
- environment,
- secretPath,
- secretName: secret.key,
- secretTags: secret.tags?.map((tag) => tag.slug)
- };
- rowsBySecret.push(
- actorGroups
- .filter(([{ permission }]) =>
- hasSecretReadValueOrDescribePermission(permission, ProjectPermissionSecretActions.ReadValue, subjectFields)
- )
- .flat()
- .flatMap((actor) => {
- if (
- reads.has(`${actor.actorType}:${actor.id}`) ||
- !coversWholeWindow(trackedSince, accessChangedAtByActorId.get(actor.id), windowStart)
- ) {
- return [];
- }
- return {
- secretKey: secret.key,
- environment,
- secretPath,
- actorType: actor.actorType,
- actorId: actor.id,
- actorName: actor.name,
- secretTags: subjectFields.secretTags ?? []
- };
- })
+ unreadCounts.push({ activityIndex, count: unreadActorsOf(activities[activityIndex]).length });
+ }
+ unreadCounts.sort((a, b) => b.count - a.count);
+
+ const rows: TCachedUnusedSecretAccess[] = [];
+ for (const { activityIndex, count } of unreadCounts) {
+ if (rows.length >= UNUSED_SECRET_ACCESS_MAX_LISTED || count === 0) break;
+ const activity = activities[activityIndex];
+ unreadActorsOf(activity).forEach((actor) =>
+ rows.push({
+ secretId: activity.secret.id,
+ secretKey: activity.secret.key,
+ environment: activity.environment,
+ secretPath: activity.secretPath,
+ actorType: actor.actorType,
+ actorId: actor.id,
+ actorName: actor.name
+ })
);
}
- const ranked = rowsBySecret.sort((a, b) => b.length - a.length).flat();
- return { rows: ranked.slice(0, UNUSED_SECRET_ACCESS_MAX_LISTED), totalCount: ranked.length };
+ return {
+ rows: rows.slice(0, UNUSED_SECRET_ACCESS_MAX_LISTED),
+ totalCount: unreadCounts.reduce((total, { count }) => total + count, 0)
+ };
+ };
+
+ // Concurrent requests on a cold cache share one computation per project instead of each scanning the project.
+ const inFlightComputations = new Map<string, ReturnType<typeof $computeUnusedSecretAccess>>();
+ const $computeUnusedSecretAccessOnce = (projectId: string, orgId: string) => {
+ const inFlight = inFlightComputations.get(projectId);
+ if (inFlight) return inFlight;
+ const computation = $computeUnusedSecretAccess(projectId, orgId).finally(() => {
+ inFlightComputations.delete(projectId);
+ });
+ inFlightComputations.set(projectId, computation);
+ return computation;
};
// The whole project is computed once a day and cached, so paging through it is cheap.
@@ -359,12 +383,18 @@ export const secretReadActivityServiceFactory = ({
keyStore,
key: cacheKey,
ttlSeconds: UNUSED_SECRET_ACCESS_CACHE_TTL_SECONDS,
- fetcher: () => $computeUnusedSecretAccess(projectId, actor.orgId)
+ fetcher: () => $computeUnusedSecretAccessOnce(projectId, actor.orgId)
});
- const [remainingTTL, instanceTrackingStartedAt, project] = await Promise.all([
+ const [remainingTTL, instanceTrackingStartedAt, project, tree, currentSecrets] = await Promise.all([
getCacheTtl(keyStore, cacheKey),
$getTrackingStartedAt(),
- projectDAL.findById(projectId)
+ projectDAL.findById(projectId),
+ loadProjectFolderTree({ folderDAL, projectEnvDAL }, projectId),
+ rows.length
+ ? secretV2BridgeDAL.find({
+ $in: { [`${TableName.SecretV2}.id` as "id"]: [...new Set(rows.map((row) => row.secretId))] }
+ })
+ : []
]);
// Reads are known from the later of when tracking began and when the project was created, and nothing can be
// called unused until a whole window has passed since then.
@@ -372,17 +402,25 @@ export const secretReadActivityServiceFactory = ({
Math.max(instanceTrackingStartedAt?.getTime() ?? 0, project?.createdAt.getTime() ?? 0)
);
- // The cache is shared by everyone in the project, so each caller only gets the secrets they could see anyway.
- const visibleRows = rows
- .filter((row) =>
- hasSecretReadValueOrDescribePermission(permission, ProjectPermissionSecretActions.DescribeSecret, {
- environment: row.environment,
- secretPath: row.secretPath,
- secretName: row.secretKey,
- secretTags: row.secretTags
- })
- )
- .map(({ secretTags, ...row }) => row);
+ // The cache is shared by everyone in the project and can be a day old, so each row is checked against the
+ // secret as it is now: deleted secrets drop out, and a caller only gets the secrets they could see anyway.
+ const currentSecretById = new Map(currentSecrets.map((secret) => [secret.id, secret]));
+ const visibleRows = rows.flatMap(({ secretId, ...row }): TUnusedSecretAccess[] => {
+ const secret = currentSecretById.get(secretId);
+ const location = secret && tree.locationOf(secret.folderId);
+ if (!secret || !location) return [];
+ const canDescribe = hasSecretReadValueOrDescribePermission(
+ permission,
+ ProjectPermissionSecretActions.DescribeSecret,
+ {
+ environment: location.environment,
+ secretPath: location.secretPath,
+ secretName: secret.key,
+ secretTags: secret.tags?.map((tag) => tag.slug)
+ }
+ );
+ return canDescribe ? [{ ...row, secretKey: secret.key, ...location }] : [];
+ });
// Unsorted, the list keeps its ranking. The sort is stable, so ties keep it too.
const sorted = orderBy
@@ -396,6 +434,7 @@ export const secretReadActivityServiceFactory = ({
unusedAccess: sorted.slice(offset, offset + limit),
totalCount: visibleRows.length,
unlistedCount: totalCount - rows.length,
+ projectName: project?.name ?? "",
trackingStartedAt: projectTrackingStartedAt,
unusedAccessFrom: new Date(projectTrackingStartedAt.getTime() + SECRET_READ_ACTIVITY_WINDOW_DAYS * DAY_IN_MS),
remainingTTL