Skip to content

Commit bae5894

Browse files
authored
feat: don’t redirect shortlinks to blacklisted domains (#1129)
1 parent b72a510 commit bae5894

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

app/Http/Controllers/RedirectController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use App\Actions\RedirectToDestination;
6+
use App\Helpers\Helper;
67
use App\Models\Url;
78
use Illuminate\Support\Facades\Gate;
89

@@ -31,6 +32,11 @@ public function __invoke(Url $url)
3132
return to_route('link.expired', $url);
3233
}
3334

35+
// Check if the domain is in the blacklist
36+
if (Helper::isDomainBlacklisted($url->destination)) {
37+
return abort(404);
38+
}
39+
3440
return app(RedirectToDestination::class)->handle($url);
3541
}
3642
}

tests/Feature/FrontPage/VisitTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,26 @@ public function linkHasExpired_AccessLandingPage()
301301
$response = $this->get(route('link.expired', $url));
302302
$response->assertRedirect(route('link_detail', $url->keyword));
303303
}
304+
305+
/**
306+
* When a link has a blacklisted domain, it should be redirected
307+
* to the landing page.
308+
*
309+
* @see \App\Http\Controllers\RedirectController::__invoke()
310+
*/
311+
#[PHPUnit\Test]
312+
public function linkHasBlacklistedDomain()
313+
{
314+
// Test case 1: domain is not blacklisted
315+
$url = Url::factory()->create([
316+
'destination' => 'https://laravel.com/docs',
317+
]);
318+
$response = $this->get($url->keyword);
319+
$response->assertStatus(config('urlhub.redirection_status_code'));
320+
321+
// Test case 2: domain is blacklisted
322+
config(['urlhub.blacklist_domain' => ['laravel.com']]);
323+
$response = $this->get($url->keyword);
324+
$response->assertNotFound();
325+
}
304326
}

0 commit comments

Comments
 (0)