Skip to content

Validate preference values set via the CLI (#7346)#10047

Merged
asheshv merged 2 commits into
pgadmin-org:masterfrom
dpage:fix-7346-cli-prefs-validation
Jun 16, 2026
Merged

Validate preference values set via the CLI (#7346)#10047
asheshv merged 2 commits into
pgadmin-org:masterfrom
dpage:fix-7346-cli-prefs-validation

Conversation

@dpage

@dpage dpage commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #7346.

Preferences set via the CLI (setup.py set-prefs) were not validated: Preferences.save_cli wrote the raw value straight to the configuration database and always returned success, so an invalid value (e.g. a non-numeric string for an integer preference) was stored silently. The GUI path validates via _Preference.set().

Fix:

  • _Preference.set() now accepts an optional user_id, so it can be used outside a request context (the CLI has no current_user).
  • Preferences.save_cli now looks up the preference (like Preferences.save) and delegates to set(), so the value is validated against the preference type.
  • setup.py set-prefs checks the result and reports any preference whose value was invalid instead of silently claiming success.

The GUI path is unaffected (save_cli is only used by the CLI; update() calls pref.set() directly).

Changes

  • web/pgadmin/utils/preferences.py, web/setup.py
  • Release note (9.16)

🤖 Generated with Claude Code

Summary by CodeRabbit

Bug Fixes

  • CLI preference setting via setup.py now validates preference values against their defined types and rejects invalid values with specific error messages instead of storing them silently.
  • Enhanced error reporting distinguishes between missing preferences and invalid values.

@coderabbitai

coderabbitai Bot commented Jun 9, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 34f1de8c-36aa-4245-9483-1cfdd879710f

📥 Commits

Reviewing files that changed from the base of the PR and between 14ccd26 and 2065653.

📒 Files selected for processing (3)
  • docs/en_US/release_notes_9_16.rst
  • web/pgadmin/utils/preferences.py
  • web/setup.py

Walkthrough

_Preference.set gains an optional user_id parameter to support non-request contexts. Preferences.save_cli is rewritten to look up module/category/preference by id and delegate to preference.set for type validation. ManagePreferences.set_prefs tracks invalid-value failures separately and reports them. A release note documents the fix.

Changes

CLI preference validation (Issue #7346)

Layer / File(s) Summary
_Preference.set user_id param and save_cli validation rewrite
web/pgadmin/utils/preferences.py
_Preference.set accepts an optional user_id argument and derives the target uid from it when present. Preferences.save_cli is replaced with a validated flow that performs structured lookups for the module, category (cid), and preference (pid), returning specific errors on failure, then delegates to preference.set(value, user_id=user_id) so all type parsing, range normalization, and validation are applied to CLI-supplied values.
set_prefs invalid-value reporting and release note
web/setup.py, docs/en_US/release_notes_9_16.rst
set_prefs initializes an invalid_value_prefs list; entries that exist but fail save_pref are appended to it rather than valid_prefs, and a dedicated "Invalid value provided for preference(s)" message is emitted when the list is non-empty. The release notes entry for Issue #7346 is added.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dpage dpage force-pushed the fix-7346-cli-prefs-validation branch 2 times, most recently from 49b4d77 to 5fb1856 Compare June 9, 2026 11:36
The CLI set-prefs path (save_pref -> Preferences.save_cli) wrote the raw
value to the configuration database without any type validation and
always reported success, unlike the GUI path which validates via
_Preference.set(). Route save_cli through the same set() validation
(set() now accepts an explicit user_id so it works outside a request
context), and make setup.py set-prefs check the result and report
preferences whose value was invalid.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@dpage dpage force-pushed the fix-7346-cli-prefs-validation branch from 5fb1856 to a868adb Compare June 9, 2026 11:37
@asheshv asheshv requested a review from Copilot June 10, 2026 14:03

This comment was marked as low quality.

@asheshv asheshv left a comment

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.

The threading of user_id through set() and routing save_cli via _Preference.set() is the right approach, but three issues:

  1. Critical — boolean prefs via --input-file will universally fail. setup.py:719 builds the value list via str(v). For native JSON booleans this produces 'True' / 'False' (capital T/F). save_pref only coerces lowercase ['true', 'false'], so the value reaches _Preference.set() as the string 'True' and trips assert isinstance(value, bool). Either normalize in save_pref (data['value'].lower() in ['true', 'false']) or stop stringifying booleans at the setup.py boundary.
  2. Error message discarded. res, _ = Preferences.save_cli(...) throws away the validator's diagnostic. Users see "invalid value" with no detail — the message should propagate up through save_pref and into setup.py's invalid_value_prefs report.
  3. No new tests for save_pref / save_cli with invalid integer, valid integer-as-string, boolean, unknown options value, or missing preference. The validation surface is currently untested.

Non-blocking observation (pre-existing): setup.py:741 uses opt.split("=")[1], which truncates any value containing = (URLs, regexes, base64). Worth a follow-up issue.

@asheshv asheshv merged commit 9c7bdd0 into pgadmin-org:master Jun 16, 2026
33 of 34 checks passed
asheshv added a commit that referenced this pull request Jun 17, 2026
…0105)

PR #10047 made `Preferences.save_cli()` validate against the registered
preference object, but `setup.py set-prefs` only enters `app_context()`
and never calls `run_before_app_start()`, leaving `Preferences.modules`
empty. Every CLI write then fell into the "Module 'X' is no longer in
use." path and was reported as "Invalid value provided".

Trigger registration explicitly (run_before_app_start enters both
app and test_request contexts internally, satisfying registration
callbacks that touch current_user) and propagate the typed error
message from save_cli so users see the actual reason a value was
rejected.
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.

Preferences should be validated in CLI mode.

3 participants