Skip to content

Commit a9bd5df

Browse files
shengyfuCopilot
andcommitted
Reserve only managed store and namespace paths
Ordinary index commands rejected any path with an ancestor named like the managed store, so an unrelated project or index beneath a directory that merely shares that name fell back to scanning or could not be indexed. Reserve only the store directory itself and paths beneath its 64-hex repository namespaces, compared lexically and case-insensitively after normalization; managed generation files stay rejected by format. Co-authored-by: Copilot App <[email protected]>
1 parent c6cb2ca commit a9bd5df

4 files changed

Lines changed: 162 additions & 8 deletions

File tree

‎SHARED_WORKTREE_INDEXES.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,10 @@ Managed generation files use `paths.tgm`, `lookup.tgm`, `postings.tgm`,
781781
`meta.tgm` and `generation.tgm`, incompatible magic and file-table format
782782
`0x4d320001`. Old and unguarded readers reject them even under renamed legacy
783783
filenames. No legacy-readable intermediate is published inside the managed
784-
namespace. Existing v1 stores are not converted or retroactively made collectible.
784+
namespace. Ordinary index commands also refuse a store directory itself and
785+
paths beneath its 64-hex repository namespaces, compared lexically and
786+
case-insensitively; an unrelated directory that merely shares the store name
787+
stays usable. Existing v1 stores are not converted or retroactively made collectible.
785788

786789
Managed namespaces also require a supported content-authentication format.
787790
Missing or incompatible producer proof is not upgraded by hashing whatever

‎tgrep-cli/tests/indexed_containment.rs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,41 @@ fn unavailable_relative_and_unrelated_metadata_roots_fall_back_to_scanning() {
141141
);
142142
}
143143
}
144+
145+
#[test]
146+
fn ordinary_indexes_beneath_unrelated_store_named_directories_stay_indexed() {
147+
let temp = tempfile::tempdir().unwrap();
148+
let store = temp.path().join("tgrep-managed-v2");
149+
let root = store.join("project");
150+
fs::create_dir_all(&root).unwrap();
151+
fs::write(root.join("kept.txt"), "needle kept\n").unwrap();
152+
let tgrep = |args: &[&str]| {
153+
Command::cargo_bin("tgrep")
154+
.unwrap()
155+
.timeout(Duration::from_secs(30))
156+
.current_dir(&root)
157+
.args(args)
158+
.output()
159+
.unwrap()
160+
};
161+
let output = tgrep(&["index", "."]);
162+
assert!(output.status.success(), "{output:?}");
163+
let output = tgrep(&["--stats", "-F", "--", "needle", "."]);
164+
assert!(output.status.success(), "{output:?}");
165+
let stdout = String::from_utf8(output.stdout).unwrap();
166+
let stderr = String::from_utf8(output.stderr).unwrap();
167+
assert!(stdout.contains("needle kept"), "{stdout}");
168+
assert!(
169+
stderr.contains("Query plan:"),
170+
"must exercise the index: {stderr}"
171+
);
172+
assert!(!stderr.contains("scanning every file"), "{stderr}");
173+
174+
// A repository-namespace-shaped path beneath the same name stays reserved.
175+
let reserved = store.join("0".repeat(64)).join("index");
176+
let output = tgrep(&["index", ".", "--index-path", reserved.to_str().unwrap()]);
177+
assert!(!output.status.success(), "{output:?}");
178+
let stderr = String::from_utf8(output.stderr).unwrap();
179+
assert!(stderr.contains("protected managed reader"), "{stderr}");
180+
assert!(!reserved.join("meta.json").exists());
181+
}

‎tgrep-core/src/managed/mod.rs‎

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,55 @@ pub const STORE_DIRECTORY: &str = "tgrep-managed-v2";
9090
pub const MAX_REQUEST_BYTES: usize = 1024 * 1024;
9191
pub const MAX_RESPONSE_BYTES: usize = 64 * 1024 * 1024;
9292

