Skip to content

Cherry pick #108464 to 26.3: Add SHOW COLUMNS access check to hasColumnInTable - #120196

Merged
robot-ch-test-poll3 merged 3 commits into
backport/26.3/108464from
cherrypick/26.3/108464
Sep 19, 2026
Merged

robot-ch-test-poll3 merged 3 commits into
backport/26.3/108464from
cherrypick/26.3/108464

Conversation

@robot-ch-test-poll2

Copy link
Copy Markdown
Contributor

Original pull-request #108464

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 2 commits July 18, 2026 18:20
hasColumnInTable read table column metadata with no access check, so a
user with no privileges on a table could still probe its column names and
test table and database existence. DESCRIBE and SHOW CREATE TABLE on the
same table are gated by SHOW COLUMNS, but this function was not.

The function stored the global context, which has full access by
construction, so a check against it would always pass. Keep the query
context, which carries the caller's rights, and check SHOW COLUMNS on the
raw database and table names before resolving the table, so a denial does
not depend on the table or database existing (no existence oracle).

The FunctionDocumentation Description notes the required privilege so the
auto-generated SQL reference (built from system.functions) matches the
new behavior.

Rebased onto master after #110881 removed the remote-server overload, so
only the local three-argument form remains and needs this gate.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ow-columns-access

Add SHOW COLUMNS access check to hasColumnInTable
@robot-ch-test-poll2 robot-ch-test-poll2 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Sep 15, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

I resolved this cherry-pick locally and validated it on the 26.3 tree. Exactly one file conflicts,
src/Functions/hasColumnInTable.cpp, in two marker regions; both new test files auto-merge cleanly. I
hold pull and triage on this repository but not push, so I cannot move
cherrypick/26.3/108464 myself. Here is the resolution instead.

Why it conflicts. #110881 removed the hostname[, username[, password]] overload from master on
2026-07-18, two days before #108464 merged, so #108464 was reconciled onto a master that no longer had
the overload and its merged form has a single code path. 26.3 predates that removal and still dispatches
on host_name.empty(). Only that region collides.

git fetch origin
git checkout -B wip origin/cherrypick/26.3/108464
git merge --no-ff origin/backport/26.3/108464
# includes    -> union: both Access/Common headers, then the branch's <Core/Settings.h>
# executeImpl -> the 3-line comment and
#                getContext()->checkAccess(AccessType::SHOW_COLUMNS, database_name, table_name);
#                ABOVE the branch's `if (host_name.empty())` dispatch, dispatch kept verbatim
git add src/Functions/hasColumnInTable.cpp   # not `add -A`: that stages master-era submodule pins
git checkout 72a9159bda6acf10ed00ec253db8d0c746a85b14 -- \
  tests/queries/0_stateless/04401_has_column_in_table_access_check.sh \
  tests/queries/0_stateless/04401_has_column_in_table_access_check.reference
git commit && git push origin HEAD:cherrypick/26.3/108464

git checkout --theirs on that file is not the resolution, and neither is rebuilding it from the
branch's copy. Both drop a third, non-conflicting change that git merges correctly on its own: the
:::note Privilege SHOW COLUMNS is required block joins the description in
REGISTER_FUNCTION(HasColumnInTable) while the branch's own variadic Syntax string and its six
Arguments entries stay. Resolve the two marker regions only.

The check has to sit above the dispatch rather than inside the host_name.empty() branch. On 26.3
create() passes context_->getGlobalContext(), which has full access by construction, so the check
getStructureOfRemoteTableInShard already performs for local shards
(src/Storages/getStructureOfRemoteTable.cpp:65) is a no-op today. Passing the query context makes that
check effective too, and hoisting the new one gates the remote path, which otherwise connects as
default before any privilege is examined.

Anchor. With backport/26.3/108464 at 795cccbc43725703ab2ff3f4c1ffb065d38b75a5 the merge tree is
50b4bd9685d21573ceb370d3b7c0e27f45a7a81c and the resolved blob is
9be8cf74c949894a97b052056bfd351b32b0f1ec. git diff origin/backport/26.3/108464 HEAD is 3 files:
+16 / -2 in the source plus the two test files. Every one of the 16 added source lines is
byte-identical to master's merged copy at 9ec0c6f89c9b; only the placement of the check differs, and
only because of the dispatch this branch still has. If the base has been force-pushed since, the recipe
is unchanged and only the two ids above move.

Expected source diff against the base (4 hunks)
@@ -1,3 +1,5 @@
+#include <Access/Common/AccessFlags.h>
+#include <Access/Common/AccessType.h>
 #include <Core/Settings.h>
 #include <Functions/IFunction.h>
 #include <Functions/FunctionFactory.h>
@@ -33,10 +35,12 @@ public:
     static constexpr auto name = "hasColumnInTable";
     static FunctionPtr create(ContextPtr context_)
     {
-        return std::make_shared<FunctionHasColumnInTable>(context_->getGlobalContext());
+        return std::make_shared<FunctionHasColumnInTable>(context_);
     }
 
-    explicit FunctionHasColumnInTable(ContextPtr global_context_) : WithContext(global_context_)
+    /// Holds the query context for the access check in executeImpl. The check must not run
+    /// against the global context, which has full access by construction.
+    explicit FunctionHasColumnInTable(ContextPtr context_) : WithContext(context_)
     {
     }
 
@@ -114,6 +118,11 @@ ColumnPtr FunctionHasColumnInTable::executeImpl(const ColumnsWithTypeAndName & a
     if (table_name.empty())
         throw Exception(ErrorCodes::UNKNOWN_TABLE, "Table name is empty");
 
+    /// Check access on the raw names before resolving the table, so denial does not
+    /// depend on the table/database existing (no existence oracle). SHOW_COLUMNS is the
+    /// same grant required by DESCRIBE and SHOW CREATE TABLE.
+    getContext()->checkAccess(AccessType::SHOW_COLUMNS, database_name, table_name);
+
     bool has_column;
     bool has_alias_column;
     if (host_name.empty())
@@ -170,6 +179,11 @@ REGISTER_FUNCTION(HasColumnInTable)
 Checks if a specific column exists in a database table.
 For elements in a nested data structure, the function checks for the existence of a column.
 For the nested data structure itself, the function returns `0`.
+
+:::note Privilege `SHOW COLUMNS` is required
+The function requires the `SHOW COLUMNS` privilege on the target table (the same grant needed by `DESCRIBE` and `SHOW CREATE TABLE`).
+Without it the call fails with `ACCESS_DENIED` instead of returning `1` or `0`, so column names cannot be probed without access.
+:::
     )";
     FunctionDocumentation::Syntax syntax = "hasColumnInTable([hostname[, username[, password]],]database, table, column)";
     FunctionDocumentation::Arguments arguments = {

Validation. I built both arms with the branch's own clang-21, one tree and one differing source file,
and used the binary Build ID to confirm which binary served each run. On the branch-tip binary a user
holding only GRANT SELECT ON system.one gets 1 from hasColumnInTable(db, 'secret', 'password') and
1 again through the localhost overload, while DESCRIBE TABLE db.secret for that same user is denied
with ACCESS_DENIED. The resolved binary denies all seven denial assertions and still returns
1, 0, 1, 0 for the four positives once SHOW COLUMNS is granted, 50 of 50 randomized runs green.
The four existing branch tests that call hasColumnInTable all run as default, so none of them is
affected.

Excluded deliberately. Nothing from master after 2026-07-20, in particular not #110881's removal of
the overload and not #117621's Alias-target arm, whose StorageAlias::isTargetTableGranted
prerequisite does not exist here. The two test files come from 72a9159bda6a, the pre-reconciliation
revision of #108464 itself, rather than from the merged form: the merged test was reconciled for the same
reason the file conflicts, master had lost the overload, so it cannot see the remote path this change
gates. That revision was CI-green on this test, 216 OK results across 20 checks including 50-run flaky
checks on amd_debug, amd_asan_ubsan, amd_tsan and amd_msan. git checkout 9ec0c6f89c9b -- <the two test paths> reverses that choice if you prefer the minimal form.

@Ergus you merged #108464 and are a co-assignee here; applying the resolution needs write access to
cherrypick/26.3/108464, which I do not have.

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

Dear @Ergus, @alexey-milovidov, @groeneai, this cherry-pick PR has not been updated for 3d0h0m51s. Please resolve the conflicts to backport #108464, or close this PR if the backport is no longer needed. This PR will be automatically closed after 7 days of inactivity.

Resolve the cherry-pick of #108464 onto 26.3. Exactly one file conflicts,
`src/Functions/hasColumnInTable.cpp`, in two marker regions.

#110881 removed the `hostname[, username[, password]]` overload from master two
days before #108464 merged, so the merged form of #108464 has a single code
path. 26.3 predates that removal and still dispatches on `host_name.empty()`.

Resolution:

  * includes: union of both sides - the two `Access/Common` headers plus the
    branch's `<Core/Settings.h>`, which 26.3 still needs for `getSettingsRef`.
  * `executeImpl`: the `checkAccess(AccessType::SHOW_COLUMNS, ...)` call is
    hoisted ABOVE the branch's `if (host_name.empty())` dispatch, which is kept
    verbatim. It must not go inside the local arm: on 26.3 the remote arm
    otherwise connects as `default` before any privilege is examined, and the
    check `getStructureOfRemoteTableInShard` already performs for local shards
    is a no-op today because `create` passed `getGlobalContext`, which has full
    access by construction. Passing the query context makes that check
    effective too.
  * the `:::note Privilege SHOW COLUMNS is required` block in
    `REGISTER_FUNCTION(HasColumnInTable)` merges on its own and is preserved
    alongside the branch's own variadic `Syntax` string and `Arguments`
    entries. Taking either side of the file wholesale would have dropped it.

The two test files are taken from `72a9159bda6a`, the pre-reconciliation
revision of #108464, rather than from its merged form. The merged test was
reconciled for the same reason the file conflicts - master had lost the
overload - so it cannot exercise the remote path that this change gates on
26.3. The pre-reconciliation test is a strict superset and covers the
`localhost` and non-loopback `127.0.0.2` overloads.

Resolution independently reproduced from the recipe in
#120196 (comment)
tree `50b4bd9685d21573ceb370d3b7c0e27f45a7a81c`, resolved blob
`9be8cf74c949894a97b052056bfd351b32b0f1ec`; both match. The resolved
translation unit type-checks against the 26.3 tree.

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

Copy link
Copy Markdown
Member

🕵 Applied. @groeneai's resolution is now pushed to cherrypick/26.3/108464 as 7d371a0ef700; the PR is MERGEABLE / CLEAN again.

I reproduced the resolution independently from the recipe rather than taking it on trust, and both anchors match exactly:

  • merge tree 50b4bd9685d21573ceb370d3b7c0e27f45a7a81c
  • resolved blob 9be8cf74c949894a97b052056bfd351b32b0f1ec

The base was still at 795cccbc4372, so the ids did not move. git diff origin/backport/26.3/108464 HEAD is the expected 3 files, +16 / -2 in the source plus the two test files.

Both marker regions resolved as described: the includes are a union (the two Access/Common headers plus the branch's <Core/Settings.h>, still needed here for getSettingsRef), and getContext()->checkAccess(AccessType::SHOW_COLUMNS, database_name, table_name) sits above the if (host_name.empty()) dispatch with the dispatch kept verbatim. The point about not taking either side of the file wholesale held up: the :::note Privilege SHOW COLUMNS is required block merges on its own and would have been dropped by --theirs, and it is present in the result alongside the branch's variadic Syntax string and its six Arguments entries.

I took the two test files from 72a9159bda6a as recommended. On this branch that is the better of the two: it is a strict superset of the merged form, and the extra localhost and non-loopback 127.0.0.2 cases are exactly the overload that 26.3 still has and that the merged test could not see.

Verification I could add on top of the recipe: I confirmed the API surface the hoisted call needs exists on 26.3 — AccessType::SHOW_COLUMNS at src/Access/Common/AccessType.h:163 and the checkAccess(const AccessFlags &, std::string_view, std::string_view) overload at src/Interpreters/Context.h:860 — and the resolved translation unit type-checks cleanly against the 26.3 tree. I did not re-run the runtime validation; the 50-of-50 green result above stands on its own.

No CI will run here — the PR carries do not test, as cherry-pick PRs do. The functional check happens on the backport/26.3/108464 → 26.3 PR the robot opens once this one merges. There are no unresolved review threads. Leaving the merge itself to a human.

@Ergus this is ready for you now; the write access that was the blocker is no longer needed.

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

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants