| 371 | |
| 372 | |
| 373 | def do_command(params, command_line): |
| 374 | # Block control characters to prevent newline-injection at the shell boundary. |
| 375 | if isinstance(command_line, str) and any(ch in command_line for ch in _DISALLOWED_COMMAND_CHARS): |
| 376 | logging.error('Invalid command: control characters are not allowed in command input.') |
| 377 | return |
| 378 | |
| 379 | def is_msp(params_local): |
| 380 | if params_local.enterprise: |
| 381 | if 'licenses' in params_local.enterprise: |
| 382 | msp_license = next((x for x in params_local.enterprise['licenses'] if x['lic_status'].startswith('msp')), |
| 383 | None) |
| 384 | if msp_license: |
| 385 | return True |
| 386 | return False |
| 387 | |
| 388 | if command_line.lower() == 'h' or command_line.lower() == 'history': |
| 389 | display.formatted_history(stack) |
| 390 | return |
| 391 | |
| 392 | if command_line.lower().startswith('server'): |
| 393 | _, sp, args = command_line.partition(' ') |
| 394 | server_command(params, args) |
| 395 | return |
| 396 | |
| 397 | if command_line.startswith('ksm'): |
| 398 | # Exclude cwd from PATH on Windows so a local `ksm.bat` can't hijack |
| 399 | # the `ksm` prefix via CreateProcess's implicit cwd search. |
| 400 | search_path = os.environ.get('PATH', os.defpath) |
| 401 | if sys.platform.startswith('win'): |
| 402 | try: |
| 403 | cwd_abs = os.path.abspath(os.getcwd()) |
| 404 | except OSError: |
| 405 | cwd_abs = None |
| 406 | if cwd_abs: |
| 407 | search_path = os.pathsep.join( |
| 408 | p for p in search_path.split(os.pathsep) |
| 409 | if p and os.path.abspath(p) != cwd_abs |
| 410 | ) |
| 411 | |
| 412 | ksm_path = shutil.which('ksm', path=search_path) |
| 413 | if not ksm_path: |
| 414 | logging.error( |
| 415 | 'Please install the ksm application to run ksm commands.\n' |
| 416 | 'See https://docs.keeper.io/secrets-manager/secrets-manager' |
| 417 | '/secrets-manager-command-line-interface' |
| 418 | '#secrets-manager-cli-installation' |
| 419 | ) |
| 420 | return |
| 421 | |
| 422 | try: |
| 423 | ksm_args = shlex.split(command_line, posix=not sys.platform.startswith('win')) |
| 424 | except ValueError as e: |
| 425 | logging.error('Invalid ksm command: %s', e) |
| 426 | return |
| 427 | |
| 428 | if not ksm_args or ksm_args[0].lower() != 'ksm': |
| 429 | logging.error('Invalid ksm command.') |
| 430 | return |