moving
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { lookup } from '../utils/mime.js';
|
||||
import { safePath, toRelativePath } from './security.js';
|
||||
import { resolveSafePath, safePath, toRelativePath, type SafePathOptions } from './security.js';
|
||||
import type { StorageProvider, FileEntry } from './types.js';
|
||||
import { STANDARD_DIRS } from './types.js';
|
||||
|
||||
@@ -11,7 +11,50 @@ import { STANDARD_DIRS } from './types.js';
|
||||
* and "local" storage (data at a user-specified path).
|
||||
*/
|
||||
export class FsStorageProvider implements StorageProvider {
|
||||
constructor(private readonly root: string) {}
|
||||
private readonly pathOptions: SafePathOptions;
|
||||
|
||||
constructor(private readonly root: string, options: SafePathOptions = {}) {
|
||||
this.pathOptions = { denySensitive: options.denySensitive === true };
|
||||
}
|
||||
|
||||
private resolve(userPath: string): string {
|
||||
return safePath(this.root, userPath, this.pathOptions);
|
||||
}
|
||||
|
||||
private resolveForOperation(userPath: string) {
|
||||
return resolveSafePath(this.root, userPath, this.pathOptions);
|
||||
}
|
||||
|
||||
/** Canonicalize the parent but preserve replacement semantics for an existing leaf link. */
|
||||
private resolveEntryDestination(userPath: string) {
|
||||
const { lexicalPath } = this.resolveForOperation(userPath);
|
||||
const relativeParent = path.relative(this.root, path.dirname(lexicalPath));
|
||||
const { operationPath: operationParent } = this.resolveForOperation(relativeParent);
|
||||
return {
|
||||
lexicalPath,
|
||||
operationPath: path.join(operationParent, path.basename(lexicalPath)),
|
||||
};
|
||||
}
|
||||
|
||||
/** Validate every descendant before a recursive filesystem operation. */
|
||||
private assertTreeSafe(start: string): void {
|
||||
const pending = [start];
|
||||
const visited = new Set<string>();
|
||||
|
||||
while (pending.length > 0) {
|
||||
const current = pending.pop()!;
|
||||
const realCurrent = fs.realpathSync(current);
|
||||
if (visited.has(realCurrent)) continue;
|
||||
visited.add(realCurrent);
|
||||
|
||||
if (!fs.statSync(current).isDirectory()) continue;
|
||||
for (const entry of fs.readdirSync(current, { withFileTypes: true })) {
|
||||
const child = path.join(current, entry.name);
|
||||
const resolvedChild = this.resolve(path.relative(this.root, child));
|
||||
if (fs.statSync(resolvedChild).isDirectory()) pending.push(resolvedChild);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Ensure the root and standard directories exist */
|
||||
ensureStructure(): void {
|
||||
@@ -22,7 +65,7 @@ export class FsStorageProvider implements StorageProvider {
|
||||
}
|
||||
|
||||
async list(dirPath: string): Promise<FileEntry[]> {
|
||||
const resolved = dirPath === '/' || dirPath === '' ? this.root : safePath(this.root, dirPath);
|
||||
const resolved = this.resolve(dirPath === '/' || dirPath === '' ? '' : dirPath);
|
||||
|
||||
if (!fs.existsSync(resolved)) return [];
|
||||
|
||||
@@ -30,8 +73,9 @@ export class FsStorageProvider implements StorageProvider {
|
||||
const result: FileEntry[] = [];
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(resolved, entry.name);
|
||||
try {
|
||||
const relativePath = path.relative(this.root, path.join(resolved, entry.name));
|
||||
const fullPath = this.resolve(relativePath);
|
||||
const stat = fs.statSync(fullPath);
|
||||
const relPath = toRelativePath(this.root, fullPath);
|
||||
|
||||
@@ -68,22 +112,22 @@ export class FsStorageProvider implements StorageProvider {
|
||||
}
|
||||
|
||||
async read(filePath: string): Promise<Buffer> {
|
||||
const resolved = safePath(this.root, filePath);
|
||||
const resolved = this.resolve(filePath);
|
||||
if (!fs.existsSync(resolved)) throw new Error(`File not found: ${filePath}`);
|
||||
return fs.readFileSync(resolved);
|
||||
}
|
||||
|
||||
async write(filePath: string, data: Buffer, _mime?: string): Promise<FileEntry> {
|
||||
const resolved = safePath(this.root, filePath);
|
||||
const dir = path.dirname(resolved);
|
||||
const { lexicalPath, operationPath } = this.resolveForOperation(filePath);
|
||||
const dir = path.dirname(operationPath);
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
fs.writeFileSync(resolved, data);
|
||||
fs.writeFileSync(operationPath, data);
|
||||
|
||||
const stat = fs.statSync(resolved);
|
||||
const name = path.basename(resolved);
|
||||
const stat = fs.statSync(operationPath);
|
||||
const name = path.basename(lexicalPath);
|
||||
return {
|
||||
name,
|
||||
path: toRelativePath(this.root, resolved),
|
||||
path: toRelativePath(this.root, lexicalPath),
|
||||
type: 'file',
|
||||
size: stat.size,
|
||||
mimeType: lookup(name),
|
||||
@@ -93,21 +137,27 @@ export class FsStorageProvider implements StorageProvider {
|
||||
}
|
||||
|
||||
async delete(targetPath: string): Promise<void> {
|
||||
const resolved = safePath(this.root, targetPath);
|
||||
const resolved = this.resolve(targetPath);
|
||||
if (!fs.existsSync(resolved)) return;
|
||||
if (this.pathOptions.denySensitive && fs.statSync(resolved).isDirectory()) {
|
||||
this.assertTreeSafe(resolved);
|
||||
}
|
||||
fs.rmSync(resolved, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
async move(from: string, to: string): Promise<FileEntry> {
|
||||
const resolvedFrom = safePath(this.root, from);
|
||||
const resolvedTo = safePath(this.root, to);
|
||||
const resolvedFrom = this.resolve(from);
|
||||
const { lexicalPath: resolvedTo, operationPath: operationTo } = this.resolveEntryDestination(to);
|
||||
|
||||
if (!fs.existsSync(resolvedFrom)) throw new Error(`Source not found: ${from}`);
|
||||
if (this.pathOptions.denySensitive && fs.statSync(resolvedFrom).isDirectory()) {
|
||||
this.assertTreeSafe(resolvedFrom);
|
||||
}
|
||||
|
||||
fs.mkdirSync(path.dirname(resolvedTo), { recursive: true });
|
||||
fs.renameSync(resolvedFrom, resolvedTo);
|
||||
fs.mkdirSync(path.dirname(operationTo), { recursive: true });
|
||||
fs.renameSync(resolvedFrom, operationTo);
|
||||
|
||||
const stat = fs.statSync(resolvedTo);
|
||||
const stat = fs.statSync(operationTo);
|
||||
const name = path.basename(resolvedTo);
|
||||
return {
|
||||
name,
|
||||
@@ -121,15 +171,17 @@ export class FsStorageProvider implements StorageProvider {
|
||||
}
|
||||
|
||||
async copy(from: string, to: string): Promise<FileEntry> {
|
||||
const resolvedFrom = safePath(this.root, from);
|
||||
const resolvedTo = safePath(this.root, to);
|
||||
const resolvedFrom = this.resolve(from);
|
||||
const { lexicalPath: resolvedTo, operationPath: operationTo } = this.resolveEntryDestination(to);
|
||||
|
||||
if (!fs.existsSync(resolvedFrom)) throw new Error(`Source not found: ${from}`);
|
||||
this.assertTreeSafe(resolvedFrom);
|
||||
if (fs.existsSync(resolvedTo)) this.assertTreeSafe(resolvedTo);
|
||||
|
||||
fs.mkdirSync(path.dirname(resolvedTo), { recursive: true });
|
||||
fs.cpSync(resolvedFrom, resolvedTo, { recursive: true });
|
||||
fs.mkdirSync(path.dirname(operationTo), { recursive: true });
|
||||
fs.cpSync(resolvedFrom, operationTo, { recursive: true });
|
||||
|
||||
const stat = fs.statSync(resolvedTo);
|
||||
const stat = fs.statSync(operationTo);
|
||||
const name = path.basename(resolvedTo);
|
||||
return {
|
||||
name,
|
||||
@@ -143,12 +195,12 @@ export class FsStorageProvider implements StorageProvider {
|
||||
}
|
||||
|
||||
async mkdir(dirPath: string): Promise<FileEntry> {
|
||||
const resolved = safePath(this.root, dirPath);
|
||||
fs.mkdirSync(resolved, { recursive: true });
|
||||
const stat = fs.statSync(resolved);
|
||||
const { lexicalPath, operationPath } = this.resolveForOperation(dirPath);
|
||||
fs.mkdirSync(operationPath, { recursive: true });
|
||||
const stat = fs.statSync(operationPath);
|
||||
return {
|
||||
name: path.basename(resolved),
|
||||
path: toRelativePath(this.root, resolved),
|
||||
name: path.basename(lexicalPath),
|
||||
path: toRelativePath(this.root, lexicalPath),
|
||||
type: 'directory',
|
||||
modifiedAt: stat.mtime.toISOString(),
|
||||
};
|
||||
@@ -156,7 +208,7 @@ export class FsStorageProvider implements StorageProvider {
|
||||
|
||||
async exists(targetPath: string): Promise<boolean> {
|
||||
try {
|
||||
const resolved = safePath(this.root, targetPath);
|
||||
const resolved = this.resolve(targetPath);
|
||||
return fs.existsSync(resolved);
|
||||
} catch {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user