Skip to content

CodeSentinel is a single-file, production-ready secure Python code analyzer and sandbox executor designed to safely inspect and execute untrusted or AI-generated Python code without risking system integrity.

License

Notifications You must be signed in to change notification settings

LegedsDaD/CodeSentinel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 

Repository files navigation

ChatGPT Image Jan 1, 2026, 06_17_28 PM

Python Platform License Status Security Static Analysis Safe Execution AI Ready Offline No API Multiprocessing Timeout Protected Windows Safe Single File Zero Dependencies CLI Tool

๐Ÿ”ฑ CodeSentinel

CodeSentinel is a single-file, production-ready secure Python code analyzer and sandbox executor designed to safely inspect and execute untrusted or AI-generated Python code without risking system integrity.

It performs static AST analysis, assigns a risk score, applies heuristic AI probability checks, and runs code inside a time-limited, restricted sandbox before allowing optional full-access execution.

๐Ÿ‘€Demo

image image

Glance Tables

Platform Compatibility

Platform Supported Notes
Windows โœ… Yes freeze_support enabled
Linux โœ… Yes Fully supported
macOS โœ… Yes Fully supported
WSL โš ๏ธ Partial Depends on config

Feature Comparison

Capability Supported Notes
AST-based static analysis โœ… No code execution required
Dangerous import detection โœ… os, subprocess, socket, etc.
Unsafe built-in detection โœ… eval, exec, compile, open
Attribute call detection โœ… os.system, os.remove, etc.
Risk scoring (0โ€“10) โœ… Severity-weighted
AI heuristic probability โœ… Offline, no APIs
Restricted sandbox execution โœ… Isolated process
Execution timeout โœ… Prevents infinite loops
Full-access execution โš ๏ธ Requires manual confirmation
Internet access โŒ Fully offline by design

AI Heuristic Signals

Signal Description Weight
Long lines >120 characters Medium
Dense logic High complexity Medium
Repetitive patterns Copy-like structure Low
Obfuscation Unusual formatting High

Security Layer Breakdown

Layer Protection Applied Purpose
Static Analysis AST inspection Detect risky code early
Runtime Isolation Separate process Prevent host contamination
Built-ins Whitelisted only Block file/system access
Imports Policy-based blocking Disable dangerous modules
Timeout Forced termination Stop infinite or hanging code
Unsafe Mode Manual consent Prevent accidental harm

Built ins Availability Table

Built-in Restricted Mode Unsafe Mode
print โœ… Allowed โœ… Allowed
input โœ… Allowed โœ… Allowed
open โŒ Blocked โœ… Allowed
exec โŒ Blocked โœ… Allowed
eval โŒ Blocked โœ… Allowed
import ๐Ÿ”’ Restricted โœ… Full

Risk Severity mapping

Severity Score Level Meaning
0โ€“2 ๐ŸŸข LOW Minimal or no risk
3โ€“4 ๐ŸŸก MEDIUM Potentially unsafe
5โ€“7 ๐ŸŸ  HIGH Dangerous patterns detected
8โ€“10 ๐Ÿ”ด CRITICAL Immediate system risk

Detected Issue Types

Issue Type Example Severity
Dangerous module import os ๐Ÿ”ด High
Unsafe built-in eval("code") ๐Ÿ”ด High
System call os.system("cmd") ๐Ÿ”ด Critical
File deletion os.remove("x") ๐Ÿ”ด Critical
Long lines >120 chars ๐ŸŸก Medium

Execution Outcome Matrix.

Scenario Restricted Mode Result Unsafe Mode Result
Safe code โœ… Executes normally โœ… Executes normally
File deletion attempt โŒ Blocked โš ๏ธ Allowed
Dangerous import โŒ Blocked โš ๏ธ Allowed
Infinite loop โŒ Terminated โŒ May hang system
Network access โŒ Blocked โš ๏ธ Allowed

CLI Command Reference

Command Description Executes Code
scan <file.py> Analyze code only โŒ No
run <file.py> Run in restricted sandbox โœ… Yes (safe)
Unsafe override Full access execution โš ๏ธ Yes (dangerous)

Sandbox mode Comparison

