Skip to content

Fix code scanning alert no. 2: Incomplete URL substring sanitization #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/ai/openaiProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { configuration } from '../system/vscode/configuration';
import type { AIModel } from './aiProviderService';
import { OpenAICompatibleProvider } from './openAICompatibleProvider';
import * as urlLib from 'url';

const provider = { id: 'openai', name: 'OpenAI' } as const;

Expand Down Expand Up @@ -183,7 +184,8 @@ export class OpenAIProvider extends OpenAICompatibleProvider<typeof provider.id>
url: string,
apiKey: string,
): Record<string, string> {
if (url.includes('.azure.com')) {
const parsedUrl = urlLib.parse(url);
if (this.isAllowedHost(parsedUrl.host)) {
return {
Accept: 'application/json',
'Content-Type': 'application/json',
Expand All @@ -193,4 +195,16 @@ export class OpenAIProvider extends OpenAICompatibleProvider<typeof provider.id>

return super.getHeaders(model, url, apiKey);
}

private isAllowedHost(host: string | null): boolean {
if (!host) return false;
const allowedHosts = [
'azure.com',
'*.azure.com'
];
return allowedHosts.some(allowedHost => {
const regex = new RegExp(`^${allowedHost.replace('*.', '.*\\.')}$`);
return regex.test(host);
});
}
}