use super::read_admission_format as format;
use super::withheld::WithheldMeta;
use crate::domain::{FileDisposition, IndexTargets, LanguageId, MetadataOnlyReason};
use crate::live_index::LiveIndex;
use std::path::Path;
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 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))
}
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_with(
live: &LiveIndex,
relative_path: &str,
record: &mut dyn FnMut(WithheldMeta),
) -> Option<String> {
if let Some(rule_id) = crate::knowledge::sensitive_path_rule(relative_path) {
record(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) =>
{
record(WithheldMeta::unscanned(relative_path));
return Some(format::content_withheld_unscanned(relative_path));
}
MetadataOnlyReason::SensitivePath { rule_id } => {
record(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 classify_admitted_bytes_with(
live: &LiveIndex,
relative_path: &str,
bytes: &[u8],
record: &mut dyn FnMut(WithheldMeta),
) -> Option<String> {
if crate::knowledge::exceeds_scan_limit(bytes.len())
|| crate::knowledge::decode_searchable_text(bytes).is_err()
{
record(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)
{
record(WithheldMeta::unscanned(relative_path));
format::content_withheld_unscanned(relative_path)
} else {
if !finding_descriptors.is_empty() {
record(WithheldMeta::from_content_findings(
relative_path,
&finding_descriptors,
));
}
format::content_withheld_by_admission(
relative_path,
&rule_ids,
finding_count,
&finding_lines,
)
},
);
}
None
}
pub(crate) fn recorded_finding_evidence_from_bytes(
relative_path: &str,
bytes: &[u8],
recorded: &[String],
) -> (
Vec<(u32, u32)>,
Vec<crate::knowledge::SecretFindingDescriptor>,
) {
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) const PATH_SPELLING_DIFFERS: &str = "path spelling differs from the on-disk name";
pub(crate) fn resolve_repo_path(
repo_root: &Path,
relative_path: &str,
) -> Result<Option<std::path::PathBuf>, String> {
use crate::discovery::PathRefusal;
crate::discovery::resolve_repo_path(repo_root, relative_path).map_err(|refusal| match refusal {
PathRefusal::OutsideRoot => format!("path '{relative_path}' is outside the repository"),
PathRefusal::Unresolvable(message) => message,
PathRefusal::WindowsAlias => format!(
"path '{relative_path}' is an alias spelling on Windows (a ':' stream \
suffix or a trailing dot or space); use the file's own name"
),
PathRefusal::CredentialAlias(rule_id) => {
format::content_withheld_by_path_rule(relative_path, rule_id)
}
PathRefusal::SpellingDiffers(None) => PATH_SPELLING_DIFFERS.to_string(),
PathRefusal::SpellingDiffers(Some(canonical)) => {
format!("{PATH_SPELLING_DIFFERS}; retry with `{canonical}`")
}
})
}
pub(crate) fn refuse_disk_spelling(root: &Path, relative_path: &str) -> Result<(), String> {
resolve_repo_path(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 read_regular_file(path: &Path) -> std::io::Result<Vec<u8>> {
read_regular_file_limited(path, None)
}
pub(crate) 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)
}
pub(crate) 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(windows)]
{
let file = std::fs::File::open(path)?;
if !windows_handle_is_disk_file(&file) {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"not a regular file",
));
}
Ok(file)
}
#[cfg(not(any(unix, windows)))]
{
std::fs::File::open(path)
}
}
#[cfg(windows)]
#[allow(unsafe_code)]
fn windows_handle_is_disk_file(file: &std::fs::File) -> bool {
use std::os::windows::io::AsRawHandle;
use windows::Win32::Foundation::HANDLE;
use windows::Win32::Storage::FileSystem::GetFileType;
let kind = unsafe { GetFileType(HANDLE(file.as_raw_handle())) };
file_type_is_disk(kind.0)
}
#[cfg(any(windows, test))]
pub(crate) fn file_type_is_disk(kind: u32) -> bool {
const FILE_TYPE_DISK: u32 = 1;
const FILE_TYPE_REMOTE: u32 = 0x8000;
kind & !FILE_TYPE_REMOTE == FILE_TYPE_DISK
}
pub(crate) fn disk_read_with(
live: &LiveIndex,
relative_path: &str,
canon_path: &Path,
name_lines: &mut dyn FnMut() -> Option<String>,
record: &mut dyn FnMut(WithheldMeta),
) -> Result<Vec<u8>, String> {
if let Some(refusal) = refuse_by_policy_with(live, relative_path, record) {
return Err(name_lines().unwrap_or(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_with(live, relative_path, &bytes, record) {
return Err(refusal);
}
Ok(bytes)
}
pub(crate) fn worktree_text_with(
repo: &crate::git::GitRepo,
relative_path: &str,
read: &mut dyn FnMut(&Path) -> Result<Vec<u8>, String>,
) -> 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 = read(&full_path)?;
Ok(String::from_utf8(bytes).ok())
}
pub(crate) fn admit_bytes_with(
live: &LiveIndex,
relative_path: &str,
bytes: Vec<u8>,
record: &mut dyn FnMut(WithheldMeta),
) -> Result<Vec<u8>, String> {
if let Some(refusal) = refuse_by_policy_with(live, relative_path, record) {
return Err(refusal);
}
if let Some(refusal) = classify_admitted_bytes_with(live, relative_path, &bytes, record) {
return Err(refusal);
}
Ok(bytes)
}
pub(crate) fn admit_git_text_with(
live: &LiveIndex,
repo: &crate::git::GitRepo,
git_ref: &str,
relative_path: &str,
record: &mut dyn FnMut(WithheldMeta),
) -> Result<Option<String>, String> {
if let Some(refusal) = refuse_by_policy_with(live, relative_path, record) {
return Err(refusal);
}
let Some(text) = repo.file_at_ref(git_ref, relative_path)? else {
return Ok(None);
};
let admitted = admit_bytes_with(live, relative_path, text.into_bytes(), record)?;
Ok(String::from_utf8(admitted).ok())
}