Feature Restricted Mode Unsafe Mode
Filesystem access โŒ Blocked โœ… Allowed
Dangerous imports โŒ Blocked โœ… Allowed
Built-ins Whitelisted Full Python
Timeout enforced โœ… Yes โŒ No
User confirmation โŒ No โœ… Required
System risk ๐ŸŸข Low ๐Ÿ”ด High

Ideal user profiles.

User How CodeSentinel Helps
AI Developers Inspect AI-generated scripts
Security Researchers Test unsafe code safely
Students Learn Python security
Educators Demonstrate sandboxing
Offline Users Secure execution without internet

Roadmap Status

Feature Status
Core analyzer โœ… Complete
Restricted sandbox โœ… Complete
Risk scoring โœ… Complete
JSON report export โณ Planned
Custom policies โณ Planned
GUI interface โณ Planned
PyPI package โณ Planned

Community Engagement

Action Impact
โญ Star the repo Helps visibility
๐Ÿ› Report issues Improves security
๐Ÿ”ง Submit PRs Grows the project
๐Ÿ“ข Share Helps safe coding

๐Ÿš€ Features

๐Ÿ” Static Security Analysis

AST-based inspection (no execution required)

Detects:

1)Dangerous module imports (os, subprocess, socket, etc.) 2)Unsafe built-ins (eval, exec, compile, open) 3)High-risk attribute calls (os.system, os.remove, etc.) 4)Line-accurate reporting with code snippets

๐Ÿงฎ Risk Scoring System

1)Severity-weighted scoring model 2)Normalized 0โ€“10 risk score 3.Automatically flags high-risk files

๐Ÿค– AI Heuristic Probability

1)Lightweight heuristic to estimate AI-generated code likelihood 2)Zero external APIs 3)Fully offline and deterministic

๐Ÿงช Secure Sandbox Execution

1)Runs code in a separate process 2)Enforced execution timeout 3)Restricted built-ins and blocked filesystem access 4)Dangerous imports disabled at runtime 5)Optional manual override for unrestricted execution

๐ŸชŸ Windows-Safe by Design

1)Uses multiprocessing.freeze_support() 2)Compatible with Windows executables and frozen builds

๐Ÿ“ฆ Installation

No dependencies required.

git clone https://github.com/LegedsDaD/codesentinel.git cd codesentinel

Python 3.8+ is required.

๐Ÿ”Ž Scan a Python file (no execution)

python codesentinel.py scan example.py

Output includes:

Detected issues Severity levels Line numbers and code snippets Risk score AI probability estimate

โ–ถ๏ธ Run code safely (restricted sandbox)

python codesentinel.py run example.py

By default:

Filesystem access is blocked Dangerous modules are disabled Execution is time-limited

๐Ÿšจ Run with full system access (dangerous)

If high-risk issues are detected, CodeSentinel requires explicit confirmation:

Type EXACTLY 'I UNDERSTAND' to run UNSAFE mode:

This prevents accidental execution of malicious code.

๐Ÿ“Š Example Output [HIGH] Line 12: Import of dangerous module 'os'

import os

[HIGH] Line 25: Dangerous call 'os.remove()'

os.remove("important.txt")

๐Ÿงฎ Risk Score: 3/10 ๐Ÿค– AI-generated probability: 0%

๐Ÿ” Security Model

Layer Protection Static Analysis AST inspection Runtime Sandbox Isolated process Built-ins Whitelisted only Imports Blocked by policy Timeout Forced termination Unsafe Mode Manual confirmation

โš ๏ธ Important:

No Python sandbox is perfectly secure. CodeSentinel is designed for risk reduction, not absolute isolation.

๐Ÿง  Use Cases

1)Reviewing AI-generated Python code 2)Executing code from untrusted sources 3)Teaching Python security concepts 4)Secure local code testing 5)Building higher-level AI agents or coding tools

๐Ÿ“„ License

MIT License You are free to use, modify, and distribute this software.

โญ Future Enhancements (Planned)

1.JSON / SARIF report export 2.Custom security policies 3.Memory & CPU usage limits 4.Plugin-based analyzers 5.GUI and editor integrations

โญ Star

If you find CodeSentinel useful don't forget to leave a star behind. Thank You

๐Ÿ‘ค Author

LegedsDaD Independent Developer

About

CodeSentinel is a single-file, production-ready secure Python code analyzer and sandbox executor designed to safely inspect and execute untrusted or AI-generated Python code without risking system integrity.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages