1111import requests
1212import csv
1313import importlib .resources
14+ import shlex
1415from pathlib import Path
1516from typing import Dict , Optional , Tuple , List
1617
@@ -309,24 +310,23 @@ def get_shell_init_file(shell: str) -> str:
309310 return str (shell_files .get (shell , home / '.bashrc' ))
310311
311312def create_api_env_script (keys : Dict [str , str ], shell : str ) -> str :
312- """Create shell-appropriate environment script"""
313+ """Create shell-appropriate environment script with proper escaping """
313314 valid_keys = {k : v for k , v in keys .items () if v }
315+ lines = []
314316
315- if shell == 'fish' :
316- lines = []
317- for key , value in valid_keys .items ():
318- lines .append (f'set -gx { key } "{ value } "' )
319- return '\n ' .join (lines ) + '\n '
320- elif shell in ['csh' , 'tcsh' ]:
321- lines = []
322- for key , value in valid_keys .items ():
323- lines .append (f'setenv { key } "{ value } "' )
324- return '\n ' .join (lines ) + '\n '
325- else : # bash, zsh, ksh, sh and others
326- lines = []
327- for key , value in valid_keys .items ():
328- lines .append (f'export { key } ="{ value } "' )
329- return '\n ' .join (lines ) + '\n '
317+ for key , value in valid_keys .items ():
318+ # shlex.quote is designed for POSIX shells (sh, bash, zsh, ksh)
319+ # It also works reasonably well for fish and csh for simple assignments
320+ quoted_val = shlex .quote (value )
321+
322+ if shell == 'fish' :
323+ lines .append (f'set -gx { key } { quoted_val } ' )
324+ elif shell in ['csh' , 'tcsh' ]:
325+ lines .append (f'setenv { key } { quoted_val } ' )
326+ else : # bash, zsh, ksh, sh and others
327+ lines .append (f'export { key } ={ quoted_val } ' )
328+
329+ return '\n ' .join (lines ) + '\n '
330330
331331def save_configuration (valid_keys : Dict [str , str ]) -> Tuple [List [str ], bool , Optional [str ]]:
332332 """Save configuration to ~/.pdd/ directory"""
@@ -645,4 +645,4 @@ def main():
645645 main ()
646646 except KeyboardInterrupt :
647647 print_colored ("\n \n Setup cancelled." , YELLOW )
648- sys .exit (0 )
648+ sys .exit (0 )
0 commit comments