fix(sqlserver): preserve legacy compatibility setting
This commit is contained in:
6 files changed
+245
-12
No files matched your search
+49
-4
@@ -1,5 +1,12 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { isSqlServerNativeEncryptionDisabled, requiresSqlServerLegacyCompatibilityComponent, setSqlServerLegacyCompatibilityConfig, setSqlServerNativeEncryptionDisabled, sqlServerUsesLegacyCompatibility } from "@/lib/connection/sqlServerLegacyCompatibility";
|
||||
import {
|
||||
isSqlServerNativeEncryptionDisabled,
|
||||
migrateSqlServerLegacyCompatibilityConfig,
|
||||
requiresSqlServerLegacyCompatibilityComponent,
|
||||
setSqlServerLegacyCompatibilityConfig,
|
||||
setSqlServerNativeEncryptionDisabled,
|
||||
sqlServerUsesLegacyCompatibility,
|
||||
} from "@/lib/connection/sqlServerLegacyCompatibility";
|
||||
import type { ConnectionConfig } from "@/types/database";
|
||||
|
||||
function connectionConfig(urlParams?: string): ConnectionConfig {
|
||||
@@ -37,11 +44,14 @@ describe("SQL Server legacy compatibility", () => {
|
||||
expect(setSqlServerNativeEncryptionDisabled("applicationName=dbx;sqlserverEncryption=disabled", false)).toBe("applicationName=dbx");
|
||||
});
|
||||
|
||||
it("keeps historical disabled-encryption connections on the native driver", () => {
|
||||
it("migrates historical disabled-encryption connections to the legacy driver profile", () => {
|
||||
const config = connectionConfig("sqlserverEncryption=disabled");
|
||||
migrateSqlServerLegacyCompatibilityConfig(config);
|
||||
|
||||
expect(sqlServerUsesLegacyCompatibility(config)).toBe(false);
|
||||
expect(requiresSqlServerLegacyCompatibilityComponent(config)).toBe(false);
|
||||
expect(sqlServerUsesLegacyCompatibility(config)).toBe(true);
|
||||
expect(requiresSqlServerLegacyCompatibilityComponent(config)).toBe(true);
|
||||
expect(config.driver_label).toBe("SQL Server legacy compatibility component");
|
||||
expect(config.url_params).toBe("");
|
||||
expect(
|
||||
requiresSqlServerLegacyCompatibilityComponent({
|
||||
...config,
|
||||
@@ -51,6 +61,41 @@ describe("SQL Server legacy compatibility", () => {
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("preserves unrelated params while migrating the historical compatibility flag", () => {
|
||||
const config = connectionConfig("applicationName=dbx;sqlserverEncryption=off;encrypt=false");
|
||||
|
||||
migrateSqlServerLegacyCompatibilityConfig(config);
|
||||
|
||||
expect(config.driver_profile).toBe("sqlserver-legacy");
|
||||
expect(config.url_params).toBe("applicationName=dbx;encrypt=false");
|
||||
});
|
||||
|
||||
it("preserves semicolons and special characters inside braced values during migration", () => {
|
||||
const config = connectionConfig("applicationName={DBX; Client};password=50%;sqlserverEncryption=disabled;encrypt=false");
|
||||
|
||||
migrateSqlServerLegacyCompatibilityConfig(config);
|
||||
|
||||
expect(config.driver_profile).toBe("sqlserver-legacy");
|
||||
expect(config.url_params).toBe("applicationName={DBX; Client};password=50%;encrypt=false");
|
||||
});
|
||||
|
||||
it("keeps escaped closing braces from exposing separators inside braced values", () => {
|
||||
const config = connectionConfig("applicationName={DBX}}; Client};sqlserverEncryption=disabled;encrypt=false");
|
||||
|
||||
migrateSqlServerLegacyCompatibilityConfig(config);
|
||||
|
||||
expect(config.url_params).toBe("applicationName={DBX}}; Client};encrypt=false");
|
||||
});
|
||||
|
||||
it("keeps generic JDBC encrypt=false on the native driver", () => {
|
||||
const config = connectionConfig("applicationName=dbx;encrypt=false");
|
||||
|
||||
migrateSqlServerLegacyCompatibilityConfig(config);
|
||||
|
||||
expect(config.driver_profile).toBe("sqlserver");
|
||||
expect(config.url_params).toBe("applicationName=dbx;encrypt=false");
|
||||
});
|
||||
|
||||
it("treats a persisted legacy driver profile as compatibility mode", () => {
|
||||
const config = connectionConfig("");
|
||||
config.driver_profile = "sqlserver-legacy";
|
||||
|
||||
@@ -31,6 +31,62 @@ export function setSqlServerNativeEncryptionDisabled(params: string | undefined,
|
||||
return parsed.toString();
|
||||
}
|
||||
|
||||
interface SqlServerJdbcParamPart {
|
||||
separator: "" | "&" | ";";
|
||||
value: string;
|
||||
}
|
||||
|
||||
function splitSqlServerJdbcParams(params: string | undefined): SqlServerJdbcParamPart[] {
|
||||
const source = (params || "").trim().replace(/^\?/, "");
|
||||
const parts: SqlServerJdbcParamPart[] = [];
|
||||
let separator: SqlServerJdbcParamPart["separator"] = "";
|
||||
let start = 0;
|
||||
let inBraces = false;
|
||||
|
||||
// SQL Server JDBC values use braces to contain separators and `}}` to escape a closing brace.
|
||||
for (let index = 0; index < source.length; index += 1) {
|
||||
const char = source[index];
|
||||
if (inBraces) {
|
||||
if (char === "}" && source[index + 1] === "}") {
|
||||
index += 1;
|
||||
} else if (char === "}") {
|
||||
inBraces = false;
|
||||
}
|
||||
} else if (char === "{") {
|
||||
inBraces = true;
|
||||
} else if (char === "&" || char === ";") {
|
||||
parts.push({ separator, value: source.slice(start, index) });
|
||||
separator = char;
|
||||
start = index + 1;
|
||||
}
|
||||
}
|
||||
|
||||
parts.push({ separator, value: source.slice(start) });
|
||||
return parts;
|
||||
}
|
||||
|
||||
function isSqlServerLegacyCompatibilityPart(part: string): boolean {
|
||||
const separatorIndex = part.indexOf("=");
|
||||
if (separatorIndex < 0) return false;
|
||||
const key = part.slice(0, separatorIndex).trim().toLowerCase();
|
||||
const value = part
|
||||
.slice(separatorIndex + 1)
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
return key === "sqlserverencryption" && SQLSERVER_ENCRYPTION_DISABLED_VALUES.has(value);
|
||||
}
|
||||
|
||||
function isSqlServerLegacyCompatibilitySetting(params: string | undefined): boolean {
|
||||
return splitSqlServerJdbcParams(params).some((part) => isSqlServerLegacyCompatibilityPart(part.value));
|
||||
}
|
||||
|
||||
function removeSqlServerLegacyCompatibilitySetting(params: string | undefined): string {
|
||||
return splitSqlServerJdbcParams(params)
|
||||
.filter((part) => part.value.trim() && !isSqlServerLegacyCompatibilityPart(part.value))
|
||||
.map((part, index) => `${index === 0 ? "" : part.separator}${part.value}`)
|
||||
.join("");
|
||||
}
|
||||
|
||||
export function sqlServerUsesLegacyCompatibility(config: Pick<ConnectionConfig, "db_type" | "driver_profile">): boolean {
|
||||
return config.db_type === "sqlserver" && config.driver_profile === SQLSERVER_LEGACY_COMPATIBILITY_DRIVER_KEY;
|
||||
}
|
||||
@@ -40,6 +96,12 @@ export function setSqlServerLegacyCompatibilityConfig(config: Pick<ConnectionCon
|
||||
config.driver_label = enabled ? SQLSERVER_LEGACY_COMPATIBILITY_DRIVER_LABEL : SQLSERVER_NATIVE_DRIVER_LABEL;
|
||||
}
|
||||
|
||||
export function migrateSqlServerLegacyCompatibilityConfig(config: Pick<ConnectionConfig, "db_type" | "driver_label" | "driver_profile" | "url_params">): void {
|
||||
if (config.db_type !== "sqlserver" || !isSqlServerLegacyCompatibilitySetting(config.url_params)) return;
|
||||
setSqlServerLegacyCompatibilityConfig(config, true);
|
||||
config.url_params = removeSqlServerLegacyCompatibilitySetting(config.url_params);
|
||||
}
|
||||
|
||||
export function requiresSqlServerLegacyCompatibilityComponent(config: ConnectionConfig): boolean {
|
||||
return sqlServerUsesLegacyCompatibility(config);
|
||||
}
|
||||
Reference in new issue
Block a user