Skip to content

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

address bot reviews

Andrey3bca681bd8da
diff --git a/backend/src/ee/services/pam-account/pam-account-connection-test.ts b/backend/src/ee/services/pam-account/pam-account-connection-test.ts
index 04cd4cdd2b..d4e561015e 100644
--- a/backend/src/ee/services/pam-account/pam-account-connection-test.ts
+++ b/backend/src/ee/services/pam-account/pam-account-connection-test.ts
@@ -389,15 +389,14 @@ export const buildGatewayConnectionTest = async (
sslCertificate?: string;
};
const c = creds as { username: string; password?: string } | null;
- const username = (c?.username ?? (credentials?.username as string | undefined))?.trim();
- if (!username) return tcp(host, port);
+ if (!c) return tcp(host, port);
return {
host,
port,
request: {
mode: TestConnectionMode.ClickHouse,
- username,
- password: c?.password,
+ username: c.username,
+ password: c.password,
database: cd.database,
sslEnabled: cd.sslEnabled,
sslRejectUnauthorized: cd.sslRejectUnauthorized,
diff --git a/backend/src/ee/services/pam-account/pam-account-schemas.ts b/backend/src/ee/services/pam-account/pam-account-schemas.ts
index 21cf91b16e..0c71c478fe 100644
--- a/backend/src/ee/services/pam-account/pam-account-schemas.ts
+++ b/backend/src/ee/services/pam-account/pam-account-schemas.ts
@@ -602,7 +602,7 @@ export const ACCOUNT_TYPE_CONFIGS = {
database: z.string().trim().min(1).max(255),
sslEnabled: z.boolean(),
sslRejectUnauthorized: z.boolean(),
- sslCertificate: optionalTrimmedString
+ sslCertificate: boundedOptionalString(16384)
}),
credentials: z.object({
username: z.string().trim().min(1).max(255),
diff --git a/backend/src/ee/services/pam-web-access/clickhouse/pam-clickhouse-client.ts b/backend/src/ee/services/pam-web-access/clickhouse/pam-clickhouse-client.ts
index 32ffe282cc..bdd909d72a 100644
--- a/backend/src/ee/services/pam-web-access/clickhouse/pam-clickhouse-client.ts
+++ b/backend/src/ee/services/pam-web-access/clickhouse/pam-clickhouse-client.ts
@@ -6,7 +6,7 @@ import { BadRequestError } from "@app/lib/errors";
const REQUEST_TIMEOUT_MS = 30 * 60 * 1000;
const CONNECT_TIMEOUT_MS = 30 * 1000;
-export const MAX_RESULT_BYTES = 64 * 1024 * 1024;
+export const MAX_RESULT_BYTES = 16 * 1024 * 1024;
export const createRelayClient = (opts: {
relayPort: number;
@@ -74,12 +74,16 @@ export const verifyRelayReachable = async (opts: { relayPort: number; database?:
}
};
-export const readStreamText = async (stream: Readable, maxBytes: number): Promise<string> => {
+export const readStreamText = async (
+ stream: Readable,
+ maxBytes: number
+): Promise<{ text: string; truncated: boolean }> => {
// Stops an unhandled 'error' from taking the process down once iteration has moved on
stream.on("error", () => {});
const chunks: Buffer[] = [];
let total = 0;
+ let truncated = false;
try {
for await (const chunk of stream) {
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk as string);
@@ -87,6 +91,7 @@ export const readStreamText = async (stream: Readable, maxBytes: number): Promis
if (buffer.length >= remaining) {
chunks.push(buffer.subarray(0, remaining));
total = maxBytes;
+ truncated = true;
break;
}
chunks.push(buffer);
@@ -96,9 +101,19 @@ export const readStreamText = async (stream: Readable, maxBytes: number): Promis
stream.destroy();
}
- return Buffer.concat(chunks).toString("utf8");
+ return { text: Buffer.concat(chunks).toString("utf8"), truncated };
};
+export class ClickhouseResultTooLargeError extends Error {
+ constructor(maxBytes: number) {
+ super(
+ `This statement returned more than the ${Math.round(maxBytes / (1024 * 1024))} MB a browser session can hold. ` +
+ `Narrow the query, or use the CLI with your own client to read the full result.`
+ );
+ this.name = "ClickhouseResultTooLargeError";
+ }
+}
+
export const clickhouseErrorFields = (err: unknown): { message?: string; detail?: string } => {
if (err instanceof ClickHouseError) {
return { message: err.message, detail: err.type ? `${err.type} (code ${err.code})` : `Code ${err.code}` };
diff --git a/backend/src/ee/services/pam-web-access/clickhouse/pam-clickhouse-connection-controller.ts b/backend/src/ee/services/pam-web-access/clickhouse/pam-clickhouse-connection-controller.ts
index 4d15584760..e104f5ac69 100644
--- a/backend/src/ee/services/pam-web-access/clickhouse/pam-clickhouse-connection-controller.ts
+++ b/backend/src/ee/services/pam-web-access/clickhouse/pam-clickhouse-connection-controller.ts
@@ -12,6 +12,7 @@ import {
} from "../pam-data-explorer-ws-types";
import {
clickhouseErrorFields,
+ ClickhouseResultTooLargeError,
isRelayGoneError,
MAX_RESULT_BYTES,
openRelayClient,
@@ -111,7 +112,10 @@ export const createClickhouseConnectionController = async (
}
});
- return parseStatementBody(await readStreamText(stream, MAX_RESULT_BYTES), summary);
+ const { text, truncated } = await readStreamText(stream, MAX_RESULT_BYTES);
+ if (truncated) throw new ClickhouseResultTooLargeError(MAX_RESULT_BYTES);
+
+ return parseStatementBody(text, summary);
};
const runQuery = async (sql: string) => {
diff --git a/backend/src/ee/services/pam-web-access/clickhouse/pam-clickhouse-data-explorer-fns.test.ts b/backend/src/ee/services/pam-web-access/clickhouse/pam-clickhouse-data-explorer-fns.test.ts
index ec8222c95d..84b6afde5a 100644
--- a/backend/src/ee/services/pam-web-access/clickhouse/pam-clickhouse-data-explorer-fns.test.ts
+++ b/backend/src/ee/services/pam-web-access/clickhouse/pam-clickhouse-data-explorer-fns.test.ts
@@ -51,6 +51,18 @@ describe("splitClickhouseStatements", () => {
expect(splitClickhouseStatements("SELECT /* a;b */ 1; SELECT 2")).toEqual(["SELECT /* a;b */ 1", "SELECT 2"]);
});
+ test("semicolon inside a heredoc", () => {
+ expect(splitClickhouseStatements("SELECT $doc$a;b$doc$; SELECT 2")).toEqual(["SELECT $doc$a;b$doc$", "SELECT 2"]);
+ expect(splitClickhouseStatements("SELECT $$a;b$$; SELECT 2")).toEqual(["SELECT $$a;b$$", "SELECT 2"]);
+ });
+
+ test("semicolon inside a nested block comment", () => {
+ expect(splitClickhouseStatements("SELECT /* outer /* inner; */ tail; */ 1; SELECT 2")).toEqual([
+ "SELECT /* outer /* inner; */ tail; */ 1",
+ "SELECT 2"
+ ]);
+ });
+
test("an unterminated literal swallows the rest rather than splitting inside it", () => {
expect(splitClickhouseStatements("SELECT 'oops; SELECT 2")).toEqual(["SELECT 'oops; SELECT 2"]);
});
@@ -65,6 +77,10 @@ describe("extractCommand", () => {
expect(extractCommand("/* a comment */ OPTIMIZE TABLE t")).toBe("OPTIMIZE");
});
+ test("skips a nested block comment before the keyword", () => {
+ expect(extractCommand("/* a /* nested */ comment */ SELECT 1")).toBe("SELECT");
+ });
+
test("stops at a delimiter and handles an empty statement", () => {
expect(extractCommand("SELECT(1)")).toBe("SELECT");
expect(extractCommand("")).toBe("");
@@ -76,6 +92,11 @@ describe("uniqueFieldNames", () => {
expect(uniqueFieldNames(["a", "b"])).toEqual(["a", "b"]);
expect(uniqueFieldNames(["a", "a", "a"])).toEqual(["a", "a_1", "a_2"]);
});
+
+ test("a generated suffix never collides with a real column of that name", () => {
+ expect(uniqueFieldNames(["a", "a", "a_1"])).toEqual(["a", "a_1", "a_1_1"]);
+ expect(uniqueFieldNames(["a", "a_1", "a"])).toEqual(["a", "a_1", "a_2"]);
+ });
});
describe("toRowObjects", () => {
diff --git a/backend/src/ee/services/pam-web-access/clickhouse/pam-clickhouse-data-explorer-fns.ts b/backend/src/ee/services/pam-web-access/clickhouse/pam-clickhouse-data-explorer-fns.ts
index 406df89586..a8c0976474 100644
--- a/backend/src/ee/services/pam-web-access/clickhouse/pam-clickhouse-data-explorer-fns.ts
+++ b/backend/src/ee/services/pam-web-access/clickhouse/pam-clickhouse-data-explorer-fns.ts
@@ -27,10 +27,37 @@ const skipLineComment = (sql: string, pos: number): number => {
return i;
};
+// ClickHouse nests block comments, so the first */ does not necessarily end one
const skipBlockComment = (sql: string, pos: number): number => {
- let i = pos + 2;
- while (i + 1 < sql.length && !(sql[i] === "*" && sql[i + 1] === "/")) i += 1;
- return Math.min(i + 2, sql.length);
+ let depth = 0;
+ let i = pos;
+ while (i < sql.length) {
+ if (sql[i] === "/" && sql[i + 1] === "*") {
+ depth += 1;
+ i += 2;
+ } else if (sql[i] === "*" && sql[i + 1] === "/") {
+ depth -= 1;
+ i += 2;
+ if (depth === 0) return i;
+ } else {
+ i += 1;
+ }
+ }
+ return sql.length;
+};
+
+// A $tag$ ... $tag$ heredoc carries its contents verbatim, semicolons included
+const heredocTagAt = (sql: string, pos: number): string | null => {
+ if (sql[pos] !== "$") return null;
+ let i = pos + 1;
+ while (i < sql.length && /[A-Za-z0-9_]/.test(sql[i])) i += 1;
+ if (sql[i] !== "$") return null;
+ return sql.slice(pos, i + 1);
+};
+
+const skipHeredoc = (sql: string, pos: number, tag: string): number => {
+ const end = sql.indexOf(tag, pos + tag.length);
+ return end === -1 ? sql.length : end + tag.length;
};
// The HTTP interface runs one statement per request, so an editor tab's script is split here
@@ -41,8 +68,11 @@ export const splitClickhouseStatements = (sql: string): string[] => {
while (pos < sql.length) {
const ch = sql[pos];
+ const heredocTag = heredocTagAt(sql, pos);
- if (ch === "'" || ch === '"' || ch === "`") {
+ if (heredocTag) {
+ pos = skipHeredoc(sql, pos, heredocTag);
+ } else if (ch === "'" || ch === '"' || ch === "`") {
pos = skipQuoted(sql, pos, ch);
} else if (isLineComment(sql, pos)) {
pos = skipLineComment(sql, pos);
@@ -86,11 +116,17 @@ export const extractCommand = (sql: string): string => {
};
export const uniqueFieldNames = (names: string[]): string[] => {
- const seen = new Map<string, number>();
+ const used = new Set<string>();
return names.map((name) => {
- const count = seen.get(name) ?? 0;
- seen.set(name, count + 1);
- return count === 0 ? name : `${name}_${count}`;
+ if (!used.has(name)) {
+ used.add(name);
+ return name;
+ }
+ let suffix = 1;
+ while (used.has(`${name}_${suffix}`)) suffix += 1;
+ const unique = `${name}_${suffix}`;
+ used.add(unique);
+ return unique;
});
};
diff --git a/docs/documentation/platform/pam/accounts/clickhouse.mdx b/docs/documentation/platform/pam/accounts/clickhouse.mdx
index 71ab5ed12d..412cfb9f89 100644
--- a/docs/documentation/platform/pam/accounts/clickhouse.mdx
+++ b/docs/documentation/platform/pam/accounts/clickhouse.mdx
@@ -54,7 +54,7 @@ CREATE USER pam_service IDENTIFIED WITH sha256_password BY '<password>';
GRANT SELECT ON analytics.* TO pam_service;
```
-The account's **Database** sets what a session opens by default, not a boundary it stays inside: a session can query any database the user holds a grant on, and Web Access lists all of them. Scope the grants rather than the account's database field.
+Every session opens on the account's **Database**, whatever a client asks for. That is a starting point, not a limit: a session can still read any database the user has a grant on, and Web Access lists them all. Scope the grants, not the **Database** field.
Command blocking matches the text of each statement, so a statement that builds SQL at runtime can run something a pattern would otherwise reject. Use ClickHouse grants for restrictions that have to hold.