Skip to content

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

fix(dashboard): don't drop deep search queries containing a slash

Claude03c079f5dfe8
diff --git a/backend/src/server/routes/v1/dashboard-router.ts b/backend/src/server/routes/v1/dashboard-router.ts
index b39ac7c11a..4c415e8616 100644
--- a/backend/src/server/routes/v1/dashboard-router.ts
+++ b/backend/src/server/routes/v1/dashboard-router.ts
@@ -37,36 +37,9 @@ import {
} from "@app/services/secret/secret-types";
import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";
-const MAX_DEEP_SEARCH_LIMIT = 500; // arbitrary limit to prevent excessive results
+import { isSecretPathMatch, resolveSecretDeepSearch } from "./dashboard-secret-search-fns";
-const parseSecretPathSearch = (search?: string) => {
- if (!search)
- return {
- searchName: "",
- searchPath: ""
- };
-
- if (!search.includes("/"))
- return {
- searchName: search,
- searchPath: ""
- };
-
- if (search === "/")
- return {
- searchName: "",
- searchPath: "/"
- };
-
- const [searchName, ...searchPathSegments] = search.split("/").reverse();
- let searchPath = removeTrailingSlash(searchPathSegments.reverse().join("/").toLowerCase());
- if (!searchPath.startsWith("/")) searchPath = `/${searchPath}`;
-
- return {
- searchName,
- searchPath
- };
-};
+const MAX_DEEP_SEARCH_LIMIT = 500; // arbitrary limit to prevent excessive results
const SECRET_METADATA_SEARCH_MAX_FILTERS = 20;
@@ -1562,7 +1535,10 @@ export const registerDashboardRouter = async (server: FastifyZodProvider) => {
req.permission
);
- const { searchName, searchPath } = parseSecretPathSearch(search);
+ const { searchName, searchPath } = resolveSecretDeepSearch(
+ search,
+ allFolders.map((folder) => folder.path)
+ );
const folderMappings = allFolders.map((folder) => ({
folderId: folder.id,
@@ -1667,7 +1643,9 @@ export const registerDashboardRouter = async (server: FastifyZodProvider) => {
const sliceQuickSearch = <T>(array: T[]) => array.slice(0, 25);
const filteredDynamicSecrets = sliceQuickSearch(
- searchPath ? dynamicSecrets.filter((dynamicSecret) => dynamicSecret.path.endsWith(searchPath)) : dynamicSecrets
+ searchPath
+ ? dynamicSecrets.filter((dynamicSecret) => isSecretPathMatch(dynamicSecret.path, searchPath))
+ : dynamicSecrets
);
if (filteredDynamicSecrets?.length) {
@@ -1689,15 +1667,17 @@ export const registerDashboardRouter = async (server: FastifyZodProvider) => {
return {
secrets: sliceQuickSearch(
- searchPath ? secrets.filter((secret) => secret.secretPath.endsWith(searchPath)) : secrets
+ searchPath ? secrets.filter((secret) => isSecretPathMatch(secret.secretPath, searchPath)) : secrets
),
dynamicSecrets: sliceQuickSearch(
searchPath
- ? dynamicSecrets.filter((dynamicSecret) => dynamicSecret.path.endsWith(searchPath))
+ ? dynamicSecrets.filter((dynamicSecret) => isSecretPathMatch(dynamicSecret.path, searchPath))
: dynamicSecrets
),
secretRotations: sliceQuickSearch(
- searchPath ? secretRotations.filter((rotation) => rotation.folder.path.endsWith(searchPath)) : secretRotations
+ searchPath
+ ? secretRotations.filter((rotation) => isSecretPathMatch(rotation.folder.path, searchPath))
+ : secretRotations
),
folders: sliceQuickSearch(
allFolders.filter((folder) => {
@@ -1714,7 +1694,10 @@ export const registerDashboardRouter = async (server: FastifyZodProvider) => {
}
// support ending partial path match
- return folderPath.endsWith(searchPath) && folderName.toLowerCase().startsWith(searchName.toLowerCase());
+ return (
+ isSecretPathMatch(folderPath, searchPath) &&
+ folderName.toLowerCase().startsWith(searchName.toLowerCase())
+ );
}
// no search path, "fuzzy" match all folders
diff --git a/backend/src/server/routes/v1/dashboard-secret-search-fns.test.ts b/backend/src/server/routes/v1/dashboard-secret-search-fns.test.ts
new file mode 100644
index 0000000000..05c1259985
--- /dev/null
+++ b/backend/src/server/routes/v1/dashboard-secret-search-fns.test.ts
@@ -0,0 +1,65 @@
+import { isSecretPathMatch, parseSecretPathSearch, resolveSecretDeepSearch } from "./dashboard-secret-search-fns";
+
+describe("parseSecretPathSearch", () => {
+ test.each([
+ [undefined, "", ""],
+ ["", "", ""],
+ ["https:", "https:", ""],
+ ["/", "", "/"],
+ ["https:/", "", "/https:"],
+ ["https://service.ariba", "service.ariba", "/https:"],
+ ["prod/api", "api", "/prod"],
+ ["prod/", "", "/prod"],
+ ["Prod/api", "api", "/prod"],
+ ["a/b/c", "c", "/a/b"],
+ ["/prod/api", "api", "/prod"]
+ ])("splits %p into name %p and path %p", (search, searchName, searchPath) => {
+ expect(parseSecretPathSearch(search)).toEqual({ searchName, searchPath });
+ });
+});
+
+describe("isSecretPathMatch", () => {
+ test("matches an ending path segment case-insensitively", () => {
+ expect(isSecretPathMatch("/Prod", "/prod")).toBe(true);
+ expect(isSecretPathMatch("/eu/Prod", "/prod")).toBe(true);
+ expect(isSecretPathMatch("/production", "/prod")).toBe(false);
+ });
+});
+
+describe("resolveSecretDeepSearch", () => {
+ const folderPaths = ["/", "/prod", "/prod/api", "/eu/Staging", "/a/b", "/a/b/c"];
+
+ test("searches a slash-containing query literally when the path is not a folder", () => {
+ expect(resolveSecretDeepSearch("https:/", folderPaths)).toEqual({ searchName: "https:/", searchPath: "" });
+ expect(resolveSecretDeepSearch("https://service.ariba", folderPaths)).toEqual({
+ searchName: "https://service.ariba",
+ searchPath: ""
+ });
+ expect(resolveSecretDeepSearch("adress1-JDANDEV/", folderPaths)).toEqual({
+ searchName: "adress1-JDANDEV/",
+ searchPath: ""
+ });
+ });
+
+ test("scopes to a folder when the derived path exists", () => {
+ expect(resolveSecretDeepSearch("prod/api", folderPaths)).toEqual({ searchName: "api", searchPath: "/prod" });
+ expect(resolveSecretDeepSearch("prod/", folderPaths)).toEqual({ searchName: "", searchPath: "/prod" });
+ });
+
+ test("supports multi-segment paths", () => {
+ expect(resolveSecretDeepSearch("a/b/c", folderPaths)).toEqual({ searchName: "c", searchPath: "/a/b" });
+ });
+
+ test("resolves a folder path case-insensitively", () => {
+ expect(resolveSecretDeepSearch("Staging/db", folderPaths)).toEqual({ searchName: "db", searchPath: "/staging" });
+ });
+
+ test("always honors the root path", () => {
+ expect(resolveSecretDeepSearch("/", [])).toEqual({ searchName: "", searchPath: "/" });
+ });
+
+ test("leaves queries without a slash untouched", () => {
+ expect(resolveSecretDeepSearch("https:", folderPaths)).toEqual({ searchName: "https:", searchPath: "" });
+ expect(resolveSecretDeepSearch(undefined, folderPaths)).toEqual({ searchName: "", searchPath: "" });
+ });
+});
diff --git a/backend/src/server/routes/v1/dashboard-secret-search-fns.ts b/backend/src/server/routes/v1/dashboard-secret-search-fns.ts
new file mode 100644
index 0000000000..293d56000f
--- /dev/null
+++ b/backend/src/server/routes/v1/dashboard-secret-search-fns.ts
@@ -0,0 +1,62 @@
+import { removeTrailingSlash } from "@app/lib/fn";
+
+/**
+ * Splits a deep search query into a folder path and a name so that a query like `prod/api`
+ * can mean "things named `api` inside a folder path ending in `/prod`".
+ *
+ * The derived path is lowercased, so it must only ever be compared against folder paths
+ * case-insensitively (see `isSecretPathMatch`).
+ */
+export const parseSecretPathSearch = (search?: string) => {
+ if (!search)
+ return {
+ searchName: "",
+ searchPath: ""
+ };
+
+ if (!search.includes("/"))
+ return {
+ searchName: search,
+ searchPath: ""
+ };
+
+ if (search === "/")
+ return {
+ searchName: "",
+ searchPath: "/"
+ };
+
+ const [searchName, ...searchPathSegments] = search.split("/").reverse();
+ let searchPath = removeTrailingSlash(searchPathSegments.reverse().join("/").toLowerCase());
+ if (!searchPath.startsWith("/")) searchPath = `/${searchPath}`;
+
+ return {
+ searchName,
+ searchPath
+ };
+};
+
+/** Case-insensitive because the search path derived by `parseSecretPathSearch` is lowercased. */
+export const isSecretPathMatch = (path: string, searchPath: string) => path.toLowerCase().endsWith(searchPath);
+
+/**
+ * Resolves a deep search query into the free-text search term and the folder path scope.
+ *
+ * The path interpretation is only honored when the derived path resolves to a folder that
+ * exists in the searched environments. Otherwise the query is searched for literally,
+ * slashes included, so that values which merely happen to contain a slash — a secret
+ * metadata value such as `https://example.com` — are still matched instead of being scoped
+ * to a folder that does not exist.
+ */
+export const resolveSecretDeepSearch = (search: string | undefined, folderPaths: string[]) => {
+ const { searchName, searchPath } = parseSecretPathSearch(search);
+
+ if (!searchPath) return { searchName, searchPath };
+
+ const searchPathExists =
+ searchPath === "/" || folderPaths.some((folderPath) => isSecretPathMatch(folderPath, searchPath));
+
+ if (searchPathExists) return { searchName, searchPath };
+
+ return { searchName: search ?? "", searchPath: "" };
+};