Skip to content

Commit d220bfd

Browse files
committed
fix: give starting a terminal sign-in a rate limit bucket of its own
Starting a sign-in shared the devices limiter, which picks polling's 300 a minute per address whenever the body has a device code, before the request is checked. The sign-in request doesn't reject that extra field, so adding one moved it to the larger bucket, and rotating keys got around the 5 per key: one address could start up to 300 sign-ins a minute instead of 60. It now has its own limiter, with the same 60 a minute per address whatever the body says.
1 parent 054f071 commit d220bfd

4 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎app/Providers/AppServiceProvider.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ private function bootRateLimitingDefaults(): void
205205
? Limit::perMinute(300)->by('device-polls:'.$request->ip())->response($rateLimited)
206206
: Limit::perMinute(60)->by((string) $request->ip())->response($rateLimited));
207207

208+
// Starting a sign-in from a terminal gets the same ceiling per address, in a bucket of its own: polling's larger
209+
// one is chosen from the body before anything is checked, so it must not be reachable from here...
210+
RateLimiter::for('device-authorizations', fn (Request $request): Limit => Limit::perMinute(60)->by((string) $request->ip())->response($rateLimited));
211+
208212
// Approving the devnull app on the web hands out a code each time...
209213
RateLimiter::for('mobile-authorizations', fn (Request $request): Limit => Limit::perMinute(10)->by($this->userId($request)));
210214

‎routes/api.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
// Signing in from a terminal: the key that isn't approved yet asks for a code the user types on the web...
157157
Route::post('device-authorizations', [DeviceAuthorizationController::class, 'store'])
158158
->withoutMiddleware('throttle:api')
159-
->middleware('throttle:devices')
159+
->middleware('throttle:device-authorizations')
160160
->name('api-device-authorization.store');
161161

162162
// Signed by the mobile app with an SSH key that the user approved as a device...

‎tests/Feature/Controllers/Api/DeviceAuthorizationControllerTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,13 @@
117117
->assertTooManyRequests()
118118
->assertJsonPath('code', 'rate_limited');
119119
});
120+
121+
it('keeps each address to 60 tries a minute when a device code is slipped in, which only polling gets more for', function (): void {
122+
foreach (range(1, 60) as $attempt) {
123+
$this->postJson(route('api-device-authorization.store'), ['device_code' => 'any-device-code'])->assertUnauthorized();
124+
}
125+
126+
$this->postJson(route('api-device-authorization.store'), ['device_code' => 'any-device-code'])
127+
->assertTooManyRequests()
128+
->assertJsonPath('code', 'rate_limited');
129+
});

‎tests/Unit/Providers/AppServiceProviderTest.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@
115115
->and($limit->decaySeconds)->toBe(60);
116116
});
117117

118+
it('limits each address to 60 terminal sign-ins started a minute, before their signatures are checked, whatever the body says', function (): void {
119+
$request = Request::create('/api/v1/device-authorizations', 'POST', server: ['REMOTE_ADDR' => '203.0.113.7', 'CONTENT_TYPE' => 'application/json'], content: '{"device_name":"devnull on a laptop","device_code":"the-device-code"}');
120+
121+
$limiter = RateLimiter::limiter('device-authorizations');
122+
123+
assert($limiter instanceof Closure);
124+
125+
$limit = $limiter($request);
126+
127+
expect($limit)->toBeInstanceOf(Limit::class)
128+
->and($limit->key)->toBe('203.0.113.7')
129+
->and($limit->maxAttempts)->toBe(60)
130+
->and($limit->decaySeconds)->toBe(60);
131+
});
132+
118133
it('limits each user to 20 device code lookups and decisions a minute', function (): void {
119134
$user = User::factory()->create();
120135

0 commit comments

Comments
 (0)