Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

test: cover the NEXT_PUBLIC_AUTH_ENABLED flag parsing

Ilia Avdeev25c6a83f91b4
diff --git a/web/lib/api.ts b/web/lib/api.ts
index 62bbb1fc6..f45a13688 100644
--- a/web/lib/api.ts
+++ b/web/lib/api.ts
@@ -131,12 +131,22 @@ export function wsUrl(path: string): string {
return `${normalizedBase}${normalizedPath}`;
}
-// Evaluate the build-time flag at runtime (not a ``=== "true"`` compare) so the
-// Docker ``__NEXT_PUBLIC_AUTH_ENABLED_PLACEHOLDER__`` token survives minification
-// and ``start-frontend.sh`` can rewrite it on startup. See lib/auth.ts.
-const AUTH_ENABLED = /^(1|true|yes|on)$/i.test(
- (process.env.NEXT_PUBLIC_AUTH_ENABLED ?? "").trim(),
-);
+/**
+ * Parse a ``NEXT_PUBLIC_AUTH_ENABLED``-style flag at runtime.
+ *
+ * The value is inlined at build time and, in the Docker image, baked as the
+ * ``__NEXT_PUBLIC_AUTH_ENABLED_PLACEHOLDER__`` token that ``start-frontend.sh``
+ * rewrites on container start. Evaluating it with a runtime regex — instead of
+ * a ``=== "true"`` literal comparison the minifier would constant-fold — keeps
+ * the placeholder in the bundle so it survives minification (the same approach
+ * used for the API base placeholder above). An unsubstituted placeholder
+ * therefore parses as "disabled".
+ */
+export function parseAuthEnabled(raw: string | undefined): boolean {
+ return /^(1|true|yes|on)$/i.test((raw ?? "").trim());
+}
+
+const AUTH_ENABLED = parseAuthEnabled(process.env.NEXT_PUBLIC_AUTH_ENABLED);
/**
* Authenticated fetch wrapper. Behaves identically to `fetch` but automatically
diff --git a/web/lib/auth.ts b/web/lib/auth.ts
index 64438a28b..43d3e8875 100644
--- a/web/lib/auth.ts
+++ b/web/lib/auth.ts
@@ -1,14 +1,13 @@
-import { apiFetch, apiUrl } from "@/lib/api";
+import { apiFetch, apiUrl, parseAuthEnabled } from "@/lib/api";
// ``NEXT_PUBLIC_AUTH_ENABLED`` is inlined at build time and, in the Docker
// image, baked as the ``__NEXT_PUBLIC_AUTH_ENABLED_PLACEHOLDER__`` token that
-// ``start-frontend.sh`` rewrites on container start. A ``=== "true"`` literal
-// comparison gets constant-folded by the minifier to ``false`` — which strips
-// the placeholder and permanently breaks the runtime rewrite. Evaluating the
-// raw value with a runtime regex keeps the placeholder string in the bundle
-// (mirroring how ``lib/api.ts`` handles the API base placeholder).
-export const AUTH_ENABLED = /^(1|true|yes|on)$/i.test(
- (process.env.NEXT_PUBLIC_AUTH_ENABLED ?? "").trim(),
+// ``start-frontend.sh`` rewrites on container start. ``parseAuthEnabled``
+// evaluates it at runtime (not a ``=== "true"`` literal comparison) so the
+// minifier cannot constant-fold the check and strip the placeholder, which
+// would permanently break the runtime rewrite. See lib/api.ts.
+export const AUTH_ENABLED = parseAuthEnabled(
+ process.env.NEXT_PUBLIC_AUTH_ENABLED,
);
export interface AuthStatus {
diff --git a/web/tests/auth-enabled.test.ts b/web/tests/auth-enabled.test.ts
new file mode 100644
index 000000000..a3ff875e9
--- /dev/null
+++ b/web/tests/auth-enabled.test.ts
@@ -0,0 +1,34 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+
+import { parseAuthEnabled } from "../lib/api";
+
+test("parseAuthEnabled is true for accepted truthy flags", () => {
+ for (const value of ["true", "1", "yes", "on", "TRUE", " true "]) {
+ assert.equal(
+ parseAuthEnabled(value),
+ true,
+ `expected ${JSON.stringify(value)} -> true`,
+ );
+ }
+});
+
+test("parseAuthEnabled is false for falsy / empty / undefined", () => {
+ for (const value of ["false", "0", "no", "off", "", undefined]) {
+ assert.equal(
+ parseAuthEnabled(value),
+ false,
+ `expected ${JSON.stringify(value)} -> false`,
+ );
+ }
+});
+
+// Guards the failure mode this flag was hardened against: if the Docker
+// placeholder is ever served unsubstituted, it must read as "disabled" rather
+// than throwing or accidentally enabling auth.
+test("parseAuthEnabled treats an unsubstituted Docker placeholder as disabled", () => {
+ assert.equal(
+ parseAuthEnabled("__NEXT_PUBLIC_AUTH_ENABLED_PLACEHOLDER__"),
+ false,
+ );
+});