@@ -3,10 +3,23 @@ import { getAISDKLanguageModelAndOptions, getConfiguredLanguageModels } from "@/
33import { env } from "@sourcebot/shared" ;
44import { generateText } from "ai" ;
55import fs from "fs" ;
6+ import path from "path" ;
67import { createLogger } from "@sourcebot/shared" ;
78
89const logger = createLogger ( 'invoke-diff-review-llm' ) ;
910
11+ export const getReviewAgentLogDir = ( ) : string => {
12+ return path . join ( env . DATA_CACHE_DIR , 'review-agent' ) ;
13+ } ;
14+
15+ const validateLogPath = ( logPath : string ) : void => {
16+ const resolved = path . resolve ( logPath ) ;
17+ const logDir = getReviewAgentLogDir ( ) ;
18+ if ( ! resolved . startsWith ( logDir + path . sep ) ) {
19+ throw new Error ( 'reviewAgentLogPath escapes log directory' ) ;
20+ }
21+ } ;
22+
1023export const invokeDiffReviewLlm = async ( reviewAgentLogPath : string | undefined , prompt : string ) : Promise < sourcebot_file_diff_review > => {
1124 logger . debug ( "Executing invoke_diff_review_llm" ) ;
1225
@@ -28,6 +41,7 @@ export const invokeDiffReviewLlm = async (reviewAgentLogPath: string | undefined
2841 const { model, providerOptions, temperature } = await getAISDKLanguageModelAndOptions ( selectedModel ) ;
2942
3043 if ( reviewAgentLogPath ) {
44+ validateLogPath ( reviewAgentLogPath ) ;
3145 fs . appendFileSync ( reviewAgentLogPath , `\n\nPrompt:\n${ prompt } ` ) ;
3246 }
3347
@@ -42,6 +56,7 @@ export const invokeDiffReviewLlm = async (reviewAgentLogPath: string | undefined
4256
4357 const responseText = result . text ;
4458 if ( reviewAgentLogPath ) {
59+ validateLogPath ( reviewAgentLogPath ) ;
4560 fs . appendFileSync ( reviewAgentLogPath , `\n\nResponse:\n${ responseText } ` ) ;
4661 }
4762
0 commit comments