|
1 | 1 | import { describe, expect, test } from "bun:test" |
2 | | -import { normalizeConfig } from "@altimateai/drivers" |
| 2 | +import { normalizeConfig, sanitizeConnectionString } from "@altimateai/drivers" |
3 | 3 | import { isSensitiveField } from "../../src/altimate/native/connections/credential-store" |
4 | 4 |
|
5 | 5 | // --------------------------------------------------------------------------- |
@@ -947,3 +947,114 @@ describe("normalizeConfig — ClickHouse", () => { |
947 | 947 | expect(result.ssl_key).toBeUndefined() |
948 | 948 | }) |
949 | 949 | }) |
| 950 | + |
| 951 | +// --------------------------------------------------------------------------- |
| 952 | +// sanitizeConnectionString — special character encoding |
| 953 | +// --------------------------------------------------------------------------- |
| 954 | + |
| 955 | +describe("sanitizeConnectionString", () => { |
| 956 | + test("encodes @ in password", () => { |
| 957 | + const input = "postgresql://user:p@ss@localhost:5432/db" |
| 958 | + const result = sanitizeConnectionString(input) |
| 959 | + expect(result).toBe("postgresql://user:p%40ss@localhost:5432/db") |
| 960 | + }) |
| 961 | + |
| 962 | + test("encodes # in password", () => { |
| 963 | + const input = "postgresql://admin:pass#word@localhost/db" |
| 964 | + const result = sanitizeConnectionString(input) |
| 965 | + expect(result).toBe("postgresql://admin:pass%23word@localhost/db") |
| 966 | + }) |
| 967 | + |
| 968 | + test("encodes : in password", () => { |
| 969 | + const input = "postgresql://admin:pass:word@localhost/db" |
| 970 | + const result = sanitizeConnectionString(input) |
| 971 | + expect(result).toBe("postgresql://admin:pass%3Aword@localhost/db") |
| 972 | + }) |
| 973 | + |
| 974 | + test("encodes multiple special characters", () => { |
| 975 | + const input = "postgresql://admin:p@ss#w:rd@localhost/db" |
| 976 | + const result = sanitizeConnectionString(input) |
| 977 | + expect(result).toBe("postgresql://admin:p%40ss%23w%3Ard@localhost/db") |
| 978 | + }) |
| 979 | + |
| 980 | + test("leaves already-encoded passwords untouched", () => { |
| 981 | + const input = "postgresql://admin:p%40ss%23word@localhost/db" |
| 982 | + expect(sanitizeConnectionString(input)).toBe(input) |
| 983 | + }) |
| 984 | + |
| 985 | + test("leaves passwords without special characters untouched", () => { |
| 986 | + const input = "postgresql://admin:simplepassword@localhost/db" |
| 987 | + expect(sanitizeConnectionString(input)).toBe(input) |
| 988 | + }) |
| 989 | + |
| 990 | + test("leaves non-URI strings untouched", () => { |
| 991 | + const input = "host=localhost dbname=mydb" |
| 992 | + expect(sanitizeConnectionString(input)).toBe(input) |
| 993 | + }) |
| 994 | + |
| 995 | + test("handles mongodb scheme", () => { |
| 996 | + const input = "mongodb://user:p@ss@localhost:27017/mydb" |
| 997 | + const result = sanitizeConnectionString(input) |
| 998 | + expect(result).toBe("mongodb://user:p%40ss@localhost:27017/mydb") |
| 999 | + }) |
| 1000 | + |
| 1001 | + test("handles mongodb+srv scheme", () => { |
| 1002 | + const input = "mongodb+srv://user:p@[email protected]/mydb" |
| 1003 | + const result = sanitizeConnectionString(input) |
| 1004 | + expect(result).toBe("mongodb+srv://user:p%[email protected]/mydb") |
| 1005 | + }) |
| 1006 | + |
| 1007 | + test("leaves URIs without password untouched", () => { |
| 1008 | + const input = "postgresql://admin@localhost/db" |
| 1009 | + expect(sanitizeConnectionString(input)).toBe(input) |
| 1010 | + }) |
| 1011 | +}) |
| 1012 | + |
| 1013 | +// --------------------------------------------------------------------------- |
| 1014 | +// normalizeConfig — connection_string sanitization integration |
| 1015 | +// --------------------------------------------------------------------------- |
| 1016 | + |
| 1017 | +describe("normalizeConfig — connection_string sanitization", () => { |
| 1018 | + test("sanitizes connection_string with special chars in password", () => { |
| 1019 | + const result = normalizeConfig({ |
| 1020 | + type: "postgres", |
| 1021 | + connection_string: "postgresql://admin:p@ss#word@localhost/db", |
| 1022 | + }) |
| 1023 | + expect(result.connection_string).toBe( |
| 1024 | + "postgresql://admin:p%40ss%23word@localhost/db", |
| 1025 | + ) |
| 1026 | + }) |
| 1027 | + |
| 1028 | + test("sanitizes connectionString alias with special chars", () => { |
| 1029 | + const result = normalizeConfig({ |
| 1030 | + type: "postgres", |
| 1031 | + connectionString: "postgresql://admin:p@ss@localhost/db", |
| 1032 | + }) |
| 1033 | + // alias resolved to connection_string, then sanitized |
| 1034 | + expect(result.connection_string).toBe( |
| 1035 | + "postgresql://admin:p%40ss@localhost/db", |
| 1036 | + ) |
| 1037 | + expect(result.connectionString).toBeUndefined() |
| 1038 | + }) |
| 1039 | + |
| 1040 | + test("does not alter connection_string without special chars", () => { |
| 1041 | + const result = normalizeConfig({ |
| 1042 | + type: "redshift", |
| 1043 | + connection_string: "postgresql://admin:password@host:5439/dev", |
| 1044 | + }) |
| 1045 | + expect(result.connection_string).toBe( |
| 1046 | + "postgresql://admin:password@host:5439/dev", |
| 1047 | + ) |
| 1048 | + }) |
| 1049 | + |
| 1050 | + test("does not alter config without connection_string", () => { |
| 1051 | + const result = normalizeConfig({ |
| 1052 | + type: "postgres", |
| 1053 | + host: "localhost", |
| 1054 | + password: "p@ss#word", |
| 1055 | + }) |
| 1056 | + // Individual fields are NOT URI-encoded — drivers handle them natively |
| 1057 | + expect(result.password).toBe("p@ss#word") |
| 1058 | + expect(result.connection_string).toBeUndefined() |
| 1059 | + }) |
| 1060 | +}) |
0 commit comments