Skip to content

fix(index): keep indexed paths and served source inside the project root - #363

Merged
sunerpy merged 33 commits into
mainfrom
fix/index-path-containment
Dec 1, 2026
Merged

sunerpy merged 33 commits into
mainfrom
fix/index-path-containment

Conversation

@sunerpy

@sunerpy sunerpy commented Dec 1, 2026

Copy link
Copy Markdown
Owner

Summary

  • Stored form. The index now holds a name or path only in a form it can store exactly on every host: a project-relative path with / separators, no . or .. segment, no backslash and valid Unicode. The scan skips any other name whole (without following, descending into or counting it), and init, index and sync print one stderr line naming up to five skipped entries. stdout and every --json output are unchanged.
  • Reads stay inside the root. Every read of a path taken from the index (resolution contexts, framework readers, the watcher and sync lanes, control inputs, dead-reference probes, served source) first proves it lexically inside the project root; a row that leaves the root, as a swapped-in database can hold, reads as unreadable and is never stat'ed, opened or listed.
  • Served source. Before the MCP views and the CLI read source for an answer, the stored path is checked lexically and then by its real path against the canonical project root. A file whose real path is outside the root, such as a symlinked source file, stays indexed, but its source, symbol map, outline, signature and docstring are not served; the answer says why.
  • Pseudo filesystems. The scan no longer follows links into /proc, /sys or /dev (unless the project itself lives there), and every reader refuses a file on a kernel pseudo filesystem once opened, before a byte is read.
  • Read limits. Source reads are clamped to a 16 MiB ceiling; a configured indexing.max_file_size above it is clamped when parsed, with a warning. Godot scenes and .gd.uid sidecars are read under the project's max_file_size, like the same file as a source file.
  • Extraction version 23 → 24. An index built by an older release may hold rows outside the stored form or content read through such links, and incremental sync keeps them, so every older index reads as outdated and is rebuilt once. A v0.56.1 database fixture (upgrade_v0_56) proves the rebuild through CLI sync, the catch-up sync and a cold MCP session.

