Skip to content

Restrict server-managed S3 credentials in user queries (alternative to #104889) - #106794

Closed
Algunenano wants to merge 4 commits into
masterfrom
s3-restrict-user-credentials
Closed

Algunenano wants to merge 4 commits into
masterfrom
s3-restrict-user-credentials

Conversation

@Algunenano

@Algunenano Algunenano commented Jun 8, 2026 •

Copy link
Copy Markdown
Member

Alternative to #104889.

Restricts S3 access that originates from user SQL so it cannot make the server authenticate to S3 with the server's own cloud identity. By default, the s3/s3Cluster table functions, the S3/S3Queue engines, named collections, dynamic disk(type=s3, ...) definitions, BACKUP/RESTORE TO S3, and Glue/DataLake catalogs may no longer resolve server-managed credentials: environment variables, instance metadata (IMDS), IRSA, ECS, instance profile, SSO, AWS config/credentials files, role_arn-based STS assume-role (without explicit keys), or the GCP OAuth metadata service (http_client = gcp_oauth without an explicit ADC triple). Such a request is rejected with ACCESS_DENIED unless it supplies explicit credentials or uses NOSIGN.

Credentials that the operator provisions explicitly — static keys in a named collection (whether created via SQL or defined in config) or in the <s3> config — keep working as before; this change only blocks resolving the server's own (ambient) credentials from user SQL.

This is gated by a new session setting s3_allow_server_credentials_in_user_queries (default off). A trusted administrative client can enable it in its session or settings profile (pinned with a readonly constraint and an explicit value for untrusted users); it has no effect in clickhouse-local. CredentialsConfiguration::forbid_implicit_credentials defaults to true so a newly added S3 entry point is denied server-managed credentials unless it explicitly opts in.

Compared to #104889 this takes a smaller, more central approach (a single credential-resolution chokepoint plus a session-setting kill switch), which is easier to back-port. It does not include #104889's ARN redaction, URI-aware endpoint matching, config-reload credential revocation, or the BACKUP ON CLUSTER endpoint-authorization handshake.

Related: #104889
Closes: https://github.com/ClickHouse/clickhouse-private/issues/54202

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

S3 access originating from user SQL can no longer resolve the server's own cloud credentials (environment, IMDS/IRSA, instance profile, AWS config files, role_arn-based STS, GCP OAuth metadata). Explicitly supplied credentials and operator-provisioned named-collection/config credentials are unaffected. Controlled by the new setting s3_allow_server_credentials_in_user_queries (disabled by default).

@clickhouse-gh

clickhouse-gh Bot commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b733b58]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_asan_ubsan, targeted) FAIL
Server died FAIL cidb
04054_deltalake_cdf_copy_paste_bug UNKNOWN cidb
04054_deltalake_cdf_copy_paste_bug UNKNOWN cidb
04054_deltalake_cdf_copy_paste_bug UNKNOWN cidb
04054_deltalake_cdf_copy_paste_bug UNKNOWN cidb
04054_deltalake_cdf_copy_paste_bug UNKNOWN cidb
Integration tests (amd_asan_ubsan, targeted) FAIL
test_disk_configuration/test.py::test_dynamic_disk_security_settings FAIL cidb
test_disk_configuration/test.py::test_merge_tree_custom_disk_setting FAIL cidb
test_disk_configuration/test.py::test_merge_tree_custom_encrypted_disk_include[node1] FAIL cidb
test_disk_configuration/test.py::test_merge_tree_custom_encrypted_disk_include[node2] FAIL cidb
test_mask_sensitive_info/test.py::test_create_table FAIL cidb
test_mask_sensitive_info/test.py::test_table_functions FAIL cidb
test_restore_external_engines/test.py::test_restore_table FAIL cidb
test_restore_external_engines/test.py::test_restore_table_null FAIL cidb
Integration tests (amd_asan_ubsan, flaky) FAIL
test_storage_delta/test.py::test_session_token FAIL cidb
test_storage_delta/test_imds.py::test_session_token FAIL cidb
test_storage_s3/test.py::test_put_with_zero_redirect FAIL cidb
test_storage_s3/test.py::test_remote_host_filter FAIL cidb
test_storage_s3/test.py::test_file_pruning_with_hive_style_partitioning FAIL cidb
test_storage_s3/test.py::test_file_pruning_with_hive_style_partitioning_2 FAIL cidb
Stateless tests (amd_asan_ubsan, distributed plan, parallel, 1/2) FAIL
02876_s3_cluster_schema_inference_names_with_spaces FAIL cidb
02245_s3_schema_desc FAIL cidb
03275_auto_cluster_functions_with_parallel_replicas FAIL cidb
04260_one_format_s3_no_object_reads FAIL cidb
04034_parquet_v3_metadata_cache_no_query_context FAIL cidb
04065_url_s3_parallel_replicas_distributed_processing FAIL cidb
03579_create_table_populate_from_s3 FAIL cidb
01801_s3_cluster_count FAIL cidb
02873_s3_presigned_url_and_url_with_special_characters FAIL cidb
03667_view_with_s3_cluster_and_sql_security_definer FAIL cidb
Stateless tests (amd_asan_ubsan, distributed plan, parallel, 2/2) FAIL
04040_object_storage_parallelize_output FAIL cidb
02922_url_s3_engine_size_virtual_column FAIL cidb
02973_s3_compressed_file_in_error_message FAIL cidb
04202_s3_write_to_globbed_path_non_partitioned FAIL cidb
03603_reading_s3_cluster_all_nodes_unavailable FAIL cidb
03595_sources_regexp_filter_grants FAIL cidb
02534_s3_cluster_insert_select_schema_inference FAIL cidb
04093_s3_settings_query_override FAIL cidb
02457_s3_cluster_schema_inference FAIL cidb
01801_s3_cluster FAIL cidb
10 more test cases not shown
Stateless tests (amd_debug, parallel) FAIL
02873_s3_presigned_url_and_url_with_special_characters FAIL cidb
04202_s3_write_to_globbed_path_non_partitioned FAIL cidb
01801_s3_cluster FAIL cidb
03822_auto_cluster_functions_one_subquery_inside_from FAIL cidb
02922_url_s3_engine_size_virtual_column FAIL cidb
03037_s3_write_to_globbed_partitioned_path FAIL cidb
02752_forbidden_headers FAIL cidb
03273_format_inference_create_query_s3_url FAIL cidb
04065_url_s3_parallel_replicas_distributed_processing FAIL cidb
03667_view_with_s3_cluster_and_sql_security_definer FAIL cidb
20 more test cases not shown
Stateless tests (amd_tsan, parallel, 1/2) FAIL
01801_s3_cluster_count FAIL cidb
04065_url_s3_parallel_replicas_distributed_processing FAIL cidb
02876_s3_cluster_schema_inference_names_with_spaces FAIL cidb
02245_s3_schema_desc FAIL cidb
03579_create_table_populate_from_s3 FAIL cidb
03275_auto_cluster_functions_with_parallel_replicas FAIL cidb
03667_view_with_s3_cluster_and_sql_security_definer FAIL cidb
04260_one_format_s3_no_object_reads FAIL cidb
02873_s3_presigned_url_and_url_with_special_characters FAIL cidb
04034_parquet_v3_metadata_cache_no_query_context FAIL cidb
Stateless tests (amd_tsan, parallel, 2/2) FAIL
04040_object_storage_parallelize_output FAIL cidb
03275_ignore_nonexistent_files_fix FAIL cidb
03636_normalize_url_in_source_grants FAIL cidb
02922_url_s3_engine_size_virtual_column FAIL cidb
03273_format_inference_create_query_s3_url FAIL cidb
03203_hive_style_partitioning FAIL cidb
04093_s3_settings_query_override FAIL cidb
03037_s3_write_to_globbed_partitioned_path FAIL cidb
04202_s3_write_to_globbed_path_non_partitioned FAIL cidb
02457_s3_cluster_schema_inference FAIL cidb
10 more test cases not shown
Stateless tests (arm_binary, parallel) FAIL
02270_errors_in_files_s3 FAIL cidb
02534_s3_cluster_insert_select_schema_inference FAIL cidb
04260_one_format_s3_no_object_reads FAIL cidb
04040_object_storage_parallelize_output FAIL cidb
04065_url_s3_parallel_replicas_distributed_processing FAIL cidb
02973_s3_compressed_file_in_error_message FAIL cidb
03579_create_table_populate_from_s3 FAIL cidb
01801_s3_cluster FAIL cidb
02457_s3_cluster_schema_inference FAIL cidb
02245_s3_schema_desc FAIL cidb
20 more test cases not shown
Stateless tests (amd_llvm_coverage, 1/3) FAIL
02922_url_s3_engine_size_virtual_column FAIL cidb
02876_s3_cluster_schema_inference_names_with_spaces FAIL cidb
02457_s3_cluster_schema_inference FAIL cidb
03275_ignore_nonexistent_files_fix FAIL cidb
03603_reading_s3_cluster_all_nodes_unavailable FAIL cidb
04034_parquet_v3_metadata_cache_no_query_context FAIL cidb
03667_view_with_s3_cluster_and_sql_security_definer FAIL cidb

