Skip to content

One-click character swap for videos: an open-source, local Higgsfield Genjutsu alternative built on ComfyUI and MiniMax H3, with FLUX.3 Klein relighting.

Refuse password-free access beyond localhost (#9)

mixbag2216b9d5d6fd
diff --git a/README.md b/README.md
index fa6eed4..1658c6b 100644
--- a/README.md
+++ b/README.md
@@ -147,8 +147,15 @@ Not tested yet. The app code does not change, only `.env.local`:
2. Create a deployment from the released Build on the Developer Platform. The workflow has only been run on a 16 GB card with 64 GB of system RAM, so choose at least that.
3. Set `COMFY_BASE_URL` to the deployment URL and `COMFY_API_KEY` to a key from the workspace that owns it. The key stays on the server; never put it in a `NEXT_PUBLIC_` variable.
-4. Before hosting or tunneling the app, set a strong `OPENJUTSU_PASSWORD` and use HTTPS. Open the page and sign in as `openjutsu` with that password; the same browser login protects uploads, job controls, progress and downloads. This is a single-owner app: everyone given the password shares access to its jobs. Never expose a password-free server, including through a reverse proxy that forwards a localhost Host header.
-5. Host the Next.js app anywhere that runs Node (`npm run build && npm start`). Streaming progress needs a host that keeps long responses open. Preserve the public Host and scheme when reverse proxying so same-origin API checks work. Local password-free use remains bound to `127.0.0.1` by the supplied launch commands.
+4. Before hosting or tunneling the app, set a strong `OPENJUTSU_PASSWORD` and use HTTPS. Without a password, Openjutsu refuses to start on any address but loopback. Open the page and sign in as `openjutsu` with that password; the same browser login protects uploads, job controls, progress and downloads. This is a single-owner app: everyone given the password shares access to its jobs. The start-up check cannot see a tunnel or reverse proxy on the same machine, so never point one at a password-free server.
+5. Host the Next.js app anywhere that runs Node. `npm start` listens on `127.0.0.1` only; to serve on a network address, build and start Next with the password set (Next uses `PORT` if the host sets it, otherwise 3000):
+
+ ```sh
+ npm run build
+ OPENJUTSU_PASSWORD='<strong password>' npx next start --hostname 0.0.0.0
+ ```
+
+ Streaming progress needs a host that keeps long responses open. Preserve the public Host and scheme when reverse proxying so same-origin API checks work.
Notes:
diff --git a/instrumentation.ts b/instrumentation.ts
new file mode 100644
index 0000000..e6c7c90
--- /dev/null
+++ b/instrumentation.ts
@@ -0,0 +1,15 @@
+import type { Server } from "node:net";
+
+// Next is already listening when register() runs, so check the real bound addresses rather than request headers.
+export async function register() {
+ if (process.env.NEXT_RUNTIME === "nodejs" && !process.env.OPENJUTSU_PASSWORD) {
+ const [{ Server }, { loopbackOnly, PASSWORD_REQUIRED }] = await Promise.all([import("node:net"), import("./lib/http.ts")]);
+ // _getActiveHandles() is undocumented; if it ever finds no listening server, this fails closed.
+ const handles = (process as unknown as { _getActiveHandles(): unknown[] })._getActiveHandles();
+ const servers = handles.filter((handle): handle is Server => handle instanceof Server && handle.listening);
+ if (!loopbackOnly(servers.map((server) => server.address()))) {
+ console.error(PASSWORD_REQUIRED);
+ process.exit(1);
+ }
+ }
+}
diff --git a/lib/http.ts b/lib/http.ts
index 34d7297..4d33818 100644
--- a/lib/http.ts
+++ b/lib/http.ts
@@ -1,4 +1,5 @@
import { createHash, timingSafeEqual } from "node:crypto";
+import type { AddressInfo } from "node:net";
export class HttpError extends Error {
status: number;
@@ -26,7 +27,7 @@ export function authorize(request: Request) {
});
}
} else {
- // Password-free use relies on the launcher's loopback binding. Reject other hostnames, including DNS rebinding.
+ // instrumentation.ts refuses to start password-free on a network address; this only stops DNS rebinding.
const local = /^(localhost|127\.0\.0\.1|\[::1\])(:\d+)?$/i;
const hosts = [new URL(request.url).host, request.headers.get("Host"), request.headers.get("X-Forwarded-Host")];
if (hosts.some((host) => host !== null && !local.test(host))) {
@@ -36,6 +37,13 @@ export function authorize(request: Request) {
return null;
}
+export const PASSWORD_REQUIRED = "Set OPENJUTSU_PASSWORD before serving Openjutsu on a network address.";
+
+// Any peer can fake a localhost Host header, so password-free use needs every listening socket on loopback or a local pipe.
+export function loopbackOnly(addresses: (AddressInfo | string | null)[]) {
+ return addresses.length > 0 && addresses.every((address) => typeof address === "string" || /^(127\.|::1$|::ffff:127\.)/.test(address?.address ?? ""));
+}
+
export function apiAccess(request: Request) {
const denied = authorize(request);
if (denied) return denied;
diff --git a/test/http.test.ts b/test/http.test.ts
index 8db6f33..6a8b9ff 100644
--- a/test/http.test.ts
+++ b/test/http.test.ts
@@ -1,7 +1,9 @@
import assert from "node:assert/strict";
+import { createServer, type Server } from "node:net";
import test from "node:test";
-import { apiAccess, authorize, HttpError, readFormData, validateJobId } from "../lib/http.ts";
+import { apiAccess, authorize, HttpError, loopbackOnly, PASSWORD_REQUIRED, readFormData, validateJobId } from "../lib/http.ts";
import { getJob } from "../lib/comfy.ts";
+import { register } from "../instrumentation.ts";
test("URL-shaped job IDs are rejected before the SDK can fetch", async (t) => {
let requests = 0;
@@ -32,6 +34,39 @@ test("local API access stays password-free; foreign hosts and browser origins ar
assert.equal(authorize(new Request("http://localhost:3000", { headers: { "Sec-Fetch-Site": "cross-site" } })), null, "ordinary page navigation is allowed");
});
+test("password-free serving starts only on loopback; a network bind needs a password", async (t) => {
+ const previous = { password: process.env.OPENJUTSU_PASSWORD, runtime: process.env.NEXT_RUNTIME };
+ delete process.env.OPENJUTSU_PASSWORD;
+ process.env.NEXT_RUNTIME = "nodejs";
+ const servers: Server[] = [];
+ t.after(() => {
+ for (const [key, value] of [["OPENJUTSU_PASSWORD", previous.password], ["NEXT_RUNTIME", previous.runtime]] as const) {
+ if (value === undefined) delete process.env[key]; else process.env[key] = value;
+ }
+ for (const server of servers) server.close();
+ });
+ const listen = (host: string) => new Promise<void>((resolve) => { const server = createServer(); servers.push(server); server.listen(0, host, resolve); });
+ const errors: unknown[] = [];
+ t.mock.method(console, "error", (message: unknown) => { errors.push(message); });
+ t.mock.method(process, "exit", (code?: number) => { throw new Error(`exit ${code}`); });
+
+ for (const address of ["127.0.0.1", "127.8.0.1", "::1", "::ffff:127.0.0.1"]) assert.equal(loopbackOnly([{ address, family: "", port: 3000 }]), true, address);
+ assert.equal(loopbackOnly(["/tmp/openjutsu.sock"]), true, "pipes are local");
+ for (const address of ["0.0.0.0", "::", "192.168.1.20", "::ffff:10.0.0.2"]) assert.equal(loopbackOnly([{ address, family: "", port: 3000 }]), false, address);
+ assert.equal(loopbackOnly([]), false, "no listening socket proves nothing");
+ assert.equal(loopbackOnly([null]), false);
+
+ await assert.rejects(register(), /exit 1/, "nothing listening yet is not proof of loopback");
+ await listen("127.0.0.1");
+ await register();
+ await listen("0.0.0.0");
+ await assert.rejects(register(), /exit 1/);
+ assert.deepEqual(errors, [PASSWORD_REQUIRED, PASSWORD_REQUIRED]);
+ process.env.OPENJUTSU_PASSWORD = "review-test-password";
+ await register();
+ assert.equal(errors.length, 2);
+});
+
test("a configured password protects local and hosted access, including media and event requests", (t) => {
const previous = process.env.OPENJUTSU_PASSWORD;
process.env.OPENJUTSU_PASSWORD = "review-test-password";