82 lines
3.3 KiB
PHP
82 lines
3.3 KiB
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
namespace StreamGoals\PayPal;
|
|
|
|
/**
|
|
* Cheap, local, pure pre-checks for the public webhook endpoint. They run BEFORE any outbound PayPal call so an
|
|
* anonymous flood cannot make this site hit PayPal's API. They are only a filter: PayPal's own
|
|
* verify-webhook-signature call stays the authority on whether an event is genuine.
|
|
*/
|
|
final class WebhookGuard {
|
|
|
|
public const MAX_BODY_BYTES = 65536;
|
|
public const MAX_SKEW_SECONDS = 3600;
|
|
public const IP_LIMIT = 120; // per visitor bucket per minute (PayPal retries and uses several IPs)
|
|
public const GLOBAL_LIMIT = 600; // whole site per minute
|
|
public const WINDOW = 60;
|
|
|
|
public const REQUIRED = array( 'paypal_transmission_id', 'paypal_transmission_time', 'paypal_transmission_sig', 'paypal_cert_url', 'paypal_auth_algo' );
|
|
|
|
/** Per-bucket then global fixed-window limit. True when the request may proceed. */
|
|
public static function admit( RateLimiter $limiter, string $ipBucket ): bool {
|
|
return $limiter->allow( 'webhook|' . $ipBucket, self::IP_LIMIT, self::WINDOW )
|
|
&& $limiter->allow( 'webhook|*global*', self::GLOBAL_LIMIT, self::WINDOW );
|
|
}
|
|
|
|
/** Lower-cases header names and maps '-' to '_'. @param array<string,mixed> $headers @return array<string,string> */
|
|
public static function normalizeHeaders( array $headers ): array {
|
|
$h = array();
|
|
foreach ( $headers as $k => $v ) {
|
|
$h[ strtolower( str_replace( '-', '_', (string) $k ) ) ] = is_array( $v ) ? (string) ( $v[0] ?? '' ) : (string) $v;
|
|
}
|
|
return $h;
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $headers
|
|
* @return array{status:int,code:string}|null Null when the request may be sent on to PayPal for verification.
|
|
*/
|
|
public static function check( array $headers, string $rawBody, int $now ): ?array {
|
|
if ( strlen( $rawBody ) > self::MAX_BODY_BYTES ) {
|
|
return array( 'status' => 413, 'code' => 'sg_webhook_too_large' );
|
|
}
|
|
$h = self::normalizeHeaders( $headers );
|
|
foreach ( self::REQUIRED as $n ) {
|
|
if ( '' === trim( $h[ $n ] ?? '' ) ) {
|
|
return array( 'status' => 401, 'code' => 'sg_webhook_headers' );
|
|
}
|
|
}
|
|
if ( ! self::isPayPalCertUrl( $h['paypal_cert_url'] ) ) {
|
|
return array( 'status' => 401, 'code' => 'sg_webhook_cert_url' );
|
|
}
|
|
if ( ! self::timeWithinWindow( $h['paypal_transmission_time'], $now ) ) {
|
|
return array( 'status' => 401, 'code' => 'sg_webhook_time' );
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/** https, no credentials/odd port, host is paypal.com or a subdomain of it. */
|
|
public static function isPayPalCertUrl( string $url ): bool {
|
|
$p = parse_url( trim( $url ) );
|
|
if ( ! is_array( $p ) || 'https' !== strtolower( (string) ( $p['scheme'] ?? '' ) ) || isset( $p['user'] ) || isset( $p['pass'] ) ) {
|
|
return false;
|
|
}
|
|
if ( isset( $p['port'] ) && 443 !== (int) $p['port'] ) {
|
|
return false;
|
|
}
|
|
$host = strtolower( (string) ( $p['host'] ?? '' ) );
|
|
return 'paypal.com' === $host || ( strlen( $host ) > 11 && str_ends_with( $host, '.paypal.com' ) );
|
|
}
|
|
|
|
/** ISO-8601 timestamp within +-MAX_SKEW_SECONDS of $now; unparsable is rejected. */
|
|
public static function timeWithinWindow( string $iso, int $now ): bool {
|
|
$iso = trim( $iso );
|
|
if ( 1 !== preg_match( '/^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:?\d{2})$/', $iso ) ) {
|
|
return false;
|
|
}
|
|
$t = strtotime( $iso );
|
|
return false !== $t && abs( $now - $t ) <= self::MAX_SKEW_SECONDS;
|
|
}
|
|
}
|