The golden corpora do not change: all 32 check identical. docs/architecture.md, docs/cli.md, docs/mcp.md, docs/data-model.md and docs/equivalence.md describe the behavior, and docs/upstream-sync/UPSTREAM.md records the related upstream change (#527).

This completes the series with #360 and #362.

Tests

Each new test was run on the base first and failed there for the reason it targets; tests added for code that was already correct were proven by a mutation instead.

  • crates/codegraph-cli/tests/intake_containment.rs, outside_root_serving.rs, max_file_size_ceiling.rs, upgrade_from_v056.rs and new sync_convergence cases: intake of unstorable names, CLI and stdio MCP serving of rows and links outside the root, swapped-in databases (including the control inputs a sync records), the size ceiling, and the upgrade from a v0.56.1 index.
  • crates/codegraph-mcp/tests/outside_root_mcp.rs, and unit tests in core, extract, watch, resolve and graph for the stored-path checks, the scan, the watcher and sync lanes, the Godot walks, the bounded read and the read limit every resolution context reports.

Verification

  • cargo fmt --all --check, cargo clippy --workspace --all-targets --locked -- -D warnings, and the same clippy cross-compiled for x86_64-pc-windows-msvc.
  • Full tests of the changed crates: core 148, extract 988, store 323, resolve 2399, graph 231, mcp 562, watch 497, daemon 214, ui 484, codegraph-rs 3240 passed.
  • Release build; scripts/regen-goldens.sh --check: 32 corpora identical.
  • Black-box probes of the three changes on a release build, with the base failing each of them.
  • The pre-push gate (make pre-ci) ran on push.

…the scan

The scan must yield only project paths it can store exactly as named.
These tests expect it to skip file and directory names holding a
backslash (Linux and macOS) or invalid UTF-8 (Linux), membership to
reject such names in both spellings, single-file extraction to refuse a
path that leads outside the root, and the scan not to follow links into
/proc or /sys. A link to a device stays skipped.

They fail on the current tree, which scans such names under translated
paths and follows those links.
…store

The watcher, the git fast path and incremental sync must accept only the
project paths the scan stores. These tests expect normalize_relative to
refuse parent components anywhere and, on Linux and macOS, names holding
a backslash; the scan and the watcher to agree on such names; an
incremental sync handed their events to store no row; and a committed
name of that kind to be pending on neither the fast path nor the full
inventory.

They fail on the current tree, which translates the backslash and keeps
the paths it produces.
The scene and script uid maps must point only at project paths the
index can store. This test expects both walks to skip names holding a
backslash (Linux and macOS) instead of mapping a uid to a path that
leaves the root, names it absolutely or aliases another file.

It fails on the current tree, which translates the backslash in the
walked names.
The dangling-reference audit must not probe a path outside the project
root. This test expects a reference that leaves the root, or names a path
absolutely, to be reported as dangling without an existence check, also
for the backslash spellings on Linux and macOS; it first checks that
every such reference is in the index.

It fails on the current tree, which probes each target and drops the
ones that exist outside the root.
A stored row may name a file outside the project root: a parent
component, an absolute path, or a link inside the root that leads out of
it. These tests plant such rows through the Store and expect the file
view (both modes), the symbol view (with code, a container, an ambiguous
name), a drifted file, explore and context, including a YAML key's code
block, to serve none of that file's text or stored signatures. A search
naming such a file must still be answered while auto-sync recovers, and
a file inside the root is served as before. Over the protocol, the
result keeps its shape and is no error.

They fail on the current tree, which reads and serves those files.
The CLI must index only the project paths it can store exactly as named.
These tests expect init to skip names holding a backslash (Linux and
macOS) or invalid UTF-8 (Linux), to read nothing through them, and to
succeed where such a name aliases no file. Status must report none of
them as pending, before or after sync, and sync must equal a fresh init
of the same tree when such names arrive or change. Stdout and --json
output must stay those of the same tree without such names.

They fail on the current tree, which indexes such names under
translated paths, fails init on a name it cannot read, and keeps
reporting them as pending.
Add the project_path module: a lexical check that a relative path stays
under the project root, and the stored form of names and paths the index
can hold exactly on every host.

Source reads now clamp their limit to MAX_FILE_SIZE_CEILING (16 MiB), and
a config whose indexing.max_file_size exceeds it is clamped when parsed,
with a warning. read_source_file and is_source_file refuse a file that is
on a kernel pseudo filesystem once opened, and read_stored_source reads a
stored row only when its path is in stored form.
The scan now checks each directory entry's name before anything else. A
name holding a backslash or invalid Unicode is skipped whole, without
following, descending into, or counting it, and its root-relative path
is listed in ScanProjectResult::unstorable for reporting. Paths are
built with the core stored-path rules instead of translating
separators, membership rejects the same names, and a file link whose
target has such a name aliases no path.

Extracting a single file accepts only a stored project path and names
the refused path in its error without touching the filesystem. Links
into /proc, /sys and /dev are no longer followed, unless the project
itself lives there.
normalize_relative now returns the path only in the exact form the scan
stores, so events, watch registration, new-directory walks and every sync
candidate refuse a parent component, a root and, on Linux and macOS, a
name holding a backslash. The git fast path skips such a name before its
first probe.

Every lane and sync read of a path taken from the index goes through
read_stored_source, and the direct reads of stored rows check the stored
form first, so a row outside the root is never read. An incremental sync
treats a file it cannot read as a regular file, including one on a
kernel pseudo filesystem, like a path that is not a regular file, which
keeps it converging with a fresh index.
Resolution read stored rows and candidates by joining them to the
project root, so a row that leaves the root (as a swapped-in index can
hold) was read from outside it. The store and snapshot contexts, the
framework extraction reader, the Dart pubspec, Go module, C# project
file and synthesis readers now prove a path lexically inside the root
first, with the rule the existence fallback already uses (#1631); a
path that fails reads as unreadable.

The Godot scene and script uid walks no longer turn a backslash in a
name into a separator: a uid maps only to a path the index can store.
The C# project file, Cargo member, JS workspace member and ohpm walks
skip such names the same way, as the scan does.
Every view reads source through one probe. Before it reads, the stored path
is checked lexically against the project root and then by its real path
against the root canonicalized when the engine opens, component by
component. A file outside the root is not read: the file view and the symbol
view say why and give no source, symbol map, outline, signature, docstring or
pointer to read it; explore and context leave it out, a config key's block
included, and context names it in its own notice. Such a file is not counted
as changed when auto-sync is unhealthy, so a graph answer naming it is still
given. The wording lives in one module.
find_dangling_references now checks the translated, res://-stripped
target with check_within_root before probing the disk; a target that
leaves the root or carries a `..` segment is reported as dangling
without a probe. default_source_reader joins through join_within_root
before it canonicalizes, so no path outside the root is probed.
init, index and sync print one stderr line naming up to five skipped
entries, Debug-escaped, so a control character in a name never reaches
the terminal raw. The text comes from codegraph_extract::unstorable_notice;
SyncOutcome carries the skipped entries from an incremental sync or a
migration, and each caller picks the level: the CLI sync prints the
warning, the daemon and catch-up syncs log it with tracing::warn!, and
the watcher and status log it at debug level. stdout and every --json
output are unchanged.

The platform-only helpers of the outside_root_serving test are gated to
the platforms whose tests use them.
…built

An index built by extraction version 23 or earlier may hold rows whose
path is not the stored form of a project-relative path, nodes of files
over the new max_file_size ceiling, and content read through links into
pseudo filesystems. Incremental sync keeps such rows, so every older
index now reads as outdated and is rebuilt from source.

upgrade_v0_56 holds the database the released v0.56.1 binary wrote for
the mini corpus; upgrade_from_v056 upgrades it through CLI sync, the
catch-up sync and a cold MCP session, and proves the rebuild drops
planted out-of-form file and node rows. The corpora do not change.
…eader shares

The pseudo filesystem check now runs in check_opened_regular, after its
regular-file check, instead of in read_source_file and is_source_file
alone. Every reader that opens a handle passes through it: the
link-following open_regular_file family, read_stored_source and the
no-follow state-file reader. A configuration or state file that resolves
into procfs or sysfs is refused once opened, before a byte is read, as a
source file already was.

/proc/self/pagemap was the bounded-read sample: it is now refused with
InvalidInput and nothing read. A regular file one byte over the limit
still fails as FileTooLarge.
…max_file_size

The scene and .gd.uid sidecar walks behind the uid maps read each file
as a regular file without a limit. They now read it under the project's
indexing.max_file_size, clamped to the 16 MiB ceiling: the limit the
same file is extracted under as a source file. A larger scene or
sidecar stays out of the uid maps, so a uid:// reference to it resolves
as an unknown uid.

The limit reaches the walks through FrameworkExtractionContext, which
init and sync now build with the configured value, and through a
max_file_size method on ResolutionContext for the autoload bindings.
This moves the graph and rides extraction version 24.
@codecov

codecov Bot commented Dec 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.02772% with 37 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/codegraph-watch/src/module_surfaces.rs 22.22% 7 Missing ⚠️
crates/codegraph-core/src/source_file.rs 97.35% 5 Missing ⚠️
crates/codegraph-mcp/src/engine.rs 98.82% 5 Missing ⚠️
crates/codegraph-extract/src/engine.rs 97.51% 4 Missing ⚠️
crates/codegraph-core/src/config.rs 95.52% 3 Missing ⚠️
crates/codegraph-core/src/project_path.rs 98.90% 2 Missing ⚠️
crates/codegraph-watch/src/control_inputs.rs 98.66% 2 Missing ⚠️
crates/codegraph-watch/src/sync.rs 95.83% 2 Missing ⚠️
crates/codegraph-watch/src/watcher.rs 60.00% 2 Missing ⚠️
crates/codegraph-daemon/src/lib.rs 66.66% 1 Missing ⚠️
... and 4 more

Impacted file tree graph

@@            Coverage Diff            @@
##             main     #363     +/-   ##
=========================================
  Coverage   96.42%   96.42%             
=========================================
  Files         370      372      +2     
  Lines      256629   258295   +1666     
=========================================
+ Hits       247445   249073   +1628     
- Misses       9184     9222     +38     
Files with missing lines Coverage Δ
crates/codegraph-cli/src/main.rs 92.01% <100.00%> (-0.07%) ⬇️
crates/codegraph-graph/src/dead_code.rs 95.28% <100.00%> (+0.09%) ⬆️
crates/codegraph-graph/src/graph/mod.rs 94.67% <100.00%> (ø)
crates/codegraph-mcp/src/outside_root.rs 100.00% <100.00%> (ø)
crates/codegraph-resolve/src/context.rs 99.12% <100.00%> (+0.02%) ⬆️
crates/codegraph-resolve/src/csharp_types.rs 97.54% <100.00%> (+0.23%) ⬆️
crates/codegraph-resolve/src/dart_libraries.rs 94.15% <100.00%> (+0.10%) ⬆️
crates/codegraph-resolve/src/framework.rs 100.00% <100.00%> (ø)
...odegraph-resolve/src/frameworks/cargo_workspace.rs 97.18% <100.00%> (+0.05%) ⬆️
crates/codegraph-resolve/src/frameworks/godot.rs 87.81% <100.00%> (+0.06%) ⬆️
... and 49 more

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sunerpy
sunerpy merged commit e3417d7 into main Dec 1, 2026
12 checks passed
@sunerpy
sunerpy deleted the fix/index-path-containment branch December 1, 2026 13:42
@github-actions github-actions Bot mentioned this pull request Dec 1, 2026
sunerpy added a commit that referenced this pull request Dec 1, 2026
The audit was written before the v0.56.2 patches merged and named them
by their plan labels. They merged as #360 (97fa800, project inputs read
only from regular files, and the single non-blocking source open) and
#363 (e3417d7, upstream #527's lexical and real-path checks, extraction
version 23 to 24). Name them by number in the owner decisions table and
in rows 201, 207 and 225.
sunerpy added a commit that referenced this pull request Dec 2, 2026
* docs(upstream): audit e8ddc4e2..461a5d87 (v1.6.4)

