Page MenuHomePhabricator

Investigate / remove custom downstream changes to PhabricatorClientRateLimit.php
Closed, DeclinedPublic

Description

As I've been going through our downstream Phab diff to reduce technical debt and maintenance costs,
https://gitlab.wikimedia.org/repos/phabricator/phabricator/-/blob/wmf/stable/support/startup/PhabricatorClientRateLimit.php has four custom downstream changes from the year 2018 per https://gitlab.wikimedia.org/repos/phabricator/phabricator/-/commits/wmf/stable/support/startup/PhabricatorClientRateLimit.php :

I assume that our more recent use of requestctl makes these our downstream changes rather moot and they could be removed?

Current upstream: https://we.phorge.it/source/phorge/browse/master/support/startup/PhabricatorClientRateLimit.php

Diff:

1diff --git a/phorge-upstream20230624-52be52d429ce/support/startup/PhabricatorClientRateLimit.php b/phab-wmf-20231125/support/startup/PhabricatorClientRateLimit.php
2index 89a273e..7537334 100644
3--- a/phorge-upstream20230624-52be52d429ce/support/startup/PhabricatorClientRateLimit.php
4+++ b/phab-wmf-20231125/support/startup/PhabricatorClientRateLimit.php
5@@ -3,6 +3,8 @@
6 final class PhabricatorClientRateLimit
7 extends PhabricatorClientLimit {
8
9+ protected $whitelist = array('87.138.110.76', '198.73.209.241');
10+
11 protected function getBucketDuration() {
12 return 60;
13 }
14@@ -13,12 +15,24 @@ final class PhabricatorClientRateLimit
15
16 protected function shouldRejectConnection($score) {
17 $limit = $this->getLimit();
18+ if ($limit == 0) {
19+ return false;
20+ }
21
22 // Reject connections if the average score across all buckets exceeds the
23 // limit.
24 $average_score = $score / $this->getBucketCount();
25
26- return ($average_score > $limit);
27+ if ($average_score <= $limit) {
28+ return false;
29+ }
30+
31+ // don't reject whitelisted connections
32+ $key = $this->getClientKey();
33+ if (in_array($key, $this->whitelist)) {
34+ return false;
35+ }
36+ return true;
37 }
38
39 protected function getConnectScore() {
40@@ -26,16 +40,21 @@ final class PhabricatorClientRateLimit
41 }
42
43 protected function getPenaltyScore() {
44- return 1;
45+ return 0;
46 }
47
48 protected function getDisconnectScore(array $request_state) {
49 $score = 1;
50
51- // If the user was logged in, let them make more requests.
52+ $key = $this->getClientKey();
53+ // whitelisted ips get unlimited requests
54+ if (in_array($key, $this->whitelist)) {
55+ $score = 0;
56+ }
57+
58 if (isset($request_state['viewer'])) {
59 $viewer = $request_state['viewer'];
60- if ($viewer->isOmnipotent()) {
61+ if ($viewer->isOmnipotent() || $viewer->getIsSystemAgent()) {
62 // If the viewer was omnipotent, this was an intracluster request or
63 // some other kind of special request, so don't give it any points
64 // toward rate limiting.
65@@ -44,10 +63,9 @@ final class PhabricatorClientRateLimit
66 // If the viewer was logged in, give them fewer points than if they
67 // were logged out, since this traffic is much more likely to be
68 // legitimate.
69- $score = 0.25;
70+ $score = $score / 4;
71 }
72 }
73-
74 return $score;
75 }
76

Details

TitleReferenceAuthorSource BranchDest Branch
Revert "Revert custom changes to PhabricatorClientRateLimit.php"repos/phabricator/phabricator!59aklapperT364839rerevertwmf/stable
Revert custom changes to PhabricatorClientRateLimit.phprepos/phabricator/phabricator!52aklapperT364839rateLimitwmf/stable
Customize query in GitLab

Event Timeline

Aklapper created this task.

@Jelto Hi, would you have any input or recommendations on this? If not that's fine, I could also remove this custom code and see what may (not) happen. :)

I left a comment in https://gitlab.wikimedia.org/repos/phabricator/phabricator/-/merge_requests/52#note_86778. +1 for removing the custom rate limiting code. I'd try to move any existing extra logic to a single place (requestctl).

Meh, declining for now.