Skip to content

Build your personal knowledge base with Trilium Notes

fix(share): redirect to login instead of 404 when bare-domain share root is missing (closes #7869)

Elian Doranc09cad2c39db
diff --git a/apps/server/src/services/auth.spec.ts b/apps/server/src/services/auth.spec.ts
index f2a0fac3c4..ec61af9011 100644
--- a/apps/server/src/services/auth.spec.ts
+++ b/apps/server/src/services/auth.spec.ts
@@ -155,15 +155,17 @@ describe("Auth", () => {
expect(res.redirectedTo).toBe("login");
});
- it("checkAuth honours redirectBareDomain: 404 when no shareRoot, else redirects to share", () => {
+ it("checkAuth honours redirectBareDomain: redirects to share when a shareRoot exists, else to login", () => {
vi.spyOn(totp, "isTotpEnabled").mockReturnValue(false);
vi.spyOn(openID, "isOpenIDEnabled").mockReturnValue(false);
cls.init(() => options.setOption("redirectBareDomain", "true"));
+ // No shareRoot configured: fall back to login instead of a 404 dead-end (#7869).
const labelSpy = vi.spyOn(attributes, "getNotesWithLabel").mockReturnValue([]);
- const res404 = makeRes();
- auth.checkAuth(makeReq({ session: { loggedIn: false } }), res404 as never, vi.fn());
- expect(res404.statusCode).toBe(404);
+ const resLogin = makeRes();
+ auth.checkAuth(makeReq({ session: { loggedIn: false } }), resLogin as never, vi.fn());
+ expect(resLogin.redirectedTo).toBe("login");
+ expect(resLogin.statusCode).not.toBe(404);
labelSpy.mockReturnValue([{ noteId: "x" } as never]);
const resShare = makeRes();
diff --git a/apps/server/src/services/auth.ts b/apps/server/src/services/auth.ts
index 868126e817..347d9e65c9 100644
--- a/apps/server/src/services/auth.ts
+++ b/apps/server/src/services/auth.ts
@@ -36,14 +36,16 @@ function checkAuth(req: Request, res: Response, next: NextFunction) {
const hasRedirectBareDomain = options.getOptionOrNull("redirectBareDomain") === "true";
if (hasRedirectBareDomain) {
- // Check if any note has the #shareRoot label
+ // Only redirect to the share page when a share root is actually configured.
+ // Otherwise (e.g. the owner's session expired before they set one up) fall back
+ // to the login page rather than stranding the user on a 404. See #7869.
const shareRootNotes = attributes.getNotesWithLabel("shareRoot");
- if (shareRootNotes.length === 0) {
- res.status(404).json({ message: "Share root not found. Please set up a note with #shareRoot label first." });
+ if (shareRootNotes.length > 0) {
+ res.redirect("share");
return;
}
}
- res.redirect(hasRedirectBareDomain ? "share" : "login");
+ res.redirect("login");
} else if (currentTotpStatus !== lastAuthState.totpEnabled || currentSsoStatus !== lastAuthState.ssoEnabled) {
req.session.destroy((err) => {
if (err) console.error('Error destroying session:', err);