From 0e5d90c3182aa19412808bad376b0243b96bc4e4 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Mon, 16 Nov 2026 14:30:32 +0000 Subject: [PATCH 1/2] Merge pull request #131963 from groeneai/clear-credential-options-from-command-line Clear `--jwt` and the other credential options from the command line of `clickhouse-client` and `clickhouse-keeper-client` --- programs/keeper-client/KeeperClient.cpp | 2 + src/Common/clearPasswordFromCommandLine.cpp | 30 +++++-- src/Common/clearPasswordFromCommandLine.h | 4 +- ...ar_credentials_from_command_line.reference | 26 ++++++ ...813_clear_credentials_from_command_line.sh | 88 +++++++++++++++++++ 5 files changed, 140 insertions(+), 10 deletions(-) create mode 100644 tests/queries/0_stateless/05813_clear_credentials_from_command_line.reference create mode 100755 tests/queries/0_stateless/05813_clear_credentials_from_command_line.sh 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..678534b3bfada --- /dev/null +++ b/tests/queries/0_stateless/05813_clear_credentials_from_command_line.reference @@ -0,0 +1,26 @@ +client, separate values +--jwt +--password +--ssh-key-passphrase +--one-time-password +--oauth-client-secret +client, values after = +--jwt= +--password= +--ssh-key-passphrase= +--one-time-password= +--oauth-client-secret= +client, values after spaced = +--jwt +--password +--ssh-key-passphrase +--one-time-password +--oauth-client-secret +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..8304d52a3af4c --- /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` and `--oauth-client-secret` need 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|oauth-client-secret|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 --oauth-client-secret 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 --oauth-client-secret=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 --oauth-client-secret = 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 From 42af140edbf1854565a918931f913dadc56550b6 Mon Sep 17 00:00:00 2001 From: blau-ai Date: Wed, 18 Nov 2026 11:21:36 +0000 Subject: [PATCH 2/2] Adapt 05813_clear_credentials_from_command_line to antalya-26.8: drop --oauth-client-secret clickhouse-client in antalya-26.8 has no --oauth-client-secret option, so the client rejected it and exited before connecting, failing the test. --- ...05813_clear_credentials_from_command_line.reference | 3 --- .../05813_clear_credentials_from_command_line.sh | 10 +++++----- 2 files changed, 5 insertions(+), 8 deletions(-) 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 index 678534b3bfada..938b436dbf816 100644 --- a/tests/queries/0_stateless/05813_clear_credentials_from_command_line.reference +++ b/tests/queries/0_stateless/05813_clear_credentials_from_command_line.reference @@ -3,19 +3,16 @@ client, separate values --password --ssh-key-passphrase --one-time-password ---oauth-client-secret client, values after = --jwt= --password= --ssh-key-passphrase= --one-time-password= ---oauth-client-secret= client, values after spaced = --jwt --password --ssh-key-passphrase --one-time-password ---oauth-client-secret client, --password without a value --password keeper-client 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 index 8304d52a3af4c..1458f3b3eefc5 100755 --- a/tests/queries/0_stateless/05813_clear_credentials_from_command_line.sh +++ b/tests/queries/0_stateless/05813_clear_credentials_from_command_line.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash # Tags: no-fasttest -# no-fasttest: `--jwt` and `--oauth-client-secret` need a build with jwt-cpp and SSL. +# no-fasttest: `--jwt` needs a build with jwt-cpp and SSL. CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) # shellcheck source=../shell_config.sh @@ -56,7 +56,7 @@ function show_credential_options() 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|oauth-client-secret|identity)|SECRET' + 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 } @@ -65,15 +65,15 @@ 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 --oauth-client-secret SECRET + --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 --oauth-client-secret=SECRET + --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 --oauth-client-secret = SECRET + --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" \