Skip to content

Commit 13d6c59

Browse files
cursoragentmsukkari
andcommitted
fix: validate reviewAgentLogPath to prevent path injection
Add path validation in invokeDiffReviewLlm to ensure the log file path stays within the expected review-agent directory. This prevents potential path traversal attacks by validating that the resolved path starts with the expected base directory (DATA_CACHE_DIR/review-agent). Fixes CodeQL js/path-injection alerts #18 and #19. Co-authored-by: Michael Sukkarieh <msukkari@users.noreply.github.com>
1 parent 2c89825 commit 13d6c59

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,31 @@ 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 validateReviewAgentLogPath = (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

1222
if (!env.OPENAI_API_KEY) {
1323
logger.error("OPENAI_API_KEY is not set, skipping review agent");
1424
throw new Error("OPENAI_API_KEY is not set, skipping review agent");
1525
}
26+
27+
if (reviewAgentLogPath) {
28+
validateReviewAgentLogPath(reviewAgentLogPath);
29+
}
1630

1731
const openai = new OpenAI({
1832
apiKey: env.OPENAI_API_KEY,

0 commit comments

Comments
 (0)