Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/lib/dataPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,45 @@ export function resolveDataDir({ isCloud = false }: { isCloud?: boolean } = {}):
return getDefaultDataDir();
}

/**
* Resolve the data directory and guarantee it is writable.
*
* Unlike {@link resolveDataDir} (a pure, side-effect-free path resolver used by
* many callers), this variant probes the resolved directory by attempting to
* create it. When a configured `DATA_DIR` is not writable (`EACCES`/`EPERM`),
* it falls back to the default user directory so the app keeps working instead
* of crashing on an unwritable, operator-supplied path. Any other error (e.g.
* `ENOTDIR`, `ENOSPC`) still propagates.
*
* Use this only at the single startup site that owns directory creation
* (currently `db/core.ts`); everywhere else keep using the pure resolver.
*/
export function resolveWritableDataDir({ isCloud = false }: { isCloud?: boolean } = {}): string {
const resolved = resolveDataDir({ isCloud });

// Cloud/serverless never owns a writable home dir; leave its sentinel alone.
if (isCloud) return resolved;

// No explicit override → already the default user dir; nothing to fall back to.
const configured = normalizeConfiguredPath(process.env.DATA_DIR);
if (!configured) return resolved;

try {
fs.mkdirSync(resolved, { recursive: true });
return resolved;
} catch (err: unknown) {
const code = (err as NodeJS.ErrnoException | null)?.code;
if (code === "EACCES" || code === "EPERM") {
const fallback = getDefaultDataDir();
console.warn(
`[DATA_DIR] '${resolved}' is not writable (${code}) → falling back to '${fallback}'`
);
return fallback;
}
Comment on lines +101 to +107

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

When a permission error occurs and the application falls back to the default user directory, process.env.DATA_DIR is left pointing to the original, unwritable path.

Because other parts of the application call the pure resolveDataDir() resolver directly, they will still resolve to the unwritable process.env.DATA_DIR path. This creates a split-brain scenario where the database is initialized in the fallback directory, but other components attempt to read/write from the unwritable configured directory, leading to runtime crashes or silent failures.

Updating process.env.DATA_DIR to the fallback path ensures consistency across all path resolvers and any spawned subprocesses.

Suggested change
if (code === "EACCES" || code === "EPERM") {
const fallback = getDefaultDataDir();
console.warn(
`[DATA_DIR] '${resolved}' is not writable (${code}) → falling back to '${fallback}'`
);
return fallback;
}
if (code === "EACCES" || code === "EPERM") {
const fallback = getDefaultDataDir();
console.warn(
`[DATA_DIR] '${resolved}' is not writable (${code}) → falling back to '${fallback}'`
);
process.env.DATA_DIR = fallback;
return fallback;
}

throw err;
}
}

