diff --git a/CHANGELOG.md b/CHANGELOG.md index da660ca..db0e25a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - process.env can be undefined +- Headers are now escaped better ## [5.2.3] - 2023-11-07 diff --git a/packages/azure-kusto-data/src/clientDetails.ts b/packages/azure-kusto-data/src/clientDetails.ts index fcb345d..c99a6e7 100644 --- a/packages/azure-kusto-data/src/clientDetails.ts +++ b/packages/azure-kusto-data/src/clientDetails.ts @@ -14,8 +14,8 @@ declare namespace NodeJS { } } -// REPLACE_REGEX = re.compile(r"[\r\n\s{}|]+") -const ReplaceRegex = /[\r\n\s{}|]+/g; +// This regex allows all printable ascii, except spaces and chars we use in the format +const ReplaceRegex = /[^\x21-\x7A]+/g; const None = "[none]"; export class ClientDetails { @@ -66,8 +66,9 @@ export class ClientDetails { ]); } - static escapeHeader(header: string): string { - return `{${header.replace(ReplaceRegex, "_")}}`; + static escapeHeader(header: string, wrapInBrackets: boolean = true): string { + const clean = header.replace(ReplaceRegex, "_"); + return wrapInBrackets ? `{${clean}}` : clean; } static formatHeader(args: [string, string][]): string { @@ -86,7 +87,7 @@ export class ClientDetails { override_user: string | null = null, additional_fields: [string, string][] | null = null ): ClientDetails { - const params: [string, string][] = [["Kusto." + name, version]]; + const params: [string, string][] = [["Kusto." + this.escapeHeader(name, false), version]]; app_name = app_name || this.defaultApplication(); app_version = app_version || None; diff --git a/packages/azure-kusto-data/test/headersTest.ts b/packages/azure-kusto-data/test/headersTest.ts index 6adaa88..8ebeacf 100644 --- a/packages/azure-kusto-data/test/headersTest.ts +++ b/packages/azure-kusto-data/test/headersTest.ts @@ -51,4 +51,12 @@ describe("Test Headers", () => { assert.strictEqual(headers["x-ms-app"], "Kusto.test:{1.0}|App.{testApp}:{1.0}"); assert.notStrictEqual(headers["x-ms-user"], "[none]"); }); + + it.concurrent("Should remove unwanted characters", () => { + const clientDetails = ClientDetails.setConnectorDetails("Café", "1 . 0", "my|test{}app", "1.0", true, null, null); + const headers = clientDetails.getHeaders(); + assert.strictEqual(headers["x-ms-client-version"]?.startsWith("Kusto.JavaScript.Client:"), true); + assert.strictEqual(headers["x-ms-app"], "Kusto.Caf_:{1_._0}|App.{my_test_app}:{1.0}"); + assert.notStrictEqual(headers["x-ms-user"], "[none]"); + }); });