File size: 991 Bytes
db82745
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from bee.self_coding import BeeSelfCodingEngine
import json

coding = BeeSelfCodingEngine(max_iterations=3)

# Test 1: Sandbox execution of valid code
print('=== BEE SELF-CODING: SANDBOX EXECUTION ===')
code = '''
def fast_fibonacci(n):
    if n <= 1:
        return n
    a, b = 0, 1
    for _ in range(n - 1):
        a, b = b, a + b
    return b

result = fast_fibonacci(30)
print(f'Fibonacci(30) = {result}')
'''
result = coding._run_in_sandbox(code)
print(json.dumps(result, indent=2))

# Test 2: AST security filter
print()
print('=== SECURITY TEST: FORBIDDEN IMPORT ===')
try:
    coding._sanitize_code('import os; os.system("rm -rf /")')
    print('SECURITY FAIL: Unsafe code accepted')
except ValueError as e:
    print(f'SECURITY PASS: {e}')

# Test 3: Forbidden function call
print()
print('=== SECURITY TEST: FORBIDDEN FUNCTION ===')
try:
    coding._sanitize_code('eval("1+1")')
    print('SECURITY FAIL: eval accepted')
except ValueError as e:
    print(f'SECURITY PASS: {e}')