export function isSamePath(a: string | null | undefined, b: string | null | undefined): boolean {
if (!a || !b) return false;
const normalizedA = path.resolve(a);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/db/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
* All domain modules import `getDbInstance` and helpers from here.
*/

import type { SqliteAdapter } from "./adapters/types";

Check failure on line 7 in src/lib/db/core.ts

View workflow job for this annotation

GitHub Actions / dast-smoke

Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './adapters/types.js'?
import {
tryOpenSync,
getSqlJsAdapter,
preInitSqlJs,
openDatabaseAsync,
} from "./adapters/driverFactory";

Check failure on line 13 in src/lib/db/core.ts

View workflow job for this annotation

GitHub Actions / dast-smoke

Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './adapters/driverFactory.js'?
import path from "path";
import fs from "fs";
import { resolveDataDir, getLegacyDotDataDir } from "../dataPaths";
import { resolveWritableDataDir, getLegacyDotDataDir } from "../dataPaths";

Check failure on line 16 in src/lib/db/core.ts

View workflow job for this annotation

GitHub Actions / dast-smoke

Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean '../dataPaths.js'?
import { runMigrations } from "./migrationRunner";

Check failure on line 17 in src/lib/db/core.ts

View workflow job for this annotation

GitHub Actions / dast-smoke

Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './migrationRunner.js'?
import { runDbHealthCheck } from "./healthCheck";

Check failure on line 18 in src/lib/db/core.ts

View workflow job for this annotation

GitHub Actions / dast-smoke

Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './healthCheck.js'?
import { resetAllDbModuleState } from "./stateReset";

Check failure on line 19 in src/lib/db/core.ts

View workflow job for this annotation

GitHub Actions / dast-smoke

Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './stateReset.js'?
import { parseStoredPayload } from "../logPayloads";

Check failure on line 20 in src/lib/db/core.ts

View workflow job for this annotation

GitHub Actions / dast-smoke

Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean '../logPayloads.js'?
import { DEFAULT_DATABASE_SETTINGS, type DatabaseSettings } from "@/types/databaseSettings";

Check failure on line 21 in src/lib/db/core.ts

View workflow job for this annotation

GitHub Actions / dast-smoke

Cannot find module '@/types/databaseSettings' or its corresponding type declarations.
import {
applyDatabaseOptimizationSettingsForDb,
applyStoredDatabaseOptimizationSettings,
Expand Down Expand Up @@ -83,7 +83,7 @@

// ──────────────── Paths ────────────────

export const DATA_DIR = resolveDataDir({ isCloud });
export const DATA_DIR = resolveWritableDataDir({ isCloud });
const LEGACY_DATA_DIR = isCloud ? null : getLegacyDotDataDir();
export const SQLITE_FILE = isCloud ? null : path.join(DATA_DIR, "storage.sqlite");
const JSON_DB_FILE = isCloud ? null : path.join(DATA_DIR, "db.json");
Expand Down
115 changes: 115 additions & 0 deletions tests/unit/data-dir-writable-fallback.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

import {
resolveWritableDataDir,
getDefaultDataDir,
resolveDataDir,
} from "../../src/lib/dataPaths.ts";

// Running as root bypasses POSIX permission bits, so a chmod-based "unwritable"
// directory would still be writable and the EACCES/EPERM branch never triggers.
const IS_ROOT = typeof process.getuid === "function" && process.getuid() === 0;
const IS_WINDOWS = process.platform === "win32";

async function withTempEnv(
fn: (paths: { root: string; home: string }) => void | Promise<void>
) {
const originalEnv = { ...process.env };
const root = fs.mkdtempSync(path.join(os.tmpdir(), "omni-datadir-"));
const home = path.join(root, "home");
fs.mkdirSync(home, { recursive: true });

delete process.env.DATA_DIR;
delete process.env.XDG_CONFIG_HOME;
delete process.env.APPDATA;
process.env.HOME = home;
process.env.USERPROFILE = home;

try {
await fn({ root, home });
} finally {
for (const key of Object.keys(process.env)) {
if (!(key in originalEnv)) delete process.env[key];
}
for (const [key, value] of Object.entries(originalEnv)) {
process.env[key] = value;
}
// Restore perms before cleanup so rmSync can delete read-only parents.
try {
fs.chmodSync(root, 0o755);
} catch {
// ignore
}
fs.rmSync(root, { recursive: true, force: true });
}
}

test("resolveWritableDataDir returns the configured DATA_DIR when it is writable", async () => {
await withTempEnv(({ root }) => {
const configured = path.join(root, "writable-data");
process.env.DATA_DIR = configured;

const resolved = resolveWritableDataDir();
assert.equal(resolved, path.resolve(configured));
// The probe creates the directory as a side effect.
assert.ok(fs.existsSync(configured));
});
});

test("resolveWritableDataDir falls back to the default dir when DATA_DIR is not writable (EACCES/EPERM)", { skip: IS_ROOT || IS_WINDOWS }, async () => {
await withTempEnv(({ root, home }) => {
// A read-only parent makes mkdir of the child fail with EACCES/EPERM.
const lockedParent = path.join(root, "locked");
fs.mkdirSync(lockedParent, { recursive: true });
fs.chmodSync(lockedParent, 0o555);

const configured = path.join(lockedParent, "data");
process.env.DATA_DIR = configured;

const resolved = resolveWritableDataDir();
const expectedFallback = getDefaultDataDir();

// It must NOT return the unwritable configured dir...
assert.notEqual(resolved, path.resolve(configured));
// ...and instead fall back to the default user dir (~/.omniroute under HOME).
assert.equal(resolved, expectedFallback);
assert.ok(resolved.startsWith(path.resolve(home)));
});
});

test("resolveWritableDataDir returns the default dir (no probe) when DATA_DIR is unset", async () => {
await withTempEnv(() => {
delete process.env.DATA_DIR;
const resolved = resolveWritableDataDir();
assert.equal(resolved, getDefaultDataDir());
// Matches the pure resolver when no override is present.
assert.equal(resolved, resolveDataDir());
});
});

test("resolveWritableDataDir rethrows non-permission errors", { skip: IS_WINDOWS }, async () => {
await withTempEnv(({ root }) => {
// Point DATA_DIR at a path whose parent is a regular file → ENOTDIR, not EACCES.
const fileParent = path.join(root, "iam-a-file");
fs.writeFileSync(fileParent, "x");

const configured = path.join(fileParent, "data");
process.env.DATA_DIR = configured;

assert.throws(() => resolveWritableDataDir(), (err: NodeJS.ErrnoException) => {
return err.code !== "EACCES" && err.code !== "EPERM";
});
});
});

test("resolveWritableDataDir leaves the cloud sentinel untouched", async () => {
await withTempEnv(() => {
process.env.DATA_DIR = "/some/configured/path";
const resolved = resolveWritableDataDir({ isCloud: true });
assert.equal(resolved, "/tmp");
});
});
Loading