vishaldhakad commited on
Commit
6372c69
Β·
1 Parent(s): 7257069

frontend adding

Browse files
Files changed (1) hide show
  1. README.md +117 -165
README.md CHANGED
@@ -1,227 +1,179 @@
1
  ---
2
- title: SecureCodeEnv
3
- emoji: πŸ”’
4
- colorFrom: red
5
- colorTo: orange
6
  sdk: docker
7
  pinned: true
8
- license: mit
9
  ---
10
 
11
- # SecureCodeEnv
12
 
13
- **An RL environment for training LLM agents to write production-ready, secure Python code.**
14
 
15
- Built for the **Meta Γ— PyTorch OpenEnv Hackathon 2026** by Vishal Dhakad (`vishaldhakad`).
16
 
17
  ---
18
 
19
  ## The Problem
20
 
21
- Studies show **12–65% of LLM-generated code contains security vulnerabilities** (2025 research). Secure-pass@1 rates remain below 12% for all frontier models even when functional pass@1 exceeds 50%.
22
 
23
  Every existing RL environment trains agents to write code that **WORKS**. None train agents to write code that is **SAFE, CONSISTENT, and PRODUCTION-READY**.
24
 
25
- SecureCodeEnv fills that gap.
26
 
27
  ---
28
 
29
- ## What Makes This Environment Unique
30
-
31
- | Feature | SecureCodeEnv | Other RL Envs |
32
- |---|---|---|
33
- | Dynamic adversarial grading | βœ… Actually FIRES attacks | ❌ Static patterns only |
34
- | CodeGraph memory | βœ… Codebase-consistency rewards | ❌ Single-function only |
35
- | CWE-grounded tasks | βœ… 9 tasks, 12+ CWE IDs | ❌ Generic correctness |
36
- | Multi-dimensional reward | βœ… 7 dimensions | ❌ Pass/fail only |
37
- | Anti-reward-hacking | βœ… Seeded random payloads | ❌ Fixed test cases |
38
-
39
- ### CodeGraph Memory System
40
-
41
- The environment maintains a `CodeGraph` β€” a structured in-memory database of every component the agent has written in the current episode. When the agent writes `auth/validator.py` in `snake_case`, and then submits `auth/middleware.py` in `camelCase`, the consistency grader penalizes the drift. No other RL environment does this.
42
-
43
- ### Dynamic Adversarial Attack Grading
44
-
45
- We don't just scan for vulnerability patterns β€” we **fire real attacks** at the agent's code:
46
- - SQL injection payloads (UNION SELECT, OR 1=1, stacked queries)
47
- - Path traversal payloads (`../../etc/passwd`, URL-encoded variants)
48
- - JWT bypass attacks (`alg: none`, expired tokens, tampered payloads)
49
- - XSS payloads (`<script>`, `onerror=`, template injection)
50
-
51
- Payloads are randomized per episode using a seed. The agent **cannot memorize** specific strings.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  ---
54
 
55
- ## Reward System (7 Dimensions)
56
-
57
- | Dimension | Weight | Tool | What It Measures |
58
- |---|---|---|---|
59
- | Correctness | 30% | Custom test runner | Does the code solve the problem? |
60
- | Attack Resistance | 20% | Dynamic harness | Does it survive real attacks? |
61
- | Static Security | 15% | bandit + AST | Known vulnerability patterns (CWE-mapped) |
62
- | CodeGraph Consistency | 15% | AST + CodeGraph | Matches existing codebase conventions? |
63
- | Performance | 10% | timeit + tracemalloc | Efficient vs naive/optimal baselines |
64
- | Documentation | 5% | AST | Docstrings + type hints coverage |
65
- | Code Structure | 5% | AST | Clean code (no bare print, no bare except) |
66
-
67
- ---
68
-
69
- ## Quick Start
70
 
71
  ```python
72
  import requests
73
 
74
- ENV_URL = "https://vishaldhakad-securecodeenv.hf.space"
75
 
76
- # 1. Start episode
77
- episode = requests.post(f"{ENV_URL}/reset", json={"difficulty": "medium"}).json()
78
  sid = episode["session_id"]
79
- print(episode["problem_statement"])
80
 
81
- # 2. Submit code
82
- result = requests.post(f"{ENV_URL}/step", json={
83
  "session_id": sid,
84
- "code": "def build_user_query(username, role):\n return ('SELECT * FROM users WHERE username = %s', (username,))",
85
  "filename": "solution.py",
 
86
  }).json()
87
 
88
- print(f"Reward: {result['total_reward']:.3f}")
89
- print(f"Scores: {result['scores']}")
90
- print(f"Feedback: {result['feedback']['summary']}")
91
  ```
