Skip to content

Commit 349da2f

Browse files
authored
Merge pull request #46 from noplanman/45-webhook_ip_ranges
Use the new Telegram API webhook IP ranges.
2 parents 6233321 + 6bd916e commit 349da2f

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
66
## [Unreleased]
77
### Added
88
### Changed
9+
- Use the new Telegram API webhook IP ranges.
910
### Deprecated
1011
### Removed
1112
### Fixed

src/BotManager.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
class BotManager
2222
{
2323
/**
24-
* @var string Telegram post servers IP range
24+
* @var array Telegram webhook servers IP ranges
25+
* @link https://core.telegram.org/bots/webhooks#the-short-version
2526
*/
26-
const TELEGRAM_IP_RANGE = '149.154.167.197-149.154.167.233';
27+
public const TELEGRAM_IP_RANGES = ['149.154.160.0/20', '91.108.4.0/22'];
2728

2829
/**
2930
* @var string The output for testing, instead of echoing
@@ -578,7 +579,7 @@ public function isValidRequest(): bool
578579
}
579580

580581
return Ip::match($ip, array_merge(
581-
[self::TELEGRAM_IP_RANGE],
582+
self::TELEGRAM_IP_RANGES,
582583
(array) $this->params->getBotParam('valid_ips', [])
583584
));
584585
}

tests/BotManagerTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -431,19 +431,23 @@ public function testIsValidRequestValidate()
431431
unset($_SERVER['HTTP_X_FORWARDED_FOR'], $_SERVER['HTTP_CLIENT_IP'], $_SERVER['REMOTE_ADDR']);
432432

433433
// Lower range.
434-
$_SERVER['REMOTE_ADDR'] = '149.154.167.196';
434+
$_SERVER['REMOTE_ADDR'] = '149.154.159.255';
435435
self::assertFalse($botManager->isValidRequest());
436-
$_SERVER['REMOTE_ADDR'] = '149.154.167.197';
436+
$_SERVER['REMOTE_ADDR'] = '149.154.160.0';
437437
self::assertTrue($botManager->isValidRequest());
438-
$_SERVER['REMOTE_ADDR'] = '149.154.167.198';
438+
$_SERVER['REMOTE_ADDR'] = '91.108.3.255';
439+
self::assertFalse($botManager->isValidRequest());
440+
$_SERVER['REMOTE_ADDR'] = '91.108.4.0';
439441
self::assertTrue($botManager->isValidRequest());
440442

441443
// Upper range.
442-
$_SERVER['REMOTE_ADDR'] = '149.154.167.232';
444+
$_SERVER['REMOTE_ADDR'] = '149.154.175.255';
443445
self::assertTrue($botManager->isValidRequest());
444-
$_SERVER['REMOTE_ADDR'] = '149.154.167.233';
446+
$_SERVER['REMOTE_ADDR'] = '149.154.176.0';
447+
self::assertFalse($botManager->isValidRequest());
448+
$_SERVER['REMOTE_ADDR'] = '91.108.7.255';
445449
self::assertTrue($botManager->isValidRequest());
446-
$_SERVER['REMOTE_ADDR'] = '149.154.167.234';
450+
$_SERVER['REMOTE_ADDR'] = '91.108.8.0';
447451
self::assertFalse($botManager->isValidRequest());
448452
}
449453

0 commit comments

Comments
 (0)