AI Review

Summary

This PR adds a central guard to stop user-originated S3 access from resolving server-managed credentials. The provider-chain guard is useful, but several request-secret and lifecycle paths still bypass the intended per-user policy, so the hardening is incomplete.

Findings

❌ Blockers

  • [src/Disks/DiskObjectStorage/ObjectStorages/S3/diskSettings.cpp:212] The new guard only blocks AWS/GCP credential providers; restricted user SQL still inherits server-configured request secrets. auth_settings.getHeaders can append <access_header> from the server <s3> config, and the same client creation still forwards server-configured SSE-C/KMS settings, so s3/S3 requests can send operator-provisioned secrets to a user-controlled endpoint even with s3_allow_server_credentials_in_user_queries = 0.
    Suggested fix: when forbid_implicit_credentials is true, clear or reject server-originated access_header, server_side_encryption_customer_key_base64, and server_side_encryption_kms_config while preserving headers supplied explicitly in the query.
  • [src/Backups/BackupIO_S3.cpp:205] BACKUP/RESTORE TO S3 still forwards server-configured SSE material into user-driven requests. settings.auth_settings is loaded from server <s3>/endpoint config, so restricted backup SQL can still send server_side_encryption_customer_key_base64 or server_side_encryption_kms_config to an arbitrary backup endpoint.
    Suggested fix: pass empty SSE values under the restricted credential policy unless the SSE material was supplied explicitly by the user.
  • [src/Core/Settings.cpp:4280] The session-scoped allow model leaks through DatabaseS3 table caching. A trusted session can materialize a DatabaseS3 table with s3_allow_server_credentials_in_user_queries = 1; DatabaseS3::getTableImpl caches that StorageS3, and later untrusted sessions return the cached storage before any current-context credential check, reusing the client/provider created with server credentials.
    Suggested fix: avoid caching DatabaseS3 tables materialized with server-managed credentials, include the credential policy in the cache key, or recreate/revalidate the S3 client for the current query context before returning a cached table.

