fix(media): refuse local disk when a NAS share is configured
CI / build (push) Has been cancelled

A bind-mounted host directory made the app report storage as ready while
media landed on the container host's local filesystem, so the NFS export was
never actually used. mediaStorage() now reads the mount table: a share written
as host:/export is only accepted when the media root is on a network
filesystem, otherwise uploads, edits and filing are blocked with the real
device and the fix. Settings and the Media page show the detected filesystem
and host-mount steps, README section 9 documents the OS-level mount (including
Docker-in-LXC) with docker-compose.nfs.yml as the alternative, and the E2E
suites use path-style shares for local test directories.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
2026-11-21 00:45:36 +00:00
parent 2fc71883d7
commit 982f9048e1
14 changed files with 439 additions and 33 deletions
+6
View File
@@ -125,6 +125,12 @@ export default async function MediaPage() {
Free space could not be read from this filesystem.
</p>
)}
{archiveStatus.mount && (
<p className="text-xs text-muted-foreground">
Filesystem {archiveStatus.mount.fileSystem} on{" "}
{archiveStatus.mount.mountPoint}
</p>
)}
</section>
)}
+1
View File
@@ -43,6 +43,7 @@ export default async function SettingsPage() {
? `Event photos and videos are stored in ${media.root}.`
: media.message,
freeBytes: media.ok ? media.freeBytes : null,
mount: media.ok ? media.mount : null,
}
: null;
const emailTransport = emailSettings?.smtpConfigured
+60 -5
View File
@@ -26,6 +26,11 @@ export function StoragePanel({
ready: boolean;
message: string;
freeBytes: number | null;
mount?: {
device: string;
mountPoint: string;
fileSystem: string;
} | null;
};
}) {
const router = useRouter();
@@ -82,9 +87,55 @@ export function StoragePanel({
{formatBytes(status.freeBytes)}
</p>
)}
{status.ready && status.mount && (
<p className="text-xs text-muted-foreground">
Filesystem {status.mount.fileSystem} on{" "}
{status.mount.mountPoint} ({status.mount.device})
</p>
)}
</div>
</div>
{!status.ready && (
<div className="space-y-2 rounded-md border border-border p-3 text-xs text-muted-foreground">
<p className="font-medium text-foreground">
How to fix: mount the export on the host
</p>
<p>
The app cannot mount NFS itself. Mount the export at an OS-level
path on the Docker host, then restart the app so the container
binds that path at <code className="font-mono">/archive</code>:
</p>
<pre className="overflow-x-auto rounded bg-muted p-2 font-mono text-[11px] leading-relaxed">
{`# on the Docker host (root)
sudo mkdir -p /mnt/glenloch-media
sudo mount -t nfs nas.example.org:/export /mnt/glenloch-media
findmnt -T /mnt/glenloch-media -o TARGET,SOURCE,FSTYPE`}
</pre>
<p>
Set <code className="font-mono">MEDIA_ARCHIVE_DIR=/mnt/glenloch-media</code>{" "}
in <code className="font-mono">.env</code> (mount first — bind mounts
are read when the container starts), then run{" "}
<code className="font-mono">docker compose up -d app</code>. Confirm
the container sees the share:
</p>
<pre className="overflow-x-auto rounded bg-muted p-2 font-mono text-[11px] leading-relaxed">
{`docker compose exec app grep ' /archive ' /proc/self/mounts
# nfs4 nas.example.org:/export /archive nfs4 rw,… ← good
# zfs ssd-main/subvol-125-disk-0 /archive … ← still local disk`}
</pre>
<p>
The export must let the container user (UID 100) write. Inside a
Proxmox LXC this needs either{" "}
<code className="font-mono">pct set &lt;ctid&gt; -features mount=nfs,nesting=1</code>{" "}
or mounting on the Proxmox host and passing it in with{" "}
<code className="font-mono">pct set &lt;ctid&gt; -mp0 /mnt/glenloch-media,mp=/mnt/glenloch-media</code>{" "}
— see README section 9. Alternatively let Docker mount the export
with <code className="font-mono">docker-compose.nfs.yml</code>.
</p>
</div>
)}
<div className="grid gap-4 sm:grid-cols-2">
<div className="flex flex-col gap-2">
<Label htmlFor="nfs-share">NFS share or local path (required)</Label>
@@ -125,11 +176,15 @@ export function StoragePanel({
<p className="text-xs text-muted-foreground">
This is primary storage for original photos and videos, not a backup.
NFS is a mounted filesystem, not an upload API: mount the export on the
Docker host, set <code className="font-mono">MEDIA_ARCHIVE_DIR</code> to
that host path, then restart this stack. The path above must be the
directory visible inside the app container (normally{" "}
<code className="font-mono">/archive</code>).
The recommended setup is an OS-level mount of the export on the Docker
host, bind-mounted in with{" "}
<code className="font-mono">MEDIA_ARCHIVE_DIR</code>; letting Compose
mount it (<code className="font-mono">docker-compose.nfs.yml</code>) also
works. Either way, the path above must be the directory visible inside
the app container (normally <code className="font-mono">/archive</code>),
and a share written as <code className="font-mono">host:/export</code>{" "}
is only accepted when that directory is a network filesystem. The
filesystem line above shows what the container is really writing to.
</p>
<div className="flex flex-wrap items-center gap-3">
+6 -5
View File
@@ -2,11 +2,12 @@
* Runtime media-storage settings: where original photos and videos live.
*
* NFS is a mounted filesystem, not an upload API. The app writes to a path that
* must already be visible inside the container; the NFS export is mounted by
* the Docker host and bind-mounted in (see docker-compose.yml). The stored
* `nfsShare` is the human-readable export name so operators can confirm which
* filesystem they are writing to, while `archivePath` is the actual mounted
* path used by the archiver.
* must already be visible inside the container; the export is mounted either by
* the Docker daemon (docker-compose.nfs.yml) or on the Docker host and
* bind-mounted in (see docker-compose.yml). The stored `nfsShare` is the
* human-readable export name so operators can confirm which filesystem they are
* writing to, while `archivePath` is the actual mounted path used by the
* archiver. `mediaStorage()` cross-checks the two against the mount table.
*/
import { constants } from "node:fs";
+30 -1
View File
@@ -23,6 +23,12 @@ import {
getStorageSettings,
type StorageSettings,
} from "@/db/storage-settings";
import {
isNetworkFileSystem,
mountInfoFor,
shareLooksRemote,
type MountInfo,
} from "@/lib/mount-info";
/** Shown wherever a media action is blocked. */
export const MEDIA_STORAGE_RULE =
@@ -43,6 +49,8 @@ export interface MediaStorageReady {
freeBytes: number | null;
/** Size of the filesystem holding the archive, when it can be read. */
totalBytes: number | null;
/** Where the root is actually mounted, when the platform exposes it. */
mount: MountInfo | null;
}
export interface MediaStorageBlocked {
@@ -127,7 +135,28 @@ export async function mediaStorage(): Promise<MediaStorage> {
// statfs is unavailable on some platforms; the destination check is enough.
}
return { ok: true, root, settings, freeBytes, totalBytes };
// A mounted directory always passes the write check, even when the host
// bind-mounted an ordinary local folder (or a Docker named volume) instead of
// the NAS export. If the saved share reads like `host:/export`, the root must
// actually be a network mount; otherwise media would silently land on local
// disk, which is exactly what this gate exists to prevent.
const mount = await mountInfoFor(root);
if (shareLooksRemote(settings.nfsShare)) {
if (!mount) {
return {
ok: false,
message: `The share ${settings.nfsShare} is configured, but the container could not read its mount table to confirm that ${root} is mounted from it. Mount the export on the Docker host and bind it in with MEDIA_ARCHIVE_DIR. ${MEDIA_STORAGE_RULE}`,
};
}
if (!isNetworkFileSystem(mount.fileSystem)) {
return {
ok: false,
message: `The share ${settings.nfsShare} is configured, but ${root} is on ${mount.fileSystem} (${mount.device}), not a network mount. Mount the export at an OS-level path on the Docker host (for example /mnt/glenloch-media), set MEDIA_ARCHIVE_DIR to that path in .env, then run docker compose up -d app. Nothing is written to local storage. ${MEDIA_STORAGE_RULE}`,
};
}
}
return { ok: true, root, settings, freeBytes, totalBytes, mount };
}
/** Throwing form for background work that cannot return an HTTP response. */
+86
View File
@@ -0,0 +1,86 @@
import { describe, expect, it } from "vitest";
import {
isNetworkFileSystem,
mountFor,
parseMountTable,
shareLooksRemote,
} from "./mount-info";
const TABLE = [
"overlay / overlay rw,relatime 0 0",
"proc /proc proc rw,nosuid 0 0",
"/dev/sda1 /etc/hosts ext4 rw,relatime 0 0",
"nas.local:/export /archive nfs4 rw,relatime 0 0",
"nas.local:/export /archive/church\\040media nfs4 rw,relatime 0 0",
].join("\n");
describe("parseMountTable", () => {
it("reads device, mount point and filesystem type per line", () => {
const mounts = parseMountTable(TABLE);
expect(mounts).toHaveLength(5);
expect(mounts[3]).toEqual({
device: "nas.local:/export",
mountPoint: "/archive",
fileSystem: "nfs4",
});
});
it("decodes kernel octal escapes in mount points", () => {
const mounts = parseMountTable(TABLE);
expect(mounts[4].mountPoint).toBe("/archive/church media");
});
it("ignores blank and truncated lines", () => {
expect(parseMountTable("\n\noverlay / overlay rw 0 0\nbad\n")).toHaveLength(
1,
);
});
});
describe("mountFor", () => {
const mounts = parseMountTable(TABLE);
it("picks the longest mount point that contains the path", () => {
expect(
mountFor("/archive/church media/events/e1", mounts)?.mountPoint,
).toBe("/archive/church media");
expect(mountFor("/archive/other/events/e1", mounts)?.mountPoint).toBe(
"/archive",
);
});
it("does not match a sibling with a shared prefix", () => {
// `/archives` must not be treated as inside `/archive`.
expect(mountFor("/archives/photos", mounts)?.mountPoint).toBe("/");
});
it("falls back to the root mount for paths outside any specific mount", () => {
expect(mountFor("/srv/media", mounts)?.fileSystem).toBe("overlay");
});
it("ignores trailing slashes on the target", () => {
expect(mountFor("/archive/", mounts)?.mountPoint).toBe("/archive");
});
});
describe("filesystem classification", () => {
it("recognises network filesystems", () => {
expect(isNetworkFileSystem("nfs")).toBe(true);
expect(isNetworkFileSystem("nfs4")).toBe(true);
expect(isNetworkFileSystem("cifs")).toBe(true);
expect(isNetworkFileSystem("fuse.sshfs")).toBe(true);
});
it("does not treat local filesystems as network mounts", () => {
expect(isNetworkFileSystem("ext4")).toBe(false);
expect(isNetworkFileSystem("overlay")).toBe(false);
expect(isNetworkFileSystem("xfs")).toBe(false);
});
it("detects NAS-style share names only", () => {
expect(shareLooksRemote("nas.local:/export")).toBe(true);
expect(shareLooksRemote("//nas.local/media")).toBe(true);
expect(shareLooksRemote("/mnt/nfs/glenloch")).toBe(false);
expect(shareLooksRemote("")).toBe(false);
});
});
+119
View File
@@ -0,0 +1,119 @@
/**
* Read where a path actually lives, according to the container's mount table.
*
* The app cannot mount an NFS export; it can only write to a path Docker has
* already made visible. A bind mount pointed at a host directory that is not
* itself mounted from the NAS looks exactly like a real share from inside the
* container — writes succeed and land on the host's local disk. Surfacing the
* filesystem type and backing device makes that silent mistake visible in
* Settings instead of only in `df`.
*/
import { readFile, realpath } from "node:fs/promises";
export interface MountInfo {
/** Backing device as the kernel reports it, e.g. `nas:/export` or `/dev/sdb1`. */
device: string;
/** Mount point that contains the path, e.g. `/archive`. */
mountPoint: string;
/** Kernel filesystem type, e.g. `nfs4`, `ext4`, `overlay`. */
fileSystem: string;
}
/**
* Filesystem types that mean "the bytes do not live on this machine".
* `fuse.sshfs` and friends are covered by the `fuse.` prefix.
*/
const NETWORK_FILESYSTEMS = new Set([
"nfs",
"nfs4",
"cifs",
"smb3",
"smbfs",
"ceph",
"glusterfs",
"afs",
]);
export function isNetworkFileSystem(fileSystem: string): boolean {
const type = fileSystem.toLowerCase();
return NETWORK_FILESYSTEMS.has(type) || type.startsWith("fuse.");
}
/**
* `%proc`-style escapes in the mount table: space, tab, newline, backslash.
* The kernel writes these as three-digit octal sequences.
*/
function decodeMountField(value: string): string {
return value.replace(/\\([0-7]{3})/g, (_, octal: string) =>
String.fromCharCode(parseInt(octal, 8)),
);
}
/** Parse `/proc/self/mounts` (or `/proc/mounts`) into mount entries. */
export function parseMountTable(contents: string): MountInfo[] {
const mounts: MountInfo[] = [];
for (const line of contents.split("\n")) {
const fields = line.trim().split(/\s+/);
if (fields.length < 3) continue;
mounts.push({
device: decodeMountField(fields[0]),
mountPoint: decodeMountField(fields[1]),
fileSystem: fields[2],
});
}
return mounts;
}
/** The longest mount point that contains `target`, boundary-aware. */
export function mountFor(
target: string,
mounts: MountInfo[],
): MountInfo | null {
const normalized = target.length > 1 ? target.replace(/\/+$/, "") : target;
let best: MountInfo | null = null;
for (const mount of mounts) {
const point =
mount.mountPoint.length > 1
? mount.mountPoint.replace(/\/+$/, "")
: mount.mountPoint;
const contains =
normalized === point ||
point === "/" ||
normalized.startsWith(`${point}/`);
if (!contains) continue;
if (!best || point.length > best.mountPoint.length) best = mount;
}
return best;
}
/**
* Resolve the mount entry holding `target`, or null when the platform has no
* readable mount table (non-Linux) or the path does not exist yet.
*/
export async function mountInfoFor(target: string): Promise<MountInfo | null> {
let resolved = target;
try {
resolved = await realpath(target);
} catch {
// Path may not exist yet; the mount table still answers for its parent.
}
let table: string;
try {
table = await readFile("/proc/self/mounts", "utf8");
} catch {
try {
table = await readFile("/proc/mounts", "utf8");
} catch {
return null;
}
}
return mountFor(resolved, parseMountTable(table));
}
/** True when the saved share reads like `host:/export` rather than a local path. */
export function shareLooksRemote(share: string): boolean {
const value = share.trim();
return value.startsWith("//") || /^[^/\\]+:/.test(value);
}