obsidian-sync-mcp 0.5.5 → 0.5.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -254,6 +254,7 @@ Without `MCP_AUTH_TOKEN`, the server runs without authentication — suitable fo
254
254
  | `BASE_URL` | Optional | `http://localhost:PORT` | Public URL (for OAuth callbacks when using a tunnel) |
255
255
  | `PORT` | Optional | `8787` | HTTP port |
256
256
  | `HOST` | Optional | `0.0.0.0` | Bind address (`127.0.0.1` to restrict to localhost) |
257
+ | `MCP_ALLOWED_HOSTS` | Optional | — | Comma-separated extra `Host` values accepted in no-auth mode (e.g. `192.168.1.5,mybox.local`). No-auth mode rejects any other Host to block browser DNS-rebinding; localhost is always allowed. Ignored when `MCP_AUTH_TOKEN` is set. |
257
258
  | `DATA_DIR` | Optional | `~/.obsidian-mcp` | Directory for persisted data (metadata index, auth tokens) |
258
259
  | `LOG_LEVEL` | Optional | — | Set to `debug` for verbose logging (library logs, change feed, index sync) |
259
260
  | `MCP_REFRESH_DAYS` | Optional | `14` | Days before auth session expires |
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env node
2
+ // Node polyfills for livesync-commonlib browser globals
3
+ if(!("navigator" in globalThis)){globalThis.navigator={language:"en"};}
4
+
5
+ // src/index-sync.ts
6
+ function deriveContent(doc) {
7
+ if (doc.deleted) return null;
8
+ return "data" in doc && Array.isArray(doc.data) ? doc.data.join("") : "";
9
+ }
10
+ function applyIndexChange(index, path, content, mtime) {
11
+ if (content !== null) {
12
+ index.update(path, content, mtime);
13
+ } else {
14
+ index.remove(path);
15
+ }
16
+ }
17
+
18
+ export {
19
+ deriveContent,
20
+ applyIndexChange
21
+ };
22
+ //# sourceMappingURL=chunk-SGLPTZJA.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index-sync.ts"],"sourcesContent":["/**\n * Shared search-index sync helpers.\n *\n * Kept dependency-free (no livesync-commonlib imports) so the delete-vs-empty\n * routing is unit-testable in isolation.\n */\n\nexport interface IndexTarget {\n update(path: string, content: string, mtime?: number): void;\n remove(path: string): void;\n}\n\n/**\n * Derive index content from a LiveSync change doc.\n *\n * Returns `null` ONLY when the note is deleted. An existing note with no body\n * yields `\"\"` so a zero-byte note stays distinguishable from a deletion —\n * without this, a note that serializes with no `data` array would look deleted.\n */\nexport function deriveContent(doc: { deleted?: boolean; data?: unknown }): string | null {\n if (doc.deleted) return null;\n return \"data\" in doc && Array.isArray(doc.data) ? doc.data.join(\"\") : \"\";\n}\n\n/**\n * Route a change into the index: `null` content removes (the note was deleted),\n * any string — including `\"\"` — upserts. Using `content !== null` (rather than a\n * truthiness check) keeps zero-byte notes indexed instead of dropping them\n * (issues #5 / #6).\n */\nexport function applyIndexChange(index: IndexTarget, path: string, content: string | null, mtime?: number): void {\n if (content !== null) {\n index.update(path, content, mtime);\n } else {\n index.remove(path);\n }\n}\n"],"mappings":";;;;;AAmBO,SAAS,cAAc,KAA2D;AACrF,MAAI,IAAI,QAAS,QAAO;AACxB,SAAO,UAAU,OAAO,MAAM,QAAQ,IAAI,IAAI,IAAI,IAAI,KAAK,KAAK,EAAE,IAAI;AAC1E;AAQO,SAAS,iBAAiB,OAAoB,MAAc,SAAwB,OAAsB;AAC7G,MAAI,YAAY,MAAM;AAClB,UAAM,OAAO,MAAM,SAAS,KAAK;AAAA,EACrC,OAAO;AACH,UAAM,OAAO,IAAI;AAAA,EACrB;AACJ;","names":[]}
package/dist/main.js CHANGED
@@ -1,6 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
  // Node polyfills for livesync-commonlib browser globals
3
3
  if(!("navigator" in globalThis)){globalThis.navigator={language:"en"};}
4
+ import {
5
+ applyIndexChange
6
+ } from "./chunk-SGLPTZJA.js";
4
7
  import {
5
8
  parseFrontmatterAndLinks
6
9
  } from "./chunk-52RMOUUH.js";
