diff --git a/programs/keeper-client/KeeperClient.cpp b/programs/keeper-client/KeeperClient.cpp index 3a55a1e6d2b72..57e8d506d2b3f 100644 --- a/programs/keeper-client/KeeperClient.cpp +++ b/programs/keeper-client/KeeperClient.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -752,6 +753,7 @@ int mainEntryClickHouseKeeperClient(int argc, char ** argv) { DB::KeeperClient client; client.init(argc, argv); + clearPasswordFromCommandLine(argc, argv); return client.run(); } catch (DB::Exception & e) diff --git a/src/Common/clearPasswordFromCommandLine.cpp b/src/Common/clearPasswordFromCommandLine.cpp index 930ce523ce98e..9695279d39f22 100644 --- a/src/Common/clearPasswordFromCommandLine.cpp +++ b/src/Common/clearPasswordFromCommandLine.cpp @@ -6,16 +6,30 @@ using namespace std::literals; void clearPasswordFromCommandLine(int argc, char ** argv) { - for (int arg = 1; arg < argc; ++arg) + /// Options of the programs calling this function whose values are credentials. + static constexpr std::string_view secret_options[] + = {"--password"sv, "--jwt"sv, "--ssh-key-passphrase"sv, "--one-time-password"sv, "--oauth-client-secret"sv, "--identity"sv}; + + /// Backwards, so that an option is recognized before a preceding `--password` without a value zeroes it. + for (int arg = argc - 1; arg >= 1; --arg) { - if (arg + 1 < argc && argv[arg] == "--password"sv) - { - ++arg; - memset(argv[arg], 0, strlen(argv[arg])); - } - else if (0 == strncmp(argv[arg], "--password=", strlen("--password="))) + const std::string_view argument = argv[arg]; + for (const auto option : secret_options) { - memset(argv[arg] + strlen("--password="), 0, strlen(argv[arg]) - strlen("--password=")); + if (argument == option) + { + /// clickhouse-client also accepts `--option = value`. + const int value = (arg + 2 < argc && argv[arg + 1] == "="sv) ? arg + 2 : arg + 1; + if (value < argc) + memset(argv[value], 0, strlen(argv[value])); + break; + } + if (argument.size() > option.size() && argument.starts_with(option) + && (argument[option.size()] == '=' || argument[option.size()] == ':')) + { + memset(argv[arg] + option.size() + 1, 0, argument.size() - option.size() - 1); + break; + } } } } diff --git a/src/Common/clearPasswordFromCommandLine.h b/src/Common/clearPasswordFromCommandLine.h index cf90fea1dc819..10271d887362c 100644 --- a/src/Common/clearPasswordFromCommandLine.h +++ b/src/Common/clearPasswordFromCommandLine.h @@ -1,6 +1,6 @@ #pragma once -/** If there are --password=... or --password ... arguments in command line, replace their values with zero bytes. - * This is needed to prevent password exposure in 'ps' and similar tools. +/** Replace with zero bytes the values of the options carrying credentials (`--password`, `--jwt`, ...), given as + * `--option value`, `--option=value`, `--option = value` or `--option:value`, to prevent their exposure in `ps` and similar tools. */ void clearPasswordFromCommandLine(int argc, char ** argv); diff --git a/tests/queries/0_stateless/05813_clear_credentials_from_command_line.reference b/tests/queries/0_stateless/05813_clear_credentials_from_command_line.reference new file mode 100644 index 0000000000000..938b436dbf816 --- /dev/null +++ b/tests/queries/0_stateless/05813_clear_credentials_from_command_line.reference @@ -0,0 +1,23 @@ +client, separate values +--jwt +--password +--ssh-key-passphrase +--one-time-password +client, values after = +--jwt= +--password= +--ssh-key-passphrase= +--one-time-password= +client, values after spaced = +--jwt +--password +--ssh-key-passphrase +--one-time-password +client, --password without a value +--password +keeper-client +--password +--identity= +keeper-client, values after : +--password: +--identity: diff --git a/tests/queries/0_stateless/05813_clear_credentials_from_command_line.sh b/tests/queries/0_stateless/05813_clear_credentials_from_command_line.sh new file mode 100755 index 0000000000000..1458f3b3eefc5 --- /dev/null +++ b/tests/queries/0_stateless/05813_clear_credentials_from_command_line.sh @@ -0,0 +1,88 @@ +#!/usr/bin/env bash +# Tags: no-fasttest +# no-fasttest: `--jwt` needs a build with jwt-cpp and SSL. + +CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=../shell_config.sh +. "$CUR_DIR"/../shell_config.sh + +# The values of the options carrying credentials must not be visible in `ps` and `/proc//cmdline`. +# The programs connect to a listener that never answers, so they stay alive after parsing their options. + +LISTENER_SCRIPT="${CLICKHOUSE_TMP}/${CLICKHOUSE_TEST_UNIQUE_NAME}_listener.py" +PORT_FILE="${CLICKHOUSE_TMP}/${CLICKHOUSE_TEST_UNIQUE_NAME}.port" +ACCEPTED_FILE="${CLICKHOUSE_TMP}/${CLICKHOUSE_TEST_UNIQUE_NAME}.accepted" +rm -f "$PORT_FILE" +: > "$ACCEPTED_FILE" + +cat > "$LISTENER_SCRIPT" <<'EOF' +import os +import socket +import sys + +listener = socket.socket() +listener.bind(("127.0.0.1", 0)) +listener.listen(16) +with open(sys.argv[1] + ".tmp", "w") as f: + f.write(str(listener.getsockname()[1])) +os.rename(sys.argv[1] + ".tmp", sys.argv[1]) + +connections = [] +while True: + connection, _ = listener.accept() + connections.append(connection) + with open(sys.argv[2], "a") as f: + f.write("accepted\n") +EOF + +python3 "$LISTENER_SCRIPT" "$PORT_FILE" "$ACCEPTED_FILE" & +LISTENER_PID=$! +trap 'kill $LISTENER_PID 2>/dev/null; wait $LISTENER_PID 2>/dev/null' EXIT + +while [ ! -s "$PORT_FILE" ] && kill -0 "$LISTENER_PID" 2>/dev/null; do sleep 0.1; done +PORT=$(cat "$PORT_FILE") + +if [ -x "${CLICKHOUSE_BINARY}-keeper-client" ]; then + KEEPER_CLIENT="${CLICKHOUSE_BINARY}-keeper-client" +else + KEEPER_CLIENT="${CLICKHOUSE_BINARY} keeper-client" +fi + +CONNECTIONS=0 +function show_credential_options() +{ + "$@" > /dev/null 2>&1 & + local pid=$! + CONNECTIONS=$((CONNECTIONS + 1)) + # A connected program has parsed its options. + while [ "$(wc -l < "$ACCEPTED_FILE")" -lt "$CONNECTIONS" ] && kill -0 "$pid" 2>/dev/null; do sleep 0.1; done + tr '\0' '\n' < "/proc/$pid/cmdline" | grep -E '^--(password|jwt|ssh-key-passphrase|one-time-password|identity)|SECRET' + kill "$pid" 2>/dev/null + wait "$pid" 2>/dev/null || true +} + +JWT='eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0In0.SECRET' + +echo "client, separate values" +show_credential_options ${CLICKHOUSE_CLIENT_BINARY} --host 127.0.0.1 --port "$PORT" --query "SELECT 1" \ + --jwt "$JWT" --password SECRET --ssh-key-passphrase SECRET --one-time-password SECRET + +echo "client, values after =" +show_credential_options ${CLICKHOUSE_CLIENT_BINARY} --host 127.0.0.1 --port "$PORT" --query "SELECT 1" \ + --jwt="$JWT" --password=SECRET --ssh-key-passphrase=SECRET --one-time-password=SECRET + +echo "client, values after spaced =" +show_credential_options ${CLICKHOUSE_CLIENT_BINARY} --host 127.0.0.1 --port "$PORT" --query "SELECT 1" \ + --jwt = "$JWT" --password = SECRET --ssh-key-passphrase = SECRET --one-time-password = SECRET + +echo "client, --password without a value" +show_credential_options ${CLICKHOUSE_CLIENT_BINARY} --host 127.0.0.1 --port "$PORT" --query "SELECT 1" \ + --password --jwt "$JWT" + +echo "keeper-client" +show_credential_options ${KEEPER_CLIENT} --host 127.0.0.1 --port "$PORT" --connection-timeout 60 --session-timeout 60 \ + --operation-timeout 60 --query "ls" --password SECRET --identity=user:SECRET + +echo "keeper-client, values after :" +show_credential_options ${KEEPER_CLIENT} --host 127.0.0.1 --port "$PORT" --connection-timeout 60 --session-timeout 60 \ + --operation-timeout 60 --query "ls" --password:SECRET --identity:user:SECRET