93+
/// Refuses ordinary index access to managed storage. Besides managed generation
94+
/// files, only a store directory itself and paths below one of its repository
95+
/// namespaces are reserved, so an unrelated directory that merely shares the
96+
/// store name stays usable. The absolute path is checked lexically after
97+
/// `.`/`..` normalization, without following links.
9398
pub(crate) fn reject_unguarded(path: &std::path::Path) -> crate::Result<()> {
94-
if path.join("paths.tgm").try_exists()?
95-
|| path.ancestors().any(|ancestor| {
96-
ancestor
97-
.file_name()
98-
.is_some_and(|name| name == STORE_DIRECTORY)
99-
})
100-
{
99+
if reserved_store_path(path)? || path.join("paths.tgm").try_exists()? {
101100
return Err(
102101
Error::incompatible("managed storage requires a protected managed reader").into(),
103102
);
104103
}
105104
Ok(())
106105
}
107106

107+
fn reserved_store_path(path: &std::path::Path) -> std::io::Result<bool> {
108+
use std::path::Component;
109+
// `Path::join` treats an empty index directory as the current directory.
110+
let path = if path.as_os_str().is_empty() {
111+
std::path::Path::new(".")
112+
} else {
113+
path
114+
};
115+
let absolute = std::path::absolute(path)?;
116+
let mut names = Vec::new();
117+
for component in absolute.components() {
118+
match component {
119+
Component::Normal(name) => names.push(name),
120+
Component::ParentDir => {
121+
names.pop();
122+
}
123+
Component::Prefix(_) | Component::RootDir | Component::CurDir => {}
124+
}
125+
}
126+
// Case-insensitive volumes resolve either spelling to the same directory.
127+
let store = |name: &std::ffi::OsStr| {
128+
name.to_str()
129+
.is_some_and(|name| name.eq_ignore_ascii_case(STORE_DIRECTORY))
130+
};
131+
let namespace = |name: &std::ffi::OsStr| {
132+
name.to_str().is_some_and(|name| {
133+
name.len() == 64 && name.bytes().all(|byte| byte.is_ascii_hexdigit())
134+
})
135+
};
136+
Ok(names.last().is_some_and(|name| store(name))
137+
|| names
138+
.windows(2)
139+
.any(|pair| store(pair[0]) && namespace(pair[1])))
140+
}
141+
108142
/// A physical incarnation, never a logical generation key or a filesystem path.
109143
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
110144
#[serde(transparent)]
@@ -302,4 +336,38 @@ mod tests {
302336
assert_eq!(json["status"], "unavailable");
303337
assert!(json.get("value").is_none());
304338
}
339+
340+
#[test]
341+
fn only_store_and_namespace_paths_are_reserved_from_unguarded_access() {
342+
use std::path::PathBuf;
343+
let namespace = "0123456789abcdef".repeat(4);
344+
let base = std::env::temp_dir().join("parent");
345+
let store = base.join(STORE_DIRECTORY);
346+
let relative = PathBuf::from(STORE_DIRECTORY);
347+
for (path, reserved) in [
348+
(store.clone(), true),
349+
(store.join(&namespace), true),
350+
(
351+
store.join(&namespace).join("objects").join("preparing"),
352+
true,
353+
),
354+
(
355+
base.join(STORE_DIRECTORY.to_ascii_uppercase())
356+
.join(namespace.to_ascii_uppercase()),
357+
true,
358+
),
359+
(store.join("project").join("..").join(&namespace), true),
360+
(relative.join(&namespace).join("objects"), true),
361+
(store.join("project"), false),
362+
(store.join("project").join(".tgrep"), false),
363+
(store.join("project").join(&namespace), false),
364+
(store.join(&namespace).join("..").join("project"), false),
365+
(store.join(&namespace[..63]), false),
366+
(base.join("other").join(&namespace), false),
367+
(relative.join("project").join(".tgrep"), false),
368+
(PathBuf::new(), false),
369+
] {
370+
assert_eq!(reserved_store_path(&path).unwrap(), reserved, "{path:?}");
371+
}
372+
}
305373
}

‎tgrep-core/tests/managed_lifecycle.rs‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,51 @@ fn managed_format_and_escaped_reader_cannot_bypass_ownership() {
16151615
fs2::FileExt::try_lock_exclusive(&guard).unwrap();
16161616
}
16171617

1618+
#[test]
1619+
fn unrelated_store_named_directories_keep_ordinary_indexes_usable() {
1620+
let fixture = Fixture::new();
1621+
let unrelated = fixture
1622+
.temp
1623+
.path()
1624+
.join("checkout")
1625+
.join(tgrep_core::managed::STORE_DIRECTORY);
1626+
let project = unrelated.join("project");
1627+
fs::create_dir_all(&project).unwrap();
1628+
fs::write(project.join("note.txt"), b"ordinary needle\n").unwrap();
1629+
let index = project.join(".tgrep");
1630+
tgrep_core::builder::build_index(&project, Some(&index), true, false, &[]).unwrap();
1631+
tgrep_core::reader::IndexReader::open(&index).unwrap();
1632+
let meta = tgrep_core::meta::IndexMeta::load(&index).unwrap();
1633+
meta.save(&index).unwrap();
1634+
let legacy = unrelated.join("legacy-shared");
1635+
fs::create_dir(&legacy).unwrap();
1636+
tgrep_core::generations::GenerationManager::with_storage(fixture.repository.clone(), &legacy)
1637+
.unwrap();
1638+
1639+
let namespace = fixture.namespace.path();
1640+
let store = namespace.parent().unwrap();
1641+
for reserved in [
1642+
store.to_path_buf(),
1643+
namespace.to_path_buf(),
1644+
namespace.join("objects"),
1645+
namespace.join("objects").join("preparing"),
1646+
] {
1647+
let error = meta.save(&reserved).unwrap_err().to_string();
1648+
assert!(
1649+
error.contains("protected managed reader"),
1650+
"{reserved:?}: {error}"
1651+
);
1652+
assert!(!reserved.join("meta.json").exists(), "{reserved:?}");
1653+
assert!(tgrep_core::reader::IndexReader::open(&reserved).is_err());
1654+
}
1655+
let error =
1656+
tgrep_core::generations::GenerationManager::with_storage(fixture.repository.clone(), store)
1657+
.err()
1658+
.expect("legacy storage cannot be the managed store directory")
1659+
.to_string();
1660+
assert!(error.contains("protected managed reader"), "{error}");
1661+
}
1662+
16181663
#[test]
16191664
fn staging_exhaustion_is_typed_and_never_published() {
16201665
let mut fixture = Fixture::new();

0 commit comments

Comments
 (0)