@@ -557,6 +560,40 @@ var SearchIndex = class {
557
560
  }
558
561
  };
559
562
 
563
+ // src/host-guard.ts
564
+ var LOCAL_HOSTS = ["localhost", "127.0.0.1", "::1"];
565
+ function parseHostname(hostHeader) {
566
+ if (!hostHeader) return null;
567
+ if (hostHeader.includes("@") || hostHeader.includes("/")) return null;
568
+ try {
569
+ const u = new URL(`http://${hostHeader}`);
570
+ if (u.username || u.password || u.pathname !== "/" || u.search || u.hash) return null;
571
+ return u.hostname.replace(/^\[|\]$/g, "").toLowerCase();
572
+ } catch {
573
+ return null;
574
+ }
575
+ }
576
+ function buildAllowedHosts(extra) {
577
+ const set = new Set(LOCAL_HOSTS);
578
+ for (const entry of (extra ?? "").split(",").map((s) => s.trim()).filter(Boolean)) {
579
+ set.add(parseHostname(entry) ?? entry.toLowerCase());
580
+ }
581
+ return set;
582
+ }
583
+ function isHostAllowed(hostHeader, allowed) {
584
+ const hostname = parseHostname(hostHeader);
585
+ return hostname !== null && allowed.has(hostname);
586
+ }
587
+ function isOriginAllowed(originHeader, allowed) {
588
+ if (!originHeader) return true;
589
+ try {
590
+ const hostname = new URL(originHeader).hostname.replace(/^\[|\]$/g, "").toLowerCase();
591
+ return allowed.has(hostname);
592
+ } catch {
593
+ return false;
594
+ }
595
+ }
596
+
560
597
  // src/tools.ts
561
598
  import { z } from "zod";
562
599
 
@@ -791,7 +828,7 @@ ${content}`;
791
828
  return `Failed to move: ${from} \u2192 ${to}`;
792
829
  }
793
830
  searchIndex2.remove(from);
794
- if (content) searchIndex2.update(to, content, Date.now());
831
+ if (content !== null) searchIndex2.update(to, content, Date.now());
795
832
  const deepLink = makeDeepLink(vaultName, to);
796
833
  return `Moved: ${from} \u2192 ${to}
797
834
  [Open in Obsidian](${deepLink})`;
@@ -895,7 +932,7 @@ if (VAULT_PATH) {
895
932
  console.error("COUCHDB_PASSWORD is required in remote mode.");
896
933
  process.exit(1);
897
934
  }
898
- const { Vault } = await import("./vault-5Y35MEZS.js");
935
+ const { Vault } = await import("./vault-BL4NJC75.js");
899
936
  vault = new Vault({
900
937
  couchdbUrl: COUCHDB_URL,
901
938
  couchdbUser: COUCHDB_USER,
@@ -924,11 +961,7 @@ async function rebuildIndex() {
924
961
  const start = performance.now();
925
962
  if (COUCHDB_URL && vault.catchUp) {
926
963
  const changeCallback = (path, content, mtime) => {
927
- if (content) {
928
- searchIndex.update(path, content, mtime);
929
- } else {
930
- searchIndex.remove(path);
931
- }
964
+ applyIndexChange(searchIndex, path, content, mtime);
932
965
  };
933
966
  let since = searchIndex.since || "0";
934
967
  if (debugLogging2) console.log(`[debug] CouchDB catch-up from since: ${since}`);
@@ -941,7 +974,7 @@ async function rebuildIndex() {
941
974
  try {
942
975
  const countingCallback = (path, content, mtime) => {
943
976
  changes++;
944
- if (debugLogging2) console.log(`[debug] Change: ${path} ${content ? "(update)" : "(delete)"}`);
977
+ if (debugLogging2) console.log(`[debug] Change: ${path} ${content !== null ? "(update)" : "(delete)"}`);
945
978
  changeCallback(path, content, mtime);
946
979
  };
947
980
  const newSince = await vault.catchUp(since, countingCallback, onBatch);
@@ -973,7 +1006,7 @@ async function rebuildIndex() {
973
1006
  for (let i = 0; i < notesWithMtime.length; i++) {
974
1007
  const { path, mtime } = notesWithMtime[i];
975
1008
  const content = await vault.readNote(path);
976
- if (content) searchIndex.update(path, content, mtime);
1009
+ if (content !== null) searchIndex.update(path, content, mtime);
977
1010
  if (notesWithMtime.length > 100 && (i + 1) % 500 === 0) {
978
1011
  console.log(` indexed ${i + 1}/${notesWithMtime.length}...`);
979
1012
  }
@@ -1011,13 +1044,8 @@ if (VAULT_PATH) {
1011
1044
  console.log("Watching vault for external changes.");
1012
1045
  } else if (COUCHDB_URL && vault.watchChanges) {
1013
1046
  vault.watchChanges((path, content, mtime, seq) => {
1014
- if (content) {
1015
- if (debugLogging2) console.log(`[debug] CouchDB change: ${path} (mtime: ${mtime})`);
1016
- searchIndex.update(path, content, mtime);
1017
- } else {
1018
- if (debugLogging2) console.log(`[debug] CouchDB delete: ${path}`);
1019
- searchIndex.remove(path);
1020
- }
1047
+ if (debugLogging2) console.log(`[debug] CouchDB ${content === null ? "delete" : "change"}: ${path}`);
1048
+ applyIndexChange(searchIndex, path, content, mtime);
1021
1049
  if (seq) searchIndex.since = String(seq);
1022
1050
  });
1023
1051
  console.log("Watching CouchDB for LiveSync changes.");
@@ -1025,7 +1053,7 @@ if (VAULT_PATH) {
1025
1053
  var BASE_INSTRUCTIONS = "Access and manage an Obsidian vault. You can read, write, list, search, move, and delete markdown notes. Every tool response includes an Obsidian deep link. Always show this link to the user using the format [obsidian://open?vault=...&file=...](obsidian://open?vault=...&file=...) so it is both clickable and visible as a URL.";
1026
1054
  var serverOptions = {
1027
1055
  name: "obsidian-sync-mcp",
1028
- version: "0.5.5",
1056
+ version: "0.5.7",
1029
1057
  instructions: MCP_EXTRA_INSTRUCTIONS ? `${BASE_INSTRUCTIONS}
