Skip to content

Commit 2f42790

Browse files
committed
fix(browse): restrict /api/browse to root; allow full access for file picker dialogs
1 parent a51a048 commit 2f42790

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
大きなコードベース(Linux カーネル、OpenSSL、curl など)を読む際に、検索結果をグラフに積み上げながら構造を把握していくことを想定しています。
88

9+
ビルドシステムが複雑で `compile_commands.json` を生成しにくいプロジェクトや、clangd のセットアップが難しい環境でも、インクルード依存グラフや定義ジャンプが動作します。
10+
911
> **ローカル専用ツールです**
1012
> 自分の PC で起動して、同じ PC のブラウザからアクセスして使います。
1113
> サーバーへのデプロイや、他の人が外部からアクセスできる環境での使用は想定していません。
@@ -402,7 +404,7 @@ grepnavi/
402404
│ ├── search.js # 検索・フィルタ・結果表示
403405
│ ├── graph.js # グラフ/ツリー操作・D3.js・詳細パネル・D&D
404406
│ ├── editor.js # Monaco エディタ・fzf・ナビ履歴・行メモ・#ifdef
405-
│ ├── memo-list.js # メモリストパネル(行・範囲メモ一覧・グループ管理)
407+
│ ├── memo-list.js # メモリストパネル(行・範囲メモ一覧・グループ管理)
406408
│ ├── editor-c.js # C/C++ 固有拡張(static変数・関数呼び出し・定数のハイライト、ローカル変数ホバー抑制)
407409
│ ├── gtags.js # GNU Global UI(エンジン選択・インデックス管理)
408410
│ ├── include-graph.js # C インクルード依存グラフ(D3.js)

api/handlers_fileops.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ func (h *Handler) handleBrowse(w http.ResponseWriter, r *http.Request) {
245245
dir := q.Get("path")
246246
ext := q.Get("ext") // e.g. ".json"
247247

248+
pick := q.Get("pick") == "1" // ルート選択ダイアログからの呼び出し
249+
explicitPath := dir != ""
248250
if dir == "" {
249251
if exe, err := os.Executable(); err == nil {
250252
dir = filepath.Dir(exe)
@@ -254,6 +256,16 @@ func (h *Handler) handleBrowse(w http.ResponseWriter, r *http.Request) {
254256
}
255257
dir = filepath.Clean(dir)
256258

259+
if explicitPath && !pick {
260+
h.mu.RLock()
261+
root := filepath.Clean(h.root)
262+
h.mu.RUnlock()
263+
if !strings.HasPrefix(dir+string(filepath.Separator), root+string(filepath.Separator)) {
264+
jsonErr(w, "path outside root", http.StatusForbidden)
265+
return
266+
}
267+
}
268+
257269
entries, err := os.ReadDir(dir)
258270
if err != nil {
259271
jsonErr(w, err.Error(), http.StatusBadRequest)

static/js/filebrowser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ async function fbNavigate(dir, pushHistory = true, focusName = null) {
9595
const ext = (_fbMode === 'open-file') ? '' : '.json';
9696
const params = new URLSearchParams({ ext });
9797
if(dir) params.set('path', dir);
98+
if(['dir', 'open', 'save', 'open-file'].includes(_fbMode)) params.set('pick', '1');
9899
const res = await fetch('/api/browse?' + params).catch(() => null);
99100
if(!res || !res.ok) { st('ディレクトリを開けませんでした'); return; }
100101
const data = await res.json();

0 commit comments

Comments
 (0)