Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

db: redact connection URLs in the migration banner

Carl-Gerhard Lindesvärd337b4ff8fe26
diff --git a/packages/db/src/code-migrations/helpers.test.ts b/packages/db/src/code-migrations/helpers.test.ts
new file mode 100644
index 000000000..9aae4f6c1
--- /dev/null
+++ b/packages/db/src/code-migrations/helpers.test.ts
@@ -0,0 +1,41 @@
+// Ported from the redaction main #511 added to code-migrations/migrate.ts.
+import { describe, expect, it } from 'bun:test';
+import { redactConnectionUrls } from './helpers';
+
+describe('redactConnectionUrls', () => {
+ it('drops the credentials but keeps host, port and database', () => {
+ expect(
+ redactConnectionUrls(
+ 'postgresql://postgres:hunter2@localhost:5432/openpanel?schema=public'
+ )
+ ).toBe('postgresql://***@localhost:5432/openpanel?schema=public');
+ });
+
+ it('redacts credential query params', () => {
+ expect(
+ redactConnectionUrls(
+ 'http://ch:8123/openpanel?user=admin&password=s3cr3t'
+ )
+ ).toBe('http://ch:8123/openpanel?user=***&password=***');
+ });
+
+ it('handles the comma-separated ClickHouse list', () => {
+ expect(
+ redactConnectionUrls(
+ 'http://a:b@ch1:8123/openpanel,http://a:b@ch2:8123/openpanel'
+ )
+ ).toBe('http://***@ch1:8123/openpanel,http://***@ch2:8123/openpanel');
+ });
+
+ it('says so for an unset or unparseable value', () => {
+ expect(redactConnectionUrls(undefined)).toBe('(not set)');
+ expect(redactConnectionUrls('')).toBe('(not set)');
+ expect(redactConnectionUrls('not a url')).toBe('(unparseable url)');
+ });
+
+ it('leaves a credential-free url alone', () => {
+ expect(redactConnectionUrls('http://localhost:8123/openpanel')).toBe(
+ 'http://localhost:8123/openpanel'
+ );
+ });
+});
diff --git a/packages/db/src/code-migrations/helpers.ts b/packages/db/src/code-migrations/helpers.ts
index 40be65a88..81404b8e6 100644
--- a/packages/db/src/code-migrations/helpers.ts
+++ b/packages/db/src/code-migrations/helpers.ts
@@ -47,6 +47,38 @@ export interface CodeMigrationEnv {
clickhouseUrl: string | undefined;
}
+const CREDENTIAL_QUERY_PARAMS = new Set(['password', 'sslpassword', 'user']);
+
+/**
+ * Connection URLs carry credentials; the startup banner must not put them in
+ * container and CI logs. Keeps scheme, host, port and database, drops the
+ * rest. ClickHouse accepts a comma-separated list, so each URL is handled.
+ */
+export function redactConnectionUrls(value: string | undefined): string {
+ if (!value) {
+ return '(not set)';
+ }
+ return value
+ .split(',')
+ .map((raw) => {
+ try {
+ const url = new URL(raw.trim());
+ const params = new URLSearchParams(url.search);
+ for (const key of [...params.keys()]) {
+ if (CREDENTIAL_QUERY_PARAMS.has(key.toLowerCase())) {
+ params.set(key, '***');
+ }
+ }
+ const query = params.toString();
+ const auth = url.username ? '***@' : '';
+ return `${url.protocol}//${auth}${url.host}${url.pathname}${query ? `?${query}` : ''}`;
+ } catch {
+ return '(unparseable url)';
+ }
+ })
+ .join(',');
+}
+
export function getIsCluster(env: CodeMigrationEnv) {
return process.argv.includes('--cluster') || env.clickhouseCluster;
}
diff --git a/packages/db/src/code-migrations/migrate.ts b/packages/db/src/code-migrations/migrate.ts
index 533a009b4..0b6e1c26f 100644
--- a/packages/db/src/code-migrations/migrate.ts
+++ b/packages/db/src/code-migrations/migrate.ts
@@ -9,6 +9,7 @@ import {
getIsSelfHosting,
getShouldIgnoreRecord,
printBoxMessage,
+ redactConnectionUrls,
} from './helpers';
// The numbered migration files live beside this runner.
@@ -58,8 +59,8 @@ export async function runCodeMigrations(env: CodeMigrationEnv) {
]);
printBoxMessage('🌍 Environment', [
- `POSTGRES: ${env.databaseUrl}`,
- `CLICKHOUSE: ${env.clickhouseUrl}`,
+ `POSTGRES: ${redactConnectionUrls(env.databaseUrl)}`,
+ `CLICKHOUSE: ${redactConnectionUrls(env.clickhouseUrl)}`,
]);
if (!getIsSelfHosting(env)) {