Skip to content

Usenet streaming with a WebDAV server and a SABnzbd-compatible API

C#

fix: funnel frontend auth through middleware.

nzbdav-deveb71ebf8432f
diff --git a/frontend/app/auth/auth-middleware.server.ts b/frontend/app/auth/auth-middleware.server.ts
new file mode 100644
index 00000000..0bbbaa51
--- /dev/null
+++ b/frontend/app/auth/auth-middleware.server.ts
@@ -0,0 +1,27 @@
+import type express from "express";
+import { isAuthenticated } from "~/auth/authentication.server";
+
+// Paths that do not require authentication. Every other path is protected.
+const PUBLIC_PATHS = [
+ "/__manifest",
+ "/login",
+ "/login.data",
+ "/onboarding",
+ "/onboarding.data",
+];
+
+export async function authMiddleware(
+ req: express.Request,
+ res: express.Response,
+ next: express.NextFunction,
+): Promise<void> {
+ // Allow explicitly public paths
+ const pathname = decodeURIComponent(req.path);
+ if (PUBLIC_PATHS.includes(pathname)) return next();
+
+ // Allow authenticated sessions
+ if (await isAuthenticated(req)) return next();
+
+ // Redirect everything else to the login page
+ res.redirect(302, "/login");
+}
diff --git a/frontend/app/root.tsx b/frontend/app/root.tsx
index 335380c0..f8c5f6aa 100644
--- a/frontend/app/root.tsx
+++ b/frontend/app/root.tsx
@@ -2,7 +2,6 @@ import {
Links,
Meta,
Outlet,
- redirect,
Scripts,
ScrollRestoration,
useLocation,
@@ -12,20 +11,17 @@ import {
import 'bootstrap/dist/css/bootstrap.min.css';
import "./app.css";
import type { Route } from "./+types/root";
-import { IS_FRONTEND_AUTH_DISABLED, isAuthenticated } from "~/auth/authentication.server";
+import { IS_FRONTEND_AUTH_DISABLED } from "~/auth/authentication.server";
import { TopNavigation } from "./routes/_index/components/top-navigation/top-navigation";
import { LeftNavigation } from "./routes/_index/components/left-navigation/left-navigation";
import { PageLayout } from "./routes/_index/components/page-layout/page-layout";
import { Loading } from "./routes/_index/components/loading/loading";
export async function loader({ request }: Route.LoaderArgs) {
- // unauthenticated routes
let path = new URL(request.url).pathname;
if (path === "/login") return { useLayout: false };
if (path === "/onboarding") return { useLayout: false };
- // ensure all other routes are authenticated
- if (!await isAuthenticated(request)) return redirect("/login");
return {
useLayout: true,
version: process.env.NZBDAV_VERSION,
diff --git a/frontend/app/routes/logout/route.tsx b/frontend/app/routes/logout/route.tsx
index 031f2059..fd152bd0 100644
--- a/frontend/app/routes/logout/route.tsx
+++ b/frontend/app/routes/logout/route.tsx
@@ -1,11 +1,8 @@
import type { Route } from "./+types/route";
-import { isAuthenticated, logout } from "~/auth/authentication.server";
+import { logout } from "~/auth/authentication.server";
import { redirect } from "react-router";
export async function action({ request }: Route.ActionArgs) {
- // if already logged out, redirect to login page
- if (!await isAuthenticated(request)) return redirect("/login");
-
// if we logout intent is not confirmed, redirect to landing page
const formData = await request.formData();
const confirm = formData.get("confirm")?.toString();
diff --git a/frontend/app/routes/settings.update/route.tsx b/frontend/app/routes/settings.update/route.tsx
index f1dc8620..c8eb5046 100644
--- a/frontend/app/routes/settings.update/route.tsx
+++ b/frontend/app/routes/settings.update/route.tsx
@@ -1,12 +1,7 @@
import type { Route } from "./+types/route";
import { backendClient, type ConfigItem } from "~/clients/backend-client.server";
-import { redirect } from "react-router";
-import { isAuthenticated } from "~/auth/authentication.server";
export async function action({ request }: Route.ActionArgs) {
- // ensure user is logged in
- if (!await isAuthenticated(request)) return redirect("/login");
-
// get the ConfigItems to update
const formData = await request.formData();
const configJson = formData.get("config")!.toString();
diff --git a/frontend/server/app.ts b/frontend/server/app.ts
index 3fec0913..b3fbd502 100644
--- a/frontend/server/app.ts
+++ b/frontend/server/app.ts
@@ -4,6 +4,7 @@ import express from "express";
import { createProxyMiddleware } from "http-proxy-middleware";
import { websocketServer } from "./websocket.server";
import { isAuthenticated } from "~/auth/authentication.server";
+import { authMiddleware } from "~/auth/auth-middleware.server";
declare module "react-router" {
interface AppLoadContext {
@@ -64,6 +65,9 @@ app.use(async (req, res, next) => {
next();
});
+// Require authentication for all React Router routes
+app.use(authMiddleware);
+
// Let frontend handle all other requests
app.use(
createRequestHandler({