Skip to content

Build your personal knowledge base with Trilium Notes

test(llm): test prompt injection

Elian Dorand6cc07bddd48
diff --git a/apps/server/src/services/llm/providers/anthropic.spec.ts b/apps/server/src/services/llm/providers/anthropic.spec.ts
index 96b6eb6af1..61c567a81e 100644
--- a/apps/server/src/services/llm/providers/anthropic.spec.ts
+++ b/apps/server/src/services/llm/providers/anthropic.spec.ts
@@ -1,4 +1,5 @@
-import { describe, expect, it, vi, beforeEach } from "vitest";
+import type { LlmMessage } from "@triliumnext/commons";
+import { beforeEach, describe, expect, it, vi } from "vitest";
const createAnthropicMock = vi.fn();
@@ -11,6 +12,15 @@ vi.mock("@ai-sdk/anthropic", () => ({
}
}));
+const { streamTextMock } = vi.hoisted(() => ({
+ streamTextMock: vi.fn((..._args: any[]) => ({}) as any)
+}));
+
+vi.mock("ai", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("ai")>();
+ return { ...actual, streamText: streamTextMock };
+});
+
import { AnthropicProvider } from "./anthropic.js";
describe("AnthropicProvider construction", () => {
@@ -41,3 +51,76 @@ describe("AnthropicProvider construction", () => {
expect(() => new AnthropicProvider("")).toThrow(/API key is required/);
});
});
+
+describe("AnthropicProvider message building", () => {
+ beforeEach(() => {
+ streamTextMock.mockClear();
+ });
+
+ it("buildSystemMessage returns a system message with an ephemeral cache breakpoint", () => {
+ const provider = new AnthropicProvider("sk-ant-test") as any;
+
+ expect(provider.buildSystemMessage("You are helpful.")).toEqual({
+ role: "system",
+ content: "You are helpful.",
+ providerOptions: { anthropic: { cacheControl: { type: "ephemeral" } } }
+ });
+ expect(provider.buildSystemMessage(undefined)).toBeUndefined();
+ expect(provider.buildSystemMessage("")).toBeUndefined();
+ });
+
+ it("buildMessages emits only user/assistant turns, never a system role", () => {
+ const provider = new AnthropicProvider("sk-ant-test") as any;
+ const messages: LlmMessage[] = [
+ { role: "user", content: "hi" },
+ { role: "assistant", content: "" },
+ { role: "user", content: "bye" }
+ ];
+
+ const built = provider.buildMessages(messages);
+
+ expect(built.map((m: any) => m.role)).toEqual(["user", "assistant", "user"]);
+ // Anthropic rejects empty content blocks — empty turns get a placeholder.
+ expect(built[1].content).toBe("(tool use)");
+ });
+
+ it("chat() routes the system prompt into the `system` option and forbids system messages in `messages`", () => {
+ const provider = new AnthropicProvider("sk-ant-test");
+ provider.chat(
+ [
+ { role: "system", content: "BASE PROMPT" },
+ { role: "user", content: "hello" }
+ ],
+ {}
+ );
+
+ expect(streamTextMock).toHaveBeenCalledOnce();
+ const opts = streamTextMock.mock.calls[0][0] as any;
+
+ expect(opts.allowSystemInMessages).toBe(false);
+ expect(opts.messages.every((m: any) => m.role !== "system")).toBe(true);
+ expect(opts.system.role).toBe("system");
+ expect(opts.system.content).toContain("BASE PROMPT");
+ expect(opts.system.providerOptions).toEqual({
+ anthropic: { cacheControl: { type: "ephemeral" } }
+ });
+ });
+
+ it("chat() with extended thinking also guards against system messages in `messages`", () => {
+ const provider = new AnthropicProvider("sk-ant-test");
+ provider.chat(
+ [
+ { role: "system", content: "BASE PROMPT" },
+ { role: "user", content: "hello" }
+ ],
+ { enableExtendedThinking: true }
+ );
+
+ const opts = streamTextMock.mock.calls[0][0] as any;
+
+ expect(opts.allowSystemInMessages).toBe(false);
+ expect(opts.messages.every((m: any) => m.role !== "system")).toBe(true);
+ expect(opts.system.role).toBe("system");
+ expect(opts.system.content).toContain("BASE PROMPT");
+ });
+});
diff --git a/apps/server/src/services/llm/providers/google.spec.ts b/apps/server/src/services/llm/providers/google.spec.ts
index 88974f22f1..afdf8542cc 100644
--- a/apps/server/src/services/llm/providers/google.spec.ts
+++ b/apps/server/src/services/llm/providers/google.spec.ts
@@ -1,4 +1,5 @@
-import { describe, expect, it, vi, beforeEach } from "vitest";
+import type { LlmMessage } from "@triliumnext/commons";
+import { beforeEach, describe, expect, it, vi } from "vitest";
const createGoogleMock = vi.fn();
@@ -11,6 +12,15 @@ vi.mock("@ai-sdk/google", () => ({
}
}));
+const { streamTextMock } = vi.hoisted(() => ({
+ streamTextMock: vi.fn((..._args: any[]) => ({}) as any)
+}));
+
+vi.mock("ai", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("ai")>();
+ return { ...actual, streamText: streamTextMock };
+});
+
import { GoogleProvider } from "./google.js";
describe("GoogleProvider construction", () => {
@@ -41,3 +51,68 @@ describe("GoogleProvider construction", () => {
expect(() => new GoogleProvider("")).toThrow(/API key is required/);
});
});
+
+describe("GoogleProvider message building", () => {
+ beforeEach(() => {
+ streamTextMock.mockClear();
+ });
+
+ it("buildSystemMessage returns the system prompt as a plain string", () => {
+ const provider = new GoogleProvider("test-key") as any;
+
+ expect(provider.buildSystemMessage("You are helpful.")).toBe("You are helpful.");
+ expect(provider.buildSystemMessage(undefined)).toBeUndefined();
+ });
+
+ it("buildMessages maps user/assistant turns and emits no system role", () => {
+ const provider = new GoogleProvider("test-key") as any;
+ const messages: LlmMessage[] = [
+ { role: "user", content: "hi" },
+ { role: "assistant", content: "hello" }
+ ];
+
+ const built = provider.buildMessages(messages);
+
+ expect(built).toEqual([
+ { role: "user", content: "hi" },
+ { role: "assistant", content: "hello" }
+ ]);
+ });
+
+ it("chat() routes the system prompt into the `system` option and forbids system messages in `messages`", () => {
+ const provider = new GoogleProvider("test-key");
+ provider.chat(
+ [
+ { role: "system", content: "BASE PROMPT" },
+ { role: "user", content: "hello" }
+ ],
+ {}
+ );
+
+ expect(streamTextMock).toHaveBeenCalledOnce();
+ const opts = streamTextMock.mock.calls[0][0] as any;
+
+ expect(opts.allowSystemInMessages).toBe(false);
+ expect(opts.messages.every((m: any) => m.role !== "system")).toBe(true);
+ expect(typeof opts.system).toBe("string");
+ expect(opts.system).toContain("BASE PROMPT");
+ });
+
+ it("chat() with extended thinking also guards against system messages in `messages`", () => {
+ const provider = new GoogleProvider("test-key");
+ provider.chat(
+ [
+ { role: "system", content: "BASE PROMPT" },
+ { role: "user", content: "hello" }
+ ],
+ { enableExtendedThinking: true }
+ );
+
+ const opts = streamTextMock.mock.calls[0][0] as any;
+
+ expect(opts.allowSystemInMessages).toBe(false);
+ expect(opts.messages.every((m: any) => m.role !== "system")).toBe(true);
+ expect(typeof opts.system).toBe("string");
+ expect(opts.system).toContain("BASE PROMPT");
+ });
+});