1030
1058
 
1031
1059
  ${MCP_EXTRA_INSTRUCTIONS}` : BASE_INSTRUCTIONS
@@ -1045,11 +1073,20 @@ if (AUTH_TOKEN) {
1045
1073
  };
1046
1074
  console.log("Auth enabled (password-gated OAuth).");
1047
1075
  } else {
1076
+ const allowedHosts = buildAllowedHosts(process.env.MCP_ALLOWED_HOSTS);
1077
+ serverOptions.authenticate = async (req) => {
1078
+ if (!isHostAllowed(req.headers["host"], allowedHosts)) {
1079
+ throw new Response("Forbidden: Host not allowed", { status: 403 });
1080
+ }
1081
+ if (!isOriginAllowed(req.headers["origin"], allowedHosts)) {
1082
+ throw new Response("Forbidden: cross-origin request rejected", { status: 403 });
1083
+ }
1084
+ return { authenticated: true };
1085
+ };
1086
+ console.log(`Auth disabled \u2014 accepting only local Host/Origin headers: ${[...allowedHosts].join(", ")}. Set MCP_ALLOWED_HOSTS to add hosts, or MCP_AUTH_TOKEN for authenticated remote access.`);
1048
1087
  const host = process.env.HOST ?? "0.0.0.0";
1049
1088
  if (host === "0.0.0.0") {
1050
- console.warn("WARNING: No authentication and listening on all interfaces. Set MCP_AUTH_TOKEN or HOST=127.0.0.1.");
1051
- } else {
1052
- console.log("Auth disabled (set MCP_AUTH_TOKEN to enable).");
1089
+ console.warn("WARNING: No authentication and listening on all interfaces. Browser attacks (DNS rebinding and cross-origin fetch) are blocked by the Host/Origin checks, but any non-browser client that can reach this port has full vault access. Set MCP_AUTH_TOKEN, or HOST=127.0.0.1 to bind to loopback only.");
1053
1090
  }
1054
1091
  }
1055
1092
  var server = new FastMCP(serverOptions);
@@ -1080,7 +1117,7 @@ server.start({
1080
1117
  transportType: "httpStream",
1081
1118
  httpStream: { port: PORT, endpoint: "/mcp", host: process.env.HOST ?? "0.0.0.0" }
1082
1119
  });
1083
- console.log(`obsidian-sync-mcp v${"0.5.5"} listening on port ${PORT}`);
1120
+ console.log(`obsidian-sync-mcp v${"0.5.7"} listening on port ${PORT}`);
1084
1121
  process.on("unhandledRejection", (err) => {
1085
1122
  console.error("Unhandled rejection:", err);
1086
1123
  });