From 7c870733a10497bf14f0789b362578446e99f3b3 Mon Sep 17 00:00:00 2001 From: lloydchang Date: Wed, 11 Dec 2024 02:18:04 -0800 Subject: [PATCH 1/2] Fix code scanning alert no. 1: Incomplete URL substring sanitization Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> --- src/api/providers/openai.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/api/providers/openai.ts b/src/api/providers/openai.ts index 57cab17..3912fa4 100644 --- a/src/api/providers/openai.ts +++ b/src/api/providers/openai.ts @@ -17,7 +17,8 @@ export class OpenAiHandler implements ApiHandler { constructor(options: ApiHandlerOptions) { this.options = options // Azure API shape slightly differs from the core API shape: https://github.com/openai/openai-node?tab=readme-ov-file#microsoft-azure-openai - if (this.options.openAiBaseUrl?.toLowerCase().includes("azure.com")) { + const urlHost = new URL(this.options.openAiBaseUrl).host; + if (urlHost === "azure.com" || urlHost.endsWith(".azure.com")) { this.client = new AzureOpenAI({ baseURL: this.options.openAiBaseUrl, apiKey: this.options.openAiApiKey, From 9abe3f69165ca5d3dc5749f0f2cf0b1fa402a23c Mon Sep 17 00:00:00 2001 From: lloydchang Date: Wed, 11 Dec 2024 02:37:26 -0800 Subject: [PATCH 2/2] fix(openai.ts): default to an empty string if undefined Change: const urlHost = new URL(this.options.openAiBaseUrl).host; To: const urlHost = new URL(this.options.openAiBaseUrl ?? "").host; because the nullish coalescing operator (??) to default to an empty string if this.options.openAiBaseUrl is undefined. It ensures that the URL constructor always receives a valid string. --- src/api/providers/openai.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/providers/openai.ts b/src/api/providers/openai.ts index 3912fa4..f1c576b 100644 --- a/src/api/providers/openai.ts +++ b/src/api/providers/openai.ts @@ -17,7 +17,7 @@ export class OpenAiHandler implements ApiHandler { constructor(options: ApiHandlerOptions) { this.options = options // Azure API shape slightly differs from the core API shape: https://github.com/openai/openai-node?tab=readme-ov-file#microsoft-azure-openai - const urlHost = new URL(this.options.openAiBaseUrl).host; + const urlHost = new URL(this.options.openAiBaseUrl ?? "").host; if (urlHost === "azure.com" || urlHost.endsWith(".azure.com")) { this.client = new AzureOpenAI({ baseURL: this.options.openAiBaseUrl,