Skip to content

Hardening AI functions (deny cleartext endpoints, bound API calls, sanitize error logs) - #111286

Merged
george-larionov merged 11 commits into
masterfrom
ai-functions-deny-insecure-endpoint
Aug 4, 2026
Merged

george-larionov merged 11 commits into
masterfrom
ai-functions-deny-insecure-endpoint

Conversation

@george-larionov

@george-larionov george-larionov commented Jul 21, 2026 •

Copy link
Copy Markdown
Member

When users enable the experimental AI functions, several defaults leave them exposed. This PR hardens the AI functions along three axes:

  • Deny cleartext endpoints by default. A named-collection endpoint that would send prompts and the API key over an unencrypted connection to a remote host is now rejected. Loopback endpoints (e.g. a local http://localhost model server) remain allowed. The new setting ai_function_allow_insecure_endpoint (default false) overrides the check.
  • Bound outbound calls by default. ai_function_max_api_calls_per_query now defaults to 1000 instead of 0 (unlimited), so an enabled workload cannot dispatch an unbounded number of provider requests. It is the only such bound for providers that do not report token usage. The previous behavior is restored via compatibility set to a version before 26.7, or by setting it back to 0.
  • Sanitize provider error responses in logs. Provider HTTP error bodies flowed verbatim into exceptions and the logs, where control characters could forge log lines or corrupt a terminal. The two providers now share a single formatProviderError that replaces control characters (including newlines) in both the parsed error fields and the raw-body fallback.

The prompt-injection risk of feeding AI output into downstream steps is also documented.

Changelog category (leave one):

  • Experimental Feature

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

Harden the experimental AI functions: deny insecure (http) endpoints to remote hosts by default (new setting ai_function_allow_insecure_endpoint), bound outbound API calls per query by default (ai_function_max_api_calls_per_query now defaults to 1000), and sanitize provider error responses written to the logs.

Version info

  • Merged into: 26.8.1.727 (included in 26.8 and later)

@george-larionov george-larionov changed the title Ai functions deny insecure endpoint AI functions deny insecure endpoint Jul 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [37ac4bf]

Summary: ✅


AI Review

Summary

This PR hardens the experimental AI functions by rejecting non-loopback cleartext endpoints by default, defaulting ai_function_max_api_calls_per_query to 1000, and sanitizing provider error bodies before they reach exception text/logs. I did not find any remaining correctness, compatibility, or safety issues in the current diff after reconciling the prior review threads; the earlier compatibility concerns are now covered by the SettingsChangesHistory entries and 04628_ai_functions_compatibility_defaults.

Final Verdict

✅ No new findings.

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label Jul 21, 2026
@mintlify

mintlify Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
ClickHouse-docs 🟢 Ready View Preview Jul 21, 2026, 9:04 PM

Comment thread src/Core/SettingsChangesHistory.cpp Resolved
@george-larionov george-larionov changed the title AI functions deny insecure endpoint Hardening AI functions (denying http endpoint, limiting defaults) Jul 21, 2026
@george-larionov george-larionov changed the title Hardening AI functions (denying http endpoint, limiting defaults) Hardening AI functions (deny cleartext endpoints, bound API calls, sanitize error logs) Jul 21, 2026
george-larionov added a commit that referenced this pull request Jul 21, 2026
…vior

Record ai_function_allow_insecure_endpoint as true -> false in the
settings history so `compatibility` set to a version before 26.7 opts
back into allowing plaintext remote endpoints, instead of leaving the
new guard on. Addresses the review on PR #111286.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@george-larionov
george-larionov marked this pull request as ready for review July 22, 2026 14:23
@george-larionov

Copy link
Copy Markdown
Member Author

test_check_table/test.py::test_check_all_tables failure is fixed on master.

test_replicated_database/test.py::test_replicated_table_structure_alter fix is approved but not yet merged.

@george-larionov
george-larionov force-pushed the ai-functions-deny-insecure-endpoint branch from c79c957 to 7a38a0a Compare July 23, 2026 16:00
george-larionov added a commit that referenced this pull request Jul 23, 2026
…vior

Record ai_function_allow_insecure_endpoint as true -> false in the
settings history so `compatibility` set to a version before 26.7 opts
back into allowing plaintext remote endpoints, instead of leaving the
new guard on. Addresses the review on PR #111286.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@george-larionov
george-larionov force-pushed the ai-functions-deny-insecure-endpoint branch from 7a38a0a to 89d63b1 Compare July 24, 2026 21:20
george-larionov added a commit that referenced this pull request Jul 24, 2026
…vior

Record ai_function_allow_insecure_endpoint as true -> false in the
settings history so `compatibility` set to a version before 26.7 opts
back into allowing plaintext remote endpoints, instead of leaving the
new guard on. Addresses the review on PR #111286.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread src/Core/SettingsChangesHistory.cpp Resolved
@rschu1ze

Copy link
Copy Markdown
Member

@george-larionov Could we split this PR in two?

  • One PR which disallows HTTP connections. As discussed on Friday, it would be nice to implement this at global scope.
  • Everything else.

@george-larionov

Copy link
Copy Markdown
Member Author

@rschu1ze I discussed this with @santrancisco, and the verdict was that it's not worth it to make the HTTP behavior global since it would likely break too many things (e.g. internal endpoints) and not really gain us much security since the addresses in question are internal or don't need encryption. @santrancisco feel free to comment if you have anything to add.

@rschu1ze rschu1ze self-assigned this Jul 28, 2026
Comment thread src/Core/Settings.cpp Resolved
Comment thread src/Core/SettingsChangesHistory.cpp Resolved
Comment thread src/Functions/AI/IAIProvider.cpp Resolved
Comment thread src/Functions/AI/IAIProvider.cpp Resolved
Comment thread src/Functions/AI/IAIProvider.cpp Resolved
Comment thread src/Functions/AI/IAIProvider.h Resolved
george-larionov and others added 7 commits July 31, 2026 21:23
Refuse a non-https AI named-collection endpoint whose host is not
loopback, so enabling the experimental AI functions does not silently
send prompts and API keys over an unencrypted connection. Loopback
endpoints (a local http model server) stay allowed. Add
ai_function_allow_insecure_endpoint (default false) to opt out.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Warn that AI function output is influenced by the input text and must be
treated as untrusted, especially when the input comes from external or
unsanitized sources and the output is used further in the pipeline.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Bound outbound AI function HTTP calls per query by default (previously
0 = unlimited), so an enabled AI workload cannot dispatch unbounded
requests. This is the only such bound for providers that do not report
token usage. Restore the old behavior via compatibility < 26.7 or by
setting it back to 0.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Both providers built the error exception from the provider's HTTP
response, whose control characters could forge log lines or corrupt a
terminal when logged. Factor the two near-identical extractProviderError
copies into a shared formatProviderError that replaces control
characters (including newlines) with spaces, applied to both the parsed
error message/type and the raw-body fallback.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…vior

Record ai_function_allow_insecure_endpoint as true -> false in the
settings history so `compatibility` set to a version before 26.7 opts
back into allowing plaintext remote endpoints, instead of leaving the
new guard on. Addresses the review on PR #111286.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The default of ai_function_max_api_calls_per_query changed from 0 to
1000, which the settings-dump section of 03300_ai_functions checks.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
george-larionov and others added 4 commits July 31, 2026 21:23
Add integration cases exercising both branches of formatProviderError:
a structured error whose message/type carry control characters, and a
non-JSON error body that falls back to the truncated raw body. Both
assert the control characters are replaced with spaces (no forged log
lines) while the text stays readable.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Add 04628_ai_functions_compatibility_defaults: with compatibility = '26.6'
a remote http endpoint stops being rejected and
ai_function_max_api_calls_per_query goes back to 0. This pins the
previous_value pairs in SettingsChangesHistory, which 02995 does not check.

Also rename sanitizeTextForAI to sanitizeForModel to pair with
sanitizeForLog, simplify formatProviderError, and trim docs.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 94/99 (94.95%) · Uncovered code

Full report · Diff report

@george-larionov
george-larionov added this pull request to the merge queue Aug 4, 2026
Merged via the queue into master with commit d0758c6 Aug 4, 2026
180 checks passed
@george-larionov
george-larionov deleted the ai-functions-deny-insecure-endpoint branch August 4, 2026 03:59
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 4, 2026
@zlareb1

zlareb1 commented Aug 10, 2026

Copy link
Copy Markdown
Member

@groeneai plz add test for ai_function_max_api_calls_per_query

@rschu1ze

rschu1ze commented Aug 10, 2026 •

Copy link
Copy Markdown
Member

@groeneai plz add test for ai_function_max_api_calls_per_query

@zlareb1 That would duplicate the coverage in tests/integration/test_ai_functions/test.py.

@groeneai Kindly ignore #111286 (comment)

This branch was successfully deployed

1 active deployment
staging - docs — 37ac4bf4 Deployed Jul 31, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-experimental Experimental Feature pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants