Skip to content

Commit 07763b0

Browse files
committed
Added Middleware for checking Request Source on webhooks,
Refactoring
1 parent 622b45d commit 07763b0

File tree

6 files changed

+86
-36
lines changed

6 files changed

+86
-36
lines changed

config/telepath.php

+44-22
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
| you don't need to worry about it.
1313
|
1414
*/
15+
1516
'default' => 'main',
1617

1718
/*
@@ -24,6 +25,7 @@
2425
| that are preconfigured for the default bot.
2526
|
2627
*/
28+
2729
'bots' => [
2830

2931
'main' => [
@@ -34,29 +36,49 @@
3436

3537
],
3638

37-
/*
38-
|--------------------------------------------------------------------------
39-
| Webhook Secret Token
40-
|--------------------------------------------------------------------------
41-
|
42-
| Here you may specify the secret token that is used to verify
43-
| the webhook url. This is used to prevent unauthorized
44-
| access to your webhook url.
45-
|
46-
*/
39+
'webhook' => [
4740

48-
'webhook_secret' => env('TELEGRAM_WEBHOOK_SECRET'),
41+
/*
42+
|--------------------------------------------------------------------------
43+
| Webhook Secret Token
44+
|--------------------------------------------------------------------------
45+
|
46+
| Here you may specify the secret token that is used to verify
47+
| the webhook url. This is used to prevent unauthorized
48+
| access to your webhook url.
49+
|
50+
*/
4951

50-
/*
51-
|--------------------------------------------------------------------------
52-
| Webhook Resolver
53-
|--------------------------------------------------------------------------
54-
|
55-
| Here you may specify the class that is responsible for resolving
56-
| the webhook url secret. The default implementation uses Laravels
57-
| Hash::make function.
58-
|
59-
*/
60-
'webhook_resolver' => \Telepath\Laravel\WebhookResolver\HashWebhookResolver::class,
52+
'secret' => env('TELEGRAM_WEBHOOK_SECRET'),
53+
54+
/*
55+
|--------------------------------------------------------------------------
56+
| Webhook Middleware
57+
|--------------------------------------------------------------------------
58+
|
59+
|
60+
|
61+
*/
62+
63+
'middleware' => [
64+
Telepath\Laravel\Http\Middleware\ResolveWebhook::class,
65+
Telepath\Laravel\Http\Middleware\ValidateRequestSource::class,
66+
Telepath\Laravel\Http\Middleware\ValidateSecretToken::class,
67+
],
68+
69+
/*
70+
|--------------------------------------------------------------------------
71+
| Webhook Resolver
72+
|--------------------------------------------------------------------------
73+
|
74+
| Here you may specify the class that is responsible for resolving
75+
| the webhook url secret. The default implementation uses Laravels
76+
| Hash::make function.
77+
|
78+
*/
79+
80+
'resolver' => \Telepath\Laravel\WebhookResolver\HashWebhookResolver::class,
81+
82+
],
6183

6284
];

routes/telepath.php

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
<?php
22

33
use Illuminate\Support\Facades\Route;
4-
use Telepath\Laravel\Http\Middleware\ResolveWebhook;
5-
use Telepath\Laravel\Http\Middleware\ValidateSecretToken;
64
use Telepath\TelegramBot;
75

8-
Route::post('/telepath/bot/{secret}', function (TelegramBot $bot) {
6+
Route::name('telepath.webhook')
7+
->middleware('telepath')
8+
->post('/telepath/bot/{secret}', function (TelegramBot $bot) {
99

10-
$bot->handleWebhook();
10+
$bot->handleWebhook();
1111

12-
})
13-
->name('telepath.webhook')
14-
->middleware([
15-
ResolveWebhook::class,
16-
ValidateSecretToken::class,
17-
]);
12+
});

src/Console/Commands/SetWebhookCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function handle(): void
3333
$this->comment("Setting webhook for '{$name}' bot to {$url}...");
3434

3535
// Configuration
36-
$secretToken = config('telepath.webhook_secret') ?: null;
36+
$secretToken = config('telepath.webhook.secret') ?: null;
3737

3838
// Options
3939
$dropPendingUpdates = $this->option('drop-pending-updates');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Telepath\Laravel\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use Symfony\Component\HttpFoundation\IpUtils;
8+
use Symfony\Component\HttpFoundation\Response;
9+
10+
class ValidateRequestSource
11+
{
12+
13+
protected array $telegramSubnets = [
14+
'149.154.160.0/20',
15+
'91.108.4.0/22',
16+
];
17+
18+
public function handle(Request $request, Closure $next): Response
19+
{
20+
abort_unless(
21+
IpUtils::checkIp($request->ip(), $this->telegramSubnets),
22+
403,
23+
'Forbidden'
24+
);
25+
26+
return $next($request);
27+
}
28+
29+
}

src/Http/Middleware/ValidateSecretToken.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ class ValidateSecretToken
1111

1212
public function handle(Request $request, Closure $next): Response
1313
{
14-
$secretToken = config('telepath.secret_token') ?: null;
14+
$secretToken = config('telepath.webhook.secret') ?: null;
1515

1616
abort_if(
1717
$secretToken !== null && $request->header('X-Telegram-Bot-Api-Secret-Token') !== $secretToken,
18-
403
18+
403,
19+
'Forbidden'
1920
);
2021

2122
return $next($request);

src/TelepathServiceProvider.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Telepath\Laravel;
44

5+
use Illuminate\Support\Facades\Route;
56
use Illuminate\Support\ServiceProvider;
67
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
78
use Telepath\Laravel\Config\BotConfig;
@@ -45,11 +46,13 @@ public function register(): void
4546
}
4647

4748
// Register Webhook Resolver
48-
$this->app->bind(WebhookResolver::class, config('telepath.webhook_resolver'));
49+
$this->app->bind(WebhookResolver::class, config('telepath.webhook.resolver'));
4950
}
5051

5152
public function boot(): void
5253
{
54+
Route::middlewareGroup('telepath', config('telepath.webhook.middleware', []));
55+
5356
$this->loadRoutesFrom(
5457
__DIR__ . '/../routes/telepath.php'
5558
);

0 commit comments

Comments
 (0)