Files
oh-my-opencode/src/agents/builtin-agents/resolve-file-uri.ts
T
YeonGyu-Kim 98659783c0 fix(security): confine file resolution to project roots
Block traversal, out-of-root absolute path, and symlink escapes for @file references, file:// URIs, and config skill file loading while logging rejected attempts.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-04-02 14:55:35 +09:00

43 lines
1.3 KiB
TypeScript

import { existsSync, readFileSync } from "node:fs"
import { homedir } from "node:os"
import { isAbsolute, resolve } from "node:path"
import { isWithinProject } from "../../shared/contains-path"
import { log } from "../../shared/logger"
export function resolvePromptAppend(promptAppend: string, configDir?: string): string {
if (!promptAppend.startsWith("file://")) return promptAppend
const encoded = promptAppend.slice(7)
let filePath: string
try {
const decoded = decodeURIComponent(encoded)
const expanded = decoded.startsWith("~/") ? decoded.replace(/^~\//, `${homedir()}/`) : decoded
filePath = isAbsolute(expanded)
? expanded
: resolve(configDir ?? process.cwd(), expanded)
} catch {
return `[WARNING: Malformed file URI (invalid percent-encoding): ${promptAppend}]`
}
const projectRoot = configDir ?? process.cwd()
if (!isWithinProject(filePath, projectRoot)) {
log("[resolve-file-uri] Rejected file URI outside project root", {
promptAppend,
filePath,
projectRoot,
})
return `[WARNING: Path rejected: ${promptAppend}]`
}
if (!existsSync(filePath)) {
return `[WARNING: Could not resolve file URI: ${promptAppend}]`
}
try {
return readFileSync(filePath, "utf8")
} catch {
return `[WARNING: Could not read file: ${promptAppend}]`
}
}