feat(worker): home-scoped box file tree/read/write with realpath containment
This commit is contained in:
@@ -21,13 +21,15 @@ const realpathOrDeepestExisting = async (target: string): Promise<string> => {
|
||||
export const assertContainedRealPath = async (
|
||||
root: string,
|
||||
requested: string,
|
||||
opts: { forWrite?: boolean } = {},
|
||||
opts: { forWrite?: boolean; label?: string } = {},
|
||||
): Promise<string> => {
|
||||
const lexical = path.resolve(root, requested);
|
||||
const realRoot = await realpath(root);
|
||||
const resolved = await realpathOrDeepestExisting(lexical);
|
||||
if (resolved !== realRoot && !resolved.startsWith(`${realRoot}${path.sep}`)) {
|
||||
throw new Error(`Refusing to access path outside root: ${requested}`);
|
||||
throw new Error(
|
||||
`Refusing to access path outside ${opts.label ?? 'root'}: ${requested}`,
|
||||
);
|
||||
}
|
||||
if (opts.forWrite) {
|
||||
const info = await lstat(lexical).catch(() => null);
|
||||
|
||||
Reference in New Issue
Block a user