Classify the 324 non-merge upstream commits from colby main of
2026-10-22 (e8ddc4e2) to the v1.6.4 tag (461a5d87; v1.6.3 is ef4bd058)
against codegraph-rs v0.56.0 (73c8dd3, extraction version 23, schema
10). Each commit has one row in
docs/upstream-sync/V1_6_4_AUDIT_2026-11-28.md, grouped P1 to P6, with
its disposition, Rust target, graph impact, evidence and route.

Dispositions: 174 PORT, 66 PARTIAL, 8 DEFER, 9 ALREADY-HAVE, 63 N/A
and 4 KEEP-RUST; 180 rows change the stored graph. Routes: 182 B,
22 C-early, 41 C-late, 1 v0.56.2 (plus the regular-file half of
329dd12f) and 78 none. Verification changed 13 dispositions.

The audit re-runs the ten ALREADY-HAVE rows of the 2026-10-22 pass B
with their commits' own fixtures (X25; five become PARTIAL), records
the nine earlier rows that commits of this range supersede, and adds
GAP-JAVA-FIELD-INIT.

UPSTREAM.md moves the last discovery audit to 461a5d87 and the next
discovery to 461a5d87..origin/main, and adds the 2026-11-28 sync log
entry; the tracked release stays v1.6.2. KNOWN_DIFFS.md updates the
entries whose colby side changed and names their audit rows.
docs-check.py checks the new boundary. The row tables are unpadded
under prettier-ignore, which keeps the file at about 540 KB.

