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.
c945462 parent: f65cec5 modified
example/e2e/run.sh +13 -1 | @@ -1,7 +1,8 @@ | ||
| 1 | 1 | #!/usr/bin/env bash |
| 2 | 2 | # |
| 3 | 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 | 6 | # against the Emulator: example/e2e/run.sh http://127.0.0.1:8000 |
| 6 | 7 | # against the deployment: example/e2e/run.sh "$(terraform -chdir=example/terraform output -raw endpoint)" |
| 7 | 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 | 77 | expect "forged X-Forwarded-Host ignored" "$(field forwarded uri | grep -c spoofed.example)" 0 |
| 77 | 78 | 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 | 91 | status=$(request json -X POST -H 'Content-Type: application/json' --data '{"answer":42}' "$base_url/echo") |
| 80 | 92 | expect "POST JSON body" "$(field json body)" '{"answer":42}' |
| 81 | 93 | |
| @@ -1,7 +1,8 @@ | |||
| 1 | #!/usr/bin/env bash | 1 | #!/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:8000 | 6 | # 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)" 0 | 77 | 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)" 0 | 78 | 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; | ||
| 6 | 6 | use Symfony\Component\HttpFoundation\Cookie; |
| 7 | 7 | use Symfony\Component\HttpFoundation\File\UploadedFile; |
| 8 | 8 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 9 | +use Symfony\Component\HttpFoundation\RedirectResponse; | |
| 9 | 10 | use Symfony\Component\HttpFoundation\Request; |
| 10 | 11 | use Symfony\Component\HttpFoundation\Response; |
| 11 | 12 | use Symfony\Component\HttpFoundation\StreamedResponse; |
| 12 | 13 | use Symfony\Component\Routing\Attribute\Route; |
| 14 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
| 13 | 15 | |
| 14 | 16 | final class DemoController |
| 15 | 17 | { |
| @@ -19,7 +21,7 @@ final class DemoController | ||
| 19 | 21 | #[Route('/', methods: ['GET'])] |
| 20 | 22 | 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 | 27 | // CRON Invocations arrive as a POST on "/" with the trigger's arguments as body. |
| @@ -34,7 +36,7 @@ final class DemoController | ||
| 34 | 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 | 40 | public function hello(Request $request): JsonResponse |
| 39 | 41 | { |
| 40 | 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 | 87 | #[Route('/cookies', methods: ['GET'])] |
| 69 | 88 | public function cookies(): JsonResponse |
| 70 | 89 | { |
| @@ -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 DemoController | 16 | final class DemoController |
| 15 | { | 17 | { |
| @@ -19,7 +21,7 @@ final class DemoController | |||
| 19 | #[Route('/', methods: ['GET'])] | 21 | #[Route('/', methods: ['GET'])] |
| 20 | public function index(): JsonResponse | 22 | 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): JsonResponse | 40 | 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(): JsonResponse | 88 | public function cookies(): JsonResponse |
| 70 | { | 89 | { |