openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

Avatar endpoint returns broken image for accounts with no linked IA itemname

Open comaeclipse opened this issue 4 months ago • 4 comments

Problem

Patron avatars render as a broken image whenever the account has no linked Internet Archive internetarchive_itemname. The /people/<username>/avatar endpoint interpolates the literal Python None into the redirect URL, producing https://archive.org/services/img/None, which Internet Archive returns as 403 Forbidden.

This affects every place a patron avatar is shown for such accounts (list-follow cards on book pages, follows/followers pages, etc.). Example account: mykaelus.

Reproducing the bug

  1. Go to a page that renders a patron avatar for a user with no linked IA itemname, e.g. https://openlibrary.org/books/OL7950851M/Warcraft_World_of_Warcraft (the "Borrow Wishlist" list-follow card, owner @mykaelus)
  2. Alternatively, request the avatar endpoint directly: https://openlibrary.org/people/mykaelus/avatar
  3. Observe the network request / the rendered image
  • Expected behavior: a default/placeholder avatar image is served
  • Actual behavior: the endpoint returns 303 See Other → location: https://archive.org/services/img/None, which returns 403 Forbidden, so the browser shows a broken-image icon and logs avatar:1 Failed to load resource: the server responded with a status of 403
$ curl -sI https://openlibrary.org/people/mykaelus/avatar
HTTP/1.1 303 See Other
location: https://archive.org/services/img/None

$ curl -sI https://archive.org/services/img/None
HTTP/1.1 403 Forbidden

Context

  • Browser (Chrome, Safari, Firefox, etc): Any (reproduced in Brave/Chromium)
  • OS (Windows, Mac, etc): Any
  • Logged in (Y/N): N (not required)
  • Environment (prod, dev, local): prod

Breakdown

Implementation Details (for maintainers)

User.get_avatar_url() reads internetarchive_itemname from the account and unconditionally builds https://archive.org/services/img/{itemname}. When the account has no linked IA item, itemname is None, so the URL becomes .../img/None (a guaranteed 403). Notably the surrounding cache.memoize already anticipates this value via cacheable=lambda key, value: not value.endswith("/None"), but the function still returns the broken URL instead of a fallback.

Suggested fix: short-circuit when itemname is falsy instead of returning a URL that always 403s:

itemname = user.get_account().get("internetarchive_itemname")
if not itemname:
    return <default avatar>  # see note below
return f"https://archive.org/services/img/{itemname}"

Note on the default: there is currently no user-avatar placeholder asset in the repo. static/images/icons/ only contains avatar_author-lg.png, avatar_author.png, avatar_book-lg.png, avatar_book-sm.png, and avatar_book.png. So a fix needs either (a) a new user-avatar asset added and returned here, or (b) reuse of an existing asset — maintainers to decide which. No onerror fallback exists on any of the <img> tags today either, so adding one in the templates below would be a defense-in-depth complement.

Requirements Checklist

  • [ ] get_avatar_url returns a default avatar when no internetarchive_itemname is linked
  • [ ] Broken-image icon no longer appears for such accounts

Related files

  • openlibrary/core/models.py — User.get_avatar_url() (interpolates None)
  • openlibrary/plugins/upstream/mybooks.py — avatar route (/people/([^/]+)/avatar)
  • openlibrary/templates/lists/list_follow.html — renders <img src="$(owner.key)/avatar"> with no fallback

Stakeholders


Instructions for Contributors

  • Please run these commands to ensure your repository is up to date before creating a new branch to work on this issue and each time after pushing code to Github, because the pre-commit bot may add commits to your PRs upstream.

comaeclipse avatar Aug 03 '26 13:08 comaeclipse

Thank you @comaeclipse for submitting this issue!

