diff --git a/README.md b/README.md index ac9ab5cb..369acb1e 100644 --- a/README.md +++ b/README.md @@ -343,11 +343,20 @@ $workspace = $seam->workspaces->create( Seam delivers webhooks with [Svix]. Verify and parse an incoming request with `SeamWebhook`, which returns the typed event. +Omit the secret to read `SEAM_WEBHOOK_SECRET` from the process +environment at construction time. An explicit secret takes precedence, including +an empty string: missing, empty, or decoded-empty secrets raise +`Seam\InvalidOptionsError` before verification. Explicit `null` and other non-string +values raise `TypeError`. [Svix]: https://www.svix.com/ ```php -$webhook = new Seam\SeamWebhook($webhook_secret); +// Set SEAM_WEBHOOK_SECRET to the signing secret from Seam Console. +$webhook = new Seam\SeamWebhook(); + +// Or pass a secret explicitly, overriding the environment. +// $webhook = new Seam\SeamWebhook(secret: $webhook_secret); try { $event = $webhook->verify($request_body, $request_headers); diff --git a/src/SeamWebhook.php b/src/SeamWebhook.php index fb2ec5b2..c8d3aeb6 100644 --- a/src/SeamWebhook.php +++ b/src/SeamWebhook.php @@ -19,8 +19,31 @@ class SeamWebhook { private Webhook $webhook; - public function __construct(string $secret) + /** + * The secret must be a string; defaults to SEAM_WEBHOOK_SECRET only when omitted. + * + * @throws InvalidOptionsError When the secret is missing or decodes to an empty key. + */ + public function __construct(mixed $secret = null) { + if (func_num_args() === 0) { + $secret = Options::get_env("SEAM_WEBHOOK_SECRET") ?? ""; + } + if (!is_string($secret)) { + throw new \TypeError("The webhook secret must be a string."); + } + $encoded_key = str_starts_with($secret, Webhook::SECRET_PREFIX) + ? substr($secret, strlen(Webhook::SECRET_PREFIX)) + : $secret; + + // Svix accepts decoded-empty keys; reject them before verification. + if (base64_decode($encoded_key) === "") { + throw new InvalidOptionsError( + "Provide a non-empty webhook secret or set SEAM_WEBHOOK_SECRET; " . + "the secret must decode to a non-empty signing key.", + ); + } + $this->webhook = new Webhook($secret); } diff --git a/tests/SeamWebhookTest.php b/tests/SeamWebhookTest.php index 37b9d888..78884393 100644 --- a/tests/SeamWebhookTest.php +++ b/tests/SeamWebhookTest.php @@ -5,6 +5,7 @@ namespace Tests; use PHPUnit\Framework\TestCase; +use Seam\InvalidOptionsError; use Seam\InvalidWebhookPayloadError; use Seam\Resources\Event; use Seam\Resources\Event\AccessCodeCreated; @@ -18,6 +19,142 @@ final class SeamWebhookTest extends TestCase { private const SECRET = "whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw"; + private const OTHER_SECRET = "whsec_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="; + + private string|false $original_secret; + + protected function setUp(): void + { + parent::setUp(); + $this->original_secret = getenv("SEAM_WEBHOOK_SECRET"); + putenv("SEAM_WEBHOOK_SECRET"); + } + + protected function tearDown(): void + { + putenv( + $this->original_secret === false + ? "SEAM_WEBHOOK_SECRET" + : "SEAM_WEBHOOK_SECRET=" . $this->original_secret, + ); + parent::tearDown(); + } + + public function testOmittedSecretUsesTheEnvironment(): void + { + putenv("SEAM_WEBHOOK_SECRET=" . self::SECRET); + $payload = $this->payload(); + $headers = $this->signed_headers($payload); + + $event = (new SeamWebhook())->verify($payload, $headers); + $this->assertInstanceOf(DeviceConnected::class, $event); + $this->assertSame("device.connected", $event->event_type); + } + + /** + * @dataProvider invalidSecrets + */ + public function testExplicitNonStringSecretIsRejected(mixed $secret): void + { + putenv("SEAM_WEBHOOK_SECRET=" . self::SECRET); + $this->expectException(\TypeError::class); + + new SeamWebhook(secret: $secret); + } + + public static function invalidSecrets(): array + { + return [[null], [false], [123], [1.5], [[]], [new \stdClass()]]; + } + + public function testExplicitNullPositionalSecretIsRejected(): void + { + putenv("SEAM_WEBHOOK_SECRET=" . self::SECRET); + $this->expectException(\TypeError::class); + + new SeamWebhook(null); + } + + public function testExplicitSecretOverridesTheEnvironment(): void + { + putenv("SEAM_WEBHOOK_SECRET=" . self::OTHER_SECRET); + $payload = $this->payload(); + + $event = (new SeamWebhook(secret: self::SECRET))->verify( + $payload, + $this->signed_headers($payload), + ); + + $this->assertInstanceOf(DeviceConnected::class, $event); + } + + public function testMissingEnvironmentAndOmittedSecretFailsAtConstruction(): void + { + $this->expectException(InvalidOptionsError::class); + $this->expectExceptionMessage("SEAM_WEBHOOK_SECRET"); + + new SeamWebhook(); + } + + /** + * @dataProvider emptySecrets + */ + public function testEmptyEffectiveSecretFailsAtConstruction( + string|false $environment, + ?string $secret, + ): void { + putenv( + $environment === false + ? "SEAM_WEBHOOK_SECRET" + : "SEAM_WEBHOOK_SECRET=" . $environment, + ); + $this->expectException(InvalidOptionsError::class); + $this->expectExceptionMessage("non-empty signing key"); + + if ($secret === null) { + new SeamWebhook(); + } else { + new SeamWebhook($secret); + } + } + + public static function emptySecrets(): array + { + return [ + "unset env and omitted secret" => [false, null], + "empty env" => ["", null], + "explicit empty overrides env" => [self::SECRET, ""], + "prefix-only env" => ["whsec_", null], + "prefix-only explicit" => [self::SECRET, "whsec_"], + "whitespace env" => [" \t\n", null], + "undecodable env" => ["whsec_!!!", null], + ]; + } + + public function testEnvironmentIsReadForEachConstruction(): void + { + putenv("SEAM_WEBHOOK_SECRET=" . self::SECRET); + $first = new SeamWebhook(); + putenv("SEAM_WEBHOOK_SECRET=" . self::OTHER_SECRET); + $second = new SeamWebhook(); + putenv("SEAM_WEBHOOK_SECRET"); + $payload = $this->payload(); + + $this->assertInstanceOf( + DeviceConnected::class, + $first->verify($payload, $this->signed_headers($payload)), + ); + $this->assertInstanceOf( + DeviceConnected::class, + $second->verify( + $payload, + $this->signed_headers($payload, secret: self::OTHER_SECRET), + ), + ); + + $this->expectException(WebhookVerificationException::class); + $second->verify($payload, $this->signed_headers($payload)); + } private function payload(): string { @@ -34,16 +171,15 @@ private function payload(): string /** * @return array */ - private function signed_headers(string $payload, ?int $at = null): array - { + private function signed_headers( + string $payload, + ?int $at = null, + string $secret = self::SECRET, + ): array { $id = "msg_test"; $timestamp = (string) ($at ?? time()); - $signature = (new Webhook(self::SECRET))->sign( - $id, - $timestamp, - $payload, - ); + $signature = (new Webhook($secret))->sign($id, $timestamp, $payload); return [ "svix-id" => $id,