Skip to content

Build your personal knowledge base with Trilium Notes

test(server): review comments marked with HIGH

Elian Doran391285ac1570
diff --git a/apps/server/src/services/image_provider.spec.ts b/apps/server/src/services/image_provider.spec.ts
index b0a3ce41d3..ebb6fcab98 100644
--- a/apps/server/src/services/image_provider.spec.ts
+++ b/apps/server/src/services/image_provider.spec.ts
@@ -112,10 +112,14 @@ describe('serverImageProvider.processImage', () => {
expect(result.format).toEqual({ ext: 'gif', mime: 'image/gif' });
});
- it('does not shrink animated images', async () => {
+ it('leaves an animated GIF untouched (skipped at the non-jpg/png format gate)', async () => {
+ // image-type classifies an animated GIF as { ext: "gif" }, so it is excluded
+ // by the format check BEFORE the isAnimated() guard is ever consulted — the
+ // buffer and detected format must be returned unchanged.
const result = await serverImageProvider.processImage(animatedGif, 'a.gif', true);
expect(result.buffer).toBe(animatedGif);
+ expect(result.format).toEqual({ ext: 'gif', mime: 'image/gif' });
});
it('does not shrink when shrink is not requested', async () => {
diff --git a/apps/server/src/services/safe_fetch.spec.ts b/apps/server/src/services/safe_fetch.spec.ts
index 98ccea6a79..e0ec1aa86a 100644
--- a/apps/server/src/services/safe_fetch.spec.ts
+++ b/apps/server/src/services/safe_fetch.spec.ts
@@ -233,6 +233,19 @@ describe("safeFetch", () => {
expect(text).toBe("final");
});
+ it("re-validates redirect targets and blocks a redirect to a private host", async () => {
+ // A public host that 302s to an internal IP — the classic SSRF redirect.
+ fetchMock.mockResolvedValueOnce(
+ makeResponse(null, { status: 302, headers: { location: "http://10.0.0.1/evil" } })
+ );
+
+ await expect(safeFetch("http://8.8.8.8/start")).rejects.toThrow("private/internal");
+ // The private redirect target must never be fetched (validation runs first).
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ // The first (public) dispatcher was closed before following the redirect.
+ expect(agentInstances[0].closed).toBe(true);
+ });
+
it("throws when a redirect response has no Location header", async () => {
fetchMock.mockResolvedValueOnce(makeResponse(null, { status: 301 }));
diff --git a/apps/server/src/services/utils.spec.ts b/apps/server/src/services/utils.spec.ts
index 5201d42bfe..6547782bc8 100644
--- a/apps/server/src/services/utils.spec.ts
+++ b/apps/server/src/services/utils.spec.ts
@@ -1,5 +1,6 @@
import { utils as coreUtils } from "@triliumnext/core";
import { EventEmitter } from "events";
+import path from "path";
import { afterEach, describe, expect, it, vi } from "vitest";
import utils from "./utils";
@@ -141,19 +142,20 @@ describe("getResourceDir", () => {
return (await import("./utils.js")).getResourceDir;
}
- it("uses __dirname in dev mode", async () => {
- process.env.TRILIUM_ENV = "dev";
- delete (process.versions as { electron?: string }).electron;
- const getResourceDir = await loadFresh();
- expect(getResourceDir().endsWith("..") || getResourceDir().length > 0).toBe(true);
- });
-
- it("uses __dirname under Electron in production", async () => {
+ it("returns the module dir under Electron prod and its parent in dev", async () => {
+ // Electron production → returns the module's own __dirname, NOT the
+ // argv-derived executable dir used by the non-electron prod branch.
delete process.env.TRILIUM_ENV;
+ process.argv[1] = "/opt/trilium/bin/server.js";
Object.defineProperty(process.versions, "electron", { value: "30.0.0", configurable: true });
- const getResourceDir = await loadFresh();
- // The electron prod branch returns __dirname of the loaded module.
- expect(getResourceDir()).toBeTypeOf("string");
+ const electronDir = (await loadFresh())();
+ expect(electronDir).not.toBe("/opt/trilium/bin");
+
+ // Dev → returns the parent of that same module dir.
+ process.env.TRILIUM_ENV = "dev";
+ delete (process.versions as { electron?: string }).electron;
+ const devDir = (await loadFresh())();
+ expect(devDir).toBe(path.join(electronDir, ".."));
});
it("uses the executable directory in non-electron production", async () => {