Skip to content

Commit 8a51184

Browse files
feat: Fix shell injection vulnerability in fix_env.py (#112)
Security fix: Properly escape shell metacharacters (backslashes and double quotes) in fix_env.py to prevent command injection when .env file is sourced by a shell.
1 parent 2223872 commit 8a51184

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

fix_env.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ def clean_val(val):
1717
val = re.sub(r'^[\"\u201c\u201d\']|[\"\u201c\u201d\']$', '', val)
1818
return val
1919

20+
# Helper to escape value for shell
21+
def escape_val(val):
22+
if not val: return ""
23+
# Escape backslashes first, then double quotes
24+
return val.replace('\\', '\\\\').replace('"', '\\"')
25+
2026
lines = content.splitlines()
2127
parsed = {}
2228

@@ -50,7 +56,7 @@ def clean_val(val):
5056
if not real_profiles: real_profiles = profile_val
5157

5258
# Write back with standard quotes
53-
new_content = f'TOKEN="{real_token}"\nPROFILE="{real_profiles}"\n'
59+
new_content = f'TOKEN="{escape_val(real_token)}"\nPROFILE="{escape_val(real_profiles)}"\n'
5460

5561
with open('.env', 'w') as f:
5662
f.write(new_content)

0 commit comments

Comments
 (0)