Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 147 additions & 1 deletion src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,67 @@ use reqwest::header;
use serde_json::Value;
use std::collections::HashSet;
use std::io::{self, Read};
use std::path::Path;
use std::path::{Path, PathBuf};
use std::process::Command;
use uuid::Uuid;

const CORGEA_POLICY_FILENAMES: &[&str] = &["corgea.yaml", "corgea.yml"];

fn is_regular_file(path: &Path) -> bool {
!path.is_symlink() && path.is_file()
}

/// `corgea.yaml` / `corgea.yml` under `root`, including a gitignored copy at `root`.
fn find_corgea_policy_files(root: &Path) -> Vec<String> {
if !root.is_dir() {
return Vec::new();
}

let mut found = Vec::new();
for name in CORGEA_POLICY_FILENAMES {
if is_regular_file(&root.join(name)) {
found.push((*name).to_string());
Comment thread
leenk7991 marked this conversation as resolved.
}
}

let walker = ignore::WalkBuilder::new(root)
.standard_filters(true)
.build();
for result in walker {
let Ok(entry) = result else {
continue;
};
let path = entry.path();
if !is_regular_file(path) {
continue;
}
let Some(name) = path.file_name().and_then(|n| n.to_str()) else {
continue;
};
if !CORGEA_POLICY_FILENAMES.contains(&name) {
continue;
}
if let Ok(rel) = path.strip_prefix(root) {
let rel = rel.to_string_lossy().replace('\\', "/");
if !found.iter().any(|existing| existing == &rel) {
found.push(rel);
}
}
}
found.sort();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: Normalize policy paths before deduplication

Deduplication compares raw strings, so an existing equivalent path such as ./corgea.yaml or an absolute path does not match discovered corgea.yaml. This can upload the same file twice. Normalize paths before comparison or deduplicate by resolved filesystem identity.

Proof or reproduction:

assert_eq!(merge_corgea_policy_files(vec!["./corgea.yaml".into()], root.path()), vec!["./corgea.yaml", "corgea.yaml"]);

found
}

fn merge_corgea_policy_files(mut paths: Vec<String>, root: &Path) -> Vec<String> {
for yaml in find_corgea_policy_files(root) {
if !paths.iter().any(|path| path == &yaml) {
debug(&format!("Including repo policy file: {yaml}"));
paths.push(yaml);
}
}
paths
}

pub fn run_command(base_cmd: &String, mut command: Command) -> String {
match which::which(base_cmd) {
Ok(_) => {
Expand Down Expand Up @@ -222,6 +279,8 @@ pub fn upload_scan(
save_to_file: bool,
project_name: Option<String>,
) -> Option<ScanUploadResult> {
let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
let paths = merge_corgea_policy_files(paths, &cwd);
let in_ci = running_in_ci();
let ci_platform = which_ci();
let github_env_vars = get_github_env_vars();
Expand Down Expand Up @@ -597,6 +656,7 @@ pub fn upload_scan(
#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;

#[test]
fn scan_url_prefers_project_id_when_present() {
Expand Down Expand Up @@ -631,4 +691,90 @@ mod tests {
"https://www.corgea.app/project/corgea_cli-1.0?scan_id=scan-123"
);
}

fn write_policy(path: &Path) {
std::fs::write(path, "policies: []\n").unwrap();
}

fn git_repo(root: &Path) {
std::fs::create_dir(root.join(".git")).unwrap();
}

#[test]
fn find_corgea_policy_files_picks_yaml_and_yml_by_basename() {
let root = tempfile::tempdir().unwrap();
write_policy(&root.path().join("corgea.yaml"));
std::fs::create_dir(root.path().join("src")).unwrap();
write_policy(&root.path().join("src/corgea.yml"));
write_policy(&root.path().join("with-guidance.yaml"));

assert_eq!(
find_corgea_policy_files(root.path()),
vec!["corgea.yaml".to_string(), "src/corgea.yml".to_string()]
);
}

#[test]
fn find_corgea_policy_files_returns_empty_when_none_exist() {
let root = tempfile::tempdir().unwrap();
assert!(find_corgea_policy_files(root.path()).is_empty());
}

#[cfg(unix)]
#[test]
fn find_corgea_policy_files_skips_symlink_outside_repo() {
use std::os::unix::fs::symlink;
let root = tempfile::tempdir().unwrap();
let secret = tempfile::NamedTempFile::new().unwrap();
std::fs::write(secret.path(), "secret").unwrap();
symlink(secret.path(), root.path().join("corgea.yaml")).unwrap();

assert!(find_corgea_policy_files(root.path()).is_empty());
}

#[test]
fn find_corgea_policy_files_skips_gitignored_nested_copies() {
let root = tempfile::tempdir().unwrap();
git_repo(root.path());
std::fs::write(root.path().join(".gitignore"), "node_modules/\n").unwrap();
write_policy(&root.path().join("corgea.yaml"));
std::fs::create_dir_all(root.path().join("node_modules/pkg")).unwrap();
write_policy(&root.path().join("node_modules/pkg/corgea.yaml"));

assert_eq!(
find_corgea_policy_files(root.path()),
vec!["corgea.yaml".to_string()]
);
}

#[test]
fn find_corgea_policy_files_keeps_root_file_even_if_gitignored() {
let root = tempfile::tempdir().unwrap();
git_repo(root.path());
std::fs::write(root.path().join(".gitignore"), "corgea.yaml\n").unwrap();
write_policy(&root.path().join("corgea.yaml"));

assert_eq!(
find_corgea_policy_files(root.path()),
vec!["corgea.yaml".to_string()]
);
}

#[test]
fn merge_corgea_policy_files_appends_missing_and_skips_duplicates() {
let root = tempfile::tempdir().unwrap();
write_policy(&root.path().join("corgea.yaml"));

assert_eq!(
merge_corgea_policy_files(vec!["src/source.py".into()], root.path()),
vec!["src/source.py".to_string(), "corgea.yaml".to_string()]
);
assert_eq!(
merge_corgea_policy_files(
vec!["src/source.py".into(), "corgea.yaml".into()],
root.path()
),
vec!["src/source.py".to_string(), "corgea.yaml".to_string()]
);
}
}
Loading