jdecool/scaleway-runtimepublic Fork
⑂ 29dad8b
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(emulator): sort query keys like the platformUnverified

Jérémy DECOOL committed 2026-11-14T13:56:42+01:00 Browse files
29dad8b parent: 22edd31
modified README.md +2 -2
@@ -392,7 +392,7 @@ PHP's built-in server receives the requests, and forwards the Events to that pro
392392
393393 The Event and the context are built the way the platform builds them ([measured](docs/probe-results.md)):
394394
395-- the `httpMethod`, `queryStringParameters` (the last value of a repeated key winning, `[]` without query string) and other keys of the platform;
395+- the `httpMethod`, `queryStringParameters` (sorted by key, the last value of a repeated key winning, `[]` without query string) and other keys of the platform;
396396 - header names in `Title-Case`, repeated headers joined with `, `, no `Host`;
397397 - `X-Real-Ip`, `X-Envoy-External-Address`, `X-Request-Id` and `X-Forwarded-Proto: https` set whatever the client sends, the client's `X-Forwarded-For` kept in front of its address, its `X-Forwarded-Host` passed through;
398398 - empty, multipart and non UTF-8 bodies base64-encoded with `isBase64Encoded: true`, text bodies without the flag.
@@ -449,7 +449,7 @@ Everything else survives, so your application must be safe for long-running exec
449449 Read the `Request` object instead; `Request::createFromGlobals()` does not describe the Invocation.
450450 - **Output**: only the returned `Response` reaches the client.
451451 `echo` outside the Response, `header()` and `http_response_code()` do not: Scaleway does not put stdout in the response body.
452-- **Query strings**: the Event carries `queryStringParameters` as a map, so a repeated key such as `a=1&a=2`, or `tags[]=a&tags[]=b`, reaches the Application with its last value only.
452+- **Query strings**: the Event carries `queryStringParameters` as a map, so a repeated key such as `a=1&a=2`, or `tags[]=a&tags[]=b`, reaches the Application with its last value only, and the keys arrive sorted, not in the order the client sent them.
453453 Keys are then parsed like PHP does, so `a.b` becomes `a_b` and `a[b]` nests.
454454 - **Form bodies**: `application/x-www-form-urlencoded` bodies are parsed for `POST`, `PUT`, `DELETE`, `PATCH` and `QUERY`, `multipart/form-data` only for `POST`, as PHP does.
455455 Uploaded files are `UploadedFile` objects in test mode: `move()` works, `is_uploaded_file()` does not.
@@ -392,7 +392,7 @@ PHP's built-in server receives the requests, and forwards the Events to that pro
392 392
393 The Event and the context are built the way the platform builds them ([measured](docs/probe-results.md)):393 The Event and the context are built the way the platform builds them ([measured](docs/probe-results.md)):
394 394
395-- the `httpMethod`, `queryStringParameters` (the last value of a repeated key winning, `[]` without query string) and other keys of the platform;395+- the `httpMethod`, `queryStringParameters` (sorted by key, the last value of a repeated key winning, `[]` without query string) and other keys of the platform;
396 - header names in `Title-Case`, repeated headers joined with `, `, no `Host`;396 - header names in `Title-Case`, repeated headers joined with `, `, no `Host`;
397 - `X-Real-Ip`, `X-Envoy-External-Address`, `X-Request-Id` and `X-Forwarded-Proto: https` set whatever the client sends, the client's `X-Forwarded-For` kept in front of its address, its `X-Forwarded-Host` passed through;397 - `X-Real-Ip`, `X-Envoy-External-Address`, `X-Request-Id` and `X-Forwarded-Proto: https` set whatever the client sends, the client's `X-Forwarded-For` kept in front of its address, its `X-Forwarded-Host` passed through;
398 - empty, multipart and non UTF-8 bodies base64-encoded with `isBase64Encoded: true`, text bodies without the flag.398 - empty, multipart and non UTF-8 bodies base64-encoded with `isBase64Encoded: true`, text bodies without the flag.
@@ -449,7 +449,7 @@ Everything else survives, so your application must be safe for long-running exec
449 Read the `Request` object instead; `Request::createFromGlobals()` does not describe the Invocation.449 Read the `Request` object instead; `Request::createFromGlobals()` does not describe the Invocation.
450 - **Output**: only the returned `Response` reaches the client.450 - **Output**: only the returned `Response` reaches the client.
451 `echo` outside the Response, `header()` and `http_response_code()` do not: Scaleway does not put stdout in the response body.451 `echo` outside the Response, `header()` and `http_response_code()` do not: Scaleway does not put stdout in the response body.
452-- **Query strings**: the Event carries `queryStringParameters` as a map, so a repeated key such as `a=1&a=2`, or `tags[]=a&tags[]=b`, reaches the Application with its last value only.452+- **Query strings**: the Event carries `queryStringParameters` as a map, so a repeated key such as `a=1&a=2`, or `tags[]=a&tags[]=b`, reaches the Application with its last value only, and the keys arrive sorted, not in the order the client sent them.
453 Keys are then parsed like PHP does, so `a.b` becomes `a_b` and `a[b]` nests.453 Keys are then parsed like PHP does, so `a.b` becomes `a_b` and `a[b]` nests.
454 - **Form bodies**: `application/x-www-form-urlencoded` bodies are parsed for `POST`, `PUT`, `DELETE`, `PATCH` and `QUERY`, `multipart/form-data` only for `POST`, as PHP does.454 - **Form bodies**: `application/x-www-form-urlencoded` bodies are parsed for `POST`, `PUT`, `DELETE`, `PATCH` and `QUERY`, `multipart/form-data` only for `POST`, as PHP does.
455 Uploaded files are `UploadedFile` objects in test mode: `move()` works, `is_uploaded_file()` does not.455 Uploaded files are `UploadedFile` objects in test mode: `move()` works, `is_uploaded_file()` does not.
modified docs/probe-results.md +1 -1
@@ -28,7 +28,7 @@ Client addresses are replaced with documentation addresses (`2001:db8::1`, `203.
2828 Keys: `resource`, `path`, `httpMethod`, `headers`, `multiValueHeaders` (null), `queryStringParameters`, `multiValueQueryStringParameters` (null), `pathParameters` (null), `stageVariables`, `requestContext` (with `httpMethod`), `body`, `isBase64Encoded`.
2929
3030 - The method is in `httpMethod`; there is no `method` key.
31-- `queryStringParameters` is decoded and keeps the last value of repeated keys: `?repeated=a&repeated=b&list[]=x&list[]=y` gives `{"repeated": "b", "list[]": "y"}`; it is an empty list `[]` when there is no query string.
31+- `queryStringParameters` is decoded, sorted by key, and keeps the last value of repeated keys: `?repeated=a&repeated=b&list[]=x&list[]=y` gives `{"repeated": "b", "list[]": "y"}`; it is an empty list `[]` when there is no query string.
3232 - Header names are canonicalised (`X-Custom`, `User-Agent`), single string values.
3333 - There is no `Host` header.
3434 - `isBase64Encoded` is `true` with a base64 body for binary and multipart bodies, `true` with an empty body for GET, and absent for text bodies (JSON, plain text).
@@ -28,7 +28,7 @@ Client addresses are replaced with documentation addresses (`2001:db8::1`, `203.
28 Keys: `resource`, `path`, `httpMethod`, `headers`, `multiValueHeaders` (null), `queryStringParameters`, `multiValueQueryStringParameters` (null), `pathParameters` (null), `stageVariables`, `requestContext` (with `httpMethod`), `body`, `isBase64Encoded`.28 Keys: `resource`, `path`, `httpMethod`, `headers`, `multiValueHeaders` (null), `queryStringParameters`, `multiValueQueryStringParameters` (null), `pathParameters` (null), `stageVariables`, `requestContext` (with `httpMethod`), `body`, `isBase64Encoded`.
29 29
30 - The method is in `httpMethod`; there is no `method` key.30 - The method is in `httpMethod`; there is no `method` key.
31-- `queryStringParameters` is decoded and keeps the last value of repeated keys: `?repeated=a&repeated=b&list[]=x&list[]=y` gives `{"repeated": "b", "list[]": "y"}`; it is an empty list `[]` when there is no query string.31+- `queryStringParameters` is decoded, sorted by key, and keeps the last value of repeated keys: `?repeated=a&repeated=b&list[]=x&list[]=y` gives `{"repeated": "b", "list[]": "y"}`; it is an empty list `[]` when there is no query string.
32 - Header names are canonicalised (`X-Custom`, `User-Agent`), single string values.32 - Header names are canonicalised (`X-Custom`, `User-Agent`), single string values.
33 - There is no `Host` header.33 - There is no `Host` header.
34 - `isBase64Encoded` is `true` with a base64 body for binary and multipart bodies, `true` with an empty body for GET, and absent for text bodies (JSON, plain text).34 - `isBase64Encoded` is `true` with a base64 body for binary and multipart bodies, `true` with an empty body for GET, and absent for text bodies (JSON, plain text).
modified example/e2e/run.sh +1 -1
@@ -63,7 +63,7 @@ status=$(request query "$base_url/echo?foo=bar&n=42&list%5Bkey%5D=v")
6363 expect "GET /echo status" "$status" 200
6464 expect "GET /echo method" "$(field query method)" GET
6565 expect "GET /echo path" "$(field query path)" /echo
66-expect "GET /echo query" "$(field query query)" '{"foo":"bar","n":"42","list":{"key":"v"}}'
66+expect "GET /echo query" "$(field query query)" '{"foo":"bar","list":{"key":"v"},"n":"42"}'
6767
6868 status=$(request headers -H 'X-Custom: hello' -H 'Accept-Language: fr' --cookie 'flavor=chocolate; size=large' "$base_url/echo")
6969 expect "custom request header" "$(field headers headers.x-custom)" hello
@@ -63,7 +63,7 @@ status=$(request query "$base_url/echo?foo=bar&n=42&list%5Bkey%5D=v")
63 expect "GET /echo status" "$status" 20063 expect "GET /echo status" "$status" 200
64 expect "GET /echo method" "$(field query method)" GET64 expect "GET /echo method" "$(field query method)" GET
65 expect "GET /echo path" "$(field query path)" /echo65 expect "GET /echo path" "$(field query path)" /echo
66-expect "GET /echo query" "$(field query query)" '{"foo":"bar","n":"42","list":{"key":"v"}}'66+expect "GET /echo query" "$(field query query)" '{"foo":"bar","list":{"key":"v"},"n":"42"}'
67 67
68 status=$(request headers -H 'X-Custom: hello' -H 'Accept-Language: fr' --cookie 'flavor=chocolate; size=large' "$base_url/echo")68 status=$(request headers -H 'X-Custom: hello' -H 'Accept-Language: fr' --cookie 'flavor=chocolate; size=large' "$base_url/echo")
69 expect "custom request header" "$(field headers headers.x-custom)" hello69 expect "custom request header" "$(field headers headers.x-custom)" hello
modified src/Emulator.php +2 -0
@@ -39,6 +39,8 @@ final class Emulator
3939 $query[urldecode($name)] = urldecode($value);
4040 }
4141 }
42+ // The platform sorts the keys, so the application never sees the client's order.
43+ ksort($query);
4244
4345 $canonical = self::canonicalize($headers);
4446 $contentType = $canonical['Content-Type'] ?? '';
@@ -39,6 +39,8 @@ final class Emulator
39 $query[urldecode($name)] = urldecode($value);39 $query[urldecode($name)] = urldecode($value);
40 }40 }
41 }41 }
42+ // The platform sorts the keys, so the application never sees the client's order.
43+ ksort($query);
42 44
43 $canonical = self::canonicalize($headers);45 $canonical = self::canonicalize($headers);
44 $contentType = $canonical['Content-Type'] ?? '';46 $contentType = $canonical['Content-Type'] ?? '';
modified tests/EmulatorTest.php +2 -2
@@ -36,7 +36,7 @@ final class EmulatorTest extends TestCase
3636 'X-Real-Ip' => '2001:db8::1',
3737 ],
3838 'multiValueHeaders' => null,
39- 'queryStringParameters' => ['sort' => 'name', 'filter[role]' => 'admin user', 'flag' => ''],
39+ 'queryStringParameters' => ['filter[role]' => 'admin user', 'flag' => '', 'sort' => 'name'],
4040 'multiValueQueryStringParameters' => null,
4141 'pathParameters' => null,
4242 'stageVariables' => [],
@@ -90,7 +90,7 @@ final class EmulatorTest extends TestCase
9090 $request = HttpConverter::toRequest(Emulator::toEvent('GET', '/search?q=caf%C3%A9&page%5Bsize%5D=10', [], '', '192.0.2.1'));
9191
9292 self::assertSame('/search', $request->getPathInfo());
93- self::assertSame(['q' => 'café', 'page' => ['size' => '10']], $request->query->all());
93+ self::assertSame(['page' => ['size' => '10'], 'q' => 'café'], $request->query->all());
9494 self::assertSame('192.0.2.1', $request->getClientIp());
9595 self::assertTrue($request->isSecure());
9696 }
@@ -36,7 +36,7 @@ final class EmulatorTest extends TestCase
36 'X-Real-Ip' => '2001:db8::1',36 'X-Real-Ip' => '2001:db8::1',
37 ],37 ],
38 'multiValueHeaders' => null,38 'multiValueHeaders' => null,
39- 'queryStringParameters' => ['sort' => 'name', 'filter[role]' => 'admin user', 'flag' => ''],39+ 'queryStringParameters' => ['filter[role]' => 'admin user', 'flag' => '', 'sort' => 'name'],
40 'multiValueQueryStringParameters' => null,40 'multiValueQueryStringParameters' => null,
41 'pathParameters' => null,41 'pathParameters' => null,
42 'stageVariables' => [],42 'stageVariables' => [],
@@ -90,7 +90,7 @@ final class EmulatorTest extends TestCase
90 $request = HttpConverter::toRequest(Emulator::toEvent('GET', '/search?q=caf%C3%A9&page%5Bsize%5D=10', [], '', '192.0.2.1'));90 $request = HttpConverter::toRequest(Emulator::toEvent('GET', '/search?q=caf%C3%A9&page%5Bsize%5D=10', [], '', '192.0.2.1'));
91 91
92 self::assertSame('/search', $request->getPathInfo());92 self::assertSame('/search', $request->getPathInfo());
93- self::assertSame(['q' => 'café', 'page' => ['size' => '10']], $request->query->all());93+ self::assertSame(['page' => ['size' => '10'], 'q' => 'café'], $request->query->all());
94 self::assertSame('192.0.2.1', $request->getClientIp());94 self::assertSame('192.0.2.1', $request->getClientIp());
95 self::assertTrue($request->isSecure());95 self::assertTrue($request->isSecure());
96 }96 }