92
 
93
- ---
94
-
95
- ## Tasks β€” 9 Tasks Across 3 Difficulty Levels
96
-
97
- ### Easy
98
- | Task | CWE Targets | Attack |
99
- |---|---|---|
100
- | Password Validator | CWE-916, CWE-521 | Weak hash detection |
101
- | Input Sanitizer | CWE-20, CWE-116 | XSS payload injection |
102
- | Token Generator | CWE-338, CWE-330 | Predictable randomness |
103
-
104
- ### Medium
105
- | Task | CWE Targets | Attack |
106
- |---|---|---|
107
- | SQL Query Builder | CWE-89 | SQL injection payloads |
108
- | File Path Handler | CWE-22 | Path traversal attacks |
109
- | Rate Limiter | CWE-770, CWE-400 | Concurrent request flood |
110
-
111
- ### Hard
112
- | Task | CWE Targets | Attack |
113
- |---|---|---|
114
- | File Upload Handler | CWE-22, CWE-434 | Traversal filenames + MIME spoofing |
115
- | JWT Validator | CWE-347, CWE-613 | `alg:none` attack, expired tokens |
116
- | Auth Middleware | CWE-287, CWE-352 | CSRF bypass, timing attacks |
117
 
118
  ---
119
 
120
- ## API Reference
121
-
122
- ### `POST /reset`
123
- Start a new episode.
124
-
125
- **Request:**
126
- ```json
127
- { "difficulty": "medium" }
128
- ```
129
-
130
- **Response:**
131
- ```json
132
- {
133
- "session_id": "uuid",
134
- "task_id": "medium_sql_query_builder",
135
- "problem_statement": "Write a Python function...",
136
- "difficulty": "medium",
137
- "cwe_targets": ["CWE-89", "CWE-20"],
138
- "codegraph": { "components": {}, "conventions": {} },
139
- "starter_code": "def build_user_query(...):"
140
- }
141
- ```
142
-
143
- ### `POST /step`
144
- Submit agent code for grading.
145
 
146
- **Request:**
147
  ```json
148
  {
149
- "session_id": "uuid",
150
- "code": "def build_user_query(username: str, role: str) -> tuple: ...",
151
- "filename": "src/db/queries.py"
152
- }
153
- ```
154
-
155
- **Response:**
156
- ```json
157
- {
158
- "total_reward": 0.847,
159
  "scores": {
160
  "correctness": 1.0,
161
  "attack_resist": 0.875,
162
- "static_security": 0.9,
163
  "consistency": 1.0,
164
- "performance": 0.72,
165
- "documentation": 0.75,
166
- "code_structure": 0.8
 
 
 
 
 
167
  },
168
- "feedback": { "summary": "🟑 Good submission β€” improve: performance" },
169
- "codegraph": { ... },
170
  "done": false,
171
- "step_count": 1
172
  }
173
  ```
174
 
175
- ### `GET /state?session_id=<id>`
176
- Get current episode state without advancing.
177
-
178
- ### `GET /health`
179
- Returns `{"status": "ok", "env": "SecureCodeEnv", "version": "2.0.0", "tasks_loaded": 9}`
180
-
181
  ---
182
 
183
- ## Setup (Local)
184
-
185
- ```bash
186
- git clone https://huggingface.co/spaces/vishaldhakad/SecureCodeEnv
187
- cd SecureCodeEnv
188
-
189
- # Docker (recommended)
190
- docker build -t secure-code-env .
191
- docker run -p 7860:7860 secure-code-env
192
-
193
- # Or direct
194
- pip install -r requirements.txt
195
- uvicorn app.main:app --host 0.0.0.0 --port 7860
196
- ```
197
-
198
- ## Run Baseline Inference
199
 
200
  ```bash
201
- export API_BASE_URL=https://api.openai.com/v1
202
- export MODEL_NAME=gpt-4o-mini
203
- export HF_TOKEN=hf_your_token
204
- export ENV_URL=http://localhost:7860
 
 
 
 
 
205
  python inference.py
206
- ```
207
 
208
- ## Validate Before Submit
209
-
210
- ```bash
211
- python validate.py --url http://localhost:7860
212
  ```
213
 
214
- ---
215
-
216
  ## Environment Variables
217
-
218
  | Variable | Required | Description |
