Skip to content

auth: key the peer rate-limit bucket on the trusted proxy address - #3504

Merged
Birfy merged 1 commit into
mainfrom
dev/3502-auth-peer-limit-behind-edge
Dec 3, 2026
Merged

Birfy merged 1 commit into
mainfrom
dev/3502-auth-peer-limit-behind-edge

Conversation

@Birfy

@Birfy Birfy commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

What this changes

The peer bucket in App.rate_limit counted REMOTE_ADDR. Behind the edge that address is the proxy, so every browser shared one bucket: 20 failed logins from anywhere locked the whole site out of /register, /login and OIDC start for the 600 s window.

  • flybot_workspace/app.py: new client_address(env) returns the address the edge accepted the connection from when the peer is loopback and X-Forwarded-For holds one valid address; otherwise it returns REMOTE_ADDR unchanged. A malformed or oversized header from a loopback peer is refused with 400 invalid, not counted. The peer bucket in rate_limit keys on it, so register, login and oidc_start all use the client address.
  • flybot_workspace/dsbot_gateway.py: the rendered location / block now sets proxy_set_header X-Forwarded-For $remote_addr; — overwrite, not append, so a value the caller put in front of it never reaches the platform.
  • tests/test_management.py, tests/test_dsbot_gateway.py: three auth cases (distinct forwarded addresses get distinct buckets, a non-loopback peer cannot choose its bucket, a mixed header is refused) and one render assertion for the header.

A header is believed only from a loopback peer, which is why the gateway must overwrite it: a direct caller elsewhere cannot pick its own bucket with a made-up value.

Not in this slice

The /social/ block's $proxy_add_x_forwarded_for and /v1/composio/callback are unchanged; name: and oidc: buckets are unchanged; making the three limits configurable (the issue's option B) is not done.

Verification

Local only, bound to head 1e4434ad920ef6e274ae0aea327c4987bb5a1004 / tree dfd056dfb7c82a5248e500e1401b4b112a2bad82; no CI read for this head yet.

  • python3 -m unittest discover -s tests on that tree: see the check log below.
  • End-to-end on a clean data-dir (ports 8814/8815, --insecure-local --open-registration): 22 registrations with 22 distinct X-Forwarded-For values -> 22x 201 (before the change the 21st was 429); 20 without the header -> 20x 201 then 429 rate_limited; a mixed header ("198.51.100.1, 10.0.0.1") -> 400 invalid.
  • dsbot_gateway render rc 0 and the rendered location / carries the new header line.

Not run here: real nginx in front of the app. The rendered config and the loopback trust rule are verified, the deployment is not.

Risk is on the auth path and the gateway renderer; no keys, no new dependencies, no upstream pin change.

Refs #3502

The peer bucket used REMOTE_ADDR, which behind the edge is the proxy for every
browser, so 20 failed logins anywhere locked the whole site out for 10 minutes.
The bucket now keys on the address the edge accepted the connection from, and
the gateway overwrites X-Forwarded-For instead of appending to it. A header is
believed only from a loopback peer, so a direct caller cannot choose its bucket.

Refs #3502
@Birfy

Birfy commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator Author

Local full suite on this head, for the log reference in the description.

Tree: dfd056dfb7c82a5248e500e1401b4b112a2bad82 (head 1e4434ad920ef6e274ae0aea327c4987bb5a1004), unpacked from codeload at main c7c27e22, patch applied, __pycache__ absent before the run:

$ python3 -m unittest discover -s tests
Ran 381 tests in 158.727s
OK

This is a local run on the exact head tree, not a CI conclusion. CI on 1e4434ad has not been read yet.

@github-actions github-actions Bot added the risk:T2 Risk tier T2 (host/plugin code or an unnamed path), set by the controller from the diff label Dec 3, 2026
@Birfy

Birfy commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator Author

Lead review — bound to head 1e4434ad920ef6e274ae0aea327c4987bb5a1004 (tree dfd056dfb7c82a5248e500e1401b4b112a2bad82).

Change: 4 files +75/-5 — app.py (new client_address, peer bucket keys on it), dsbot_gateway.py (rendered location / overwrites X-Forwarded-For with $remote_addr), tests/test_management.py +3 cases, tests/test_dsbot_gateway.py +1 render assertion. One slice, matching the issue's option A; option B is not in it.

Rule checks: trailer is Refs #3502 with no closing keyword; risk:T2 was set by risk-gate, not by hand; base is main c7c27e22.

Acceptance evidence:

  • I rebuilt the pushed head from codeload: tree dfd056df, 2,579 entries, byte-identical to the tree I verified locally (Runtime's reported b7fb3943 does not reproduce; his base differed — recorded, not a content difference).
  • Local full suite on that tree: Ran 381 tests in 158.727s, OK.
  • Local end-to-end: 22 distinct forwarded addresses -> 22x 201; no header -> 20x 201 then 429 rate_limited; mixed header -> 400 invalid; dsbot_gateway render rc 0 with the new header present.
  • CI, read-only at this head: 18/18 success, every run completed, no failure, no cancellation, no in-progress. That is head green, not merge-commit green.

Not claimed: real nginx in front of the app was not run; the deployment is unverified.

@Birfy
Birfy added this pull request to the merge queue Dec 3, 2026
Merged via the queue into main with commit bf39655 Dec 3, 2026
18 checks passed
@Birfy
Birfy deleted the dev/3502-auth-peer-limit-behind-edge branch December 3, 2026 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:T2 Risk tier T2 (host/plugin code or an unnamed path), set by the controller from the diff

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant