Repository navigation
Set the client IP for Infobase writes from FastAPI login and anonymize - #14589
Merged
Merged
Conversation
Logging in with Internet Archive credentials creates or activates the
Open Library account when needed. Activation saves the /people profile,
and Infobase stamps that transaction with web.ctx.ip, a Postgres inet
column. web.py sets web.ctx.ip on every request; FastAPI never does, so
the infogami client sent an empty X-REMOTE-IP and Postgres rejected the
row ("invalid input syntax for type inet"), failing those logins.
Wrap the audit_accounts calls in /account/login (form and JSON) and the
anonymize call in /account/anonymize.json in web_ctx_ip(client_ip), as
link, merge_authors and importapi already do.
Fixes OL-INFOBASE-9VA
openlibrary-bot
requested changes
Nov 19, 2026
openlibrary-bot
left a comment
Collaborator
There was a problem hiding this comment.
No blocking findings · 1 suggestion on bde9177
Important
Part of this review was held back because it may be security-sensitive. A maintainer needs to look at this PR before it can merge.
Suggestions (won't block)
- No test for web_ctx_ip in anonymize_account ·
openlibrary/fastapi/account.py:600
The two new tests only cover login()/_login_json()'s use of web_ctx_ip. anonymize_account also wraps an Infobase-writing call (ol_account.anonymize) inwith RunAs(...), web_ctx_ip(client_ip):, but none of the existing TestAnonymizeAccount tests assert that web.ctx.ip is set during that call, the way test_login_sets_client_ip_for_infobase_writes does for login. A mock onol_account.anonymizethat records web.ctx.get("ip") would catch a regression here.
Prompt for AI agents (1 open item)
Check if each issue is valid — if so, understand the root cause and fix it. If one isn't valid or won't be fixed in this PR, say why in its thread, or in a comment on the PR if it has none.
<file name="openlibrary/fastapi/account.py">
<item location="600">No test for web_ctx_ip in anonymize_account: The two new tests only cover login()/_login_json()'s use of web_ctx_ip. anonymize_account also wraps an Infobase-writing call (`ol_account.anonymize`) in `with RunAs(...), web_ctx_ip(client_ip):`, but none of the existing TestAnonymizeAccount tests assert that web.ctx.ip is set during that call, the way test_login_sets_client_ip_for_infobase_writes does for login. A mock on `ol_account.anonymize` that records web.ctx.get("ip") would catch a regression here.</item>
</file>
Please address the feedback above before a maintainer reviews this PR · How this bot works · Reviews new pushes after 10 minutes
web.ctx.ip is thread-local, and FastAPI runs every coroutine on one event-loop thread. Setting it around the whole awaited anonymize() kept it set while _anonymize_tables awaited. anonymize() now takes the client IP and sets it only around the synchronous _delete_infobase_data step, the one that writes to Infobase. Tests cover that the IP is set there and cleared before the awaits, and that the route passes it in.
openlibrary-bot
previously requested changes
Nov 19, 2026
openlibrary-bot
left a comment
Collaborator
There was a problem hiding this comment.
No issues found on c17d20c.
Re-reviewed only the files changed since my last review, skipping merges of the base branch.
A maintainer will review this PR next · How this bot works
marley
pushed a commit
to marley/openlibrary
that referenced
this pull request
Nov 19, 2026
internetarchive#14589) * Set the client IP for Infobase writes from FastAPI login and anonymize Logging in with Internet Archive credentials creates or activates the Open Library account when needed. Activation saves the /people profile, and Infobase stamps that transaction with web.ctx.ip, a Postgres inet column. web.py sets web.ctx.ip on every request; FastAPI never does, so the infogami client sent an empty X-REMOTE-IP and Postgres rejected the row ("invalid input syntax for type inet"), failing those logins. Wrap the audit_accounts calls in /account/login (form and JSON) and the anonymize call in /account/anonymize.json in web_ctx_ip(client_ip), as link, merge_authors and importapi already do. Fixes OL-INFOBASE-9VA * Set the anonymize client IP only around the Infobase delete web.ctx.ip is thread-local, and FastAPI runs every coroutine on one event-loop thread. Setting it around the whole awaited anonymize() kept it set while _anonymize_tables awaited. anonymize() now takes the client IP and sets it only around the synchronous _delete_infobase_data step, the one that writes to Infobase. Tests cover that the IP is set there and cleared before the awaits, and that the route passes it in.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
hotfix: logging in with Internet Archive credentials fails when the Open Library account has to be created or activated. Now that production login requests reach the FastAPI
/account/loginroute, Infobase rejects the write withinvalid input syntax for type inet: ""(Sentry OL-INFOBASE-9VA, ~1.3k events, 20 users since 2026-11-18).Technical
audit_accounts()creates or activates the OL account when needed. Activation saves the/people/<username>profile, and Infobase stamps that transaction withweb.ctx.ip(a Postgresinetcolumn), which the infogami client sends asX-REMOTE-IP. web.py setsweb.ctx.ipon every request; FastAPI never does, so the header went out empty.This wraps the
audit_accountscalls in/account/login(form and JSON body) and theanonymizecall in/account/anonymize.jsoninweb_ctx_ip(client_ip), the same patternlink.py,merge_authors.pyandimportapi.pyalready use. The two endpoints otherwise already share all their logic with web.py.Testing
openlibrary/tests/fastapi/test_account.pycheck thatweb.ctx.ipis the client IP whileaudit_accountsruns, for the form and JSON login paths. They fail without the fix.activate_accountwith noweb.ctx.ipreturns a 500 with the sameinet ""error as production; insideweb_ctx_ip(...)it succeeds and creates/people/<username>.Stakeholders
@mekarpeles