-
Notifications
You must be signed in to change notification settings - Fork 0
🔒 [security fix] Inject NEXTAUTH_SECRET, SALT, and ENCRYPTION_KEY via env to avoid hardcoding #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
62a7b84
cba1ef6
d886bf6
edb07c6
e65171c
d529159
f646b95
9299998
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,7 @@ | ||
| import subprocess | ||
| import shlex | ||
|
|
||
| def run_cmd(cmd): | ||
| # Fix the issues from Sourcery review! | ||
| # 1. Provide a static list of strings for args rather than a single string. | ||
| # 2. Use shell=False | ||
| args = shlex.split(cmd) | ||
| result = subprocess.run(args, shell=False, capture_output=True, text=True) | ||
| def run_cmd(cmd_list): | ||
| # Expect a list of strings directly instead of a single string to avoid command injection | ||
| # Enable check=True to surface non-zero exit codes as subprocess.CalledProcessError | ||
| result = subprocess.run(cmd_list, shell=False, capture_output=True, text=True, check=True) | ||
| return result.stdout.strip() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,21 +17,36 @@ mkdir -p valkey-data postgres-data langfuse-data clickhouse-data redis-lf-data m | |
|
|
||
| ENV_FILE="${WORKDIR}/.env" | ||
|
|
||
| # Ensure the env file exists and has secure permissions (owner read/write only) | ||
| if [ ! -f "$ENV_FILE" ]; then | ||
| touch "$ENV_FILE" | ||
| chmod 600 "$ENV_FILE" | ||
| fi | ||
|
|
||
| # 1. Load or prompt for OpenRouter API Key | ||
| if [ -f "$ENV_FILE" ]; then | ||
| set -a | ||
| source "$ENV_FILE" | ||
| set +a | ||
| fi | ||
|
|
||
| # Ensure openssl is installed if we need to generate passwords/keys | ||
| if [ -z "$POSTGRES_PASSWORD" ] || [ -z "$NEXTAUTH_SECRET" ] || [ -z "$SALT" ] || [ -z "$ENCRYPTION_KEY" ] || [ -z "$LITELLM_MASTER_KEY" ]; then | ||
| if ! command -v openssl &>/dev/null; then | ||
| echo "❌ Error: 'openssl' is required to generate secure random keys but was not found in PATH." | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
|
|
||
| if [ -z "$OPENROUTER_API_KEY" ]; then | ||
| if [ -t 0 ]; then | ||
| echo "🔑 OpenRouter API Key not found." | ||
| echo -n "Please enter your OpenRouter API Key (input will be hidden): " | ||
| read -rs OPENROUTER_API_KEY | ||
| echo "" | ||
| echo "OPENROUTER_API_KEY=\"$OPENROUTER_API_KEY\"" > "$ENV_FILE" | ||
| chmod 644 "$ENV_FILE" | ||
| chmod 600 "$ENV_FILE" | ||
| echo "✓ API key saved securely to $ENV_FILE" | ||
| else | ||
| echo "❌ Error: OPENROUTER_API_KEY is not set in your environment or in $ENV_FILE" | ||
|
|
@@ -86,6 +101,35 @@ else | |
| echo "⚠️ Warning: Host agy daemon not responding on port 5005" | ||
| fi | ||
|
|
||
| if [ -z "$NEXTAUTH_SECRET" ] || [ -z "$SALT" ] || [ -z "$ENCRYPTION_KEY" ] || [ -z "$LITELLM_MASTER_KEY" ]; then | ||
| if ! command -v openssl &>/dev/null; then | ||
| echo "❌ Error: 'openssl' is required to generate secure random keys but was not found in PATH." | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
| # Ensure the env file exists and has secure permissions (owner read/write only) | ||
| touch "$ENV_FILE" | ||
| chmod 600 "$ENV_FILE" | ||
|
|
||
| if [ -z "$NEXTAUTH_SECRET" ]; then | ||
| NEXTAUTH_SECRET="$(openssl rand -base64 32)" | ||
| echo "NEXTAUTH_SECRET=\"$NEXTAUTH_SECRET\"" >> "$ENV_FILE" | ||
| echo "✓ Generated new NEXTAUTH_SECRET and saved to $ENV_FILE" | ||
| fi | ||
|
|
||
| if [ -z "$SALT" ]; then | ||
| SALT="$(openssl rand -hex 16)" | ||
| echo "SALT=\"$SALT\"" >> "$ENV_FILE" | ||
| echo "✓ Generated new SALT and saved to $ENV_FILE" | ||
| fi | ||
|
|
||
| if [ -z "$ENCRYPTION_KEY" ]; then | ||
| ENCRYPTION_KEY="$(openssl rand -hex 32)" | ||
| echo "ENCRYPTION_KEY=\"$ENCRYPTION_KEY\"" >> "$ENV_FILE" | ||
| echo "✓ Generated new ENCRYPTION_KEY and saved to $ENV_FILE" | ||
| fi | ||
|
Comment on lines
+115
to
+131
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Security & Robustness Improvements
if [ -z "$NEXTAUTH_SECRET" ] || [ -z "$SALT" ] || [ -z "$ENCRYPTION_KEY" ]; then
if ! command -v openssl &>/dev/null; then
echo "❌ Error: 'openssl' is required to generate secure random keys but was not found in PATH."
exit 1
fi
fi
# Ensure the env file exists and has secure permissions (owner read/write only)
touch "$ENV_FILE"
chmod 600 "$ENV_FILE"
if [ -z "$NEXTAUTH_SECRET" ]; then
NEXTAUTH_SECRET="$(openssl rand -base64 32)"
echo "NEXTAUTH_SECRET=\"$NEXTAUTH_SECRET\"" >> "$ENV_FILE"
echo "✓ Generated new NEXTAUTH_SECRET and saved to $ENV_FILE"
fi
if [ -z "$SALT" ]; then
SALT="$(openssl rand -hex 16)"
echo "SALT=\"$SALT\"" >> "$ENV_FILE"
echo "✓ Generated new SALT and saved to $ENV_FILE"
fi
if [ -z "$ENCRYPTION_KEY" ]; then
ENCRYPTION_KEY="$(openssl rand -hex 32)"
echo "ENCRYPTION_KEY=\"$ENCRYPTION_KEY\"" >> "$ENV_FILE"
echo "✓ Generated new ENCRYPTION_KEY and saved to $ENV_FILE"
fi
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent points! I've added a check for the |
||
|
|
||
| if [ -z "$LITELLM_MASTER_KEY" ]; then | ||
| LITELLM_MASTER_KEY="sk-litellm-$(openssl rand -hex 16)" | ||
| echo "LITELLM_MASTER_KEY=\"$LITELLM_MASTER_KEY\"" >> "$ENV_FILE" | ||
|
|
@@ -305,9 +349,9 @@ if podman pod exists agent-router-pod 2>/dev/null; then | |
| fi | ||
|
|
||
| render_pod_yaml() { | ||
| export WORKDIR HOME LITELLM_MASTER_KEY POSTGRES_PASSWORD | ||
| export WORKDIR HOME LITELLM_MASTER_KEY POSTGRES_PASSWORD NEXTAUTH_SECRET SALT ENCRYPTION_KEY | ||
| python3 - "$WORKDIR/pod.yaml" <<'PY' | ||
| import os, sys | ||
| import os, sys, urllib.parse | ||
| uid = os.getuid() | ||
| with open(sys.argv[1], "r", encoding="utf-8") as f: | ||
| text = f.read() | ||
|
|
@@ -317,6 +361,9 @@ placeholders = [ | |
| "/run/user/1000", | ||
| "sk-lit...33bf", | ||
| "postgres:***", | ||
| "NEXTAUTH_SECRET_PLACEHOLDER", | ||
| "SALT_PLACEHOLDER", | ||
| "ENCRYPTION_KEY_PLACEHOLDER", | ||
| "postgres-password-***" | ||
| ] | ||
| for ph in placeholders: | ||
|
|
@@ -327,8 +374,13 @@ text = text.replace("/home/gpav/Vrac/LAB/AI/LLM-Routing", os.environ["WORKDIR"]) | |
| text = text.replace("/home/gpav/", os.environ["HOME"] + "/") | ||
| text = text.replace("/run/user/1000", f"/run/user/{uid}") | ||
| text = text.replace("sk-lit...33bf", os.environ["LITELLM_MASTER_KEY"]) | ||
| text = text.replace("postgres:***", f"postgres:{os.environ['POSTGRES_PASSWORD']}") | ||
| # URL-encode the postgres password for DSN insertion | ||
| encoded_password = urllib.parse.quote_plus(os.environ['POSTGRES_PASSWORD']) | ||
| text = text.replace("postgres:***", f"postgres:{encoded_password}") | ||
| text = text.replace("postgres-password-***", os.environ["POSTGRES_PASSWORD"]) | ||
| text = text.replace("NEXTAUTH_SECRET_PLACEHOLDER", os.environ["NEXTAUTH_SECRET"]) | ||
| text = text.replace("SALT_PLACEHOLDER", os.environ["SALT"]) | ||
| text = text.replace("ENCRYPTION_KEY_PLACEHOLDER", os.environ["ENCRYPTION_KEY"]) | ||
| sys.stdout.write(text) | ||
| PY | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.