From f31cd42d8e7f4b1edd2085470349a8de3d922d91 Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Sat, 13 May 2023 10:38:43 -0400 Subject: [PATCH 01/26] Redirect fomr pages that require auth on logout --- src/shared/services/UserService.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/shared/services/UserService.ts b/src/shared/services/UserService.ts index e4ec5f98..b5f8d260 100644 --- a/src/shared/services/UserService.ts +++ b/src/shared/services/UserService.ts @@ -48,7 +48,15 @@ export class UserService { this.myUserInfo = undefined; IsomorphicCookie.remove("jwt"); // TODO is sometimes unreliable for some reason document.cookie = "jwt=; Max-Age=0; path=/; domain=" + location.hostname; - location.reload(); + if ( + /create_.*|inbox|settings|setup|admin|reports|registration_applications/g.test( + location.pathname + ) + ) { + location.replace("/"); + } else { + location.reload(); + } } public auth(throwErr = true): string | undefined { From ab3fed3ddf7e0141985ebda761fd6e3085516ede Mon Sep 17 00:00:00 2001 From: abias Date: Sat, 13 May 2023 23:09:05 -0400 Subject: [PATCH 02/26] Extract helper function --- src/shared/services/UserService.ts | 8 ++------ src/shared/utils.ts | 6 ++++++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/shared/services/UserService.ts b/src/shared/services/UserService.ts index b5f8d260..16540f23 100644 --- a/src/shared/services/UserService.ts +++ b/src/shared/services/UserService.ts @@ -5,7 +5,7 @@ import { LoginResponse, MyUserInfo } from "lemmy-js-client"; import { BehaviorSubject } from "rxjs"; import { isHttps } from "../env"; import { i18n } from "../i18next"; -import { isBrowser, toast } from "../utils"; +import { isAuthPath, isBrowser, toast } from "../utils"; interface Claims { sub: number; @@ -48,11 +48,7 @@ export class UserService { this.myUserInfo = undefined; IsomorphicCookie.remove("jwt"); // TODO is sometimes unreliable for some reason document.cookie = "jwt=; Max-Age=0; path=/; domain=" + location.hostname; - if ( - /create_.*|inbox|settings|setup|admin|reports|registration_applications/g.test( - location.pathname - ) - ) { + if (isAuthPath(location.pathname)) { location.replace("/"); } else { location.reload(); diff --git a/src/shared/utils.ts b/src/shared/utils.ts index ec6d92d1..821d22f3 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -1603,3 +1603,9 @@ export function getQueryString>( "?" ); } + +export function isAuthPath(pathname: string) { + return /create_.*|inbox|settings|setup|admin|reports|registration_applications/g.test( + pathname + ); +} From bcee6aad5b85b81f6fc7ad5680533d1a443ddb01 Mon Sep 17 00:00:00 2001 From: abias Date: Sun, 14 May 2023 11:08:06 -0400 Subject: [PATCH 03/26] Set up logic for handling errors --- src/server/index.tsx | 59 ++++++++++++-------- src/shared/components/app/app.tsx | 32 ++++++++--- src/shared/components/app/error-page.tsx | 56 +++++++++++++++++++ src/shared/components/app/no-match.tsx | 26 --------- src/shared/components/common/auth-guard.tsx | 13 +++++ src/shared/components/common/error-guard.tsx | 25 +++++++++ src/shared/utils.ts | 6 ++ src/shared/version.ts | 2 +- 8 files changed, 163 insertions(+), 56 deletions(-) create mode 100644 src/shared/components/app/error-page.tsx delete mode 100644 src/shared/components/app/no-match.tsx create mode 100644 src/shared/components/common/auth-guard.tsx create mode 100644 src/shared/components/common/error-guard.tsx diff --git a/src/server/index.tsx b/src/server/index.tsx index 05988cf7..80882887 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -19,7 +19,14 @@ import { IsoData, } from "../shared/interfaces"; import { routes } from "../shared/routes"; -import { favIconPngUrl, favIconUrl, initializeSite } from "../shared/utils"; +import { + ErrorPageData, + favIconPngUrl, + favIconUrl, + initializeSite, + isAuthPath, +} from "../shared/utils"; +import { VERSION } from "../shared/version"; const server = express(); const [hostname, port] = process.env["LEMMY_UI_HOST"] @@ -109,7 +116,6 @@ server.get("/css/themelist", async (_req, res) => { server.get("/*", async (req, res) => { try { const activeRoute = routes.find(route => matchPath(req.path, route)); - const context = {} as any; let auth: string | undefined = IsomorphicCookie.load("jwt", req); const getSiteForm: GetSite = { auth }; @@ -119,6 +125,8 @@ server.get("/*", async (req, res) => { const headers = setForwardedHeaders(req.headers); const client = new LemmyHttp(getHttpBaseInternal(), headers); + const { path, url, query } = req; + // Get site data first // This bypasses errors, so that the client can hit the error on its own, // in order to remove the jwt on the browser. Necessary for wrong jwts @@ -131,14 +139,18 @@ server.get("/*", async (req, res) => { auth = undefined; try_site = await client.getSite(getSiteForm); } + + if (!auth && isAuthPath(path)) { + res.redirect("/"); + } const site: GetSiteResponse = try_site; initializeSite(site); const initialFetchReq: InitialFetchRequest = { client, auth, - path: req.path, - query: req.query, + path, + query, site, }; @@ -146,7 +158,7 @@ server.get("/*", async (req, res) => { promises.push(...activeRoute.fetchInitialData(initialFetchReq)); } - const routeData = await Promise.all(promises); + let routeData = await Promise.all(promises); // Redirect to the 404 if there's an API error if (routeData[0] && routeData[0].error) { @@ -155,24 +167,36 @@ server.get("/*", async (req, res) => { if (error === "instance_is_private") { return res.redirect(`/signup`); } else { - return res.send(`404: ${removeAuthParam(error)}`); + const errorPageData: ErrorPageData = { type: "error" }; + + // Exact error should only be seen in a development environment. Users + // in production will get a more generic message. + if (VERSION === "dev") { + errorPageData.error = error; + } + + const adminMatrixIds = site.admins + .map(({ person: { matrix_user_id } }) => matrix_user_id) + .filter(id => id) as string[]; + if (adminMatrixIds.length > 0) { + errorPageData.adminMatrixIds = adminMatrixIds; + } + + routeData = [errorPageData]; } } const isoData: IsoData = { - path: req.path, + path, site_res: site, routeData, }; const wrapper = ( - + ); - if (context.url) { - return res.redirect(context.url); - } const eruda = ( <> @@ -260,7 +284,8 @@ server.get("/*", async (req, res) => { `); } catch (err) { console.error(err); - return res.send(`404: ${removeAuthParam(err)}`); + res.statusCode = 500; + return res.send(VERSION === "dev" ? err.message : "Server error"); } }); @@ -292,16 +317,6 @@ process.on("SIGINT", () => { process.exit(0); }); -function removeAuthParam(err: any): string { - return removeParam(err.toString(), "auth"); -} - -function removeParam(url: string, parameter: string): string { - return url - .replace(new RegExp("[?&]" + parameter + "=[^&#]*(#.*)?$"), "$1") - .replace(new RegExp("([?&])" + parameter + "=[^&]*&"), "$1"); -} - const iconSizes = [72, 96, 128, 144, 152, 192, 384, 512]; const defaultLogoPathDirectory = path.join( process.cwd(), diff --git a/src/shared/components/app/app.tsx b/src/shared/components/app/app.tsx index 624d6e52..1251a5e4 100644 --- a/src/shared/components/app/app.tsx +++ b/src/shared/components/app/app.tsx @@ -3,10 +3,12 @@ import { Provider } from "inferno-i18next-dess"; import { Route, Switch } from "inferno-router"; import { i18n } from "../../i18next"; import { routes } from "../../routes"; -import { setIsoData } from "../../utils"; +import { isAuthPath, setIsoData } from "../../utils"; +import AuthGuard from "../common/auth-guard"; +import ErrorGuard from "../common/error-guard"; +import { ErrorPage } from "./error-page"; import { Footer } from "./footer"; import { Navbar } from "./navbar"; -import { NoMatch } from "./no-match"; import "./styles.scss"; import { Theme } from "./theme"; @@ -16,8 +18,8 @@ export class App extends Component { super(props, context); } render() { - let siteRes = this.isoData.site_res; - let siteView = siteRes.site_view; + const siteRes = this.isoData.site_res; + const siteView = siteRes.site_view; return ( <> @@ -27,10 +29,26 @@ export class App extends Component {
- {routes.map(({ path, component }) => ( - + {routes.map(({ path, component: RouteComponent }) => ( + ( + + {RouteComponent && + (isAuthPath(path ?? "") ? ( + + + + ) : ( + + ))} + + )} + /> ))} - +