jdecool/scaleway-runtimepublic Fork
⑂ 08955ad
Commits
⬇ Clone ▾
git clone https://git.rickub.com/jdecool/scaleway-runtime.git
git clone ssh://git@rickub.com/jdecool/scaleway-runtime.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP

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.
Jérémy DECOOL committed 2026-11-14T11:53:14+01:00 Browse files
08955ad parent: 2352980
modified docs/adr/0005-authenticate-command-invocations-with-a-shared-secret.md +9 -7
@@ -1,18 +1,20 @@
1-# Command Invocations are authenticated by a shared secret carried in the CRON arguments
1+# Command Invocations need both a CRON-shaped Event and a shared secret carried in the CRON arguments
22
3-A CRON Invocation reaches the function as an Event whose body holds the trigger's arguments, and nothing documented tells it apart from an HTTP Invocation: a public function would otherwise run any console command an internet client POSTs as `{"command": ...}`.
3+A CRON Invocation reaches the function as an Event whose body holds the trigger's arguments: a public function would otherwise run any console command an internet client POSTs as `{"command": ...}`.
44 We therefore only run a Command Invocation when its body also carries a `secret` equal, in constant time, to the `SCALEWAY_CRON_SECRET` environment variable (a secret one on Scaleway); otherwise, and always when that variable is unset or empty, the body goes to the Application as a plain HTTP body, so Command Invocations are off by default.
5-The check lives in `CommandInvocation::fromEvent()` alone, to be replaced or complemented once the probe finds a platform marker that HTTP clients cannot forge.
5+The probe then found that CRON Events carry neither `X-Real-Ip` nor `X-Envoy-External-Address`, which the platform sets on every HTTP call, overwriting client values: an Event carrying either is never a Command Invocation, even with the right secret.
6+Both checks live in `CommandInvocation::fromEvent()`, as defense in depth: the marker is undocumented platform behaviour that may change, and the secret alone would run commands for anyone who reads it.
67
78 ## Considered Options
89
9-- Trusting a body naming a command, or a CRON-looking Event shape (method, path, missing headers): rejected, any HTTP client can send the same.
10-- A platform marker (header, context field): preferred once known, but the CRON Event shape is still unmeasured, and a header is only a marker if the platform strips it from HTTP calls.
11-- Private functions, authenticated by Scaleway tokens: rejected as the default, as the Application could no longer serve public HTTP Invocations, and whether CRON Invocations of a private function carry anything verifiable is unknown.
10+- Trusting a body naming a command, or a CRON-looking Event shape (method, path): rejected, any HTTP client can send the same.
11+- The marker alone: rejected, it is undocumented, so a platform change that stops setting those headers would open every public function.
12+- Private functions, authenticated by Scaleway tokens: rejected as the default, as the Application could no longer serve public HTTP Invocations.
1213 - An allowlist of commands: rejected, as the CRON arguments that name the commands already live next to the secret, in the Scaleway project and its Terraform state, so whoever can read the secret can usually redeploy the function too.
1314
1415 ## Consequences
1516
1617 - The secret is stored in clear in the CRON arguments, visible to anyone who can read the function's triggers or the Terraform state, and it is the same for every run: rotating it means updating the variable and every CRON trigger together.
17-- A body carrying a wrong `secret` is logged, without its value, so a misconfigured trigger shows up in the logs instead of silently landing on `POST /`.
18+- A body carrying a `secret` that is rejected, for a wrong value or because the Event comes from an HTTP call, is logged without its value, so a misconfigured trigger shows up in the logs instead of silently landing on `POST /`.
19+- Command Invocations cannot be sent over HTTP, so the Emulator simulates CRON triggers on a reserved path.
1820 - A Front Controller returning a Console Application or a Command serves Command Invocations only, and answers every other Invocation with a 403.
@@ -1,18 +1,20 @@
1-# Command Invocations are authenticated by a shared secret carried in the CRON arguments1+# Command Invocations need both a CRON-shaped Event and a shared secret carried in the CRON arguments
2 2
3-A CRON Invocation reaches the function as an Event whose body holds the trigger's arguments, and nothing documented tells it apart from an HTTP Invocation: a public function would otherwise run any console command an internet client POSTs as `{"command": ...}`.3+A CRON Invocation reaches the function as an Event whose body holds the trigger's arguments: a public function would otherwise run any console command an internet client POSTs as `{"command": ...}`.
4 We therefore only run a Command Invocation when its body also carries a `secret` equal, in constant time, to the `SCALEWAY_CRON_SECRET` environment variable (a secret one on Scaleway); otherwise, and always when that variable is unset or empty, the body goes to the Application as a plain HTTP body, so Command Invocations are off by default.4 We therefore only run a Command Invocation when its body also carries a `secret` equal, in constant time, to the `SCALEWAY_CRON_SECRET` environment variable (a secret one on Scaleway); otherwise, and always when that variable is unset or empty, the body goes to the Application as a plain HTTP body, so Command Invocations are off by default.
5-The check lives in `CommandInvocation::fromEvent()` alone, to be replaced or complemented once the probe finds a platform marker that HTTP clients cannot forge.5+The probe then found that CRON Events carry neither `X-Real-Ip` nor `X-Envoy-External-Address`, which the platform sets on every HTTP call, overwriting client values: an Event carrying either is never a Command Invocation, even with the right secret.
6+Both checks live in `CommandInvocation::fromEvent()`, as defense in depth: the marker is undocumented platform behaviour that may change, and the secret alone would run commands for anyone who reads it.
6 7
7 ## Considered Options8 ## Considered Options
8 9
9-- Trusting a body naming a command, or a CRON-looking Event shape (method, path, missing headers): rejected, any HTTP client can send the same.10+- Trusting a body naming a command, or a CRON-looking Event shape (method, path): rejected, any HTTP client can send the same.
10-- A platform marker (header, context field): preferred once known, but the CRON Event shape is still unmeasured, and a header is only a marker if the platform strips it from HTTP calls.11+- The marker alone: rejected, it is undocumented, so a platform change that stops setting those headers would open every public function.
11-- Private functions, authenticated by Scaleway tokens: rejected as the default, as the Application could no longer serve public HTTP Invocations, and whether CRON Invocations of a private function carry anything verifiable is unknown.12+- Private functions, authenticated by Scaleway tokens: rejected as the default, as the Application could no longer serve public HTTP Invocations.
12 - An allowlist of commands: rejected, as the CRON arguments that name the commands already live next to the secret, in the Scaleway project and its Terraform state, so whoever can read the secret can usually redeploy the function too.13 - An allowlist of commands: rejected, as the CRON arguments that name the commands already live next to the secret, in the Scaleway project and its Terraform state, so whoever can read the secret can usually redeploy the function too.
13 14
14 ## Consequences15 ## Consequences
15 16
16 - The secret is stored in clear in the CRON arguments, visible to anyone who can read the function's triggers or the Terraform state, and it is the same for every run: rotating it means updating the variable and every CRON trigger together.17 - The secret is stored in clear in the CRON arguments, visible to anyone who can read the function's triggers or the Terraform state, and it is the same for every run: rotating it means updating the variable and every CRON trigger together.
17-- A body carrying a wrong `secret` is logged, without its value, so a misconfigured trigger shows up in the logs instead of silently landing on `POST /`.18+- A body carrying a `secret` that is rejected, for a wrong value or because the Event comes from an HTTP call, is logged without its value, so a misconfigured trigger shows up in the logs instead of silently landing on `POST /`.
19+- Command Invocations cannot be sent over HTTP, so the Emulator simulates CRON triggers on a reserved path.
18 - A Front Controller returning a Console Application or a Command serves Command Invocations only, and answers every other Invocation with a 403.20 - A Front Controller returning a Console Application or a Command serves Command Invocations only, and answers every other Invocation with a 403.
modified src/CommandInvocation.php +15 -6
@@ -7,15 +7,18 @@ namespace JDecool\ScalewayRuntime;
77 /**
88 * A console command an authenticated CRON Invocation asks to run.
99 *
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.
1414 */
1515 final readonly class CommandInvocation
1616 {
1717 public const string SECRET_VAR = 'SCALEWAY_CRON_SECRET';
1818
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+
1922 /**
2023 * @param array<string, mixed> $args ArrayInput parameters: argument names and "--option" names to values
2124 */
@@ -41,10 +44,16 @@ final readonly class CommandInvocation
4144 return null;
4245 }
4346
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) {
4554 // Only bodies meant as Command Invocations are worth a line: APIs may legitimately take a "command" field.
4655 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);
4857 }
4958
5059 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 carry10+ * 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 body11+ * 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 CommandInvocation15 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 values23 * @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
9393 self::assertStringNotContainsString('guess', $this->errorLog());
9494 }
9595
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+
96137 public function testArgsMustNameTheirParameters(): void
97138 {
98139 $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(): void137 public function testArgsMustNameTheirParameters(): void
97 {138 {
98 $this->expectException(\UnexpectedValueException::class);139 $this->expectException(\UnexpectedValueException::class);