Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): check the destination of a page Codex opens by a search result's id

Elian Doran328cc7b2c43d
diff --git a/apps/server/src/services/llm/providers/codex_agent.ts b/apps/server/src/services/llm/providers/codex_agent.ts
index 72d87f3faa..461455e0ab 100644
--- a/apps/server/src/services/llm/providers/codex_agent.ts
+++ b/apps/server/src/services/llm/providers/codex_agent.ts
@@ -229,7 +229,10 @@ export class CodexAgentProvider extends AcpAgentProvider {
}
const search = codexSearchSources(payload);
if (!search) {
- return decideCodexToolCall(payload, sessionId => this.turnConfigOf(sessionId));
+ return decideCodexToolCall(payload, {
+ configOf: sessionId => this.turnConfigOf(sessionId),
+ urlOf: (sessionId, ref) => this.sourceUrl(sessionId, ref)
+ });
}
const config = this.turnConfigOf(search.sessionId);
if (config) {
@@ -252,9 +255,8 @@ export class CodexAgentProvider extends AcpAgentProvider {
if (event?.hook_event_name !== "PreToolUse" || event.tool_name !== "webrun" || typeof event.tool_use_id !== "string") {
return;
}
- const config = typeof event.session_id === "string" ? this.turnConfigOf(event.session_id) : undefined;
- const sources = config ? sourcesByTurn.get(config) : undefined;
- webrunCalls.set(event.tool_use_id, describeWebrunInput(event.tool_input, ref => sources?.get(ref)?.url));
+ const sessionId = typeof event.session_id === "string" ? event.session_id : undefined;
+ webrunCalls.set(event.tool_use_id, describeWebrunInput(event.tool_input, ref => (sessionId ? this.sourceUrl(sessionId, ref) : undefined)));
for (const oldest of webrunCalls.keys()) {
if (webrunCalls.size <= MAX_WEBRUN_CALLS) {
break;
@@ -263,6 +265,12 @@ export class CodexAgentProvider extends AcpAgentProvider {
}
}
+ /** The URL of a search result the web searches of the turn running in `sessionId` returned, by its id. */
+ private sourceUrl(sessionId: string, ref: string): string | undefined {
+ const config = this.turnConfigOf(sessionId);
+ return config ? sourcesByTurn.get(config)?.get(ref)?.url : undefined;
+ }
+
protected describeFailure(error: unknown): string {
const text = describeError(error);
const signInFailure = this.describeSignInFailure(error);
diff --git a/apps/server/src/services/llm/providers/codex_hook.spec.ts b/apps/server/src/services/llm/providers/codex_hook.spec.ts
index 5281ee7087..105ba62252 100644
--- a/apps/server/src/services/llm/providers/codex_hook.spec.ts
+++ b/apps/server/src/services/llm/providers/codex_hook.spec.ts
@@ -21,17 +21,22 @@ const denied = (reason: RegExp) => ({ hookSpecificOutput: { hookEventName: "PreT
describe("decideCodexToolCall", () => {
const configs: Record<string, { enableWebSearch?: boolean }> = { web: { enableWebSearch: true }, offline: {} };
- const configOf = (sessionId: string) => configs[sessionId];
+ // The web chat's searches returned two results: one on the public internet, one on the local network.
+ const results: Record<string, string> = { turn1search0: "https://example.com/news", turn1search1: "http://router.lan/admin" };
+ const turns = {
+ configOf: (sessionId: string) => configs[sessionId],
+ urlOf: (sessionId: string, ref: string) => (sessionId === "web" ? results[ref] : undefined)
+ };
// example.com resolves to a public address, everything else to a private one.
const lookup = async (host: string) => (host === "example.com" ? [ "93.184.215.14" ] : [ "192.168.1.1" ]);
- const decide = (event: object, sessionId = "web") => decideCodexToolCall({ ...event, session_id: sessionId, hook_event_name: "PreToolUse" }, configOf, lookup);
+ const decide = (event: object, sessionId = "web") => decideCodexToolCall({ ...event, session_id: sessionId, hook_event_name: "PreToolUse" }, turns, lookup);
it("lets Trilium's note tools through and denies every other tool, whatever the chat allows", async () => {
expect(await decide(EVENTS.noteTool)).toEqual({});
for (const event of [ EVENTS.shell, EVENTS.viewImage, EVENTS.otherMcp, { tool_name: "browser_use" }, {} ]) {
expect(await decide(event)).toEqual(denied(/note tools/));
}
- expect(await decideCodexToolCall(null, configOf, lookup)).toEqual(denied(/note tools/));
+ expect(await decideCodexToolCall(null, turns, lookup)).toEqual(denied(/note tools/));
});
it("lets the web search through only in a chat that allows it, and only to public addresses", async () => {
@@ -41,7 +46,14 @@ describe("decideCodexToolCall", () => {
expect(await decide(EVENTS.search, "offline")).toEqual(denied(/turned off/));
// A session no chat turn is attached to, such as the title's, has no web access.
expect(await decide(EVENTS.search, "unknown")).toEqual(denied(/turned off/));
- expect(await decideCodexToolCall(EVENTS.search, configOf, lookup)).toEqual(denied(/turned off/));
+ expect(await decideCodexToolCall(EVENTS.search, turns, lookup)).toEqual(denied(/turned off/));
+ });
+
+ it("checks a page opened by a search result's id by that result's URL, and refuses an id it cannot check", async () => {
+ const openResult = (ref: string) => ({ tool_name: "webrun", tool_input: { open: [ { ref_id: ref } ], response_length: "medium" } });
+ expect(await decide(openResult("turn1search0"))).toEqual({});
+ expect(await decide(openResult("turn1search1"))).toEqual(denied(/private addresses/));
+ expect(await decide(openResult("turn9search9"))).toEqual(denied(/search results this chat/));
});
});
diff --git a/apps/server/src/services/llm/providers/codex_hook.ts b/apps/server/src/services/llm/providers/codex_hook.ts
index 7ebc5c03b6..b3031c27ed 100644
--- a/apps/server/src/services/llm/providers/codex_hook.ts
+++ b/apps/server/src/services/llm/providers/codex_hook.ts
@@ -54,18 +54,24 @@ const HOOK_TIMEOUT_S = 10;
const NOTE_TOOLS_ONLY = "Trilium does not allow this tool. Use Trilium's note tools to read, search and edit the user's notes instead.";
const WEB_SEARCH_OFF = "Web search is turned off for this chat.";
const PRIVATE_ADDRESS = "Trilium does not allow opening local or private addresses.";
+const UNKNOWN_RESULT = "Trilium can only open search results this chat's searches returned. Open the page by its URL instead.";
+
+/** What the hook knows of the chat turn running in a session (the event's `session_id`, the ACP session id). */
+export interface CodexTurns {
+ /** The turn's configuration. */
+ configOf(sessionId: string): LlmProviderConfig | undefined;
+ /** The URL of a search result the turn's web searches returned, by its id (`turn1search0`). */
+ urlOf(sessionId: string, ref: string): string | undefined;
+}
/**
* The answer to one `PreToolUse` call: Trilium's note tools pass, and the web
* search passes in a chat that allows it when every URL it opens is on the
- * public internet. Everything else is denied. `configOf` gives the chat turn's
- * configuration for the event's `session_id`, the ACP session id.
+ * public internet. A page opened by a search result's id is checked by that
+ * result's URL, and an id the turn's searches never returned is refused, as
+ * its destination cannot be checked. Everything else is denied.
*/
-export async function decideCodexToolCall(
- payload: unknown,
- configOf: (sessionId: string) => LlmProviderConfig | undefined,
- lookup?: HostLookup
-): Promise<CodexHookAnswer> {
+export async function decideCodexToolCall(payload: unknown, turns: CodexTurns, lookup?: HostLookup): Promise<CodexHookAnswer> {
const event = payload as { tool_name?: unknown; tool_input?: unknown; session_id?: unknown } | null;
const toolName = typeof event?.tool_name === "string" ? event.tool_name : "";
if (toolName.startsWith(`mcp__${NOTE_TOOLS_MCP_SERVER_NAME}__`)) {
@@ -74,11 +80,20 @@ export async function decideCodexToolCall(
if (toolName !== WEB_TOOL) {
return deny(NOTE_TOOLS_ONLY);
}
- const config = typeof event?.session_id === "string" ? configOf(event.session_id) : undefined;
- if (config?.enableWebSearch !== true) {
+ const sessionId = typeof event?.session_id === "string" ? event.session_id : undefined;
+ if (sessionId === undefined || turns.configOf(sessionId)?.enableWebSearch !== true) {
return deny(WEB_SEARCH_OFF);
}
- const urls = stringsIn(event?.tool_input).filter(value => /^https?:\/\//i.test(value));
+ const isUrl = (value: string) => /^https?:\/\//i.test(value);
+ const opened = (event?.tool_input as { open?: unknown } | null | undefined)?.open;
+ const urls = stringsIn(event?.tool_input).filter(isUrl);
+ for (const ref of stringsIn(opened).filter(value => !isUrl(value))) {
+ const url = turns.urlOf(sessionId, ref);
+ if (url === undefined) {
+ return deny(UNKNOWN_RESULT);
+ }
+ urls.push(url);
+ }
const allPublic = await Promise.all(urls.map(url => isPublicHttpUrl(url, lookup)));
return allPublic.every(Boolean) ? {} : deny(PRIVATE_ADDRESS);
}