openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

Saving user preferences should validate keys

Open mekarpeles opened this issue 8 years ago • 6 comments

Currently, there's a risk of other GET params being saved into the user's preferences, like:

debug: true
save: 
has_fulltext: true

mekarpeles avatar Mar 07 '18 21:03 mekarpeles

Related to #740 feature

mekarpeles avatar Mar 07 '18 21:03 mekarpeles

@mekarpeles Is this still an issue? The reference issue is closed but obviously this one isn't. It's a high priority so it should be scheduled if it isn't already.

xayhewalo avatar Oct 18 '19 22:10 xayhewalo

FWIW, I was able to reproduce this.

SouthGoingZax avatar Oct 16 '20 16:10 SouthGoingZax

Hi @mekarpeles — I can confirm this is still live on master, and I'd like to take it since the previous volunteer went quiet.

Both account_privacy.POST and account_notifications.POST in openlibrary/plugins/upstream/account.py hand the whole web.input() to user.save_preferences(), so any extra form/query param (save, debug, has_fulltext, …) ends up persisted in the user's preferences doc — the classic mass-assignment shape.

Plan: in each handler, build an explicit dict with only the keys that form owns — {"updates"} for notifications, {"public_readlog", "safe_mode"} for privacy — and pass that to save_preferences. I'd leave save_preferences itself untouched: other callers (yrg_banner_pref, pda, rpd) rely on it accepting arbitrary keys, and narrowing it would widen the blast radius of this fix. Two unit tests in test_account.py assert stray params are dropped. It's ~7 lines plus tests, already green locally, so I can open the PR the moment it's assigned.

Any reason not to go with the handler-level fix rather than a global whitelist in save_preferences?

(Full disclosure: I used an AI assistant to help trace the call sites; the analysis, patch and tests are mine and I've read every line.)

edubraqd avatar Sep 12 '26 18:09 edubraqd

It looks like @jabrailkhalil may already have a PR for this?

mekarpeles avatar Sep 13 '26 21:09 mekarpeles

You're right, #13598 (opened Sept 10) covers it and I missed it when I asked — withdrawing. Happy to review it if that helps.

edubraqd avatar Sep 13 '26 23:09 edubraqd