Forked from harshallmstudio/document-streamer
Forked from harshallmstudio/document-streamer
src / pathGuard.ts
import { realpath, stat } from "node:fs/promises";
import path from "node:path";
function containsPath(root: string, candidate: string): boolean {
const relative = path.relative(root, candidate);
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
}
/**
* Resolve symlinks and require the document to be inside the working directory or
* an explicitly configured allowed directory.
*/
export async function resolveReadableDocument(
requestedPath: string,
workingDirectory: string | undefined,
additionalDirectories: string[],
): Promise<{ filePath: string; size: number }> {
if (!requestedPath.trim()) {
throw new Error("Provide an absolute document path, or a path relative to the working directory.");
}
const roots = [workingDirectory, ...additionalDirectories]
.filter((directory): directory is string => Boolean(directory && directory.trim()))
.map((directory) => path.resolve(directory.trim()));
if (roots.length === 0) {
throw new Error(
"No readable directory is configured. Set the chat working directory or add an allowed directory.",
);
}
const canonicalRoots: string[] = [];
for (const root of roots) {
try {
canonicalRoots.push(await realpath(root));
} catch {
// A configured but missing directory is skipped; existing roots remain usable.
}
}
const firstRoot = canonicalRoots[0];
if (!firstRoot) {
throw new Error("None of the configured readable directories exists.");
}
const candidate = path.isAbsolute(requestedPath)
? path.resolve(requestedPath)
: path.resolve(firstRoot, requestedPath);
const filePath = await realpath(candidate);
if (!canonicalRoots.some((root) => containsPath(root, filePath))) {
throw new Error(
`Access denied: ${requestedPath} is outside the configured readable directories.`,
);
}
const information = await stat(filePath);
if (!information.isFile()) {
throw new Error(`Not a regular file: ${requestedPath}`);
}
return { filePath: filePath, size: information.size };
}
src / pathGuard.ts
import { realpath, stat } from "node:fs/promises";
import path from "node:path";
function containsPath(root: string, candidate: string): boolean {
const relative = path.relative(root, candidate);
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
}
/**
* Resolve symlinks and require the document to be inside the working directory or
* an explicitly configured allowed directory.
*/
export async function resolveReadableDocument(
requestedPath: string,
workingDirectory: string | undefined,
additionalDirectories: string[],
): Promise<{ filePath: string; size: number }> {
if (!requestedPath.trim()) {
throw new Error("Provide an absolute document path, or a path relative to the working directory.");
}
const roots = [workingDirectory, ...additionalDirectories]
.filter((directory): directory is string => Boolean(directory && directory.trim()))
.map((directory) => path.resolve(directory.trim()));
if (roots.length === 0) {
throw new Error(
"No readable directory is configured. Set the chat working directory or add an allowed directory.",
);
}
const canonicalRoots: string[] = [];
for (const root of roots) {
try {
canonicalRoots.push(await realpath(root));
} catch {
// A configured but missing directory is skipped; existing roots remain usable.
}
}
const firstRoot = canonicalRoots[0];
if (!firstRoot) {
throw new Error("None of the configured readable directories exists.");
}
const candidate = path.isAbsolute(requestedPath)
? path.resolve(requestedPath)
: path.resolve(firstRoot, requestedPath);
const filePath = await realpath(candidate);
if (!canonicalRoots.some((root) => containsPath(root, filePath))) {
throw new Error(
`Access denied: ${requestedPath} is outside the configured readable directories.`,
);
}
const information = await stat(filePath);
if (!information.isFile()) {
throw new Error(`Not a regular file: ${requestedPath}`);
}
return { filePath: filePath, size: information.size };
}