@@ -170,31 +170,54 @@ def _scan_ast(code: str, cell_num: int) -> List[str]:
170170 for node in ast .walk (tree ):
171171 # Check Imports
172172 if isinstance (node , (ast .Import , ast .ImportFrom )):
173- names = []
173+ module_names = []
174174 if isinstance (node , ast .Import ):
175- names = [n .name for n in node .names ]
175+ module_names = [alias .name for alias in node .names ]
176176 elif isinstance (node , ast .ImportFrom ) and node .module :
177- names = [node .module ]
178-
179- for name in names :
180- severity = get_severity (name , "*" )
177+ module_names = [node .module ]
178+
179+ for mod_name in module_names :
180+ # FIX: Check both with a wildcard (for fully-blocked modules like os/subprocess)
181+ # AND with specific imported names (for partially-blocked modules like builtins).
182+ severity = get_severity (mod_name , "*" )
183+
184+ if severity is None and isinstance (node , ast .ImportFrom ):
185+ # For "from builtins import eval" — check individual imported names
186+ for alias in node .names :
187+ severity = get_severity (mod_name , alias .name )
188+ if severity :
189+ threats .append (
190+ f"{ severity } : Dangerous import in cell { cell_num } : "
191+ f"'from { mod_name } import { alias .name } '"
192+ )
193+ continue
194+
181195 if severity == "CRITICAL" :
182- threats .append (f"CRITICAL: Unsafe import in cell { cell_num } : '{ name } '" )
196+ threats .append (
197+ f"CRITICAL: Unsafe import in cell { cell_num } : '{ mod_name } '"
198+ )
183199
184- # Check Function Calls
200+ # Check Function Calls (unchanged)
185201 if isinstance (node , ast .Call ):
186202 if isinstance (node .func , ast .Attribute ):
187203 if isinstance (node .func .value , ast .Name ):
188204 module = node .func .value .id
189205 method = node .func .attr
190206 severity = get_severity (module , method )
191207 if severity :
192- threats .append (f"{ severity } : Dangerous call in cell { cell_num } : { module } .{ method } ()" )
208+ threats .append (
209+ f"{ severity } : Dangerous call in cell { cell_num } : "
210+ f"{ module } .{ method } ()"
211+ )
193212 elif isinstance (node .func , ast .Name ):
194213 func_name = node .func .id
195214 severity = get_severity ("builtins" , func_name )
196215 if severity :
197- threats .append (f"{ severity } : Dangerous call in cell { cell_num } : { func_name } ()" )
216+ threats .append (
217+ f"{ severity } : Dangerous call in cell { cell_num } : { func_name } ()"
218+ )
198219 except Exception :
199220 pass
221+
200222 return threats
223+
0 commit comments