Reject paths in project documentation fallback filenames (#45865)

## Why

Probing Windows network paths can send ambient credentials, even during metadata checks. Entries in `project_doc_fallback_filenames` must be validated before filesystem probes.

## What changed

Ignore entries containing path syntax according to the executor's OS, rather than the host's. Reject `.` and `..`, slashes, and NUL characters on all executors; also reject backslashes and colons on Windows. Log a warning for rejected entries and continue considering valid filenames.

## Testing

Add tests that verify invalid entries never reach metadata probes under POSIX and Windows path conventions, preserve backslashes and colons in POSIX filenames, and confirm valid fallback instructions still load alongside invalid entries.

GitOrigin-RevId: 4369e9b97ae6a3560d09e79efd8c85adac3d1d90
This commit is contained in:
Eric Traut authored and copyberry committed 2026-09-16 04:59:23 +00:00
1 parent 83dc7d11e8
commit 50d77959bf
3 files changed
+140 -4

No files matched your search

+73 -2
View File
@@ -51,6 +51,7 @@ use tokio::sync::Semaphore;
#[derive(Clone, Copy)]
enum InjectedFailure {
MetadataNotFound,
Metadata(io::ErrorKind),
MetadataBlocked,
MetadataBlockedByFilenamePrefix(&'static str),
@@ -128,13 +129,16 @@ impl FailingFileSystem {
options: GetMetadataOptions,
sandbox: Option<&FileSystemSandboxContext>,
) -> io::Result<FileMetadata> {
let path_abs = path.to_abs_path()?;
self.metadata_calls
.paths
.lock()
.expect("metadata paths lock")
.push(path.clone());
self.metadata_calls.started.notify_one();
if matches!(self.failure, InjectedFailure::MetadataNotFound) {
return Err(io::Error::from(io::ErrorKind::NotFound));
}
let path_abs = path.to_abs_path()?;
match self.failure {
InjectedFailure::Metadata(kind) if path_abs == self.path => {
Err(io::Error::new(kind, "injected metadata failure"))
@@ -165,7 +169,8 @@ impl FailingFileSystem {
InjectedFailure::MetadataPending if path_abs == self.path => {
std::future::pending().await
}
InjectedFailure::Metadata(_)
InjectedFailure::MetadataNotFound
| InjectedFailure::Metadata(_)
| InjectedFailure::MetadataBlocked
| InjectedFailure::MetadataBlockedByFilenamePrefix(_)
| InjectedFailure::MetadataPending
@@ -1566,6 +1571,72 @@ async fn agents_local_md_preferred() {
);
}
#[tokio::test]
async fn fallback_paths_are_rejected_before_filesystem_probes() {
let tmp = tempfile::tempdir().expect("tempdir");
let mut cfg = make_config_with_project_root_markers(
&tmp,
/*limit*/ 4096,
/*instructions*/ None,
&[],
)
.await;
let windows_paths = [
r"..\AGENTS.md",
r"nested\AGENTS.md",
r"\AGENTS.md",
r"C:\AGENTS.md",
"C:AGENTS.md",
r"\\server\share\AGENTS.md",
r"\\?\UNC\server\share\AGENTS.md",
r"\\.\pipe\instructions",
"AGENTS.md:stream",
];
cfg.project_doc_fallback_filenames = [
"",
".",
"..",
"/AGENTS.md",
"../AGENTS.md",
"nested/AGENTS.md",
"//server/share/AGENTS.md",
"AGENTS\0.md",
]
.into_iter()
.chain(windows_paths)
.chain(["WORKFLOW.md", "WORKFLOW.md", ".instructions.md"])
.map(str::to_owned)
.collect();
// Backslashes and colons are ordinary filename characters on POSIX executors.
for (cwd, extra_filenames) in [
("file:///repo", windows_paths.as_slice()),
("file:///C:/repo", &[][..]),
] {
let cwd: PathUri = cwd.parse().expect("cwd URI");
let metadata_calls = Arc::new(MetadataCallCounts::default());
let filesystem = FailingFileSystem {
path: tmp.abs(),
failure: InjectedFailure::MetadataNotFound,
metadata_calls: Arc::clone(&metadata_calls),
};
let paths = super::agents_md_paths(&cfg, &cwd, &filesystem, /*sandbox*/ None)
.await
.expect("discover paths");
assert_eq!(paths, Vec::<PathUri>::new());
assert_eq!(
*metadata_calls.paths.lock().expect("metadata paths lock"),
["AGENTS.override.md", "AGENTS.md"]
.into_iter()
.chain(extra_filenames.iter().copied())
.chain(["WORKFLOW.md", ".instructions.md"])
.map(|name| cwd.join(name).expect("filename"))
.collect::<Vec<_>>()
);
}
}
/// When AGENTS.md is absent but a configured fallback exists, the fallback is used.
#[tokio::test]
async fn uses_configured_fallback_when_agents_missing() {