Context

  • Confirmed in code: User.get_avatar_url() unconditionally builds the URL from internetarchive_itemname with no fallback for a falsy value: https://github.com/internetarchive/openlibrary/blob/927a1c95a38ff4510192745fcc7858819b18cf5c/openlibrary/core/models.py#L1011-L1023 The avatar route just redirects to whatever this returns: https://github.com/internetarchive/openlibrary/blob/927a1c95a38ff4510192745fcc7858819b18cf5c/openlibrary/plugins/upstream/mybooks.py#L47-L52
  • The cacheable=lambda key, value: not value.endswith("/None") guard on line 1016 was added by #12355 (merged 2026-04-22), which restored/fixed caching on this method to close #9282 (a performance issue about /followers fetching every user object). That work anticipated the /None case only to keep it from being cached — it didn't fix the underlying redirect, so the bug this issue describes has been live since at least that change.
  • Confirmed the issue's claim about missing assets: static/images/icons/ contains only avatar_author-lg.png, avatar_author.png, avatar_book-lg.png, avatar_book-sm.png, avatar_book.png — no generic/user-silhouette placeholder exists anywhere in static/images or the CSS icon sets today.
  • The codebase's existing convention for missing images is a server-side fallback computed before render (e.g. cover_url or "/static/images/icons/avatar_book-sm.png" in lists/preview.html, lists/home.html, etc.), not a client-side onerror handler — so fixing this inside get_avatar_url() (as proposed in the issue) is consistent with how the rest of the app handles missing images, rather than the onerror defense-in-depth idea also mentioned in the issue.
  • Other call sites that would benefit from this fix: templates/account/follows.html (follower/following lists) and templates/account/view.html (breadcrumb avatar), in addition to the list_follow.html card already identified.

Issue Assessment

Open questions

  • [x] Does a default/placeholder avatar asset already exist to reuse? (Answer: No — confirmed by inspecting static/images/icons/; only book/author placeholders exist. A new asset (or an explicit maintainer/design decision to repurpose an existing one, e.g. avatar_author-lg.png) is needed before this can be closed.)
  • [ ] Which image should be used as the default avatar — a new dedicated asset, or reuse of an existing icon? This is a design call for a maintainer/lead rather than something resolvable from the code alone.

Action items

  • [ ] Decide on the default avatar asset (new file vs. reuse) so a contributor can implement the short-circuit in get_avatar_url().

🔔 Stakeholders

@jimchamp — for visibility, since this touches the My Books / bookshelf-adjacent avatar rendering path (mybooks.py, list_follow.html, account/follows.html).

Full triage checklist
  • [x] Triage — correct labels applied or removed
  • [x] Problem / opportunity — problem is clear, audience identified, actionable
  • [x] Scope — issue is well-scoped; does not try to do too many things
  • [x] Justification — reasoning and impact stated; sufficient for leads to prioritize
  • [x] Success & testing criteria — defines what "done" looks like, verifiable by a contributor
  • [x] Proposal & breakdown — concrete plan present; checklist actionable for a contributor
  • [x] If bug: environment & repro steps — browser, version, local vs prod; steps to reproduce
  • [x] Risks, concerns, open questions — identified and surfaced
  • [x] Richy context — relevant code, commits, PRs reviewed; no duplicate found
  • [x] References — screenshots, designs, docs, and links present where needed

[!NOTE] This comment was automatically generated by Richy, Open Library's issue enrichment assistant, on behalf of @mekarpeles. This is NOT a sign-off. This issue needs lead review before work begins.

openlibrary-bot avatar Aug 03 '26 13:08 openlibrary-bot

I suppose we could fallback to https://github.com/internetarchive/openlibrary/blob/master/static/images/icons/avatar_author.png

mekarpeles avatar Aug 04 '26 03:08 mekarpeles

I'd like to work on this issue. I understand that User.get_avatar_url() currently interpolates a missing internetarchive_itemname into /services/img/None, so the avatar route redirects to a guaranteed 403.

Following the maintainer's decision above, I plan to:

  • return /static/images/icons/avatar_author.png when the linked IA item name is missing;
  • preserve the existing archive.org avatar URL for linked accounts;
  • update the memoization predicate if the fallback makes the /None special case obsolete;
  • add focused model tests covering both missing and present item names.

The expected code scope is openlibrary/core/models.py and its corresponding tests. Could you please assign this issue to me?

richardmilles avatar Aug 07 '26 13:08 richardmilles

Hi @richardmilles, are you still working on this? If not, I'd like to pick it up.

VanshajPoonia avatar Nov 18 '26 18:11 VanshajPoonia