Skip to content

feat(accounts): require valid IA S3 keys on OTP service endpoints#12841

Merged
mekarpeles merged 5 commits into
masterfrom
12840/otp-require-s3-auth
Jun 25, 2026
Merged

feat(accounts): require valid IA S3 keys on OTP service endpoints#12841
mekarpeles merged 5 commits into
masterfrom
12840/otp-require-s3-auth

Conversation

@mekarpeles

Copy link
Copy Markdown
Member

Summary

Closes #12840

The OTP service endpoints (POST /account/otp/issue and /account/otp/redeem) are used by Lenny to implement patron passwordless login. Lenny already sends Authorization: LOW <access>:<secret> headers on every OTP call (via ol_auth_headers()), but OL was silently ignoring them.

This PR adds validation: both endpoints now require a valid IA S3 Authorization: LOW header before processing the request. Invalid or missing credentials return {"error": "unauthorized"}.

Changes

openlibrary/plugins/upstream/account.py

  • New module-level _parse_low_auth_header() helper — parses Authorization: LOW access:secret from the WSGI env (same pattern as the existing account_anonymize._parse_auth_header() instance method)
  • otp_service_issue.POST() — validate S3 keys via InternetArchiveAccount.s3auth() before issuing OTP
  • otp_service_redeem.POST() — same validation before redeeming OTP

Why This Matters

Without this, any HTTP client can hit /account/otp/issue and 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 via make ol-login.

Companion PR

Test Plan

  • Request without Authorization header → {"error": "missing_or_invalid_authorization"}
  • Request with invalid S3 keys → {"error": "unauthorized"}
  • Request with valid S3 keys → OTP issued/redeemed normally
  • Local dev: fake S3 auth endpoint (/internal/fake/s3auth) accepts foo:foo, so existing local tests are unaffected
  • make test passes

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.

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 the Authorization: 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.

Comment thread openlibrary/plugins/upstream/account.py Outdated
Comment thread openlibrary/plugins/upstream/account.py Outdated
Comment thread openlibrary/plugins/upstream/account.py Outdated
Comment thread openlibrary/plugins/upstream/account.py Outdated
Comment thread openlibrary/plugins/upstream/account.py Outdated
mekarpeles added a commit that referenced this pull request Jun 2, 2026
…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
@mekarpeles mekarpeles added the Priority: 2 Important, as time permits. [managed] label Jun 8, 2026
mekarpeles added a commit that referenced this pull request Jun 15, 2026
…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
@mekarpeles mekarpeles force-pushed the 12840/otp-require-s3-auth branch from be20661 to 78df9fc Compare June 15, 2026 00:07
@mekarpeles

Copy link
Copy Markdown
Member Author

Folded 1 pre-commit.ci bot commit into its parent (feat(accounts): require valid IA S3 keys on OTP service endpoints). History is now 3 real commits, no noise.

Merge conflict still present in openlibrary/plugins/upstream/tests/test_account.py: master added a new TestAccountLoginRedirect class at the same insertion point where this PR adds TestOtpServiceS3Auth. The resolution is to keep both classes, but since this involves test code I'm flagging it for Mek rather than auto-resolving.

To reverse bot-fold: git push --force-with-lease origin be20661cb:12840/otp-require-s3-auth

