@@ -61,14 +61,22 @@ def parse_crates_toml(path: Path) -> list[dict]:
6161
6262def get_capsec_version () -> str :
6363 """Get the installed cargo-capsec version."""
64+ # Try --version first (most reliable)
65+ result = subprocess .run (
66+ ["cargo" , "capsec" , "--version" ],
67+ capture_output = True ,
68+ text = True ,
69+ )
70+ if result .returncode == 0 and result .stdout .strip ():
71+ return result .stdout .strip ()
72+ # Fall back to parsing help output
6473 result = subprocess .run (
6574 ["cargo" , "capsec" , "audit" , "--help" ],
6675 capture_output = True ,
6776 text = True ,
6877 )
69- # Try to extract from help output or just return unknown
7078 for line in result .stdout .splitlines ():
71- if "version" in line .lower ():
79+ if "version" in line .lower () or "capsec" in line . lower () :
7280 return line .strip ()
7381 return "unknown"
7482
@@ -97,6 +105,20 @@ def clone_repo(repo_url: str, dest: Path) -> bool:
97105 return True
98106
99107
108+ def strip_temp_paths (audit_data : dict | None , crate_dir : Path ) -> dict | None :
109+ """Strip absolute temp directory prefix from file paths in audit output."""
110+ if not audit_data :
111+ return audit_data
112+ prefix = str (crate_dir )
113+ if not prefix .endswith ("/" ):
114+ prefix += "/"
115+ for crate_entry in audit_data .get ("crates" , []):
116+ for finding in crate_entry .get ("findings" , []):
117+ if "file" in finding and finding ["file" ].startswith (prefix ):
118+ finding ["file" ] = finding ["file" ][len (prefix ):]
119+ return audit_data
120+
121+
100122def run_audit (crate_dir : Path ) -> dict :
101123 """Run cargo capsec audit on a directory. Returns parsed result."""
102124 start = time .monotonic ()
@@ -117,7 +139,8 @@ def run_audit(crate_dir: Path) -> dict:
117139
118140 if result .stdout .strip ():
119141 try :
120- output ["audit" ] = json .loads (result .stdout )
142+ audit = json .loads (result .stdout )
143+ output ["audit" ] = strip_temp_paths (audit , crate_dir )
121144 except json .JSONDecodeError :
122145 output ["audit" ] = None
123146 output ["parse_error" ] = "Failed to parse JSON output"
0 commit comments