* docs(upstream): tighten the v1.6.4 audit wording

* docs(upstream): name the v0.56.2 pull requests in the v1.6.4 audit

The audit was written before the v0.56.2 patches merged and named them
by their plan labels. They merged as #360 (97fa800, project inputs read
only from regular files, and the single non-blocking source open) and
#363 (e3417d7, upstream #527's lexical and real-path checks, extraction
version 23 to 24). Name them by number in the owner decisions table and
in rows 201, 207 and 225.
sunerpy added a commit that referenced this pull request Dec 2, 2026
The six CLI daemon tests each carried a process_is_gone_or_zombie that
read /proc/<pid>/stat and took any read error for an exited process. On
macOS, which has no /proc, a live daemon therefore looked gone: the idle
test verified nothing and skipped the teardown kill, leaking the daemon.

A shared tests/common/process.rs now reads /proc on Linux and runs
`ps -o stat= -p <pid>` elsewhere (exit status 1 is gone, a Z state is a
zombie). A live pid whose state cannot be read panics instead of passing
as exited. daemon_spawn.rs gains a positive control: a running sleep
child is Running, a killed unreaped one is a Zombie, a reaped one is
Gone, and on Linux the ps probe must agree with /proc.

Three comments that named internal plan items now cite #363 and #360.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant