Repository navigation
Cherry pick #108464 to 26.3: Add SHOW COLUMNS access check to hasColumnInTable - #120196
Conversation
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
2be79cb to
795cccb
Compare
|
I resolved this cherry-pick locally and validated it on the 26.3 tree. Exactly one file conflicts, Why it conflicts. #110881 removed the 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
The check has to sit above the dispatch rather than inside the Anchor. With 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, Excluded deliberately. Nothing from master after 2026-07-20, in particular not #110881's removal of @Ergus you merged #108464 and are a co-assignee here; applying the resolution needs write access to |
|
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]>
|
🕵 Applied. @groeneai's resolution is now pushed to I reproduced the resolution independently from the recipe rather than taking it on trust, and both anchors match exactly:
The base was still at Both marker regions resolved as described: the includes are a union (the two I took the two test files from Verification I could add on top of the recipe: I confirmed the API surface the hoisted call needs exists on 26.3 — No CI will run here — the PR carries @Ergus this is ready for you now; the write access that was the blocker is no longer needed. |
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-picklocally.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:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job