Skip to content

fix(models): only save known user preference keys - #13598

Merged
jimchamp merged 2 commits into
internetarchive:masterfrom
jabrailkhalil:832/fix/preference-key-validation
Sep 19, 2026
Merged

jimchamp merged 2 commits into
internetarchive:masterfrom
jabrailkhalil:832/fix/preference-key-validation

Conversation

@jabrailkhalil

@jabrailkhalil jabrailkhalil commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Closes #832

Summary

account_privacy and account_notifications POST handlers save web.input() (the full form/query payload) into the user's preferences document via User.save_preferences. As #832 reports, that persists junk keys such as the save submit button, debug, or has_fulltext into /people/{user}/preferences.

This PR centralizes key validation in User.save_preferences: only keys the codebase actually reads or writes are merged into stored preferences; everything else is dropped.

Whitelist (from usages across the repo):

  • public_readlog, safe_mode - privacy form
  • updates, update, notify - notifications form and legacy defaults
  • pda, rpd - print-disability helpers (OpenLibraryAccount.update_pd)
  • yrg_banner_pref - banner dismissal persistence

type is managed internally and is forced to preferences after the merge, so it is intentionally not in the whitelist.

Behavior

Unchanged for all legitimate flows: both forms submit only whitelisted fields, and backend callers (update_pd, hide-banner endpoint) use whitelisted keys. Unknown keys posted by clients are no longer persisted.

Testing

  • New unit tests in openlibrary/plugins/upstream/tests/test_models.py:
    • test_save_preferences_ignores_unknown_keys - reproduces the issue payload (save, debug, has_fulltext); fails without the fix, passes with it.
    • test_save_preferences_keeps_known_keys - guards the whitelisted keys.
  • pytest openlibrary/plugins/upstream/tests/test_models.py openlibrary/tests/fastapi/test_hide_banner.py openlibrary/tests/fastapi/test_account.py: my new tests plus all hide-banner/account endpoint tests pass (54 passed, 1 xfailed when run with the related fastapi files; test_models.py standalone: 11 passed with 2 pre-existing ContextVar-ordering failures in TestModels that also fail on unmodified master in a standalone run).
  • ruff check + ruff format, codespell, auto-walrus, mypy on the changed files: clean.

Pre-commit hooks were not run as a whole (no Python 3.14 on PATH on this machine); equivalent individual hooks were run directly.

The account privacy and notifications forms POST the full web.input()
payload into save_preferences, so arbitrary query/form params (like the
submit button 'save', or 'debug'/'has_fulltext') were persisted to the
user's preferences document.

Whitelist the preference keys that are actually read or written by the
codebase and drop everything else before merging into the stored
preferences.
Copilot AI lite review requested due to automatic review settings September 10, 2026 17:43

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@jabrailkhalil

Copy link
Copy Markdown
Contributor Author

The CI workflows for the current head a4923c28db are waiting for contributor workflow approval (action_required): python_tests. Could a maintainer approve the pending runs when convenient? These runs have not executed their checks yet; I will address any failures once they run.

@github-actions github-actions Bot added the Needs: Response Issues which require feedback from lead label Sep 14, 2026

@jimchamp jimchamp 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.

Thanks. @jabrailkhalil!

Noting that this will not remove any "junk" keys that may have been persisted prior to this change. Staff could potentially clean this up by updating preferences in the store and store_index tables directly (they should take care not to delete/modify the type, _key, and _rev fields in the record).

@jimchamp
jimchamp merged commit 94681cb into internetarchive:master Sep 19, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs: Lead Needs: Response Issues which require feedback from lead

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Saving user preferences should validate keys

3 participants