Skip to content

Set the client IP for Infobase writes from FastAPI login and anonymize - #14589

Merged
RayBB merged 2 commits into
masterfrom
fix/fastapi-login-client-ip
Nov 19, 2026
Merged

RayBB merged 2 commits into
masterfrom
fix/fastapi-login-client-ip

Conversation

@RayBB

@RayBB RayBB commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

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/login route, Infobase rejects the write with invalid 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 with web.ctx.ip (a Postgres inet column), which the infogami client sends as X-REMOTE-IP. web.py sets web.ctx.ip on every request; FastAPI never does, so the header went out empty.

This wraps the audit_accounts calls in /account/login (form and JSON body) and the anonymize call in /account/anonymize.json in web_ctx_ip(client_ip), the same pattern link.py, merge_authors.py and importapi.py already use. The two endpoints otherwise already share all their logic with web.py.

Testing

  • New tests in openlibrary/tests/fastapi/test_account.py check that web.ctx.ip is the client IP while audit_accounts runs, for the form and JSON login paths. They fail without the fix.
  • Reproduced against a local Postgres + Infobase: registering a pending account and calling activate_account with no web.ctx.ip returns a 500 with the same inet "" error as production; inside web_ctx_ip(...) it succeeds and creates /people/<username>.

Stakeholders

@mekarpeles

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
openlibrary-bot requested a review from a team November 19, 2026 06:17

@openlibrary-bot openlibrary-bot 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.

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) 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.
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
openlibrary-bot requested a review from a team November 19, 2026 06:40

@openlibrary-bot openlibrary-bot 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.

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

@RayBB
RayBB dismissed openlibrary-bot’s stale review November 19, 2026 06:43

checked and it's fine

@RayBB
RayBB merged commit f845602 into master Nov 19, 2026
19 of 20 checks passed
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.
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.

2 participants