Skip to content

refactor: pass request params as args to is_spam and Recaptcha (#13538) - #13611

Merged
RayBB merged 1 commit into
internetarchive:masterfrom
csy20:13538/refactor/spam-recaptcha-params
Sep 17, 2026
Merged

RayBB merged 1 commit into
internetarchive:masterfrom
csy20:13538/refactor/spam-recaptcha-params

Conversation

@csy20

@csy20 csy20 commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Closes #13538

Part of #13536. Thread spam and captcha request data as explicit arguments so these helpers no longer hide web.input() / web.ctx.ip reads. Same behavior.

This is a second attempt after #13547. That PR renamed Recaptcha.validate so web.py forms passed the empty field value in as the Google token, which would fail every signup when reCAPTCHA is on. Ray's review asked to keep validate(self, value=None) as the form shim.

Technical

  • spamcheck.is_spam(i, ...) now requires i. The web.input() fallback is gone.
  • book_edit.POST / work_edit.POST pass web.input() (full POST), not the GET-only i.
  • edit.POST for people pages passes web.input().
  • Recaptcha.validate(self, value=None) stays registered as the web.form.Validator callback (signup /account/create).
  • New Recaptcha.validate_response(response, remoteip) does the Google call with no web globals. Add/edit book call that.

Testing

  • ruff check / ruff format on the four files.
  • python3.14 -m py_compile on the four files.
  • Did not add tests (issue asks to run existing ones only). GitHub Actions should run make test-py-uv.
  • Signup path is unchanged: still Validator(..., self.validate), which still reads g-recaptcha-response from the request. I do not have recaptcha keys in local docker, so I could not screenshot a live signup.

Screenshot

N/A — no UI change. Recaptcha markup is the same.

Stakeholders

@RayBB

…netarchive#13538)

Require caller-provided form data in is_spam. Keep Recaptcha.validate as
the web.py form shim so signup still works, and move the Google check to
validate_response for add/edit book.
@openlibrary-bot

Copy link
Copy Markdown
Collaborator

Thanks for the PR, @csy20 — and welcome, this looks like your first contribution to Open Library! 🎉

🤖 A reviewer has been assigned for an initial pass.

PR triage checklist (maintainers / Pierre)
  • PR description — not empty; explains what the change does and how to verify it
  • References an issue — PR body contains a #NNN reference
    • Linked issue is triaged — has a Priority: * label (not just Needs: Triage)
    • Linked issue is assigned — has at least one assignee
  • Commit history clean — no WIP/fixup/conflict noise; commit messages are meaningful
  • CI passing — no failing check-runs
  • Test cases present — if the change touches substantive logic, test coverage exists or is explained
  • Proof of testing — PR body includes a description of what was tested, a screenshot, or a video

Note

This comment was automatically generated by Pierre, Open Library's PR first-touch assistant, on behalf of @mekarpeles. This is NOT a code review or sign-off.

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.

🟡 Changes recommended

addbook.py rereads the request body before saving, which can result in submitted fields not being saved.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Refactors spam and reCAPTCHA helpers to receive request data explicitly while preserving signup validation behavior.

Changes:

  • Requires explicit input for spam checks.
  • Passes form data and client IP from edit handlers.
  • Adds explicit reCAPTCHA response validation.
File summaries
File Summary
openlibrary/plugins/upstream/spamcheck.py Removes the implicit request-input fallback.
openlibrary/plugins/upstream/code.py Passes people-page POST data explicitly.
openlibrary/plugins/upstream/addbook.py Passes form data to spam/reCAPTCHA checks; the save path must reuse parsed form data.
openlibrary/plugins/recaptcha/recaptcha.py Adds explicit validation while preserving the signup form shim.
Review details

Suppressed comments (1)

openlibrary/plugins/upstream/addbook.py:887

  • This parses the full POST body, but the handler still calls helper.save(web.input()) below. A second read can see an empty body because full web.input() parsing drains wsgi.input (as documented in openlibrary/utils/request_context.py), so work edits may pass these checks and then save no submitted fields. Reuse form_data for the save.
        recap = get_recaptcha()

        if recap and not recap.validate_response(form_data.get("g-recaptcha-response"), web.ctx.ip):
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread openlibrary/plugins/upstream/addbook.py Resolved
@RayBB RayBB self-assigned this Sep 12, 2026
@github-project-automation github-project-automation Bot moved this to Waiting Review/Merge from Staff in Ray's Project Sep 12, 2026

@RayBB RayBB 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 for this it looks great and is a step in the direction we need to go for fastapi.

Tested it locally pretty thoroughly and put it on testing and it it seems solid.

@RayBB
RayBB merged commit 4faf170 into internetarchive:master Sep 17, 2026
7 checks passed
@github-project-automation github-project-automation Bot moved this from Waiting Review/Merge from Staff to Done in Ray's Project Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Thread spam and captcha params: is_spam, Recaptcha.validate

4 participants