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

test(example): check absolute URLs and redirects against the hostUnverified

GET /urls and GET /redirect expose the router's absolute URLs, getUri(), getSchemeAndHttpHost() and an absolute Location; e2e/run.sh expects SCALEWAY_HOST in them, localhost when unset, whatever X-Forwarded-Host the client forges.
Jérémy DECOOL committed 2026-11-14T15:13:31+01:00 Browse files
c945462 parent: f65cec5
modified example/e2e/run.sh +13 -1
@@ -1,7 +1,8 @@
11 #!/usr/bin/env bash
22 #
33 # End-to-end scenarios for the example application.
4-# Usage: [SCALEWAY_CRON_SECRET=...] example/e2e/run.sh <base-url>
4+# Usage: [SCALEWAY_CRON_SECRET=...] [SCALEWAY_HOST=...] example/e2e/run.sh <base-url>
5+# SCALEWAY_HOST: the function's "host", which absolute URLs must carry; localhost, the runtime's default, when unset
56 # against the Emulator: example/e2e/run.sh http://127.0.0.1:8000
67 # against the deployment: example/e2e/run.sh "$(terraform -chdir=example/terraform output -raw endpoint)"
78 # Requires bash, curl and php (used to read JSON and hash files).
@@ -76,6 +77,17 @@ status=$(request forwarded -H 'X-Forwarded-For: 203.0.113.66' -H 'X-Forwarded-Ho
7677 expect "forged X-Forwarded-Host ignored" "$(field forwarded uri | grep -c spoofed.example)" 0
7778 expect "forged client IP ignored" "$(field forwarded clientIp | grep -c 203.0.113.66)" 0
7879
80+# The host comes from the "host" runtime option, never from the client.
81+host=${SCALEWAY_HOST:-localhost}
82+status=$(request urls -H 'X-Forwarded-Host: spoofed.example' "$base_url/urls?a=1")
83+expect "GET /urls status" "$status" 200
84+expect "absolute URL from the router" "$(field urls absoluteUrl)" "https://$host/hello?name=url"
85+expect "Request URI" "$(field urls uri)" "https://$host/urls?a=1"
86+expect "scheme and host" "$(field urls schemeAndHttpHost)" "https://$host"
87+
88+status=$(request redirect "$base_url/redirect")
89+expect "redirect to an absolute URL" "$status $(header redirect location)" "302 https://$host/hello?name=redirect"
90+
7991 status=$(request json -X POST -H 'Content-Type: application/json' --data '{"answer":42}' "$base_url/echo")
8092 expect "POST JSON body" "$(field json body)" '{"answer":42}'
8193
@@ -1,7 +1,8 @@
1 #!/usr/bin/env bash1 #!/usr/bin/env bash
2 #2 #
3 # End-to-end scenarios for the example application.3 # End-to-end scenarios for the example application.
4-# Usage: [SCALEWAY_CRON_SECRET=...] example/e2e/run.sh <base-url>4+# Usage: [SCALEWAY_CRON_SECRET=...] [SCALEWAY_HOST=...] example/e2e/run.sh <base-url>
5+# SCALEWAY_HOST: the function's "host", which absolute URLs must carry; localhost, the runtime's default, when unset
5 # against the Emulator: example/e2e/run.sh http://127.0.0.1:80006 # against the Emulator: example/e2e/run.sh http://127.0.0.1:8000
6 # against the deployment: example/e2e/run.sh "$(terraform -chdir=example/terraform output -raw endpoint)"7 # against the deployment: example/e2e/run.sh "$(terraform -chdir=example/terraform output -raw endpoint)"
7 # Requires bash, curl and php (used to read JSON and hash files).8 # Requires bash, curl and php (used to read JSON and hash files).
@@ -76,6 +77,17 @@ status=$(request forwarded -H 'X-Forwarded-For: 203.0.113.66' -H 'X-Forwarded-Ho
76 expect "forged X-Forwarded-Host ignored" "$(field forwarded uri | grep -c spoofed.example)" 077 expect "forged X-Forwarded-Host ignored" "$(field forwarded uri | grep -c spoofed.example)" 0
77 expect "forged client IP ignored" "$(field forwarded clientIp | grep -c 203.0.113.66)" 078 expect "forged client IP ignored" "$(field forwarded clientIp | grep -c 203.0.113.66)" 0
78 79
80+# The host comes from the "host" runtime option, never from the client.
81+host=${SCALEWAY_HOST:-localhost}
82+status=$(request urls -H 'X-Forwarded-Host: spoofed.example' "$base_url/urls?a=1")
83+expect "GET /urls status" "$status" 200
84+expect "absolute URL from the router" "$(field urls absoluteUrl)" "https://$host/hello?name=url"
85+expect "Request URI" "$(field urls uri)" "https://$host/urls?a=1"
86+expect "scheme and host" "$(field urls schemeAndHttpHost)" "https://$host"
87+
88+status=$(request redirect "$base_url/redirect")
89+expect "redirect to an absolute URL" "$status $(header redirect location)" "302 https://$host/hello?name=redirect"
90+
79 status=$(request json -X POST -H 'Content-Type: application/json' --data '{"answer":42}' "$base_url/echo")91 status=$(request json -X POST -H 'Content-Type: application/json' --data '{"answer":42}' "$base_url/echo")
80 expect "POST JSON body" "$(field json body)" '{"answer":42}'92 expect "POST JSON body" "$(field json body)" '{"answer":42}'
81 93
modified example/src/Controller/DemoController.php +21 -2
@@ -6,10 +6,12 @@ use Psr\Log\LoggerInterface;
66 use Symfony\Component\HttpFoundation\Cookie;
77 use Symfony\Component\HttpFoundation\File\UploadedFile;
88 use Symfony\Component\HttpFoundation\JsonResponse;
9+use Symfony\Component\HttpFoundation\RedirectResponse;
910 use Symfony\Component\HttpFoundation\Request;
1011 use Symfony\Component\HttpFoundation\Response;
1112 use Symfony\Component\HttpFoundation\StreamedResponse;
1213 use Symfony\Component\Routing\Attribute\Route;
14+use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1315
1416 final class DemoController
1517 {
@@ -19,7 +21,7 @@ final class DemoController
1921 #[Route('/', methods: ['GET'])]
2022 public function index(): JsonResponse
2123 {
22- return new JsonResponse(['routes' => ['/hello', '/echo', '/cookies', '/binary', '/stream', '/counter']]);
24+ return new JsonResponse(['routes' => ['/hello', '/echo', '/cookies', '/binary', '/stream', '/counter', '/urls', '/redirect']]);
2325 }
2426
2527 // CRON Invocations arrive as a POST on "/" with the trigger's arguments as body.
@@ -34,7 +36,7 @@ final class DemoController
3436 return new JsonResponse(['cron' => true, 'args' => $args]);
3537 }
3638
37- #[Route('/hello', methods: ['GET'])]
39+ #[Route('/hello', name: 'hello', methods: ['GET'])]
3840 public function hello(Request $request): JsonResponse
3941 {
4042 return new JsonResponse(['message' => \sprintf('Hello %s!', $request->query->getString('name', 'world'))]);
@@ -65,6 +67,23 @@ final class DemoController
6567 ]);
6668 }
6769
70+ // Every host in here is the "host" runtime option's, as no Host header reaches the function.
71+ #[Route('/urls', methods: ['GET'])]
72+ public function urls(Request $request, UrlGeneratorInterface $urlGenerator): JsonResponse
73+ {
74+ return new JsonResponse([
75+ 'absoluteUrl' => $urlGenerator->generate('hello', ['name' => 'url'], UrlGeneratorInterface::ABSOLUTE_URL),
76+ 'uri' => $request->getUri(),
77+ 'schemeAndHttpHost' => $request->getSchemeAndHttpHost(),
78+ ]);
79+ }
80+
81+ #[Route('/redirect', methods: ['GET'])]
82+ public function redirect(UrlGeneratorInterface $urlGenerator): RedirectResponse
83+ {
84+ return new RedirectResponse($urlGenerator->generate('hello', ['name' => 'redirect'], UrlGeneratorInterface::ABSOLUTE_URL));
85+ }
86+
6887 #[Route('/cookies', methods: ['GET'])]
6988 public function cookies(): JsonResponse
7089 {
@@ -6,10 +6,12 @@ use Psr\Log\LoggerInterface;
6 use Symfony\Component\HttpFoundation\Cookie;6 use Symfony\Component\HttpFoundation\Cookie;
7 use Symfony\Component\HttpFoundation\File\UploadedFile;7 use Symfony\Component\HttpFoundation\File\UploadedFile;
8 use Symfony\Component\HttpFoundation\JsonResponse;8 use Symfony\Component\HttpFoundation\JsonResponse;
9+use Symfony\Component\HttpFoundation\RedirectResponse;
9 use Symfony\Component\HttpFoundation\Request;10 use Symfony\Component\HttpFoundation\Request;
10 use Symfony\Component\HttpFoundation\Response;11 use Symfony\Component\HttpFoundation\Response;
11 use Symfony\Component\HttpFoundation\StreamedResponse;12 use Symfony\Component\HttpFoundation\StreamedResponse;
12 use Symfony\Component\Routing\Attribute\Route;13 use Symfony\Component\Routing\Attribute\Route;
14+use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
13 15
14 final class DemoController16 final class DemoController
15 {17 {
@@ -19,7 +21,7 @@ final class DemoController
19 #[Route('/', methods: ['GET'])]21 #[Route('/', methods: ['GET'])]
20 public function index(): JsonResponse22 public function index(): JsonResponse
21 {23 {
22- return new JsonResponse(['routes' => ['/hello', '/echo', '/cookies', '/binary', '/stream', '/counter']]);24+ return new JsonResponse(['routes' => ['/hello', '/echo', '/cookies', '/binary', '/stream', '/counter', '/urls', '/redirect']]);
23 }25 }
24 26
25 // CRON Invocations arrive as a POST on "/" with the trigger's arguments as body.27 // CRON Invocations arrive as a POST on "/" with the trigger's arguments as body.
@@ -34,7 +36,7 @@ final class DemoController
34 return new JsonResponse(['cron' => true, 'args' => $args]);36 return new JsonResponse(['cron' => true, 'args' => $args]);
35 }37 }
36 38
37- #[Route('/hello', methods: ['GET'])]39+ #[Route('/hello', name: 'hello', methods: ['GET'])]
38 public function hello(Request $request): JsonResponse40 public function hello(Request $request): JsonResponse
39 {41 {
40 return new JsonResponse(['message' => \sprintf('Hello %s!', $request->query->getString('name', 'world'))]);42 return new JsonResponse(['message' => \sprintf('Hello %s!', $request->query->getString('name', 'world'))]);
@@ -65,6 +67,23 @@ final class DemoController
65 ]);67 ]);
66 }68 }
67 69
70+ // Every host in here is the "host" runtime option's, as no Host header reaches the function.
71+ #[Route('/urls', methods: ['GET'])]
72+ public function urls(Request $request, UrlGeneratorInterface $urlGenerator): JsonResponse
73+ {
74+ return new JsonResponse([
75+ 'absoluteUrl' => $urlGenerator->generate('hello', ['name' => 'url'], UrlGeneratorInterface::ABSOLUTE_URL),
76+ 'uri' => $request->getUri(),
77+ 'schemeAndHttpHost' => $request->getSchemeAndHttpHost(),
78+ ]);
79+ }
80+
81+ #[Route('/redirect', methods: ['GET'])]
82+ public function redirect(UrlGeneratorInterface $urlGenerator): RedirectResponse
83+ {
84+ return new RedirectResponse($urlGenerator->generate('hello', ['name' => 'redirect'], UrlGeneratorInterface::ABSOLUTE_URL));
85+ }
86+
68 #[Route('/cookies', methods: ['GET'])]87 #[Route('/cookies', methods: ['GET'])]
69 public function cookies(): JsonResponse88 public function cookies(): JsonResponse
70 {89 {