⚠️ Majors

  • [src/Databases/DataLake/GlueCatalog.cpp:115] Glue still evaluates the new setting on the global context because DatabaseDataLake::getCatalog constructs GlueCatalog with Context::getGlobalContextInstance. That makes SETTINGS s3_allow_server_credentials_in_user_queries = 1 on CREATE/SELECT ineffective for aws_role_arn or ambient Glue credentials, contradicting the documented trusted-session escape hatch and the updated Glue tests.
    Suggested fix: evaluate the restriction against the current query/user context when Glue credentials are resolved or refreshed, and avoid caching a provider whose allow/deny state came from a different user.
Tests
  • ⚠️ Add a focused regression showing restricted s3/S3 access does not send server <access_header> or SSE material to a user-controlled endpoint.
  • ⚠️ Add a two-user DatabaseS3 regression: one trusted query with s3_allow_server_credentials_in_user_queries = 1, then an untrusted query for the same cached table must not reuse server credentials.
  • ⚠️ Add or repair a Glue regression proving the allow setting is evaluated from the current query context for aws_role_arn or ambient credentials.
Final Verdict

Status: ❌ Block

Minimum required actions: close the remaining secret-forwarding paths, fix or explicitly redesign the cross-user cache behavior, make Glue honor the intended setting semantics, and add the focused regressions above.

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Jun 8, 2026
@@ -190,16 +210,7 @@ class S3BackupClientCreator
settings.auth_settings[S3AuthSetting::server_side_encryption_customer_key_base64],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still forwards server-configured SSE material into user-driven BACKUP/RESTORE TO S3 requests when restrict_credentials is true. settings.auth_settings was loaded from the server <s3> config and endpoint config, so server_side_encryption_customer_key_base64/server_side_encryption_kms_config can be sent to a user-controlled S3 endpoint even though the PR promises that restricted user SQL cannot inherit server SSE material. Please pass empty SSE values under restrict_credentials, the same way S3StorageParsedArguments::fromAST clears them for the s3/S3 paths.

