assertTrue( WebhookGuard::admit( $l, '203.0.113.1' ) ); } $this->assertFalse( WebhookGuard::admit( $l, '203.0.113.1' ), 'over the per-IP limit' ); $this->assertTrue( WebhookGuard::admit( $l, '203.0.113.2' ), 'PayPal\'s other addresses keep working' ); } public function test_global_cap(): void { $l = new RateLimiter( new ArrayCache() ); $ok = 0; for ( $i = 0; $i < WebhookGuard::GLOBAL_LIMIT + 50; $i++ ) { $ok += WebhookGuard::admit( $l, '10.0.' . intdiv( $i, 100 ) . '.' . ( $i % 100 ) ) ? 1 : 0; // every request from a fresh bucket } $this->assertSame( WebhookGuard::GLOBAL_LIMIT, $ok ); } public function test_rejections_have_the_expected_status(): void { $good = array( 'paypal-transmission-id' => 'T', 'paypal-transmission-time' => gmdate( 'Y-m-d\TH:i:s\Z' ), 'paypal-transmission-sig' => 'S', 'paypal-cert-url' => 'https://api.sandbox.paypal.com/c', 'paypal-auth-algo' => 'A' ); $this->assertNull( WebhookGuard::check( $good, '{}', time() ) ); $this->assertSame( 413, WebhookGuard::check( $good, str_repeat( 'a', 65537 ), time() )['status'] ); $this->assertNull( WebhookGuard::check( $good, str_repeat( 'a', 65536 ), time() ) ); $this->assertSame( 401, WebhookGuard::check( array_diff_key( $good, array( 'paypal-auth-algo' => 1 ) ), '{}', time() )['status'] ); $this->assertSame( 401, WebhookGuard::check( array( 'paypal-cert-url' => 'https://evil.test/' ) + $good, '{}', time() )['status'] ); $this->assertSame( 401, WebhookGuard::check( array( 'paypal-transmission-time' => '2020-01-01T00:00:00Z' ) + $good, '{}', time() )['status'] ); $this->assertSame( 401, WebhookGuard::check( array( 'paypal-transmission-time' => 'garbage' ) + $good, '{}', time() )['status'] ); } public function test_time_window_boundaries_and_offsets(): void { $now = 1800000000; $this->assertTrue( WebhookGuard::timeWithinWindow( gmdate( 'Y-m-d\TH:i:s\Z', $now - 3600 ), $now ) ); $this->assertFalse( WebhookGuard::timeWithinWindow( gmdate( 'Y-m-d\TH:i:s\Z', $now - 3601 ), $now ) ); $this->assertFalse( WebhookGuard::timeWithinWindow( gmdate( 'Y-m-d\TH:i:s\Z', $now + 3601 ), $now ) ); $this->assertTrue( WebhookGuard::timeWithinWindow( gmdate( 'Y-m-d\TH:i:s', $now + 3600 ) . '+01:00', $now ) ); } }