Skip to content

Commit 867e3ad

Browse files
committed
conflicts fixed
conflicts fixed
1 parent 1728e36 commit 867e3ad

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

h origin main --dry-run

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1728e368 (HEAD -> main) Add Python Compiler and Enhanced Quiz Integration
2+
b1d2f663 Update xss_attempts.log
3+
f67b95b4 cyber challenges page update by krishna chaudhari
4+
d9f42dfd Merge pull request #345 from M0Nabel13/main
5+
530b046e Updated codeql.yml

home/views.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,6 +2323,13 @@ def execute_code(request):
23232323
def execute_python_code(code, input_data, settings):
23242324
"""
23252325
Execute Python code in a secure environment
2326+
2327+
Security measures implemented:
2328+
- Sandboxed execution with restricted built-ins
2329+
- Malicious pattern detection
2330+
- Execution timeouts
2331+
- Error message sanitization
2332+
- Input validation and length limits
23262333
"""
23272334
result = {'output': '', 'error': None, 'execution_time': 0, 'memory_used': 0}
23282335

@@ -2401,7 +2408,18 @@ def timeout_handler():
24012408
error_output = io.StringIO()
24022409

24032410
with redirect_stdout(output), redirect_stderr(error_output):
2404-
exec(code, safe_globals)
2411+
# Additional security: compile first to catch syntax errors
2412+
try:
2413+
compiled_code = compile(code, '<user_code>', 'exec')
2414+
exec(compiled_code, safe_globals)
2415+
except SyntaxError as e:
2416+
result['error'] = f'Syntax Error: {str(e)}'
2417+
return result
2418+
except Exception as e:
2419+
# Sanitize error messages to prevent information leakage
2420+
error_type = type(e).__name__
2421+
result['error'] = f'{error_type}: {str(e)[:200]}' # Limit error message length
2422+
return result
24052423

24062424
execution_time = time.time() - start_time
24072425
timer.cancel()
@@ -2412,17 +2430,20 @@ def timeout_handler():
24122430
# Check for errors in stderr
24132431
error_msg = error_output.getvalue()
24142432
if error_msg:
2415-
result['error'] = error_msg
2433+
# Sanitize stderr output
2434+
result['error'] = error_msg[:200] # Limit error message length
24162435

24172436
except TimeoutError:
24182437
timer.cancel()
24192438
result['error'] = f'Code execution timed out (max {settings.max_execution_time} seconds)'
24202439
except Exception as e:
24212440
timer.cancel()
2422-
result['error'] = str(e)
2441+
# Sanitize exception messages to prevent information leakage
2442+
result['error'] = f'Execution error: {type(e).__name__}'
24232443

24242444
except Exception as e:
2425-
result['error'] = f'Execution error: {str(e)}'
2445+
# Sanitize outer exception messages
2446+
result['error'] = f'System error: {type(e).__name__}'
24262447

24272448
return result
24282449

0 commit comments

Comments
 (0)