219
- |---|---|---|
220
- | `API_BASE_URL` | Yes | LLM API endpoint (OpenAI-compatible) |
221
- | `MODEL_NAME` | Yes | Model identifier (e.g. `gpt-4o-mini`) |
222
- | `HF_TOKEN` | Yes | HuggingFace token |
223
- | `ENV_URL` | No | Override environment URL (default: localhost:7860) |
 
 
 
 
 
 
 
 
 
 
 
 
224
 
225
  ---
226
 
227
- *SecureCodeEnv v2.0 Β· Meta Γ— PyTorch OpenEnv Hackathon 2026 Β· Vishal Dhakad*
 
1
  ---
2
+ title: Trainx
3
+ emoji: πŸ”
4
+ colorFrom: blue
5
+ colorTo: red
6
  sdk: docker
7
  pinned: true
8
+ license: apache-2.0
9
  ---
10
 
11
+ # πŸ” SecureCodeEnv V2
12
 
13
+ **RL environment for training LLM agents to write production-ready, secure Python code.**
14
 
15
+ Built for the **Meta Γ— HuggingFace OpenEnv Hackathon 2026** by [Vishal Dhakad](https://huggingface.co/vishaldhakad).
16
 
17
  ---
18
 
19
  ## The Problem
20
 
21
+ Studies show **12–65% of LLM-generated code contains security vulnerabilities** depending on the model (2025 studies). Secure-pass@1 rates remain below 12% for all frontier models even when functional pass@1 exceeds 50%.
22
 
23
  Every existing RL environment trains agents to write code that **WORKS**. None train agents to write code that is **SAFE, CONSISTENT, and PRODUCTION-READY**.
24
 
25
+ SecureCodeEnv fills that exact gap.
26
 
27
  ---
28
 
29
+ ## What Makes This Unique
30
+
31
+ ### 1. Behavioral Adversarial Attack Grading (Unfakeable)
32
+ We don't just scan for patterns β€” we **fire real attacks** at the agent's code and monitor side effects:
33
+ - **SQL injection** β†’ spy on `sqlite3.Cursor.execute` at C-extension level
34
+ - **Path traversal** β†’ hook `builtins.open` via `sys.settrace`
35
+ - **Shell injection** β†’ replace `subprocess.run` + `os.system` before agent code loads
36
+ - **JWT bypass** β†’ check if alg:none tokens are accepted
37
+
38
+ V1 checked return values (`if '..' not in result`). An agent could return a clean string while actually opening `../../etc/passwd`. **V2 checks what the code DOES, not what it returns.**
39
+
40
+ ### 2. CodeGraph Memory System (Novel in RL)
41
+ The agent receives a structured snapshot of everything it has already written this episode. The grader checks cross-file consistency:
42
+ - Naming convention (snake_case vs camelCase) β€” 60% threshold, "mixed" state
43
+ - Error handling style (try/except vs returns)
44
+ - Import reuse (reuse existing modules, don't rewrite)
45
+
46
+ **No other RL environment penalises style drift across files.**
47
+
48
+ ### 3. 9 CWE-Grounded Tasks
49
+ | # | Task | Difficulty | CWE | Primary Attack |
50
+ |---|------|-----------|-----|----------------|
51
+ | 1 | `password_validator` | Easy | CWE-916 | Weak hash acceptance |
52
+ | 2 | `input_sanitizer` | Easy | CWE-20 | XSS payload pass-through |
53
+ | 3 | `hash_generator` | Easy | CWE-327 | Shell invocation for hashing |
54
+ | 4 | `sql_query_builder` | Medium | CWE-89 | SQL injection via cursor spy |
55
+ | 5 | `file_path_handler` | Medium | CWE-22 | Path traversal via open() spy |
56
+ | 6 | `api_rate_limiter` | Medium | CWE-307 | Rate bypass with spoofed client ID |
57
+ | 7 | `file_upload_handler` | Hard | CWE-434 | Malicious file extension upload |
58
+ | 8 | `jwt_validator` | Hard | CWE-347 | JWT alg:none bypass |
59
+ | 9 | `auth_middleware` | Hard | CWE-287 | Shell-based auth + timing attack |
60
+
61
+ ### 4. 8-Dimensional Reward System
62
+ | Grader | Weight | Tool | Type |
63
+ |--------|--------|------|------|
64
+ | Correctness | 25% | Custom test runner | Functional |
65
+ | Attack Resistance | 25% | Behavioral harness V2 | Security β€” unfakeable |
66
+ | Static Security | 15% | bandit + semgrep | Security β€” static |
67
+ | CodeGraph Consistency | 15% | tree-sitter + CodeGraph | Architectural |
68
+ | Performance | 10% | timeit + tracemalloc | Efficiency |
69
+ | Documentation | 5% | ast | Quality |
70
+ | Code Structure | 3% | ast | Quality |
71
+ | Supply Chain | 2% | pip-audit + typosquat | Security |
72
 
73
  ---
74
 
75
+ ## API
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  ```python
78
  import requests
79
 
80
+ BASE = "https://vishaldhakad-securecodeenv.hf.space"
81
 
82
+ # Start episode
83
+ episode = requests.post(f"{BASE}/reset", json={"difficulty": "medium"}).json()
84
  sid = episode["session_id"]
 
85
 
86
+ # Submit code
87
+ result = requests.post(f"{BASE}/step", json={
88
  "session_id": sid,
89
+ "task_id": episode["task_id"],
90
  "filename": "solution.py",
91
+ "code": your_secure_code,
92
  }).json()
93
 
94
+ print(result["total_reward"]) # 0.0 – 1.0
95
+ print(result["feedback"]) # per-grader feedback
96
+ print(result["codegraph"]) # updated codebase context
97
  ```
98
 
99
+ ### Endpoints
100
+ | Endpoint | Method | Description |
101
+ |----------|--------|-------------|
102
+ | `/reset` | POST | Start new episode β€” returns task, CodeGraph, session_id |
103
+ | `/step` | POST | Submit code β€” returns reward, feedback, updated CodeGraph |
104
+ | `/state` | GET | Read current episode state |
105
+ | `/health` | GET | Health check |
106
+ | `/docs` | GET | Interactive Swagger UI |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
  ---
109
 
110
+ ## Action Space
111
+ Python source code string (max 50KB). Filename used for CodeGraph tracking.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
+ ## Observation Space
114
  ```json
115
  {
116
+ "total_reward": 0.84,
 
 
 
 
 
 
 
 
 
117
  "scores": {
118
  "correctness": 1.0,
119
  "attack_resist": 0.875,
120
+ "static_security": 0.7,
121
  "consistency": 1.0,
122
+ "performance": 0.8,
123
+ "documentation": 0.5,
124
+ "code_structure": 1.0,
125
+ "supply_chain": 1.0
126
+ },
127
+ "feedback": {
128
+ "correctness": "βœ… Excellent (1.00) β€” 8/8 tests passed.",
129
+ "attack_resist": "🟑 Good (0.88) β€” 7/8 attacks blocked."
130
  },
131
+ "codegraph": { "conventions": {}, "components": {} },
 
132
  "done": false,
133
+ "step_count": 2
134
  }
135
  ```
136
 
 
 
 
 
 
 
137
  ---
138
 
139
+ ## Quick Start
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
 
141
  ```bash
142
+ # Local dev
143
+ docker build -t securecodeenv .
144
+ docker run -p 7860:7860 -e REDIS_URL=<upstash_url> securecodeenv
145
+
146
+ # Run baseline inference
147
+ API_BASE_URL=https://api.groq.com/openai/v1 \
148
+ MODEL_NAME=llama-3.3-70b-versatile \
149
+ HF_TOKEN=<your_token> \
150
+ ENV_URL=http://localhost:7860 \
151
  python inference.py
 
152
 
153
+ # Pre-submission validation
154
+ python validate.py
 
 
155
  ```
156
 
 
 
157
  ## Environment Variables
 
158
  | Variable | Required | Description |
159
+ |----------|----------|-------------|
160
+ | `REDIS_URL` | Yes | Upstash Redis URL (`rediss://default:<token>@<host>.upstash.io:6379`) |
161
+ | `API_BASE_URL` | For inference | LLM API base URL |
162
+ | `MODEL_NAME` | For inference | Model name |
163
+ | `HF_TOKEN` | For inference | HuggingFace token |
164
+
165
+ ---
166
+
167
+ ## Infrastructure (100% Free)
168
+ | Component | Solution | Cost |
169
+ |-----------|----------|------|
170
+ | Compute | HuggingFace Spaces CPU (2 vCPU / 16GB) | βœ… $0 |
171
+ | Containerisation | Docker | βœ… $0 |
172
+ | Session persistence | Upstash Redis free tier | βœ… $0 |
173
+ | Static analysis | bandit + semgrep | βœ… $0 |
174
+ | Multi-language parsing | tree-sitter | βœ… $0 |
175
+ | LLM for inference | Groq free tier | βœ… $0 |
176
 
177
  ---
178
 
179
+ *SecureCodeEnv V2 β€” Built by Vishal Dhakad | Meta Γ— HuggingFace OpenEnv Hackathon 2026 | Total infrastructure cost: $0.00*