Skip to content

fix: don't wipe policy_grants and drop_trigger_grants on a rejected value - #279

Open
utkarash2991 wants to merge 2 commits into
masterfrom
fix-grants-guc-check-hooks
Open

utkarash2991 wants to merge 2 commits into
masterfrom
fix-grants-guc-check-hooks

Conversation

@utkarash2991

Copy link
Copy Markdown
Contributor

Fixes #213.

The check hooks for policy_grants and drop_trigger_grants freed the live arrays and parsed the new value into them before validating it. If the value was bad, Postgres kept the old string but supautils had already lost the parsed grants. In that backend show still printed the old JSON, yet privileged_role could no longer create policies or drop triggers until the next good reload. The partially parsed entries also leaked in TopMemoryContext, and a slot left half parsed kept its table count, so a later valid value for the same role failed with "too many policy grant tables specified".

Fix: the check hook parses into a scratch array and throws it away. A new assign hook, same shape as extensions_parameter_overrides, clears every slot and loads the live arrays.

…alue

The check hooks freed the live arrays and parsed the new value into them
before validating it. A bad value left the backend without grants until
the next good reload, leaked the partial entries in TopMemoryContext and
kept a stale table count on the half parsed slot, so a later valid value
for that role was rejected too.

Parse into a scratch array in the check hook and load the live arrays
from a new assign hook, same shape as extensions_parameter_overrides.
@utkarash2991
utkarash2991 requested a review from a team as a code owner November 24, 2026 19:37
@utkarash2991
utkarash2991 requested a review from imor November 24, 2026 19:39

@imor imor left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Left just one minor comment to improve tests, otherwise LGTM.

alter system set supautils.policy_grants to '{"stale_role": ["public.table_1"]}';
alter system reset supautils.policy_grants;
\echo

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can the tests be improved by using a show supautils.<guc> command in the output which would make it clear whether the change took effect or not instead of indirectly exercising the effect of those changes?

Comment thread src/supautils.c Outdated
Comment on lines +484 to +486
clear_policy_grants_array(pgs, MAX_POLICY_GRANTS);
total_pgs = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What happens if there is an error i.e for example run out of memory halfway through loading the new grants? We’ve already cleared the old grants, and the new list is left in partial state. Could we keep the old grants in place until the new list is ready? Same comment applies to drop_trigger_grants as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. Assign hooks are not meant to fail, but there is no reason to leave a half-loaded array if one does. Going to take care in next revision

grant usage on schema allow_policies to grants_role;
alter system set supautils.policy_grants to '{"grants_role": ["allow_policies.my_table"], "oops": 1}';
ERROR: supautils.policy_grants: unexpected scalar, expected an array
alter system set supautils.policy_grants to '{"grants_role": ["allow_policies.other_table"]}';

@mahendrakar-dev mahendrakar-dev Nov 28, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

should we add create policy statement for other_table to confirm the desired behaviour after policy creation for my_table is denied.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added a test case in next revision

…w and reload tests

The assign hooks now parse into a scratch array and only then replace the
live one, so a failure halfway leaves the old grants in place.

Tests show the GUC after a rejected value, and a new case reloads the
config to check the accepted value is the one enforced.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak while setting some GUCs

3 participants