Comment thread src/Backups/registerBackupEngineS3.cpp Resolved
Comment thread src/Core/Settings.cpp Resolved
Comment thread src/Backups/BackupIO_S3.cpp Resolved
Algunenano and others added 3 commits June 9, 2026 11:09
Adds the session setting `s3_allow_server_credentials_in_user_queries`
(default off). When off in clickhouse-server, S3 access from user SQL
(`s3`/`s3Cluster`, the `S3`/`S3Queue` engines, named collections, dynamic
`disk(type=s3, ...)`, `BACKUP`/`RESTORE TO S3`, and Glue/DataLake catalogs)
may not resolve the server's own ambient credentials: environment variables,
instance metadata (IMDS), IRSA, ECS, instance profile, SSO, AWS config/
credentials files, `role_arn`-based STS assume-role (without explicit keys),
or the GCP OAuth metadata service. Such a request is rejected with
`ACCESS_DENIED` unless it supplies explicit credentials or uses NOSIGN.
Credentials supplied explicitly in the query, or operator-provisioned static
credentials in named collections or the `<s3>` config, are unaffected.

Enforced at a single credential-resolution chokepoint
(`getCredentialsProvider`) plus a static check for dynamic S3 disks in
`getDiskConfigurationFromAST` (skipped when loading existing metadata).
`CredentialsConfiguration::forbid_implicit_credentials` defaults to true
(fail closed); server-internal callers (server disks, Keeper snapshots) opt
out explicitly. `gcp_oauth` is matched case-insensitively and refused unless a
complete explicit ADC triple is given. clickhouse-local is unaffected.

The setting can be enabled per session/profile for a trusted administrative
client and pinned for untrusted users with an explicit value plus a readonly
constraint (the explicit value is required so an old `compatibility` version
cannot restore the previous default).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
A restricted user query must not use the server's AWS shared-credentials file
(the profile provider) when there are no explicit keys and no env/IMDS
credentials; it is rejected with ACCESS_DENIED and works once the setting is
enabled. The server's own data disk is unaffected.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
These tests exercise user-facing S3 access that intentionally uses
server-managed credentials (IMDS, environment, role_arn STS, gcp_oauth, or
auth headers from the `<s3>` config). Enable
`s3_allow_server_credentials_in_user_queries` for them, via the default
settings profile or per-query, so they keep passing under the new default.

