Skip to content

Build your personal knowledge base with Trilium Notes

chore(deps): address requested changes

Elian Doran48cf214f4c66
diff --git a/apps/client/src/widgets/type_widgets/options/llm.tsx b/apps/client/src/widgets/type_widgets/options/llm.tsx
index 2f7bc7d5df..9da9f50ca6 100644
--- a/apps/client/src/widgets/type_widgets/options/llm.tsx
+++ b/apps/client/src/widgets/type_widgets/options/llm.tsx
@@ -83,7 +83,7 @@ function ProviderSettings() {
function getMcpEndpointUrl() {
const port = window.location.port || (window.location.protocol === "https:" ? "443" : "80");
- return `http://localhost:${port}/mcp`;
+ return `${window.location.protocol}//localhost:${port}/mcp`;
}
function McpSettings() {
diff --git a/apps/server/src/routes/mcp.ts b/apps/server/src/routes/mcp.ts
index baf48d0e59..c47fb6bf48 100644
--- a/apps/server/src/routes/mcp.ts
+++ b/apps/server/src/routes/mcp.ts
@@ -30,7 +30,9 @@ function mcpGuard(req: express.Request, res: express.Response, next: express.Nex
return;
}
- if (!isLoopback(req.socket.remoteAddress)) {
+ // Use req.ip which respects trust proxy settings, falling back to socket address
+ const clientIp = req.ip || req.socket.remoteAddress;
+ if (!isLoopback(clientIp)) {
res.status(403).json({ error: "MCP is only available from localhost" });
return;
}
diff --git a/apps/server/src/services/llm/providers/base_provider.ts b/apps/server/src/services/llm/providers/base_provider.ts
index 3bf1d64010..5000b4fcbf 100644
--- a/apps/server/src/services/llm/providers/base_provider.ts
+++ b/apps/server/src/services/llm/providers/base_provider.ts
@@ -105,6 +105,10 @@ export abstract class BaseProvider implements LlmProvider {
parts.push(
`When referring to notes in your responses, use the wiki-link format [[noteId]] to create clickable internal links. Use the note ID (not the title) from tool results. The link will automatically display the note's title and icon, so don't repeat the title in your text. For example: "You can find more details in [[ZjSfLhzlqNY6]]" instead of "You can find more details in the Meeting Notes note ([[ZjSfLhzlqNY6]])".`
);
+ } else if (config.contextNoteId) {
+ parts.push(
+ `You can see the current note's metadata above, but you cannot search or access other notes. If the user asks about other notes, inform them that "Note access" is disabled and they need to enable it in the chat settings (click on the model name dropdown and toggle "Note access").`
+ );
} else {
parts.push(
`You do not have access to the user's notes. If the user asks about their notes, inform them that "Note access" is disabled and they need to enable it in the chat settings (click on the model name dropdown and toggle "Note access").`
diff --git a/apps/server/src/services/llm/tools/helpers.ts b/apps/server/src/services/llm/tools/helpers.ts
index 2f71524541..fbfebb1e12 100644
--- a/apps/server/src/services/llm/tools/helpers.ts
+++ b/apps/server/src/services/llm/tools/helpers.ts
@@ -2,9 +2,9 @@
* Shared helpers for LLM tools — content conversion, metadata building, and previews.
*/
+import becca from "../../../becca/becca.js";
import type BAttachment from "../../../becca/entities/battachment.js";
import type BNote from "../../../becca/entities/bnote.js";
-import becca from "../../../becca/becca.js";
import markdownExport from "../../export/markdown.js";
import markdownImport from "../../import/markdown.js";
@@ -179,7 +179,7 @@ export function getNoteMeta(note: BNote, limits: NoteMetaLimits) {
return {
noteId: note.noteId,
isProtected: flag(note.isProtected),
- title: note.title,
+ title: note.getTitleOrProtected(),
type: note.type,
mime: note.mime,
dateCreated: note.dateCreated,
diff --git a/packages/commons/src/lib/marked_extensions.spec.ts b/packages/commons/src/lib/marked_extensions.spec.ts
new file mode 100644
index 0000000000..963c0a4a71
--- /dev/null
+++ b/packages/commons/src/lib/marked_extensions.spec.ts
@@ -0,0 +1,96 @@
+import { describe, expect, it } from "vitest";
+import { Marked } from "marked";
+import { createWikiLinkExtension, createTransclusionExtension } from "./marked_extensions.js";
+
+describe("marked_extensions", () => {
+ describe("createWikiLinkExtension", () => {
+ it("should render basic wiki links", () => {
+ const marked = new Marked({ extensions: [createWikiLinkExtension()] });
+ const result = marked.parse("[[abc123]]");
+ expect(result).toContain('<a class="reference-link" href="/abc123">abc123</a>');
+ });
+
+ it("should escape HTML in link text to prevent XSS", () => {
+ const marked = new Marked({ extensions: [createWikiLinkExtension()] });
+ // Malicious input attempting to inject HTML/script via link text
+ const result = marked.parse("[[<script>alert('xss')</script>]]");
+
+ // The output should NOT contain unescaped script tags
+ expect(result).not.toContain("<script>");
+ expect(result).not.toContain("</script>");
+ // Should be properly escaped
+ expect(result).toContain("&lt;script&gt;");
+ });
+
+ it("should escape attribute-breaking characters in href to prevent XSS", () => {
+ const marked = new Marked({ extensions: [createWikiLinkExtension()] });
+ // Malicious input attempting to break out of href attribute
+ const result = marked.parse('[[x" onclick="alert(1)"]]');
+
+ // The output should NOT allow breaking out of the href attribute
+ // The key is that quotes are escaped, so onclick can't become an actual attribute
+ expect(result).not.toContain('href="/x"'); // Would indicate unescaped quote breaking out
+ expect(result).not.toContain('" onclick="'); // Unescaped pattern that would create event handler
+ // Double quotes should be escaped
+ expect(result).toContain('&quot;');
+ // The href should contain the escaped malicious input, not be broken by it
+ expect(result).toContain('href="/x&quot;');
+ });
+
+ it("should handle custom formatHref safely", () => {
+ const marked = new Marked({
+ extensions: [createWikiLinkExtension({ formatHref: (id) => `#root/${id}` })]
+ });
+ const result = marked.parse('[[x"><img src=x onerror=alert(1)>]]');
+
+ // The < and > should be escaped so no img tag is injected
+ expect(result).not.toContain('<img src'); // Actual img tag
+ expect(result).toContain('&lt;img'); // Escaped version
+ expect(result).toContain('&gt;'); // Escaped >
+ });
+ });
+
+ describe("createTransclusionExtension", () => {
+ it("should render basic transclusions", () => {
+ const marked = new Marked({ extensions: [createTransclusionExtension()] });
+ const result = marked.parse("![[abc123]]");
+ expect(result).toContain('<img src="/abc123">');
+ });
+
+ it("should escape attribute-breaking characters in src to prevent XSS", () => {
+ const marked = new Marked({ extensions: [createTransclusionExtension()] });
+ // Malicious input attempting to break out of src attribute
+ const result = marked.parse('![[x" onerror="alert(1)"]]');
+
+ // The output should NOT allow breaking out of the src attribute
+ // The key is that quotes are escaped, so onerror can't become an actual attribute
+ expect(result).not.toContain('src="/x"'); // Would indicate unescaped quote
+ expect(result).not.toContain('" onerror="'); // Unescaped pattern
+ // Double quotes should be escaped
+ expect(result).toContain('&quot;');
+ // The src should contain the escaped malicious input
+ expect(result).toContain('src="/x&quot;');
+ });
+
+ it("should escape HTML injection attempts in transclusion", () => {
+ const marked = new Marked({ extensions: [createTransclusionExtension()] });
+ // Attempt to close img tag and inject script
+ const result = marked.parse('![[x"><script>alert(1)</script>]]');
+
+ expect(result).not.toContain('<script>');
+ expect(result).not.toContain('</script>');
+ });
+
+ it("should handle custom formatSrc safely", () => {
+ const marked = new Marked({
+ extensions: [createTransclusionExtension({ formatSrc: (id) => `/api/images/${id}` })]
+ });
+ const result = marked.parse('![[x" onload="alert(1)]]');
+
+ // The quote should be escaped so onload can't become an actual attribute
+ expect(result).not.toContain('src="/api/images/x"'); // Would indicate unescaped quote
+ expect(result).toContain('&quot;'); // Quote should be escaped
+ expect(result).toContain('src="/api/images/x&quot;'); // Escaped version
+ });
+ });
+});
diff --git a/packages/commons/src/lib/marked_extensions.ts b/packages/commons/src/lib/marked_extensions.ts
index 3522ace7b4..413a8220e4 100644
--- a/packages/commons/src/lib/marked_extensions.ts
+++ b/packages/commons/src/lib/marked_extensions.ts
@@ -1,5 +1,18 @@
import type { TokenizerAndRendererExtension } from "marked";
+/**
+ * Escapes HTML special characters to prevent XSS attacks.
+ * Used for both attribute values and text content.
+ */
+function escapeHtml(str: string): string {
+ return str
+ .replace(/&/g, "&amp;")
+ .replace(/</g, "&lt;")
+ .replace(/>/g, "&gt;")
+ .replace(/"/g, "&quot;")
+ .replace(/'/g, "&#39;");
+}
+
export interface WikiLinkOptions {
/** Format the href for the link. Defaults to `/${noteId}` */
formatHref?: (noteId: string) => string;
@@ -40,7 +53,7 @@ export function createWikiLinkExtension(options: WikiLinkOptions = {}): Tokenize
renderer(token) {
const noteId = token.href as string;
- return `<a class="reference-link" href="${formatHref(noteId)}">${token.text}</a>`;
+ return `<a class="reference-link" href="${escapeHtml(formatHref(noteId))}">${escapeHtml(token.text as string)}</a>`;
}
};
}
@@ -82,7 +95,7 @@ export function createTransclusionExtension(options: TransclusionOptions = {}):
renderer(token) {
const noteId = token.href as string;
- return `<img src="${formatSrc(noteId)}">`;
+ return `<img src="${escapeHtml(formatSrc(noteId))}">`;
}
};
}