import subprocess
import os
from flask import request
@app.route('/run_with_env')
def run_with_env():
# Vulnerable: User input directly in environment
user_var = request.args.get('env_var')
env = os.environ.copy()
env['USER_INPUT'] = user_var # Tainted environment variable
result = subprocess.run(
['printenv', 'USER_INPUT'],
env=env,
capture_output=True,
text=True
)
return result.stdout
@app.route('/execute_script')
def execute_script():
# Vulnerable: Tainted arguments from user
script_args = request.args.getlist('args')
cmd = ['python', 'script.py'] + script_args # Unsafe concatenation
result = subprocess.run(cmd, capture_output=True, text=True)
return result.stdout
@app.route('/custom_path')
def custom_path():
# Vulnerable: User controls PATH environment
custom_path = request.args.get('path')
env = {'PATH': custom_path} # Dangerous PATH manipulation
result = subprocess.run(
['ls'],
env=env,
capture_output=True,
text=True
)
return result.stdout
import subprocess
import re
from flask import request
# Allowlists for safe values
ALLOWED_ENV_VARS = ['LANG', 'LC_ALL', 'TZ']
ALLOWED_ARG_PATTERN = re.compile(r'^[a-zA-Z0-9_\-\.]+$')
@app.route('/run_with_env')
def run_with_env():
# Secure: Validate and sanitize environment variables
user_var = request.args.get('env_var', '')
# Validate environment variable value
if not user_var or not re.match(r'^[a-zA-Z0-9_\-\.]+$', user_var):
return 'Invalid environment variable value', 400
# Use explicit safe environment
safe_env = {
'PATH': '/usr/bin:/bin', # Fixed safe PATH
'USER_INPUT': user_var[:50] # Limit length
}
result = subprocess.run(
['printenv', 'USER_INPUT'],
env=safe_env,
capture_output=True,
text=True,
timeout=5
)
return result.stdout
@app.route('/execute_script')
def execute_script():
# Secure: Validate all arguments
script_args = request.args.getlist('args')
# Validate each argument
validated_args = []
for arg in script_args[:5]: # Limit number of args
if ALLOWED_ARG_PATTERN.match(arg) and len(arg) <= 50:
validated_args.append(arg)
else:
return f'Invalid argument: {arg}', 400
# Safe command construction
cmd = ['python3', '/opt/scripts/safe_script.py'] + validated_args
# Safe environment
safe_env = {
'PATH': '/usr/bin:/bin',
'PYTHONPATH': '/opt/scripts'
}
try:
result = subprocess.run(
cmd,
env=safe_env,
capture_output=True,
text=True,
timeout=30
)
return result.stdout
except subprocess.TimeoutExpired:
return 'Script execution timeout', 408
@app.route('/custom_path')
def custom_path():
# Secure: Use fixed safe environment
# Don't allow user to control PATH
safe_env = {
'PATH': '/usr/bin:/bin:/usr/local/bin',
'HOME': '/tmp',
'USER': 'www-data'
}
result = subprocess.run(
['ls', '/tmp'], # Fixed safe directory
env=safe_env,
capture_output=True,
text=True,
timeout=5
)
return result.stdout
# Secure helper function for environment validation
def create_safe_environment(base_vars=None):
"""Create a safe environment with only allowed variables."""
safe_env = {
'PATH': '/usr/bin:/bin',
'HOME': '/tmp',
'LANG': 'en_US.UTF-8'
}
if base_vars:
for key, value in base_vars.items():
if key in ALLOWED_ENV_VARS and isinstance(value, str):
# Sanitize value
clean_value = re.sub(r'[^a-zA-Z0-9_\-\./:]', '', value)
safe_env[key] = clean_value[:100] # Limit length
return safe_env