feat: refuse Command Invocations from Events of HTTP callsUnverified
The probe showed that CRON Events carry neither X-Real-Ip nor X-Envoy-External-Address, which the platform sets on every HTTP call, overwriting client values. Requiring their absence on top of the CRON Secret keeps a leaked secret from running commands over HTTP.
08955ad parent: 2352980 modified
src/CommandInvocation.php +15 -6 | @@ -7,15 +7,18 @@ namespace JDecool\ScalewayRuntime; | ||
| 7 | 7 | /** |
| 8 | 8 | * A console command an authenticated CRON Invocation asks to run. |
| 9 | 9 | * |
| 10 | - * Nothing known in an Event tells a CRON Invocation from an HTTP one, so the CRON arguments must carry | |
| 11 | - * the secret set in SCALEWAY_CRON_SECRET; without it, a body naming a command is a plain HTTP body | |
| 12 | - * (see docs/adr/0005-authenticate-command-invocations-with-a-shared-secret.md). | |
| 13 | - * This is the one place to change once the probe finds a platform marker HTTP clients cannot forge. | |
| 10 | + * Two independent checks, as each alone could fail: the Event must lack the client address headers the platform sets | |
| 11 | + * on every HTTP call, an undocumented behaviour, and the CRON arguments must carry the secret set in | |
| 12 | + * SCALEWAY_CRON_SECRET (see docs/adr/0005-authenticate-command-invocations-with-a-shared-secret.md). | |
| 13 | + * Otherwise a body naming a command is a plain HTTP body. | |
| 14 | 14 | */ |
| 15 | 15 | final readonly class CommandInvocation |
| 16 | 16 | { |
| 17 | 17 | public const string SECRET_VAR = 'SCALEWAY_CRON_SECRET'; |
| 18 | 18 | |
| 19 | + /** Set by the platform on every HTTP call, overwriting client values, and absent from CRON Events. */ | |
| 20 | + public const array HTTP_ONLY_HEADERS = ['x-real-ip', 'x-envoy-external-address']; | |
| 21 | + | |
| 19 | 22 | /** |
| 20 | 23 | * @param array<string, mixed> $args ArrayInput parameters: argument names and "--option" names to values |
| 21 | 24 | */ |
| @@ -41,10 +44,16 @@ final readonly class CommandInvocation | ||
| 41 | 44 | return null; |
| 42 | 45 | } |
| 43 | 46 | |
| 44 | - if (!\is_string($secret) || '' === $secret || !\is_string($data['secret'] ?? null) || !hash_equals($secret, $data['secret'])) { | |
| 47 | + $headers = \is_array($event['headers'] ?? null) ? array_change_key_case($event['headers']) : []; | |
| 48 | + $reason = match (true) { | |
| 49 | + [] !== array_intersect_key($headers, array_flip(self::HTTP_ONLY_HEADERS)) => 'the Event comes from an HTTP call, not a CRON trigger', | |
| 50 | + !\is_string($secret) || '' === $secret || !\is_string($data['secret'] ?? null) || !hash_equals($secret, $data['secret']) => \sprintf('the "secret" does not match %s', self::SECRET_VAR), | |
| 51 | + default => null, | |
| 52 | + }; | |
| 53 | + if (null !== $reason) { | |
| 45 | 54 | // Only bodies meant as Command Invocations are worth a line: APIs may legitimately take a "command" field. |
| 46 | 55 | if (\array_key_exists('secret', $data)) { |
| 47 | - fwrite($errorLog, \sprintf('Command Invocation rejected: the "secret" does not match %s, the body goes to the Application as a plain HTTP body.', self::SECRET_VAR).\PHP_EOL); | |
| 56 | + fwrite($errorLog, \sprintf('Command Invocation rejected: %s, the body goes to the Application as a plain HTTP body.', $reason).\PHP_EOL); | |
| 48 | 57 | } |
| 49 | 58 | |
| 50 | 59 | return null; |
| @@ -7,15 +7,18 @@ namespace JDecool\ScalewayRuntime; | |||
| 7 | /** | 7 | /** |
| 8 | * A console command an authenticated CRON Invocation asks to run. | 8 | * A console command an authenticated CRON Invocation asks to run. |
| 9 | * | 9 | * |
| 10 | - * Nothing known in an Event tells a CRON Invocation from an HTTP one, so the CRON arguments must carry | 10 | + * Two independent checks, as each alone could fail: the Event must lack the client address headers the platform sets |
| 11 | - * the secret set in SCALEWAY_CRON_SECRET; without it, a body naming a command is a plain HTTP body | 11 | + * on every HTTP call, an undocumented behaviour, and the CRON arguments must carry the secret set in |
| 12 | - * (see docs/adr/0005-authenticate-command-invocations-with-a-shared-secret.md). | 12 | + * SCALEWAY_CRON_SECRET (see docs/adr/0005-authenticate-command-invocations-with-a-shared-secret.md). |
| 13 | - * This is the one place to change once the probe finds a platform marker HTTP clients cannot forge. | 13 | + * Otherwise a body naming a command is a plain HTTP body. |
| 14 | */ | 14 | */ |
| 15 | final readonly class CommandInvocation | 15 | final readonly class CommandInvocation |
| 16 | { | 16 | { |
| 17 | public const string SECRET_VAR = 'SCALEWAY_CRON_SECRET'; | 17 | public const string SECRET_VAR = 'SCALEWAY_CRON_SECRET'; |
| 18 | 18 | ||
| 19 | + /** Set by the platform on every HTTP call, overwriting client values, and absent from CRON Events. */ | ||
| 20 | + public const array HTTP_ONLY_HEADERS = ['x-real-ip', 'x-envoy-external-address']; | ||
| 21 | + | ||
| 19 | /** | 22 | /** |
| 20 | * @param array<string, mixed> $args ArrayInput parameters: argument names and "--option" names to values | 23 | * @param array<string, mixed> $args ArrayInput parameters: argument names and "--option" names to values |
| 21 | */ | 24 | */ |
| @@ -41,10 +44,16 @@ final readonly class CommandInvocation | |||
| 41 | return null; | 44 | return null; |
| 42 | } | 45 | } |
| 43 | 46 | ||
| 44 | - if (!\is_string($secret) || '' === $secret || !\is_string($data['secret'] ?? null) || !hash_equals($secret, $data['secret'])) { | 47 | + $headers = \is_array($event['headers'] ?? null) ? array_change_key_case($event['headers']) : []; |
| 48 | + $reason = match (true) { | ||
| 49 | + [] !== array_intersect_key($headers, array_flip(self::HTTP_ONLY_HEADERS)) => 'the Event comes from an HTTP call, not a CRON trigger', | ||
| 50 | + !\is_string($secret) || '' === $secret || !\is_string($data['secret'] ?? null) || !hash_equals($secret, $data['secret']) => \sprintf('the "secret" does not match %s', self::SECRET_VAR), | ||
| 51 | + default => null, | ||
| 52 | + }; | ||
| 53 | + if (null !== $reason) { | ||
| 45 | // Only bodies meant as Command Invocations are worth a line: APIs may legitimately take a "command" field. | 54 | // Only bodies meant as Command Invocations are worth a line: APIs may legitimately take a "command" field. |
| 46 | if (\array_key_exists('secret', $data)) { | 55 | if (\array_key_exists('secret', $data)) { |
| 47 | - fwrite($errorLog, \sprintf('Command Invocation rejected: the "secret" does not match %s, the body goes to the Application as a plain HTTP body.', self::SECRET_VAR).\PHP_EOL); | 56 | + fwrite($errorLog, \sprintf('Command Invocation rejected: %s, the body goes to the Application as a plain HTTP body.', $reason).\PHP_EOL); |
| 48 | } | 57 | } |
| 49 | 58 | ||
| 50 | return null; | 59 | return null; |
modified
tests/CommandInvocationTest.php +41 -0 | @@ -93,6 +93,47 @@ final class CommandInvocationTest extends TestCase | ||
| 93 | 93 | self::assertStringNotContainsString('guess', $this->errorLog()); |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | + /** | |
| 97 | + * @return iterable<string, array{string}> | |
| 98 | + */ | |
| 99 | + public static function httpOnlyHeaders(): iterable | |
| 100 | + { | |
| 101 | + yield 'X-Real-Ip' => ['X-Real-Ip']; | |
| 102 | + yield 'X-Envoy-External-Address' => ['X-Envoy-External-Address']; | |
| 103 | + yield 'lowercase' => ['x-real-ip']; | |
| 104 | + } | |
| 105 | + | |
| 106 | + #[DataProvider('httpOnlyHeaders')] | |
| 107 | + public function testHttpCallCarryingTheSecretIsNotACommandInvocation(string $header): void | |
| 108 | + { | |
| 109 | + $event = ['httpMethod' => 'POST', 'path' => '/', 'headers' => [$header => '2001:db8::1'], 'body' => '{"command":"app:sync","secret":"s3cret"}']; | |
| 110 | + | |
| 111 | + self::assertNull(CommandInvocation::fromEvent($event, $this->errorLog)); | |
| 112 | + self::assertStringContainsString('the Event comes from an HTTP call', $this->errorLog()); | |
| 113 | + self::assertStringNotContainsString('s3cret', $this->errorLog()); | |
| 114 | + } | |
| 115 | + | |
| 116 | + public function testRealCronEventIsACommandInvocation(): void | |
| 117 | + { | |
| 118 | + $event = [ | |
| 119 | + 'path' => '/', | |
| 120 | + 'httpMethod' => 'POST', | |
| 121 | + 'headers' => [ | |
| 122 | + 'Content-Type' => 'application/json', | |
| 123 | + 'Forwarded' => 'for=100.96.2.180;proto=http', | |
| 124 | + 'K-Proxy-Request' => 'activator', | |
| 125 | + 'User-Agent' => 'Go-http-client/1.1', | |
| 126 | + 'X-Forwarded-For' => '100.96.2.180, 100.96.36.219', | |
| 127 | + 'X-Forwarded-Proto' => 'http', | |
| 128 | + 'X-Request-Id' => '0b8e4a52-7c1d-4e0f-8a3b-5d6c7e8f9a01', | |
| 129 | + ], | |
| 130 | + 'queryStringParameters' => [], | |
| 131 | + 'body' => '{"command":"app:sync","args":{"--force":true},"secret":"s3cret"}', | |
| 132 | + ]; | |
| 133 | + | |
| 134 | + self::assertSame(['--force' => true], CommandInvocation::fromEvent($event, $this->errorLog)?->args); | |
| 135 | + } | |
| 136 | + | |
| 96 | 137 | public function testArgsMustNameTheirParameters(): void |
| 97 | 138 | { |
| 98 | 139 | $this->expectException(\UnexpectedValueException::class); |
| @@ -93,6 +93,47 @@ final class CommandInvocationTest extends TestCase | |||
| 93 | self::assertStringNotContainsString('guess', $this->errorLog()); | 93 | self::assertStringNotContainsString('guess', $this->errorLog()); |
| 94 | } | 94 | } |
| 95 | 95 | ||
| 96 | + /** | ||
| 97 | + * @return iterable<string, array{string}> | ||
| 98 | + */ | ||
| 99 | + public static function httpOnlyHeaders(): iterable | ||
| 100 | + { | ||
| 101 | + yield 'X-Real-Ip' => ['X-Real-Ip']; | ||
| 102 | + yield 'X-Envoy-External-Address' => ['X-Envoy-External-Address']; | ||
| 103 | + yield 'lowercase' => ['x-real-ip']; | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + #[DataProvider('httpOnlyHeaders')] | ||
| 107 | + public function testHttpCallCarryingTheSecretIsNotACommandInvocation(string $header): void | ||
| 108 | + { | ||
| 109 | + $event = ['httpMethod' => 'POST', 'path' => '/', 'headers' => [$header => '2001:db8::1'], 'body' => '{"command":"app:sync","secret":"s3cret"}']; | ||
| 110 | + | ||
| 111 | + self::assertNull(CommandInvocation::fromEvent($event, $this->errorLog)); | ||
| 112 | + self::assertStringContainsString('the Event comes from an HTTP call', $this->errorLog()); | ||
| 113 | + self::assertStringNotContainsString('s3cret', $this->errorLog()); | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + public function testRealCronEventIsACommandInvocation(): void | ||
| 117 | + { | ||
| 118 | + $event = [ | ||
| 119 | + 'path' => '/', | ||
| 120 | + 'httpMethod' => 'POST', | ||
| 121 | + 'headers' => [ | ||
| 122 | + 'Content-Type' => 'application/json', | ||
| 123 | + 'Forwarded' => 'for=100.96.2.180;proto=http', | ||
| 124 | + 'K-Proxy-Request' => 'activator', | ||
| 125 | + 'User-Agent' => 'Go-http-client/1.1', | ||
| 126 | + 'X-Forwarded-For' => '100.96.2.180, 100.96.36.219', | ||
| 127 | + 'X-Forwarded-Proto' => 'http', | ||
| 128 | + 'X-Request-Id' => '0b8e4a52-7c1d-4e0f-8a3b-5d6c7e8f9a01', | ||
| 129 | + ], | ||
| 130 | + 'queryStringParameters' => [], | ||
| 131 | + 'body' => '{"command":"app:sync","args":{"--force":true},"secret":"s3cret"}', | ||
| 132 | + ]; | ||
| 133 | + | ||
| 134 | + self::assertSame(['--force' => true], CommandInvocation::fromEvent($event, $this->errorLog)?->args); | ||
| 135 | + } | ||
| 136 | + | ||
| 96 | public function testArgsMustNameTheirParameters(): void | 137 | public function testArgsMustNameTheirParameters(): void |
| 97 | { | 138 | { |
| 98 | $this->expectException(\UnexpectedValueException::class); | 139 | $this->expectException(\UnexpectedValueException::class); |