From c1154335998ca7c9100cb5a886ddbf0e1d9ed940 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 23 Oct 2026 15:19:41 +0000 Subject: [PATCH 1/5] =?UTF-8?q?fix(server):=20=E3=83=95=E3=82=A1=E3=82=A4?= =?UTF-8?q?=E3=83=AB=E7=9B=A3=E8=A6=96=E3=81=8C=E5=85=AC=E9=96=8B=E3=83=AB?= =?UTF-8?q?=E3=83=BC=E3=83=88=E5=A4=96=E3=81=B8=E3=81=AE=E3=82=B7=E3=83=B3?= =?UTF-8?q?=E3=83=9C=E3=83=AA=E3=83=83=E3=82=AF=E3=83=AA=E3=83=B3=E3=82=AF?= =?UTF-8?q?=E3=82=92=E8=BE=BF=E3=82=89=E3=81=AA=E3=81=84=E3=82=88=E3=81=86?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Chokidar は既定でシンボリックリンクを追跡するため、公開ディレクトリ内に / や $HOME へのリンクがあると、tree・search・file API がすべて拒否する ルート外のツリーを監視し、そのファイル名を SSE (/api/watch) で配信していた。 リンク先が巨大な場合は inotify ウォッチャーも枯渇しうる。 ignored フックで、シンボリックリンクと stat 情報のないイベントパスについて realpath が公開ルート外を指すものを除外する(safePath と同じ境界)。 ルート内を指すリンクは従来どおり監視される。 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019SSD9fCuehjSnSpFPab74B --- server.mjs | 18 +++++++++++++++++- tests/server-audit.test.mjs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/server.mjs b/server.mjs index d350280..ba2f38d 100644 --- a/server.mjs +++ b/server.mjs @@ -1,5 +1,5 @@ import { createServer } from 'node:http'; -import { createReadStream, readFileSync } from 'node:fs'; +import { createReadStream, readFileSync, realpathSync } from 'node:fs'; import { readFile, readdir, stat, realpath, open } from 'node:fs/promises'; import { createInterface } from 'node:readline'; import { join, resolve, relative, extname, basename, dirname, sep } from 'node:path'; @@ -2486,12 +2486,28 @@ function tryLocalDiagramCLI(type, source) { }); } +/** Same boundary as safePath(), synchronously, for Chokidar's ignore hook. */ +function resolvesOutsideRoot(path) { + let real; + try { + real = realpathSync.native(resolve(targetDir, path)); + } catch { + return false; // Missing paths are reported as unlink events. + } + const prefix = targetDir.endsWith(sep) ? targetDir : targetDir + sep; + return real !== targetDir && !real.startsWith(prefix); +} + // File watcher — chokidar v5 has no glob support, so watch the directory // itself, prune ignored paths via the `ignored` function, and filter events // by extension before broadcasting. function isWatchIgnored(path, stats) { const rel = relative(targetDir, resolve(targetDir, path)); if (!rel || rel.startsWith('..')) return false; + // Chokidar follows symlinks, so a link to /, $HOME, etc. would watch (and + // broadcast names from) a tree every other API refuses to expose. Stat-less + // calls cover event paths beneath a link; real files and dirs need no check. + if ((!stats || stats.isSymbolicLink()) && resolvesOutsideRoot(path)) return true; const segments = rel.split(sep); for (let i = 0; i < segments.length; i++) { const seg = segments[i]; diff --git a/tests/server-audit.test.mjs b/tests/server-audit.test.mjs index f52c4ca..0c145e6 100644 --- a/tests/server-audit.test.mjs +++ b/tests/server-audit.test.mjs @@ -310,6 +310,43 @@ test('watcher broadcasts changes to supported hidden files such as .env', async assert.match(events, /"event":"change","path":"\.env"/); }); +test('watcher does not follow symlinks out of the served root', async (t) => { + const directory = join(fixture, 'watch-boundary'); + const outside = join(fixture, 'watch-outside'); + await mkdir(join(directory, 'real'), { recursive: true }); + await mkdir(outside); + await writeFile(join(outside, 'leak.md'), 'outside\n'); + await writeFile(join(directory, 'real', 'inside.md'), 'inside\n'); + await symlink(outside, join(directory, 'escape')); + await symlink(join(directory, 'real'), join(directory, 'alias')); + const server = await startServer(t, { directory }); + const controller = new AbortController(); + t.after(() => controller.abort()); + const response = await fetch(server.base + '/api/watch', { signal: controller.signal }); + const reader = response.body.getReader(); + await reader.read(); + // Touch both sides until the in-root change arrives, so a missing outside + // event is not just the watcher still starting up. + const interval = setInterval(() => { + void writeFile(join(outside, 'leak.md'), `outside ${Date.now()}\n`); + void writeFile(join(directory, 'real', 'inside.md'), `inside ${Date.now()}\n`); + }, 200); + const timeout = setTimeout(() => controller.abort(), 4000); + t.after(() => { clearInterval(interval); clearTimeout(timeout); }); + let events = ''; + while (!/"path":"alias\/inside\.md"/.test(events) || !/"path":"real\/inside\.md"/.test(events)) { + const { value, done } = await reader.read().catch(() => ({ done: true })); + if (done) break; + events += Buffer.from(value).toString(); + } + // Give a straggling outside event one more interval to show up. + await new Promise((resolve) => setTimeout(resolve, 400)); + clearInterval(interval); + assert.match(events, /"path":"real\/inside\.md"/); + assert.match(events, /"path":"alias\/inside\.md"/, 'in-root symlinked directories are still watched'); + assert.doesNotMatch(events, /escape\//); +}); + test('CLI rejects an unsafe remote byte limit instead of silently disabling the cap', async () => { const child = spawn(NODE, [join(ROOT, 'server.mjs'), docs, '--remote-max-size', '9'.repeat(400)], { stdio: ['ignore', 'ignore', 'pipe'], From 69cf49a36f59d264582b2d4c80aa9159613657a8 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 23 Oct 2026 15:19:42 +0000 Subject: [PATCH 2/5] =?UTF-8?q?fix(server):=20CRLF=20=E3=83=95=E3=82=A1?= =?UTF-8?q?=E3=82=A4=E3=83=AB=E3=81=AE=E5=85=A8=E6=96=87=E6=A4=9C=E7=B4=A2?= =?UTF-8?q?=E3=81=A7=E6=AD=A3=E8=A6=8F=E8=A1=A8=E7=8F=BE=E3=81=AE=20$=20?= =?UTF-8?q?=E3=81=8C=E4=B8=80=E8=87=B4=E3=81=97=E3=81=AA=E3=81=84=E5=95=8F?= =?UTF-8?q?=E9=A1=8C=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /api/search は本文を '\n' で分割していたため、CRLF のファイルでは各行末に \r が残り、正規表現 'end$' のような行末アンカーが一切一致しなかった。 結果の text・contextLines にも \r が混入していた。 ファイル内検索 (/api/file/search) と同様に CRLF を行区切りとして扱う。 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019SSD9fCuehjSnSpFPab74B --- server.mjs | 4 +++- tests/server-audit.test.mjs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/server.mjs b/server.mjs index ba2f38d..7b2b7fe 100644 --- a/server.mjs +++ b/server.mjs @@ -2013,7 +2013,9 @@ const server = createServer(async (req, res) => { const st = await stat(fullPath); if (st.size > MAX_SEARCH_FILE_BYTES) continue; const content = await readFileText(fullPath); - const lines = content.split('\n'); + // CRLF files would otherwise keep a trailing \r on every line, + // which breaks `$` anchors and leaks into the result text. + const lines = content.split(/\r?\n/); const matchingLines = regexSearch ? new Set(await regexSearch.findMatches(lines, 100 - results.length)) : null; diff --git a/tests/server-audit.test.mjs b/tests/server-audit.test.mjs index 0c145e6..3b225de 100644 --- a/tests/server-audit.test.mjs +++ b/tests/server-audit.test.mjs @@ -347,6 +347,18 @@ test('watcher does not follow symlinks out of the served root', async (t) => { assert.doesNotMatch(events, /escape\//); }); +test('regex search anchors work on CRLF files and results omit carriage returns', async (t) => { + const directory = join(fixture, 'crlf'); + await mkdir(directory); + await writeFile(join(directory, 'windows.md'), 'alpha end\r\nbeta end\r\n'); + const server = await startServer(t, { directory }); + const regex = await (await server.request('/api/search?regex=1&context=1&q=' + encodeURIComponent('end$'))).json(); + assert.deepEqual(regex.map(({ line }) => line), [1, 2]); + const plain = await (await server.request('/api/search?context=1&q=alpha')).json(); + assert.equal(plain[0].text, 'alpha end'); + assert.deepEqual(plain[0].contextLines, ['alpha end', 'beta end']); +}); + test('CLI rejects an unsafe remote byte limit instead of silently disabling the cap', async () => { const child = spawn(NODE, [join(ROOT, 'server.mjs'), docs, '--remote-max-size', '9'.repeat(400)], { stdio: ['ignore', 'ignore', 'pipe'], From 33533422cdf9d20aa292f68422a443aa3b7b9412 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 23 Oct 2026 15:19:42 +0000 Subject: [PATCH 3/5] =?UTF-8?q?fix(server):=20=E3=83=90=E3=83=83=E3=82=AF?= =?UTF-8?q?=E3=83=AA=E3=83=B3=E3=82=AF=E5=88=A4=E5=AE=9A=E3=82=92=E3=83=93?= =?UTF-8?q?=E3=83=A5=E3=83=BC=E3=82=A2=E3=81=AE=E3=83=AA=E3=83=B3=E3=82=AF?= =?UTF-8?q?=E8=A7=A3=E6=B1=BA=E3=81=A8=E4=B8=80=E8=87=B4=E3=81=95=E3=81=9B?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /api/backlinks は Markdown リンクを未デコードの文字列で比較していたため、 次の不整合があった。 - [a](my%20doc.md) や [a]() のリンクを検出できない - [a](notes.txt) を notes.md へのリンクと誤検出する(拡張子を無視した名前一致) - sub/x.md 内の [a](notes.md) を、実際には sub/notes.md を開くのに ルートの notes.md へのリンクと誤検出する フロントエンド (resolveDocumentResource / wiki_link) と同じく、ソースファイル 基準で URL として解決・デコードして比較する。ディレクトリを問わない名前一致は wiki リンク [[name]] のみに限定する。 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019SSD9fCuehjSnSpFPab74B --- server.mjs | 41 ++++++++++++++++++++++++------------- tests/server-audit.test.mjs | 22 ++++++++++++++++++++ 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/server.mjs b/server.mjs index 7b2b7fe..887482e 100644 --- a/server.mjs +++ b/server.mjs @@ -2223,20 +2223,33 @@ const server = createServer(async (req, res) => { refreshDocviewIgnore(); const MARKDOWN_LIKE = new Set(['.md', '.markdown', '.mdx', '.txt']); - const targetName = basename(targetPath).replace(/\.[^.]+$/, ''); + const targetName = basename(targetPath); const backlinks = []; - function isLinkToTarget(link, sourcePath) { - if (!link) return false; - const linkPath = link.split('#')[0].replace(/^\.\//, ''); - if (!linkPath) return false; - if (linkPath === targetPath) return true; - const sourceDir = dirname(sourcePath); - const resolved = join(sourceDir, linkPath).replace(/\\/g, '/'); - if (resolved === targetPath) return true; - const linkName = basename(linkPath).replace(/\.[^.]+$/, ''); - if (linkName === targetName && !linkPath.includes('/')) return true; - return false; + // Mirror the viewer: Markdown destinations resolve like URLs against the + // source file and are percent-decoded (resolveDocumentResource), and wiki + // links default to `.md`. Only wiki links also match by name in any folder. + function isLinkToTarget(link, sourcePath, isWiki) { + let dest = link.trim(); + if (isWiki) { + dest = dest.split(/[?#]/)[0].trim(); + if (dest && !(dest.split('/').pop() || '').includes('.')) dest += '.md'; + } else if (dest.startsWith('<')) { + const close = dest.indexOf('>'); + dest = dest.slice(1, close === -1 ? undefined : close); + } else { + dest = dest.split(/\s/)[0]; // drop an optional "title" + } + if (!dest || /^(?:[a-z][a-z\d+.-]*:|\/\/)/i.test(dest)) return false; + let resolvedLink; + try { + const base = sourcePath.split('/').map(encodeURIComponent).join('/'); + resolvedLink = decodeURIComponent(new URL(dest, `https://docview.invalid/${base}`).pathname.slice(1)); + } catch { + return false; + } + if (resolvedLink === targetPath) return true; + return isWiki && !dest.includes('/') && basename(resolvedLink) === targetName; } async function scanBacklinks(dir) { @@ -2267,8 +2280,8 @@ const server = createServer(async (req, res) => { const linkRegex = /\[(?:[^\]]*)\]\(([^)]+)\)|\[\[([^\]|]+)(?:\|[^\]]*?)?\]\]/g; let match; while ((match = linkRegex.exec(content)) !== null) { - const linkTarget = match[1] || match[2]; - if (isLinkToTarget(linkTarget, relPath)) { + const isWiki = match[1] === undefined; + if (isLinkToTarget(isWiki ? match[2] : match[1], relPath, isWiki)) { const line = content.substring(0, match.index).split('\n').length; backlinks.push({ path: relPath, line }); break; diff --git a/tests/server-audit.test.mjs b/tests/server-audit.test.mjs index 3b225de..24a0bc7 100644 --- a/tests/server-audit.test.mjs +++ b/tests/server-audit.test.mjs @@ -359,6 +359,28 @@ test('regex search anchors work on CRLF files and results omit carriage returns' assert.deepEqual(plain[0].contextLines, ['alpha end', 'beta end']); }); +test('backlinks resolve Markdown links like the viewer does', async (t) => { + const directory = join(fixture, 'backlinks'); + await mkdir(join(directory, 'sub'), { recursive: true }); + await writeFile(join(directory, 'my doc.md'), '# Target\n'); + await writeFile(join(directory, 'sub', 'notes.md'), '# Notes\n'); + await writeFile(join(directory, 'encoded.md'), '[a](my%20doc.md)\n'); + await writeFile(join(directory, 'angle.md'), '[a]( "Title")\n'); + await writeFile(join(directory, 'parent.md'), 'x\n\n[a](sub/../my%20doc.md#top)\n'); + await writeFile(join(directory, 'wiki.md'), '[[my doc]]\n'); + await writeFile(join(directory, 'other-ext.md'), '[a](my%20doc.txt)\n'); + await writeFile(join(directory, 'wrong-dir.md'), '[a](notes.md)\n'); + await writeFile(join(directory, 'wiki-notes.md'), '[[notes]]\n'); + const server = await startServer(t, { directory }); + const target = await (await server.request('/api/backlinks?path=' + encodeURIComponent('my doc.md'))).json(); + assert.deepEqual(target.map(({ path }) => path).sort(), ['angle.md', 'encoded.md', 'parent.md', 'wiki.md']); + assert.equal(target.find(({ path }) => path === 'parent.md').line, 3); + const notes = await (await server.request('/api/backlinks?path=sub/notes.md')).json(); + // A plain relative link opens notes.md beside its source; only wiki links + // match by name across directories. + assert.deepEqual(notes.map(({ path }) => path), ['wiki-notes.md']); +}); + test('CLI rejects an unsafe remote byte limit instead of silently disabling the cap', async () => { const child = spawn(NODE, [join(ROOT, 'server.mjs'), docs, '--remote-max-size', '9'.repeat(400)], { stdio: ['ignore', 'ignore', 'pipe'], From c7b4c71dc9f0bda2556826ae8e854f550a9c0bb5 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 23 Oct 2026 15:19:42 +0000 Subject: [PATCH 4/5] =?UTF-8?q?docs:=20README=20=E3=81=A8=20--help=20?= =?UTF-8?q?=E3=81=AE=E5=AF=BE=E5=BF=9C=E5=BD=A2=E5=BC=8F=E4=B8=80=E8=A6=A7?= =?UTF-8?q?=E3=82=92=E5=AE=9F=E8=A3=85=E3=81=A8=E4=B8=80=E8=87=B4=E3=81=95?= =?UTF-8?q?=E3=81=9B=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SUPPORTED_EXTENSIONS にある HTML・Mermaid・CSV/TSV・JSONL・ログ・動画などが README の表と CLI ヘルプから欠落していた。 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019SSD9fCuehjSnSpFPab74B --- README.md | 6 +++++- bin/mdv.mjs | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e72ccc7..5634fac 100644 --- a/README.md +++ b/README.md @@ -63,10 +63,14 @@ If the browser doesn't open, copy `http://localhost:4000` into any browser — i | Category | Extensions | |-----------|----------------------------------------------| | Markdown | `.md` `.markdown` `.mdx` `.txt` | -| Data | `.json` `.yaml` `.yml` | +| HTML | `.html` `.htm` (sandboxed iframe preview) | +| Diagrams | `.mmd` `.mermaid` | +| Data | `.json` `.jsonl` `.ndjson` `.yaml` `.yml` `.csv` `.tsv` | | Config | `.toml` `.ini` `.conf` `.env` `.cfg` `.properties` | +| Logs | `.log` | | Crontab | `.cron` `.crontab` (and files named `crontab`) | | Images | `.png` `.jpg` `.jpeg` `.gif` `.svg` `.webp` `.bmp` `.ico` | +| Videos | `.mp4` `.m4v` `.webm` `.ogv` `.mov` | | Office / iWork | `.xls` `.xlsx` `.ppt` `.pptx` `.numbers` `.pages` `.key` (listed, open in default app / download fallback for normal files; no direct WebView preview) | | PDF | `.pdf` (browser built-in viewer in an iframe; open in new tab / default app / download) | diff --git a/bin/mdv.mjs b/bin/mdv.mjs index 1d4c44d..5411b30 100755 --- a/bin/mdv.mjs +++ b/bin/mdv.mjs @@ -45,9 +45,15 @@ if (args.includes('--help') || args.includes('-h')) { Supported formats: Markdown .md .markdown .mdx .txt - Data .json .yaml .yml + HTML .html .htm + Diagrams .mmd .mermaid + Data .json .jsonl .ndjson .yaml .yml .csv .tsv Config .toml .ini .conf .env .cfg .properties + Logs .log + Crontab .cron .crontab crontab Images .png .jpg .jpeg .gif .svg .webp .bmp .ico + Videos .mp4 .m4v .webm .ogv .mov + Documents .pdf .xls .xlsx .ppt .pptx .numbers .pages .key Examples: docview # View current directory From 89553553ca80da556010a0fa599be1f40a5a7a49 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 23 Oct 2026 15:31:30 +0000 Subject: [PATCH 5/5] =?UTF-8?q?fix(server):=20=E5=8C=BA=E5=88=87=E3=82=8A?= =?UTF-8?q?=E3=82=92=E3=82=A8=E3=83=B3=E3=82=B3=E3=83=BC=E3=83=89=E3=81=97?= =?UTF-8?q?=E3=81=9F=20wiki=20=E3=83=AA=E3=83=B3=E3=82=AF=E3=82=92?= =?UTF-8?q?=E5=90=8D=E5=89=8D=E4=B8=80=E8=87=B4=E3=81=AE=E5=AF=BE=E8=B1=A1?= =?UTF-8?q?=E3=81=8B=E3=82=89=E5=A4=96=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [[sub%2Fnotes]] はビューアではデコード後の sub/notes.md を開くが、 バックリンクの名前一致はデコード前の文字列で '/' の有無を判定していたため、 other/notes.md へのバックリンクとしても誤検出していた。 デコード後のパスでフォルダ指定の有無を判定する。 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019SSD9fCuehjSnSpFPab74B --- server.mjs | 5 ++++- tests/server-audit.test.mjs | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/server.mjs b/server.mjs index 887482e..5b0caba 100644 --- a/server.mjs +++ b/server.mjs @@ -2242,14 +2242,17 @@ const server = createServer(async (req, res) => { } if (!dest || /^(?:[a-z][a-z\d+.-]*:|\/\/)/i.test(dest)) return false; let resolvedLink; + let qualified; try { const base = sourcePath.split('/').map(encodeURIComponent).join('/'); resolvedLink = decodeURIComponent(new URL(dest, `https://docview.invalid/${base}`).pathname.slice(1)); + // `sub%2Fnotes` names a folder just like `sub/notes` once decoded. + qualified = decodeURIComponent(dest).includes('/'); } catch { return false; } if (resolvedLink === targetPath) return true; - return isWiki && !dest.includes('/') && basename(resolvedLink) === targetName; + return isWiki && !qualified && basename(resolvedLink) === targetName; } async function scanBacklinks(dir) { diff --git a/tests/server-audit.test.mjs b/tests/server-audit.test.mjs index 24a0bc7..b420c8b 100644 --- a/tests/server-audit.test.mjs +++ b/tests/server-audit.test.mjs @@ -371,6 +371,10 @@ test('backlinks resolve Markdown links like the viewer does', async (t) => { await writeFile(join(directory, 'other-ext.md'), '[a](my%20doc.txt)\n'); await writeFile(join(directory, 'wrong-dir.md'), '[a](notes.md)\n'); await writeFile(join(directory, 'wiki-notes.md'), '[[notes]]\n'); + // An encoded separator still qualifies the wiki link to one folder. + await mkdir(join(directory, 'other')); + await writeFile(join(directory, 'other', 'notes.md'), '# Other notes\n'); + await writeFile(join(directory, 'wiki-encoded.md'), '[[sub%2Fnotes]]\n'); const server = await startServer(t, { directory }); const target = await (await server.request('/api/backlinks?path=' + encodeURIComponent('my doc.md'))).json(); assert.deepEqual(target.map(({ path }) => path).sort(), ['angle.md', 'encoded.md', 'parent.md', 'wiki.md']); @@ -378,7 +382,9 @@ test('backlinks resolve Markdown links like the viewer does', async (t) => { const notes = await (await server.request('/api/backlinks?path=sub/notes.md')).json(); // A plain relative link opens notes.md beside its source; only wiki links // match by name across directories. - assert.deepEqual(notes.map(({ path }) => path), ['wiki-notes.md']); + assert.deepEqual(notes.map(({ path }) => path).sort(), ['wiki-encoded.md', 'wiki-notes.md']); + const otherNotes = await (await server.request('/api/backlinks?path=other/notes.md')).json(); + assert.deepEqual(otherNotes.map(({ path }) => path), ['wiki-notes.md']); }); test('CLI rejects an unsafe remote byte limit instead of silently disabling the cap', async () => {