— Pierre, via PAM (Open Library's Project AI Manager)

mekarpeles added a commit that referenced this pull request Jun 24, 2026
…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
@mekarpeles mekarpeles force-pushed the 12840/otp-require-s3-auth branch from 78df9fc to 387d0ea Compare June 24, 2026 07:46
@openlibrary-bot

Copy link
Copy Markdown
Collaborator

Hi — I'm Pierre, an AI-assisted PR tidier running on behalf of @mekarpeles.

I rebased this branch onto the latest master (branch was CONFLICTING).

What was done: rebased 12840/otp-require-s3-auth onto origin/master — all 3 commits applied cleanly with no conflicts.

Why: keeps the branch mergeable and avoids drift from master.

Safeguards: confirmed all 3 commits are authored by @mekarpeles; 0 unresolved review threads before pushing; used git push --force-with-lease.

To reverse: run git reflog locally to find the SHA before this push (78df9fc55), then git reset --hard <sha> and git push --force-with-lease.

@github-actions github-actions Bot added the Needs: Response Issues which require feedback from lead label Jun 24, 2026
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
…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
Adds direct unit tests for the new _parse_low_auth_header() helper
(missing header, wrong prefix, no colon, empty parts, padded whitespace,
colon-in-secret preservation) and a missing happy-path test for the
redeem endpoint that mirrors the existing issue test.
@mekarpeles mekarpeles force-pushed the 12840/otp-require-s3-auth branch from b2bd697 to 292ea22 Compare June 25, 2026 06:27
@openlibrary-bot

Copy link
Copy Markdown
Collaborator

Code smell — _parse_auth_header / _parse_low_auth_header duplication

account_anonymize._parse_auth_header() (instance method, ~line 1412) and the new _parse_low_auth_header() (module-level function) serve the same purpose. The new one is strictly better — it validates empty parts and uses a more informative ValueError message. The old one silently returns empty strings when the key part is missing.

Suggested follow-up (not blocking this PR):

  1. Have account_anonymize call _parse_low_auth_header() directly and remove _parse_auth_header.
  2. Or rename the module-level function to _parse_low_auth_header_parse_s3_auth_header to signal it's the canonical version for any future callers.

Leaving as a non-blocking note — the existing PR logic is correct as written.

— PAM (Open Library's Project AI Manager), on behalf of @mekarpeles

@openlibrary-bot

openlibrary-bot commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator

E2E Testing — auth gate + OTP flow

This comment was authored by Leno, via PAM (Open Library's Project AI Manager), on behalf of @mekarpeles.


Setup

  • Branch: 12840/otp-require-s3-auth
  • Docker: OL dev stack on port 8081 (compose.yaml + custom compose override, project openlibrary-12840-otp-s3-auth)
  • S3 auth: LOW foo:foo validated against /internal/fake/s3auth (dev fake endpoint)

Required before running: add otp_seed to conf/openlibrary.yml — it is absent from the dev config and OTP.generate() crashes with AttributeError: 'NoneType' object has no attribute 'encode' without it. This is a pre-existing gap in dev config, not introduced by this PR.

# conf/openlibrary.yml — add alongside ia_s3_auth_url
otp_seed: dev-otp-seed-for-e2e-testing

Auth gate — rejection paths

# No Authorization header → rejected
curl -s -X POST "http://localhost:8081/account/otp/issue" \
  -H "X-Forwarded-For: 10.0.0.8" \
  -d "email=test@example.com&ip=10.0.0.8&sendmail=false"
# {"error": "missing_or_invalid_authorization"}

# Wrong prefix → rejected
curl -s -X POST "http://localhost:8081/account/otp/issue" \
  -H "Authorization: Bearer sometoken" \
  -H "X-Forwarded-For: 10.0.0.8" \
  -d "email=test@example.com&ip=10.0.0.8&sendmail=false"
# {"error": "missing_or_invalid_authorization"}

# Same rejections on /account/otp/redeem — verified ✓

Happy path — issue + redeem

# Step 1: issue OTP
curl -s -X POST "http://localhost:8081/account/otp/issue" \
  -H "Authorization: LOW foo:foo" \
  -H "X-Forwarded-For: 10.0.3.1" \
  -d "email=test-e2e@example.com&ip=10.0.3.1&sendmail=false"
# {"success": "issued"}

# Step 2: compute OTP locally (same HMAC as OL uses)
python3 -c "
import hmac, hashlib, string, time
def shorten(d, n=6):
    alph = string.digits + string.ascii_uppercase
    n2, b = int.from_bytes(d, 'big'), ''
    while n2 > 0:
        n2, i = divmod(n2, 36); b = alph[i] + b
    return b[:n].lower()
seed, sip, email, ip = 'dev-otp-seed-for-e2e-testing', '10.0.3.1', 'test-e2e@example.com', '10.0.3.1'
ts = int(time.time() // 60)
payload = f'{sip}:{email}:{ip}:{ts}'.encode()
print(shorten(hmac.new(seed.encode(), payload, hashlib.sha256).digest()))
"
# e.g. 5sp17k

# Step 3: redeem
curl -s -X POST "http://localhost:8081/account/otp/redeem" \
  -H "Authorization: LOW foo:foo" \
  -H "X-Forwarded-For: 10.0.3.1" \
  -d "email=test-e2e@example.com&ip=10.0.3.1&otp=5sp17k"
# {"success": "redeemed"}

Lenny integration

Lenny's OTP.issue() sends Authorization: LOW {OL_S3_ACCESS_KEY}:{OL_S3_SECRET_KEY} as a header with email + ip as query params. Verified this exact call pattern against the PR branch — auth gate accepts it and the request proceeds to OTP generation.

To point a local Lenny instance at a dev OL stack:

OTP_SERVER=http://localhost:8081 OL_S3_ACCESS_KEY=foo OL_S3_SECRET_KEY=foo LENNY_LENDING_ENABLED=true ...

Unit tests

15/15 passing in Docker:

  • Added TestParseLowAuthHeader (7 tests) — direct coverage of _parse_low_auth_header(): missing header, wrong prefix, no colon, empty access, empty secret, padded values stripped, colon-in-secret preserved
  • Added test_redeem_valid_keys_proceeds — symmetric happy-path test for redeem (issue already had one)
docker compose run --rm home python -m pytest openlibrary/plugins/upstream/tests/test_account.py::TestOtpServiceS3Auth openlibrary/plugins/upstream/tests/test_account.py::TestParseLowAuthHeader -v

CI: all checks green (pre-commit.ci, python_tests, CodeQL).


Known dev-only gaps (not PR bugs)

  1. otp_seed absent from dev config — see setup note above. PR might warrant an assertion or startup warning if config.get("otp_seed") is None.

  2. /internal/fake/s3auth does not return {"error": ...} for invalid keys — returns {"authorized": False} instead, so _require_s3_auth()'s if "error" in result check doesn't fire for bad dev keys. Production behavior (real IA s3auth) is correct. This only affects dev testing of the rejection-with-bad-keys path.

  3. Lenny does not pass sendmail=false — OL defaults sendmail=true, so Lenny OTP calls in dev will reach SMTP and 500 after the auth gate passes. Not a gate bug; a dev-env SMTP gap. In production SMTP is configured and this is a non-issue.


Automated test script

All of the above is reproducible with a standalone stdlib-only script (no dependencies beyond Python 3):

https://gist.github.com/mekarpeles/c615ee72e807aa67d9f8df72359531ea

python3 test_otp_auth_gate.py http://localhost:8080 dev-otp-seed-for-e2e-testing foo foo

Leno, via PAM (Open Library's Project AI Manager)

@mekarpeles mekarpeles removed the Needs: Response Issues which require feedback from lead label Jun 25, 2026
OTP.generate() reads config.get('otp_seed') and crashes with
AttributeError if absent. Production has a real seed configured;
dev config was missing it entirely, breaking any local OTP testing.

Adds a placeholder value so the endpoint is exercisable in local
Docker. Documented in E2E test script (test_otp_auth_gate.py).
@github-actions github-actions Bot added the Needs: Response Issues which require feedback from lead label Jun 25, 2026
@mekarpeles mekarpeles removed the Needs: Response Issues which require feedback from lead label Jun 25, 2026
@mekarpeles mekarpeles merged commit d779e2c into master Jun 25, 2026
8 checks passed
@mekarpeles mekarpeles deleted the 12840/otp-require-s3-auth branch June 25, 2026 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Priority: 2 Important, as time permits. [managed]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(accounts): Validate caller IA S3 keys on OTP service endpoints to restrict access to authorized Lenny instances

4 participants