The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
fix: handle whitespaces in the connection string, decode username and password (#62328)
Jakub Chodorowicz2656ef82cd46
diff --git a/frontend/src/metabase/databases/components/DatabaseConnectionUri/database-field-mapper.ts b/frontend/src/metabase/databases/components/DatabaseConnectionUri/database-field-mapper.tsindex d750c1dc7d9..ec80de4f201 100644--- a/frontend/src/metabase/databases/components/DatabaseConnectionUri/database-field-mapper.ts+++ b/frontend/src/metabase/databases/components/DatabaseConnectionUri/database-field-mapper.ts@@ -391,7 +391,7 @@ export function mapFieldsToNestedObject(* @returns The semicolon-separated string.*/function objectToString(- obj: Record<string, string> | undefined,+ obj: Record<string, string | undefined> | undefined,filterProperties: Array<string> = [],) {if (!obj) {diff --git a/frontend/src/metabase/databases/components/DatabaseConnectionUri/parse-connection-regex.ts b/frontend/src/metabase/databases/components/DatabaseConnectionUri/parse-connection-regex.tsindex b90ee32c6cf..1f7abf6063f 100644--- a/frontend/src/metabase/databases/components/DatabaseConnectionUri/parse-connection-regex.ts+++ b/frontend/src/metabase/databases/components/DatabaseConnectionUri/parse-connection-regex.ts@@ -9,7 +9,7 @@ export interface RegexFields {username?: string;password?: string;protocol?: string;- params?: Record<string, string> | undefined;+ params?: Record<string, string | undefined> | undefined;path?: string;hasJdbcPrefix: boolean;}@@ -197,17 +197,23 @@ export function parseConnectionUriRegex(}const regex = connectionStringRegexes[engineKey];+ const trimmedString = connectionUri.trim();+if (!regex) {return null;}++ // Some of engines have more than one matching regexconst candidate: RegExp | undefined = Array.isArray(regex)- ? regex.find((r) => connectionUri.match(r))+ ? regex.find((r) => trimmedString.match(r)): regex;if (!candidate) {return null;}- const match = connectionUri.match(candidate);++ const match = trimmedString.match(candidate);+if (match) {const params = match.groups?.params? Object.fromEntries(new URLSearchParams(match.groups.params))@@ -215,6 +221,8 @@ export function parseConnectionUriRegex(const semicolonParams = mapSemicolonParams(match.groups?.semicolonParams);return {...match.groups,+ username: safeDecode(match.groups?.username),+ password: safeDecode(match.groups?.password),params: params ?? semicolonParams,hasJdbcPrefix: Boolean(match.groups?.hasJdbcPrefix),};@@ -230,11 +238,23 @@ function mapSemicolonParams(semicolonParams: string | undefined) {return semicolonParams.split(";")- .reduce<Record<string, string>>((acc, param) => {+ .reduce<Record<string, string | undefined>>((acc, param) => {const [key, value] = param.split("=");if (key !== "") {- acc[key] = decodeURIComponent(value);+ acc[key] = safeDecode(value);}return acc;}, {});}++function safeDecode(text: string | undefined) {+ if (!text) {+ return text;+ }++ try {+ return decodeURIComponent(text);+ } catch {+ return text;+ }+}diff --git a/frontend/src/metabase/databases/components/DatabaseConnectionUri/parse-connection-regex.unit.spec.ts b/frontend/src/metabase/databases/components/DatabaseConnectionUri/parse-connection-regex.unit.spec.tsindex b8cad4f04f8..926666b99cb 100644--- a/frontend/src/metabase/databases/components/DatabaseConnectionUri/parse-connection-regex.unit.spec.ts+++ b/frontend/src/metabase/databases/components/DatabaseConnectionUri/parse-connection-regex.unit.spec.ts@@ -1,6 +1,72 @@import { enginesConfig } from "./engines-config";import { parseConnectionUriRegex } from "./parse-connection-regex";+describe("parseConnectionUri - whitespace and encoding", () => {+ it("should parse a connection string with whitespace", () => {+ const connectionString =+ " jdbc:mysql://user:pass@host:3306/dbname?ssl=true ";+ const result = parseConnectionUriRegex(connectionString, "mysql");+ expect(result).toEqual(+ expect.objectContaining({+ protocol: "mysql",+ host: "host",+ port: "3306",+ database: "dbname",+ params: {+ ssl: "true",+ },+ }),+ );+ });++ it("should parse a connection string with new lines", () => {+ const connectionString =+ "\n\njdbc:mysql://user:pass@host:3306/dbname?ssl=true\n";+ const result = parseConnectionUriRegex(connectionString, "mysql");+ expect(result).toEqual(+ expect.objectContaining({+ protocol: "mysql",+ host: "host",+ port: "3306",+ database: "dbname",+ params: {+ ssl: "true",+ },+ }),+ );+ });++ it("should handle special characters in username and password", () => {+ const connectionString =+ "postgres://me%40ry:pe%40ce%26lo%2F3@host:5432/dbname";+ const result = parseConnectionUriRegex(connectionString, "postgres");+ expect(result).toEqual(+ expect.objectContaining({+ protocol: "postgres",+ host: "host",+ port: "5432",+ database: "dbname",+ username: "me@ry",+ password: "pe@ce&lo/3",+ }),+ );+ });++ it("should handle malformed uri component", () => {+ const connectionString = "postgres://me%ZZry@host:5432/dbname";+ const result = parseConnectionUriRegex(connectionString, "postgres");+ expect(result).toEqual(+ expect.objectContaining({+ protocol: "postgres",+ host: "host",+ port: "5432",+ database: "dbname",+ username: "me%ZZry",+ }),+ );+ });+});+describe("parseConnectionUriRegex - Amazon Athena", () => {it("should parse a basic Amazon Athena connection string", () => {const connectionString = enginesConfig["athena"];