use std::path::{Component, Path};
use crate::domain::{FileDisposition, IndexTargets, LanguageId, MetadataOnlyReason};
use crate::live_index::LiveIndex;
use crate::protocol::format;
#[derive(Debug, PartialEq, Eq)]
pub enum GenerationResolution<'a> {
Published(&'a [u8]),
NotInGeneration,
}
pub fn resolve_generation_bytes<'a>(
live: &'a LiveIndex,
relative_path: &str,
) -> GenerationResolution<'a> {
match live.get_file(relative_path) {
Some(file) => GenerationResolution::Published(&file.content),
None => GenerationResolution::NotInGeneration,
}
}
pub fn observe_disk_beneath(
live: &LiveIndex,
workspace_root: &Path,
relative_path: &str,
) -> Result<Vec<u8>, String> {
observe_beneath(live, workspace_root, relative_path, true)
}
pub(crate) fn observe_disk_beneath_without_lines(
live: &LiveIndex,
workspace_root: &Path,
relative_path: &str,
) -> Result<Vec<u8>, String> {
observe_beneath(live, workspace_root, relative_path, false)
}
fn observe_beneath(
live: &LiveIndex,
workspace_root: &Path,
relative_path: &str,
name_lines: bool,
) -> Result<Vec<u8>, String> {
let candidate = Path::new(relative_path);
let escapes = candidate.components().any(|component| {
matches!(
component,
Component::ParentDir | Component::RootDir | Component::Prefix(_)
)
});
if escapes || candidate.is_absolute() {
return Err(format!(
"{relative_path} [error: path escapes the workspace root; a disk \
observation is confined beneath it]"
));
}
refuse_disk_spelling(workspace_root, relative_path)?;
let full_path = workspace_root.join(candidate);
disk_read(live, relative_path, &full_path, name_lines)
}
fn refuse_disk_spelling(root: &Path, relative_path: &str) -> Result<(), String> {
crate::protocol::edit::refuse_path_alias(root, relative_path)?;
if let Some(refusal) = hard_scope_refusal(relative_path) {
return Err(refusal);
}
if let Some(rule_id) =
crate::knowledge::sensitive_path_rule_at(relative_path, &root.join(relative_path))
{
return Err(format::content_withheld_by_path_rule(
relative_path,
rule_id,
));
}
Ok(())
}
pub(crate) fn hard_scope_refusal(relative_path: &str) -> Option<String> {
crate::discovery::path_is_hard_scope_excluded(Path::new(relative_path)).then(|| {
format!(
"{relative_path} [error: VCS and runtime-state internals are outside \
source scope; a disk observation never reads them]"
)
})
}
pub(crate) fn admit_worktree_text(
live: &LiveIndex,
repo: &crate::git::GitRepo,
relative_path: &str,
) -> Result<Option<String>, String> {
worktree_text(live, repo, relative_path, true)
}
pub(crate) fn admit_worktree_text_without_lines(
live: &LiveIndex,
repo: &crate::git::GitRepo,
relative_path: &str,
) -> Result<Option<String>, String> {
worktree_text(live, repo, relative_path, false)
}
fn worktree_text(
live: &LiveIndex,
repo: &crate::git::GitRepo,
relative_path: &str,
name_lines: bool,
) -> Result<Option<String>, String> {
let Some(workdir) = repo.workdir() else {
return Err("bare repository has no working directory".to_string());
};
let full_path = workdir.join(relative_path);
if !full_path.is_file() {
return Ok(None);
}
refuse_disk_spelling(workdir, relative_path)?;
let bytes = disk_read(live, relative_path, &full_path, name_lines)?;
Ok(String::from_utf8(bytes).ok())
}
pub(crate) fn disk_read_would_refuse(
live: &LiveIndex,
relative_path: &str,
size: Option<u64>,
) -> bool {
if crate::knowledge::sensitive_path_rule(relative_path).is_some() {
return true;
}
if let Some(FileDisposition::MetadataOnly {
reason:
MetadataOnlyReason::SensitivePath { .. } | MetadataOnlyReason::SensitiveContent { .. },
}) = live.capture_file_disposition(relative_path)
{
return true;
}
size.is_some_and(|size| crate::knowledge::exceeds_scan_limit(size as usize))
}
pub(crate) fn refuse_by_policy(live: &LiveIndex, relative_path: &str) -> Option<String> {
if let Some(rule_id) = crate::knowledge::sensitive_path_rule(relative_path) {
crate::protocol::withheld::record_pending_withheld(
crate::protocol::withheld::WithheldMeta::path_rule_only(relative_path, rule_id),
);
return Some(format::content_withheld_by_path_rule(
relative_path,
rule_id,
));
}
if let Some(FileDisposition::MetadataOnly { reason }) =
live.capture_file_disposition(relative_path)
{
match reason {
MetadataOnlyReason::SensitiveContent { rule_ids, .. }
if rule_ids
.iter()
.any(|id| id == crate::knowledge::INDETERMINATE_RULE_ID) =>
{
crate::protocol::withheld::record_pending_withheld(
crate::protocol::withheld::WithheldMeta::unscanned(relative_path),
);
return Some(format::content_withheld_unscanned(relative_path));
}
MetadataOnlyReason::SensitivePath { rule_id } => {
crate::protocol::withheld::record_pending_withheld(
crate::protocol::withheld::WithheldMeta::path_rule_only(relative_path, rule_id),
);
return Some(format::content_withheld_by_path_rule(
relative_path,
rule_id,
));
}
MetadataOnlyReason::SensitiveContent {
rule_ids,
finding_count,
} => {
return Some(format::content_withheld_by_admission(
relative_path,
rule_ids,
*finding_count,
&[],
));
}
_ => {}
}
}
unverified_notice(live, relative_path)
}
pub(crate) fn normalize_requested_path(raw: &str) -> String {
let mut normalized = raw.trim().replace('\\', "/");
while normalized.starts_with("./") {
normalized = normalized[2..].to_string();
}
normalized.trim_matches('/').to_string()
}
pub(crate) fn unverified_notice(live: &LiveIndex, requested: &str) -> Option<String> {
let path = normalize_requested_path(requested);
if crate::knowledge::sensitive_path_rule(&path).is_some() {
return None;
}
live.unverified_since_restore(&path)
.map(|reason| format::unverified_since_restore(&path, reason))
}
fn recorded_refusal_naming_lines(live: &LiveIndex, relative_path: &str) -> Option<String> {
if crate::knowledge::sensitive_path_rule(relative_path).is_some() {
return None;
}
let Some(FileDisposition::MetadataOnly {
reason:
MetadataOnlyReason::SensitiveContent {
rule_ids,
finding_count,
},
}) = live.capture_file_disposition(relative_path)
else {
return None;
};
if rule_ids
.iter()
.any(|id| id == crate::knowledge::INDETERMINATE_RULE_ID)
{
return None;
}
let (line_ranges, findings) = recorded_finding_evidence(live, relative_path, rule_ids);
if !findings.is_empty() {
crate::protocol::withheld::record_pending_withheld(
crate::protocol::withheld::WithheldMeta::from_content_findings(
relative_path,
&findings,
),
);
}
(!line_ranges.is_empty()).then(|| {
format::content_withheld_by_admission(relative_path, rule_ids, *finding_count, &line_ranges)
})
}
pub(crate) fn read_regular_file(path: &Path) -> std::io::Result<Vec<u8>> {
read_regular_file_limited(path, None)
}
fn read_regular_file_limited(path: &Path, limit: Option<u64>) -> std::io::Result<Vec<u8>> {
let metadata = std::fs::symlink_metadata(path)?;
if !metadata.is_file() {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"not a regular file",
));
}
let mut file = open_for_gate_read(path)?;
if !file.metadata().is_ok_and(|opened| opened.is_file()) {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"not a regular file",
));
}
let mut bytes = Vec::new();
if let Some(limit) = limit {
std::io::Read::read_to_end(&mut std::io::Read::take(&mut file, limit), &mut bytes)?;
} else {
std::io::Read::read_to_end(&mut file, &mut bytes)?;
}
Ok(bytes)
}
fn open_for_gate_read(path: &Path) -> std::io::Result<std::fs::File> {
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
std::fs::OpenOptions::new()
.read(true)
.custom_flags(libc::O_NONBLOCK)
.open(path)
}
#[cfg(not(unix))]
{
std::fs::File::open(path)
}
}
fn recorded_finding_evidence(
live: &LiveIndex,
relative_path: &str,
recorded: &[String],
) -> (
Vec<(u32, u32)>,
Vec<crate::knowledge::SecretFindingDescriptor>,
) {
let Some(root) = live.indexed_root.as_deref() else {
return (Vec::new(), Vec::new());
};
let Ok(Some(full_path)) = crate::discovery::resolve_repo_path(root, relative_path) else {
return (Vec::new(), Vec::new());
};
let budget = crate::knowledge::SECRET_SCAN_MAX_BYTES as u64 + 1;
let Ok(bytes) = read_regular_file_limited(&full_path, Some(budget)) else {
return (Vec::new(), Vec::new());
};
if crate::knowledge::exceeds_scan_limit(bytes.len()) {
return (Vec::new(), Vec::new());
}
match crate::knowledge::scan_secret_bytes(relative_path, &bytes) {
crate::knowledge::SecretScan::Sensitive {
rule_ids,
line_ranges,
findings,
..
} if rule_ids.len() == recorded.len()
&& rule_ids
.iter()
.all(|rule| recorded.iter().any(|seen| seen == rule)) =>
{
(line_ranges, findings)
}
_ => (Vec::new(), Vec::new()),
}
}
pub(crate) fn admit_bytes(
live: &LiveIndex,
relative_path: &str,
bytes: Vec<u8>,
) -> Result<Vec<u8>, String> {
if let Some(refusal) = refuse_by_policy(live, relative_path) {
return Err(refusal);
}
if let Some(refusal) = classify_admitted_bytes(live, relative_path, &bytes) {
return Err(refusal);
}
Ok(bytes)
}
pub(crate) fn admit_git_text(
live: &LiveIndex,
repo: &crate::git::GitRepo,
git_ref: &str,
relative_path: &str,
) -> Result<Option<String>, String> {
if let Some(refusal) = refuse_by_policy(live, relative_path) {
return Err(refusal);
}
let Some(text) = repo.file_at_ref(git_ref, relative_path)? else {
return Ok(None);
};
let admitted = admit_bytes(live, relative_path, text.into_bytes())?;
Ok(String::from_utf8(admitted).ok())
}
pub(crate) fn admit_disk_read(
live: &LiveIndex,
relative_path: &str,
canon_path: &Path,
) -> Result<Vec<u8>, String> {
disk_read(live, relative_path, canon_path, true)
}
fn disk_read(
live: &LiveIndex,
relative_path: &str,
canon_path: &Path,
name_lines: bool,
) -> Result<Vec<u8>, String> {
if let Some(refusal) = refuse_by_policy(live, relative_path) {
return Err(if name_lines {
recorded_refusal_naming_lines(live, relative_path).unwrap_or(refusal)
} else {
refusal
});
}
let bytes = match read_regular_file(canon_path) {
Ok(bytes) => bytes,
Err(e) => return Err(format!("{relative_path} [error: could not read file: {e}]")),
};
if let Some(refusal) = classify_admitted_bytes(live, relative_path, &bytes) {
return Err(refusal);
}
Ok(bytes)
}
fn classify_admitted_bytes(live: &LiveIndex, relative_path: &str, bytes: &[u8]) -> Option<String> {
if crate::knowledge::exceeds_scan_limit(bytes.len())
|| crate::knowledge::decode_searchable_text(bytes).is_err()
{
crate::protocol::withheld::record_pending_withheld(
crate::protocol::withheld::WithheldMeta::unscanned(relative_path),
);
return Some(format::content_withheld_unscanned(relative_path));
}
let language = Path::new(relative_path)
.extension()
.and_then(|extension| extension.to_str())
.and_then(LanguageId::from_extension);
let targets = IndexTargets::for_path(relative_path, language.as_ref());
let mut finding_lines = Vec::new();
let mut finding_descriptors: Vec<crate::knowledge::SecretFindingDescriptor> = Vec::new();
if let crate::knowledge::StableContentAdmission::MetadataOnly(
MetadataOnlyReason::SensitiveContent {
rule_ids,
finding_count,
},
) = crate::knowledge::classify_stable_content_with(
relative_path,
targets,
bytes,
|path, bytes| {
let scan = match live.indexed_root.as_deref() {
Some(root) => {
crate::knowledge::secret_dismissals::scan_with_dismissals(root, path, bytes)
}
None => crate::knowledge::scan_secret_bytes(path, bytes),
};
if let crate::knowledge::SecretScan::Sensitive {
line_ranges,
findings,
..
} = &scan
{
finding_lines.clone_from(line_ranges);
finding_descriptors.clone_from(findings);
}
scan
},
) {
return Some(
if rule_ids
.iter()
.any(|id| id == crate::knowledge::INDETERMINATE_RULE_ID)
{
crate::protocol::withheld::record_pending_withheld(
crate::protocol::withheld::WithheldMeta::unscanned(relative_path),
);
format::content_withheld_unscanned(relative_path)
} else {
if !finding_descriptors.is_empty() {
crate::protocol::withheld::record_pending_withheld(
crate::protocol::withheld::WithheldMeta::from_content_findings(
relative_path,
&finding_descriptors,
),
);
}
format::content_withheld_by_admission(
relative_path,
&rule_ids,
finding_count,
&finding_lines,
)
},
);
}
None
}
pub struct ReadGate;
impl ReadGate {
pub fn admit_disk_read(
live: &LiveIndex,
relative_path: &str,
canon_path: &Path,
) -> Result<Vec<u8>, String> {
admit_disk_read(live, relative_path, canon_path)
}
}
#[cfg(test)]
mod tests {
#[test]
fn recorded_line_reread_is_called_only_from_the_disk_read_lane() {
let needle = concat!("recorded_refusal_", "naming_lines(");
let manifest = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
let src = manifest.join("src");
let mut callers: Vec<(String, String)> = Vec::new();
let mut stack = vec![src.clone()];
while let Some(dir) = stack.pop() {
for entry in std::fs::read_dir(&dir).expect("read source dir") {
let path = entry.expect("dir entry").path();
if path.is_dir() {
stack.push(path);
continue;
}
if path.extension().and_then(|e| e.to_str()) != Some("rs") {
continue;
}
let relative = path
.strip_prefix(&src)
.expect("under src")
.to_string_lossy()
.replace('\\', "/");
let contents = std::fs::read_to_string(&path).expect("read source file");
let mut enclosing = String::new();
for line in contents.lines() {
let trimmed = line.trim_start();
if trimmed.starts_with("//") {
continue;
}
for prefix in ["fn ", "pub fn ", "pub(crate) fn "] {
if let Some(rest) = trimmed.strip_prefix(prefix) {
enclosing = rest.split(['(', '<']).next().unwrap_or("").to_string();
}
}
if line.contains(needle) && !trimmed.contains(&format!("fn {needle}")) {
callers.push((relative.clone(), enclosing.clone()));
}
}
}
}
assert_eq!(
callers,
vec![("protocol/read_gate.rs".to_string(), "disk_read".to_string())],
"the line re-read must be reached only through the disk-read lane"
);
}
}