jdecool/scaleway-runtimepublic Fork
⑂ ec8387c
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

fix: accept raw callable Function Responses without headersUnverified

Scaleway's minimal documented PHP response only carries statusCode and
body, which raw callables were answering with a 500. Headers are now
optional and stay absent when omitted, and a null isBase64Encoded is
rejected instead of being turned into false.

HttpConverter keeps returning headers, so its own return type stays
narrower than the widened FunctionResponse.
Jérémy DECOOL committed 2026-11-14T02:15:32+01:00 Browse files
ec8387c parent: 80a8c50
modified docs/plan.md +1 -1
@@ -18,7 +18,7 @@ Vocabulary follows [GLOSSARY.md](../GLOSSARY.md); decisions are recorded in [doc
1818 - `vendor/bin/scaleway-package` builds a zip with `vendor/` (`--no-dev --classmap-authoritative`, from `composer.lock`) and a warmed prod cache, excluding `var/`, `tests/`, `.env.local` (ADR 0003).
1919 - CRON Invocations become `POST /` HTTP requests by default; a JSON body `{"command": "...", "args": {...}, "secret": "..."}` whose secret matches `SCALEWAY_CRON_SECRET` makes it a Command Invocation run through the Console Application of the same Kernel, non-interactive, `args` mapped to an `ArrayInput`; the exit code 0 yields a 200 and any other a 500, both with the command output (ADR 0005).
2020 - A Front Controller returning a Console `Application` or a `Command` serves Command Invocations only and answers a 403 to the others; services are reset before and after each command, as no HTTP request follows to trigger the Kernel's own reset.
21-- A Front Controller closure returning a `Closure` makes it a raw callable `fn (array $event, mixed $context): array`, built once like a Kernel (GenericRuntime would run it as a zero-argument script, meaningless per Invocation; invokable objects keep their type-based dispatch, a `$object(...)` turns one into a raw callable); it gets every Event and context untouched, CRON Invocations included without Command Invocation routing (raw means raw), and its return value must be the documented Function Response shape (int `statusCode`, `headers` of strings or lists of strings, string `body`, optional bool `isBase64Encoded`), undocumented keys dropped, anything else being a 500.
21+- A Front Controller closure returning a `Closure` makes it a raw callable `fn (array $event, mixed $context): array`, built once like a Kernel (GenericRuntime would run it as a zero-argument script, meaningless per Invocation; invokable objects keep their type-based dispatch, a `$object(...)` turns one into a raw callable); it gets every Event and context untouched, CRON Invocations included without Command Invocation routing (raw means raw), and its return value must be the documented Function Response shape (int `statusCode`, optional `headers` of strings or lists of strings, string `body`, optional bool `isBase64Encoded`), undocumented keys dropped, anything else being a 500.
2222 - Tests: PHPUnit on conversions, GitHub Actions CI (PHPUnit, PHPStan max, PHP-CS-Fixer; PHP 8.4/8.5 x Symfony 7.4/8.x), local Emulator `vendor/bin/scaleway-serve`, manual real E2E deployed with Terraform.
2323
2424 ## Phases
@@ -18,7 +18,7 @@ Vocabulary follows [GLOSSARY.md](../GLOSSARY.md); decisions are recorded in [doc
18 - `vendor/bin/scaleway-package` builds a zip with `vendor/` (`--no-dev --classmap-authoritative`, from `composer.lock`) and a warmed prod cache, excluding `var/`, `tests/`, `.env.local` (ADR 0003).18 - `vendor/bin/scaleway-package` builds a zip with `vendor/` (`--no-dev --classmap-authoritative`, from `composer.lock`) and a warmed prod cache, excluding `var/`, `tests/`, `.env.local` (ADR 0003).
19 - CRON Invocations become `POST /` HTTP requests by default; a JSON body `{"command": "...", "args": {...}, "secret": "..."}` whose secret matches `SCALEWAY_CRON_SECRET` makes it a Command Invocation run through the Console Application of the same Kernel, non-interactive, `args` mapped to an `ArrayInput`; the exit code 0 yields a 200 and any other a 500, both with the command output (ADR 0005).19 - CRON Invocations become `POST /` HTTP requests by default; a JSON body `{"command": "...", "args": {...}, "secret": "..."}` whose secret matches `SCALEWAY_CRON_SECRET` makes it a Command Invocation run through the Console Application of the same Kernel, non-interactive, `args` mapped to an `ArrayInput`; the exit code 0 yields a 200 and any other a 500, both with the command output (ADR 0005).
20 - A Front Controller returning a Console `Application` or a `Command` serves Command Invocations only and answers a 403 to the others; services are reset before and after each command, as no HTTP request follows to trigger the Kernel's own reset.20 - A Front Controller returning a Console `Application` or a `Command` serves Command Invocations only and answers a 403 to the others; services are reset before and after each command, as no HTTP request follows to trigger the Kernel's own reset.
21-- A Front Controller closure returning a `Closure` makes it a raw callable `fn (array $event, mixed $context): array`, built once like a Kernel (GenericRuntime would run it as a zero-argument script, meaningless per Invocation; invokable objects keep their type-based dispatch, a `$object(...)` turns one into a raw callable); it gets every Event and context untouched, CRON Invocations included without Command Invocation routing (raw means raw), and its return value must be the documented Function Response shape (int `statusCode`, `headers` of strings or lists of strings, string `body`, optional bool `isBase64Encoded`), undocumented keys dropped, anything else being a 500.21+- A Front Controller closure returning a `Closure` makes it a raw callable `fn (array $event, mixed $context): array`, built once like a Kernel (GenericRuntime would run it as a zero-argument script, meaningless per Invocation; invokable objects keep their type-based dispatch, a `$object(...)` turns one into a raw callable); it gets every Event and context untouched, CRON Invocations included without Command Invocation routing (raw means raw), and its return value must be the documented Function Response shape (int `statusCode`, optional `headers` of strings or lists of strings, string `body`, optional bool `isBase64Encoded`), undocumented keys dropped, anything else being a 500.
22 - Tests: PHPUnit on conversions, GitHub Actions CI (PHPUnit, PHPStan max, PHP-CS-Fixer; PHP 8.4/8.5 x Symfony 7.4/8.x), local Emulator `vendor/bin/scaleway-serve`, manual real E2E deployed with Terraform.22 - Tests: PHPUnit on conversions, GitHub Actions CI (PHPUnit, PHPStan max, PHP-CS-Fixer; PHP 8.4/8.5 x Symfony 7.4/8.x), local Emulator `vendor/bin/scaleway-serve`, manual real E2E deployed with Terraform.
23 23
24 ## Phases24 ## Phases
modified src/HttpConverter.php +2 -2
@@ -12,7 +12,7 @@ use Symfony\Component\HttpFoundation\Response;
1212 /**
1313 * Converts an Event into a Symfony Request, and a Symfony Response into a Function Response.
1414 *
15- * @phpstan-type FunctionResponse array{statusCode: int, headers: array<string, string|list<string>>, body: string, isBase64Encoded?: bool}
15+ * @phpstan-type FunctionResponse array{statusCode: int, headers?: array<string, string|list<string>>, body: string, isBase64Encoded?: bool}
1616 */
1717 final class HttpConverter
1818 {
@@ -144,7 +144,7 @@ final class HttpConverter
144144 }
145145
146146 /**
147- * @return FunctionResponse
147+ * @return array{statusCode: int, headers: array<string, string|list<string>>, body: string, isBase64Encoded?: true}
148148 */
149149 public static function toFunctionResponse(Response $response): array
150150 {
@@ -12,7 +12,7 @@ use Symfony\Component\HttpFoundation\Response;
12 /**12 /**
13 * Converts an Event into a Symfony Request, and a Symfony Response into a Function Response.13 * Converts an Event into a Symfony Request, and a Symfony Response into a Function Response.
14 *14 *
15- * @phpstan-type FunctionResponse array{statusCode: int, headers: array<string, string|list<string>>, body: string, isBase64Encoded?: bool}15+ * @phpstan-type FunctionResponse array{statusCode: int, headers?: array<string, string|list<string>>, body: string, isBase64Encoded?: bool}
16 */16 */
17 final class HttpConverter17 final class HttpConverter
18 {18 {
@@ -144,7 +144,7 @@ final class HttpConverter
144 }144 }
145 145
146 /**146 /**
147- * @return FunctionResponse147+ * @return array{statusCode: int, headers: array<string, string|list<string>>, body: string, isBase64Encoded?: true}
148 */148 */
149 public static function toFunctionResponse(Response $response): array149 public static function toFunctionResponse(Response $response): array
150 {150 {
modified src/Runner/CallableRunner.php +13 -8
@@ -24,20 +24,25 @@ final readonly class CallableRunner implements InvocationRunner
2424 {
2525 $response = ($this->callable)($event, $context);
2626
27- // Scaleway only documents this shape for PHP, and what it makes of anything else is unknown: keys it does
28- // not document are dropped rather than passed along.
27+ // Scaleway only documents this shape for PHP, its minimal example without headers, and what it makes of
28+ // anything else is unknown: keys it does not document are dropped rather than passed along.
2929 $fields = \is_array($response) ? $response : [];
3030 $statusCode = $fields['statusCode'] ?? null;
31- $headers = $fields['headers'] ?? null;
3231 $body = $fields['body'] ?? null;
33- $base64 = $fields['isBase64Encoded'] ?? false;
34- if (!\is_int($statusCode) || !self::isHeaders($headers) || !\is_string($body) || !\is_bool($base64)) {
35- throw new \UnexpectedValueException(\sprintf('A raw callable must return a Function Response: an array with an int "statusCode", "headers" mapping names to a string or a list of strings, a string "body" and an optional bool "isBase64Encoded"; "%s" returned.', get_debug_type($response)));
32+ if (!\is_int($statusCode) || !\is_string($body)
33+ || \array_key_exists('headers', $fields) && !self::isHeaders($fields['headers'])
34+ || \array_key_exists('isBase64Encoded', $fields) && !\is_bool($fields['isBase64Encoded'])
35+ ) {
36+ throw new \UnexpectedValueException(\sprintf('A raw callable must return a Function Response: an array with an int "statusCode", optional "headers" mapping names to a string or a list of strings, a string "body" and an optional bool "isBase64Encoded"; "%s" returned.', get_debug_type($response)));
3637 }
3738
38- $functionResponse = ['statusCode' => $statusCode, 'headers' => $headers, 'body' => $body];
39+ $functionResponse = ['statusCode' => $statusCode, 'body' => $body];
40+ // Absent keys stay absent, as defaulting them would make up what the platform does without them.
41+ if (\array_key_exists('headers', $fields)) {
42+ $functionResponse['headers'] = $fields['headers'];
43+ }
3944 if (\array_key_exists('isBase64Encoded', $fields)) {
40- $functionResponse['isBase64Encoded'] = $base64;
45+ $functionResponse['isBase64Encoded'] = $fields['isBase64Encoded'];
4146 }
4247
4348 return $functionResponse;
@@ -24,20 +24,25 @@ final readonly class CallableRunner implements InvocationRunner
24 {24 {
25 $response = ($this->callable)($event, $context);25 $response = ($this->callable)($event, $context);
26 26
27- // Scaleway only documents this shape for PHP, and what it makes of anything else is unknown: keys it does27+ // Scaleway only documents this shape for PHP, its minimal example without headers, and what it makes of
28- // not document are dropped rather than passed along.28+ // anything else is unknown: keys it does not document are dropped rather than passed along.
29 $fields = \is_array($response) ? $response : [];29 $fields = \is_array($response) ? $response : [];
30 $statusCode = $fields['statusCode'] ?? null;30 $statusCode = $fields['statusCode'] ?? null;
31- $headers = $fields['headers'] ?? null;
32 $body = $fields['body'] ?? null;31 $body = $fields['body'] ?? null;
33- $base64 = $fields['isBase64Encoded'] ?? false;32+ if (!\is_int($statusCode) || !\is_string($body)
34- if (!\is_int($statusCode) || !self::isHeaders($headers) || !\is_string($body) || !\is_bool($base64)) {33+ || \array_key_exists('headers', $fields) && !self::isHeaders($fields['headers'])
35- throw new \UnexpectedValueException(\sprintf('A raw callable must return a Function Response: an array with an int "statusCode", "headers" mapping names to a string or a list of strings, a string "body" and an optional bool "isBase64Encoded"; "%s" returned.', get_debug_type($response)));34+ || \array_key_exists('isBase64Encoded', $fields) && !\is_bool($fields['isBase64Encoded'])
35+ ) {
36+ throw new \UnexpectedValueException(\sprintf('A raw callable must return a Function Response: an array with an int "statusCode", optional "headers" mapping names to a string or a list of strings, a string "body" and an optional bool "isBase64Encoded"; "%s" returned.', get_debug_type($response)));
36 }37 }
37 38
38- $functionResponse = ['statusCode' => $statusCode, 'headers' => $headers, 'body' => $body];39+ $functionResponse = ['statusCode' => $statusCode, 'body' => $body];
40+ // Absent keys stay absent, as defaulting them would make up what the platform does without them.
41+ if (\array_key_exists('headers', $fields)) {
42+ $functionResponse['headers'] = $fields['headers'];
43+ }
39 if (\array_key_exists('isBase64Encoded', $fields)) {44 if (\array_key_exists('isBase64Encoded', $fields)) {
40- $functionResponse['isBase64Encoded'] = $base64;45+ $functionResponse['isBase64Encoded'] = $fields['isBase64Encoded'];
41 }46 }
42 47
43 return $functionResponse;48 return $functionResponse;
modified tests/Fixtures/front-controllers/raw.php +2 -0
@@ -22,6 +22,8 @@ return static function (): Closure {
2222 '/invalid' => ['statusCode' => '200', 'headers' => [], 'body' => 'not an int status'],
2323 '/bad-header' => ['statusCode' => 200, 'headers' => ['X-Bad' => ['a' => 'b']], 'body' => ''],
2424 '/string' => 'plain body',
25+ '/null-base64' => ['statusCode' => 200, 'body' => '', 'isBase64Encoded' => null],
26+ '/minimal' => ['statusCode' => 200, 'body' => 'no headers'],
2527 default => [
2628 'statusCode' => 202,
2729 'headers' => ['Content-Type' => 'application/json', 'X-Multi' => ['a', 'b']],
@@ -22,6 +22,8 @@ return static function (): Closure {
22 '/invalid' => ['statusCode' => '200', 'headers' => [], 'body' => 'not an int status'],22 '/invalid' => ['statusCode' => '200', 'headers' => [], 'body' => 'not an int status'],
23 '/bad-header' => ['statusCode' => 200, 'headers' => ['X-Bad' => ['a' => 'b']], 'body' => ''],23 '/bad-header' => ['statusCode' => 200, 'headers' => ['X-Bad' => ['a' => 'b']], 'body' => ''],
24 '/string' => 'plain body',24 '/string' => 'plain body',
25+ '/null-base64' => ['statusCode' => 200, 'body' => '', 'isBase64Encoded' => null],
26+ '/minimal' => ['statusCode' => 200, 'body' => 'no headers'],
25 default => [27 default => [
26 'statusCode' => 202,28 'statusCode' => 202,
27 'headers' => ['Content-Type' => 'application/json', 'X-Multi' => ['a', 'b']],29 'headers' => ['Content-Type' => 'application/json', 'X-Multi' => ['a', 'b']],
modified tests/HandlerTest.php +9 -1
@@ -267,7 +267,7 @@ final class HandlerTest extends TestCase
267267 $second = $handler->invoke(['path' => '/again'], null);
268268
269269 self::assertSame(202, $first['statusCode']);
270- self::assertSame(['Content-Type' => 'application/json', 'X-Multi' => ['a', 'b']], $first['headers']);
270+ self::assertSame(['Content-Type' => 'application/json', 'X-Multi' => ['a', 'b']], $first['headers'] ?? null);
271271 self::assertFalse($first['isBase64Encoded'] ?? null);
272272 self::assertArrayNotHasKey('multiValueHeaders', $first, 'Keys Scaleway does not document must be dropped.');
273273 self::assertSame(['calls' => 1, 'event' => $event, 'context' => ['memoryLimitInMb' => 128]], $this->decode($first));
@@ -275,6 +275,13 @@ final class HandlerTest extends TestCase
275275 self::assertSame(1, RecordingKernel::$builds);
276276 }
277277
278+ public function testRawCallableMayOmitHeadersAsInScalewaysMinimalExample(): void
279+ {
280+ $response = $this->handler(['front_controller' => 'front-controllers/raw.php'])->invoke(['path' => '/minimal'], []);
281+
282+ self::assertSame(['statusCode' => 200, 'body' => 'no headers'], $response);
283+ }
284+
278285 public function testRawCallableGetsCommandInvocationsAsPlainEvents(): void
279286 {
280287 $event = $this->commandEvent('app:greet');
@@ -304,6 +311,7 @@ final class HandlerTest extends TestCase
304311 yield 'non-int status' => ['/invalid', 'must return a Function Response'];
305312 yield 'nested header value' => ['/bad-header', 'must return a Function Response'];
306313 yield 'plain string' => ['/string', '"string" returned'];
314+ yield 'null base64 flag' => ['/null-base64', 'must return a Function Response'];
307315 }
308316
309317 #[DataProvider('failingRawCallables')]
@@ -267,7 +267,7 @@ final class HandlerTest extends TestCase
267 $second = $handler->invoke(['path' => '/again'], null);267 $second = $handler->invoke(['path' => '/again'], null);
268 268
269 self::assertSame(202, $first['statusCode']);269 self::assertSame(202, $first['statusCode']);
270- self::assertSame(['Content-Type' => 'application/json', 'X-Multi' => ['a', 'b']], $first['headers']);270+ self::assertSame(['Content-Type' => 'application/json', 'X-Multi' => ['a', 'b']], $first['headers'] ?? null);
271 self::assertFalse($first['isBase64Encoded'] ?? null);271 self::assertFalse($first['isBase64Encoded'] ?? null);
272 self::assertArrayNotHasKey('multiValueHeaders', $first, 'Keys Scaleway does not document must be dropped.');272 self::assertArrayNotHasKey('multiValueHeaders', $first, 'Keys Scaleway does not document must be dropped.');
273 self::assertSame(['calls' => 1, 'event' => $event, 'context' => ['memoryLimitInMb' => 128]], $this->decode($first));273 self::assertSame(['calls' => 1, 'event' => $event, 'context' => ['memoryLimitInMb' => 128]], $this->decode($first));
@@ -275,6 +275,13 @@ final class HandlerTest extends TestCase
275 self::assertSame(1, RecordingKernel::$builds);275 self::assertSame(1, RecordingKernel::$builds);
276 }276 }
277 277
278+ public function testRawCallableMayOmitHeadersAsInScalewaysMinimalExample(): void
279+ {
280+ $response = $this->handler(['front_controller' => 'front-controllers/raw.php'])->invoke(['path' => '/minimal'], []);
281+
282+ self::assertSame(['statusCode' => 200, 'body' => 'no headers'], $response);
283+ }
284+
278 public function testRawCallableGetsCommandInvocationsAsPlainEvents(): void285 public function testRawCallableGetsCommandInvocationsAsPlainEvents(): void
279 {286 {
280 $event = $this->commandEvent('app:greet');287 $event = $this->commandEvent('app:greet');
@@ -304,6 +311,7 @@ final class HandlerTest extends TestCase
304 yield 'non-int status' => ['/invalid', 'must return a Function Response'];311 yield 'non-int status' => ['/invalid', 'must return a Function Response'];
305 yield 'nested header value' => ['/bad-header', 'must return a Function Response'];312 yield 'nested header value' => ['/bad-header', 'must return a Function Response'];
306 yield 'plain string' => ['/string', '"string" returned'];313 yield 'plain string' => ['/string', '"string" returned'];
314+ yield 'null base64 flag' => ['/null-base64', 'must return a Function Response'];
307 }315 }
308 316
309 #[DataProvider('failingRawCallables')]317 #[DataProvider('failingRawCallables')]