Skip to content

Commit

Permalink
Exports: Added rate limits for UI exports
Browse files Browse the repository at this point in the history
Just as a measure to prevent potential abuse of these potentially
longer-running endpoints.
Adds test to cover for ZIP exports, but applied to all formats.
  • Loading branch information
ssddanbrown committed Jan 1, 2025
1 parent 7e31725 commit 1ff2826
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/App/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,12 @@ protected function configureRateLimiting(): void
RateLimiter::for('public', function (Request $request) {
return Limit::perMinute(10)->by($request->ip());
});

RateLimiter::for('exports', function (Request $request) {
$user = user();
$attempts = $user->isGuest() ? 4 : 10;
$key = $user->isGuest() ? $request->ip() : $user->id;
return Limit::perMinute($attempts)->by($key);
});
}
}
1 change: 1 addition & 0 deletions app/Exports/Controllers/BookExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function __construct(
protected ExportFormatter $exportFormatter,
) {
$this->middleware('can:content-export');
$this->middleware('throttle:exports');
}

/**
Expand Down
1 change: 1 addition & 0 deletions app/Exports/Controllers/ChapterExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function __construct(
protected ExportFormatter $exportFormatter,
) {
$this->middleware('can:content-export');
$this->middleware('throttle:exports');
}

/**
Expand Down
1 change: 1 addition & 0 deletions app/Exports/Controllers/PageExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct(
protected ExportFormatter $exportFormatter,
) {
$this->middleware('can:content-export');
$this->middleware('throttle:exports');
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/Exports/ZipExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,28 @@ public function test_links_in_markdown_are_parsed()
$this->assertStringContainsString("[Link to chapter]([[bsexport:chapter:{$chapter->id}]])", $pageData['markdown']);
}

public function test_exports_rate_limited_low_for_guest_viewers()
{
$this->setSettings(['app-public' => 'true']);

$page = $this->entities->page();
for ($i = 0; $i < 4; $i++) {
$this->get($page->getUrl("/export/zip"))->assertOk();
}
$this->get($page->getUrl("/export/zip"))->assertStatus(429);
}

public function test_exports_rate_limited_higher_for_logged_in_viewers()
{
$this->asAdmin();

$page = $this->entities->page();
for ($i = 0; $i < 10; $i++) {
$this->get($page->getUrl("/export/zip"))->assertOk();
}
$this->get($page->getUrl("/export/zip"))->assertStatus(429);
}

protected function extractZipResponse(TestResponse $response): ZipResultData
{
$zipData = $response->streamedContent();
Expand Down

0 comments on commit 1ff2826

Please sign in to comment.