Skip to content

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

fix(dynamic-secret): check SAP ASE connection values before connecting

Vlad Ligaia8374d36be77
diff --git a/backend/src/ee/services/dynamic-secret/providers/sap-ase.test.ts b/backend/src/ee/services/dynamic-secret/providers/sap-ase.test.ts
index 8fa2871823..366cbb425c 100644
--- a/backend/src/ee/services/dynamic-secret/providers/sap-ase.test.ts
+++ b/backend/src/ee/services/dynamic-secret/providers/sap-ase.test.ts
@@ -10,16 +10,19 @@ const mockedConnect = vi.mocked(odbc.connect);
const metadata = { projectId: "proj-1" };
-const inputs = (password: string) => ({
+const inputs = (overrides: { password?: string; database?: string }) => ({
host: "db.example.com",
port: 5000,
database: "app",
username: "sa",
- password,
+ password: "plain",
creationStatement: "sp_addlogin '{{username}}', '{{password}}'",
- revocationStatement: "sp_droplogin '{{username}}'"
+ revocationStatement: "sp_droplogin '{{username}}'",
+ ...overrides
});
+const connectionStrings = () => mockedConnect.mock.calls.map(([value]) => value as unknown as string);
+
describe("SapAseProvider connection string", () => {
beforeEach(() => {
mockedConnect.mockReset();
@@ -29,15 +32,28 @@ describe("SapAseProvider connection string", () => {
} as unknown as Awaited<ReturnType<typeof odbc.connect>>);
});
- test("wraps a value containing ';' in braces and leaves plain values as they are", async () => {
- await SapAseProvider().validateConnection(inputs("pa;ss"), metadata);
+ test.each([
+ ["plain", "PWD=plain;"],
+ ["pa;ss", "PWD={pa;ss};"],
+ ["{abc", "PWD={{abc};"],
+ ["{pa;ss}", "PWD={pa;ss};"]
+ ])("formats password %s as %s", async (password, expected) => {
+ await SapAseProvider().validateConnection(inputs({ password }), metadata);
+
+ expect(connectionStrings()[0]).toContain(`UID=sa;${expected}`);
+ });
+
+ test("braces the database on the non-master connection", async () => {
+ await SapAseProvider().validateConnection(inputs({ database: "a;b" }), metadata);
- const connectionString = mockedConnect.mock.calls[0][0] as unknown as string;
- expect(connectionString).toContain("UID=sa;PWD={pa;ss};");
+ expect(connectionStrings()[1]).toContain("DATABASE={a;b};");
});
- test("refuses a value containing both ';' and '}'", async () => {
- await expect(SapAseProvider().validateConnection(inputs("pa;ss}"), metadata)).rejects.toThrow('cannot contain "}"');
+ test.each([
+ ["pa;ss}", 'SAP ASE password cannot contain "}"'],
+ ["pa\0ss", "SAP ASE password cannot contain a null character"]
+ ])("refuses password %j before connecting", async (password, message) => {
+ await expect(SapAseProvider().validateConnection(inputs({ password }), metadata)).rejects.toThrow(message);
expect(mockedConnect).not.toHaveBeenCalled();
});
});
diff --git a/backend/src/ee/services/dynamic-secret/providers/sap-ase.ts b/backend/src/ee/services/dynamic-secret/providers/sap-ase.ts
index e45d12146d..895fa9ffae 100644
--- a/backend/src/ee/services/dynamic-secret/providers/sap-ase.ts
+++ b/backend/src/ee/services/dynamic-secret/providers/sap-ase.ts
@@ -25,7 +25,11 @@ enum SapCommands {
// values with ";" or a leading "{" go in braces; unixODBC and FreeTDS disagree on escaping "}" inside them
const formatConnectionValue = (field: string, value: string) => {
- if (!value.includes(";") && !value.startsWith("{")) return value;
+ if (value.includes("\0")) {
+ throw new BadRequestError({ message: `SAP ASE ${field} cannot contain a null character` });
+ }
+ const isAlreadyBraced = value.startsWith("{") && value.indexOf("}") === value.length - 1;
+ if (isAlreadyBraced || (!value.includes(";") && !value.startsWith("{"))) return value;
if (!value.includes("}")) return `{${value}}`;
throw new BadRequestError({
message: `SAP ASE ${field} cannot contain "}" together with ";" or a leading "{"`
@@ -35,6 +39,10 @@ const formatConnectionValue = (field: string, value: string) => {
export const SapAseProvider = (): TDynamicProviderFns => {
const validateProviderInputs = async (inputs: unknown) => {
const providerInputs = await DynamicSecretSapAseSchema.parseAsync(inputs);
+ formatConnectionValue("host", providerInputs.host);
+ formatConnectionValue("database", providerInputs.database);
+ formatConnectionValue("username", providerInputs.username);
+ formatConnectionValue("password", providerInputs.password);
await verifyHostInputValidity({ host: providerInputs.host, isDynamicSecret: true });
validateHandlebarTemplate("SAP ASE creation", providerInputs.creationStatement, {