@@ -41,39 +41,37 @@ def assess(self, repository: Repository) -> Finding:
4141 """
4242 claude_md_path = repository .path / "CLAUDE.md"
4343
44- if claude_md_path .exists ():
45- # Check file size (should have content)
46- try :
47- size = claude_md_path .stat ().st_size
48- if size < 50 :
49- # File exists but is too small
50- return Finding (
51- attribute = self .attribute ,
52- status = "fail" ,
53- score = 25.0 ,
54- measured_value = f"{ size } bytes" ,
55- threshold = ">50 bytes" ,
56- evidence = [f"CLAUDE.md exists but is minimal ({ size } bytes)" ],
57- remediation = self ._create_remediation (),
58- error_message = None ,
59- )
44+ # Fix TOCTOU: Use try-except around file read instead of existence check
45+ try :
46+ with open (claude_md_path , "r" , encoding = "utf-8" ) as f :
47+ content = f .read ()
6048
49+ size = len (content )
50+ if size < 50 :
51+ # File exists but is too small
6152 return Finding (
6253 attribute = self .attribute ,
63- status = "pass " ,
64- score = 100 .0 ,
65- measured_value = "present " ,
66- threshold = "present " ,
67- evidence = [f"CLAUDE.md found at { claude_md_path } " ],
68- remediation = None ,
54+ status = "fail " ,
55+ score = 25 .0 ,
56+ measured_value = f" { size } bytes " ,
57+ threshold = ">50 bytes " ,
58+ evidence = [f"CLAUDE.md exists but is minimal ( { size } bytes) " ],
59+ remediation = self . _create_remediation () ,
6960 error_message = None ,
7061 )
7162
72- except OSError :
73- return Finding .error (
74- self .attribute , reason = "Could not read CLAUDE.md file"
75- )
76- else :
63+ return Finding (
64+ attribute = self .attribute ,
65+ status = "pass" ,
66+ score = 100.0 ,
67+ measured_value = "present" ,
68+ threshold = "present" ,
69+ evidence = [f"CLAUDE.md found at { claude_md_path } " ],
70+ remediation = None ,
71+ error_message = None ,
72+ )
73+
74+ except FileNotFoundError :
7775 return Finding (
7876 attribute = self .attribute ,
7977 status = "fail" ,
@@ -84,6 +82,10 @@ def assess(self, repository: Repository) -> Finding:
8482 remediation = self ._create_remediation (),
8583 error_message = None ,
8684 )
85+ except OSError as e :
86+ return Finding .error (
87+ self .attribute , reason = f"Could not read CLAUDE.md file: { e } "
88+ )
8789
8890 def _create_remediation (self ) -> Remediation :
8991 """Create remediation guidance for missing/inadequate CLAUDE.md."""
@@ -171,19 +173,7 @@ def assess(self, repository: Repository) -> Finding:
171173 """
172174 readme_path = repository .path / "README.md"
173175
174- if not readme_path .exists ():
175- return Finding (
176- attribute = self .attribute ,
177- status = "fail" ,
178- score = 0.0 ,
179- measured_value = "missing" ,
180- threshold = "present with sections" ,
181- evidence = ["README.md not found" ],
182- remediation = self ._create_remediation (),
183- error_message = None ,
184- )
185-
186- # Read README and check for key sections
176+ # Fix TOCTOU: Use try-except around file read instead of existence check
187177 try :
188178 with open (readme_path , "r" , encoding = "utf-8" ) as f :
189179 content = f .read ().lower ()
@@ -231,6 +221,17 @@ def assess(self, repository: Repository) -> Finding:
231221 error_message = None ,
232222 )
233223
224+ except FileNotFoundError :
225+ return Finding (
226+ attribute = self .attribute ,
227+ status = "fail" ,
228+ score = 0.0 ,
229+ measured_value = "missing" ,
230+ threshold = "present with sections" ,
231+ evidence = ["README.md not found" ],
232+ remediation = self ._create_remediation (),
233+ error_message = None ,
234+ )
234235 except OSError as e :
235236 return Finding .error (
236237 self .attribute , reason = f"Could not read README.md: { str (e )} "
0 commit comments