feat(accounts): require valid IA S3 keys on OTP service endpoints#12841
Open
mekarpeles wants to merge 4 commits into
Open
feat(accounts): require valid IA S3 keys on OTP service endpoints#12841mekarpeles wants to merge 4 commits into
mekarpeles wants to merge 4 commits into
Conversation
Adds Authorization: LOW <access>:<secret> validation to /account/otp/issue and /account/otp/redeem. Callers (Lenny instances) must supply valid IA S3 credentials — validated via s3auth — before OTP emails are issued or redeemed. Lenny already sends ol_auth_headers() with every OTP request; OL was previously ignoring them. This closes the gap so only Lenny nodes that have been configured with a linked IA/OL account can trigger OTP emails. Closes #12840
for more information, see https://pre-commit.ci
Contributor
There was a problem hiding this comment.
Pull request overview
This PR tightens access control for the legacy web.py OTP service endpoints (POST /account/otp/issue and POST /account/otp/redeem) by requiring callers to present valid IA S3 credentials via an Authorization: LOW access:secret header (intended to restrict OTP issuance/redemption to authorized Lenny instances).
Changes:
- Adds a module-level
_parse_low_auth_header()helper to parse theAuthorization: LOW ...header. - Validates parsed credentials via
InternetArchiveAccount.s3auth()at the start of both OTP endpoints. - Returns JSON error payloads for missing/malformed auth headers and invalid credentials.
…outages; add tests - _parse_low_auth_header(): strip both parts and raise ValueError if either is empty (catches 'LOW access:' or 'LOW :secret') - Extract _require_s3_auth() helper used by both OTP handlers; returns a distinct error when s3auth responds with a 5xx (auth_service_unavailable) vs invalid credentials (unauthorized) - Add TestOtpServiceS3Auth: tests for missing header, empty secret, invalid keys, 5xx outage, and valid keys proceeding to OTP issue/redeem Addresses Copilot review threads on #12841
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.
Summary
Closes #12840
The OTP service endpoints (
POST /account/otp/issueand/account/otp/redeem) are used by Lenny to implement patron passwordless login. Lenny already sendsAuthorization: LOW <access>:<secret>headers on every OTP call (viaol_auth_headers()), but OL was silently ignoring them.This PR adds validation: both endpoints now require a valid IA S3
Authorization: LOWheader before processing the request. Invalid or missing credentials return{"error": "unauthorized"}.Changes
openlibrary/plugins/upstream/account.py_parse_low_auth_header()helper — parsesAuthorization: LOW access:secretfrom the WSGI env (same pattern as the existingaccount_anonymize._parse_auth_header()instance method)otp_service_issue.POST()— validate S3 keys viaInternetArchiveAccount.s3auth()before issuing OTPotp_service_redeem.POST()— same validation before redeeming OTPWhy This Matters
Without this, any HTTP client can hit
/account/otp/issueand cause OL to send unsolicited OTP emails to arbitrary addresses. Requiring valid IA S3 keys ties OTP access to Lenny instances that have been explicitly linked to an IA account viamake ol-login.Companion PR
Test Plan
Authorizationheader →{"error": "missing_or_invalid_authorization"}{"error": "unauthorized"}/internal/fake/s3auth) acceptsfoo:foo, so existing local tests are unaffectedmake testpasses