Skip to content

Commit 3518185

Browse files
committed
Allow local subnets if configured or in local environment
1 parent 1bc045b commit 3518185

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

config/telepath.php

+13
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@
5151

5252
'secret' => env('TELEGRAM_WEBHOOK_SECRET'),
5353

54+
/*
55+
|--------------------------------------------------------------------------
56+
| Allow Local Subnets
57+
|--------------------------------------------------------------------------
58+
|
59+
| Here you may specify if you want to allow local subnets to access
60+
| your webhook url in non-local environment. This is useful for
61+
| testing purposes or if a custom bot API server is used.
62+
|
63+
*/
64+
65+
'allow_local_subnets' => env('TELEPATH_ALLOW_LOCAL_SUBNETS', false),
66+
5467
/*
5568
|--------------------------------------------------------------------------
5669
| Webhook Middleware

src/Http/Middleware/ValidateRequestSource.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,39 @@ class ValidateRequestSource
1515
'91.108.4.0/22',
1616
];
1717

18+
protected array $localSubnets = [
19+
'127.0.0.1/32',
20+
'192.168.0.0/16',
21+
'172.16.0.0/12',
22+
'10.0.0.0/8',
23+
];
24+
1825
public function handle(Request $request, Closure $next): Response
1926
{
2027
abort_unless(
21-
IpUtils::checkIp($request->ip(), $this->telegramSubnets),
28+
$this->isTelegramSubnet($request->ip())
29+
|| $this->allowLocalSubnets() && $this->isLocalSubnet($request->ip()),
2230
403,
2331
'Forbidden'
2432
);
2533

2634
return $next($request);
2735
}
2836

37+
protected function allowLocalSubnets(): bool
38+
{
39+
return app()->environment('local')
40+
|| config('telepath.webhook.allow_local_subnets', false);
41+
}
42+
43+
protected function isTelegramSubnet(string $ip): bool
44+
{
45+
return IpUtils::checkIp($ip, $this->telegramSubnets);
46+
}
47+
48+
protected function isLocalSubnet(string $ip): bool
49+
{
50+
return IpUtils::checkIp($ip, $this->localSubnets);
51+
}
52+
2953
}

0 commit comments

Comments
 (0)