Covers test_s3_imds, test_storage_gcp_auth, test_s3_access_headers,
test_e2e_catalogs, test_storage_delta, test_database_glue, and test_storage_s3.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@Algunenano
Algunenano force-pushed the s3-restrict-user-credentials branch from e02cafb to b3ec892 Compare June 9, 2026 11:10

/// A Glue catalog is created by user SQL (`CREATE DATABASE ... ENGINE = DataLakeCatalog`), so it must
/// not be able to reuse the server's own credentials.
creds_config.forbid_implicit_credentials = getContext()->shouldRestrictUserQueryS3Credentials();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getContext here is the global context because DatabaseDataLake::getCatalog constructs GlueCatalog with Context::getGlobalContextInstance. That makes the new session/profile setting ineffective for Glue: CREATE/SELECT with SETTINGS s3_allow_server_credentials_in_user_queries = 1 still reads the global default and rejects aws_role_arn/ambient credentials, which contradicts the PR contract and the updated tests. If this is moved to the caller's context, please also avoid caching a credentials provider created under one user's setting in catalog_impl, otherwise the first caller's allow/deny state leaks to later users. The restriction needs to be evaluated against the current query/user context when Glue credentials are resolved/refreshed.

Comment thread src/Core/Settings.cpp Resolved
BigLakeCatalog::retrieveGoogleCloudAccessToken fell back to the GCP metadata
service when no complete ADC triple was configured, and even after an explicit
ADC token request failed, letting user SQL mint a token with the server's own
identity. Now an explicit ADC triple is used and fails closed (no metadata
fallback), and the metadata-service path is refused when user queries are
restricted.

Glue and BigLake catalogs are constructed once with the global server context
and cached/shared, so the per-session setting cannot enable their server-managed
credentials; the setting documentation now states that such catalogs require
explicit credentials.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
/// DataLake table reads) is driven by user SQL, so it must not use the server's own credentials.
/// Server disks use the server's own credentials. User-created dynamic S3 disks are handled earlier,
/// in getDiskConfigurationFromAST.
credentials_configuration.forbid_implicit_credentials = !for_disk_s3 && context->shouldRestrictUserQueryS3Credentials();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only gates the AWS/GCP credential providers; restricted user SQL still inherits server-configured request secrets below. auth_settings.getHeaders at line 217 can append <access_header> from the server <s3> config when no explicit access key is set, and lines 255-256 still forward server-configured SSE-C/KMS settings into the client. That means SELECT * FROM s3('https://attacker/...') can still make ClickHouse send operator-provisioned auth headers/SSE material to a user-controlled endpoint even with s3_allow_server_credentials_in_user_queries = 0; the integration-test comments already treat <access_header> as server-managed and blocked by default. Please clear server-originated headers/SSE material, or reject the request, whenever forbid_implicit_credentials is true while preserving headers explicitly supplied in the query.

Comment thread src/Core/Settings.cpp

This prevents an authenticated user from making the server access S3 with its own (ambient) credentials (server-side credential hijacking). Credentials supplied explicitly — in the query, or as static keys in a named collection (whether created via SQL or defined in config) or in the server `<s3>` config — are not affected by this setting.

A trusted administrative client may need server-managed credentials for legitimate operations (for example, attaching system tables on an `s3_plain_rewritable` disk via SQL). Enable this setting in that client's session or settings profile to permit it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This per-session allow model does not hold for DatabaseS3 cached tables. A trusted session can read db.file with s3_allow_server_credentials_in_user_queries = 1; DatabaseS3::getTableImpl then stores the resulting StorageS3 in loaded_tables, and later untrusted sessions return that cached storage before any current-context credential check, reusing the client/provider created with server credentials. That makes the allow setting bleed across sessions for user-controlled S3 database reads. Please avoid caching DatabaseS3 tables materialized with server-managed credentials, include the credential policy in the cache key, or recreate/revalidate the S3 client for the current query context before returning a cached table.

@Algunenano

Copy link
Copy Markdown
Member Author

#106855 is more complete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants