Merge pull request #3785 from gitbutlerapp/fix-ollama-request-security

Fix ollama request security
This commit is contained in:
Kiril Videlov 2024-05-18 18:08:39 +02:00 committed by GitHub
commit 07b863731f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 8 deletions

View File

@ -1,6 +1,7 @@
import { LONG_DEFAULT_BRANCH_TEMPLATE, LONG_DEFAULT_COMMIT_TEMPLATE } from '$lib/ai/prompts';
import { MessageRole, type PromptMessage, type AIClient } from '$lib/ai/types';
import { isNonEmptyObject } from '$lib/utils/typeguards';
import { fetch, Body, Response } from '@tauri-apps/api/http';
export const DEFAULT_OLLAMA_ENDPOINT = 'http://127.0.0.1:11434';
export const DEFAULT_OLLAMA_MODEL_NAME = 'llama3';
@ -123,14 +124,15 @@ ${JSON.stringify(OLLAMA_CHAT_MESSAGE_FORMAT_SCHEMA, null, 2)}`
* @param request - The OllamaChatRequest object containing the request details.
* @returns A Promise that resolves to the Response object.
*/
private async fetchChat(request: OllamaChatRequest): Promise<Response> {
private async fetchChat(request: OllamaChatRequest): Promise<Response<any>> {
const url = new URL(OllamaAPEndpoint.Chat, this.endpoint);
const result = await fetch(url, {
const body = Body.json(request);
const result = await fetch(url.toString(), {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(request)
body
});
return result;
}
@ -155,11 +157,10 @@ ${JSON.stringify(OLLAMA_CHAT_MESSAGE_FORMAT_SCHEMA, null, 2)}`
format: 'json'
});
const json = await result.json();
if (!isOllamaChatResponse(json)) {
throw new Error('Invalid response\n' + JSON.stringify(json));
if (!isOllamaChatResponse(result.data)) {
throw new Error('Invalid response\n' + JSON.stringify(result.data));
}
return json;
return result.data;
}
}

View File

@ -42,7 +42,12 @@
"http": {
"all": true,
"request": true,
"scope": [" https://api.anthropic.com/v1/messages"]
"scope": [
"https://api.anthropic.com/v1/messages",
"http://127.0.0.1:11434/api/chat",
"http://127.0.0.1:11434/api/generate",
"http://127.0.0.1:11434/api/embeddings"
]
}
},
"bundle": {