File tree 2 files changed +38
-1
lines changed
2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change 51
51
52
52
'secret ' => env ('TELEGRAM_WEBHOOK_SECRET ' ),
53
53
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
+
54
67
/*
55
68
|--------------------------------------------------------------------------
56
69
| Webhook Middleware
Original file line number Diff line number Diff line change @@ -15,15 +15,39 @@ class ValidateRequestSource
15
15
'91.108.4.0/22 ' ,
16
16
];
17
17
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
+
18
25
public function handle (Request $ request , Closure $ next ): Response
19
26
{
20
27
abort_unless (
21
- IpUtils::checkIp ($ request ->ip (), $ this ->telegramSubnets ),
28
+ $ this ->isTelegramSubnet ($ request ->ip ())
29
+ || $ this ->allowLocalSubnets () && $ this ->isLocalSubnet ($ request ->ip ()),
22
30
403 ,
23
31
'Forbidden '
24
32
);
25
33
26
34
return $ next ($ request );
27
35
}
28
36
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
+
29
53
}
You can’t perform that action at this time.
0 commit comments