File size: 2,980 Bytes
b01d539
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { lstat, mkdir, realpath, stat } from "node:fs/promises";
import { dirname, isAbsolute, parse, relative, resolve, sep } from "node:path";

export interface PreparedDatabasePath {
  databasePath: string;
  existed: boolean;
}

export async function prepareLocalDatabasePath(input: {
  databasePath: string;
  objectStorageRoot: string;
}): Promise<PreparedDatabasePath> {
  if (hasTraversalSegment(input.databasePath)) {
    throw new Error("Database path must not contain traversal segments");
  }
  if (!isAbsolute(input.databasePath) || !isAbsolute(input.objectStorageRoot)) {
    throw new Error("Database and object storage paths must be absolute");
  }

  const databasePath = resolve(input.databasePath);
  const objectStorageRoot = resolve(input.objectStorageRoot);
  if (isSameOrInside(databasePath, objectStorageRoot)) {
    throw new Error("The live database must be outside object storage");
  }

  await assertNoSymbolicLinkComponent(databasePath);
  await mkdir(dirname(databasePath), { recursive: true });
  await assertNoSymbolicLinkComponent(databasePath);

  const realObjectStorageRoot = await realpathIfPresent(objectStorageRoot);
  const realDatabaseParent = await realpath(dirname(databasePath));
  if (
    realObjectStorageRoot !== undefined &&
    isSameOrInside(realDatabaseParent, realObjectStorageRoot)
  ) {
    throw new Error("The live database resolves inside object storage");
  }

  try {
    const details = await stat(databasePath);
    if (!details.isFile()) throw new Error("Database path is not a file");
    if (details.size === 0) throw new Error("Database file is zero bytes");
    return { databasePath, existed: true };
  } catch (error) {
    if (hasCode(error, "ENOENT")) return { databasePath, existed: false };
    throw error;
  }
}

function hasTraversalSegment(path: string): boolean {
  return path.split(/[\\/]/u).includes("..");
}

async function assertNoSymbolicLinkComponent(path: string): Promise<void> {
  const root = parse(path).root;
  const parts = path.slice(root.length).split(sep).filter(Boolean);
  let current = root;
  for (const part of parts) {
    current = resolve(current, part);
    try {
      if ((await lstat(current)).isSymbolicLink()) {
        throw new Error("Database path contains a symbolic link");
      }
    } catch (error) {
      if (hasCode(error, "ENOENT")) return;
      throw error;
    }
  }
}

async function realpathIfPresent(path: string): Promise<string | undefined> {
  try {
    return await realpath(path);
  } catch (error) {
    if (hasCode(error, "ENOENT")) return undefined;
    throw error;
  }
}

function isSameOrInside(candidate: string, root: string): boolean {
  const child = relative(root, candidate);
  return child === "" || (!child.startsWith(`..${sep}`) && child !== "..");
}

function hasCode(error: unknown, code: string): boolean {
  return (
    error instanceof Error &&
    "code" in error &&
    (error as NodeJS.ErrnoException).code === code
  );
}