Skip to content

Commit 61affd7

Browse files
cursoragentmsukkari
andcommitted
fix: validate reviewAgentLogPath to prevent path injection
Add path validation to invokeDiffReviewLlm to ensure the log path stays within the expected DATA_CACHE_DIR/review-agent directory. This addresses CodeQL alert #19 (js/path-injection) by resolving the path and verifying it does not escape the log directory. The validation is performed before each fs.appendFileSync call to prevent path traversal attacks even if the call chain changes in the future. Co-authored-by: Michael Sukkarieh <msukkari@users.noreply.github.com>
1 parent 2c89825 commit 61affd7

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

packages/web/src/features/agents/review-agent/nodes/invokeDiffReviewLlm.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@ import OpenAI from "openai";
22
import { sourcebot_file_diff_review, sourcebot_file_diff_review_schema } from "@/features/agents/review-agent/types";
33
import { env } from "@sourcebot/shared";
44
import fs from "fs";
5+
import path from "path";
56
import { createLogger } from "@sourcebot/shared";
67

78
const logger = createLogger('invoke-diff-review-llm');
89

10+
const REVIEW_AGENT_LOG_BASE = path.join(env.DATA_CACHE_DIR, 'review-agent');
11+
12+
const validateLogPath = (logPath: string): void => {
13+
const resolved = path.resolve(logPath);
14+
if (!resolved.startsWith(REVIEW_AGENT_LOG_BASE + path.sep)) {
15+
throw new Error('reviewAgentLogPath escapes log directory');
16+
}
17+
};
18+
919
export const invokeDiffReviewLlm = async (reviewAgentLogPath: string | undefined, prompt: string): Promise<sourcebot_file_diff_review> => {
1020
logger.debug("Executing invoke_diff_review_llm");
1121

@@ -19,6 +29,7 @@ export const invokeDiffReviewLlm = async (reviewAgentLogPath: string | undefined
1929
});
2030

2131
if (reviewAgentLogPath) {
32+
validateLogPath(reviewAgentLogPath);
2233
fs.appendFileSync(reviewAgentLogPath, `\n\nPrompt:\n${prompt}`);
2334
}
2435

@@ -32,6 +43,7 @@ export const invokeDiffReviewLlm = async (reviewAgentLogPath: string | undefined
3243

3344
const openaiResponse = completion.choices[0].message.content;
3445
if (reviewAgentLogPath) {
46+
validateLogPath(reviewAgentLogPath);
3547
fs.appendFileSync(reviewAgentLogPath, `\n\nResponse:\n${openaiResponse}`);
3648
}
3749

0 commit comments

Comments
 (0)