*/ private function redirectMap(): array { $found = preg_match('/^map\s+"[^"]*"\s+\$luf_canonical_redirect\s*\{(.*?)^\}/ms', $this->config(), $match); $this->assertSame(1, $found, 'Die map $luf_canonical_redirect fehlt.'); return array_values(array_filter(array_map('trim', explode("\n", $match[1])))); } /** * @return list Zeilen der map, die weiterleiten (Wert 1) */ private function redirectRules(): array { return array_values(array_filter($this->redirectMap(), fn (string $line) => str_ends_with($line, '" 1;'))); } public function test_nginx_resolves_the_app_container_at_request_time(): void { $config = $this->config(); $this->assertMatchesRegularExpression('/^\s*resolver\s+127\.0\.0\.11\b[^;]*valid=\d+s[^;]*;/m', $config); $this->assertMatchesRegularExpression('/^\s*set\s+\$php_upstream\s+app:9000;/m', $config); $this->assertMatchesRegularExpression('/^\s*fastcgi_pass\s+\$php_upstream;/m', $config); $this->assertDoesNotMatchRegularExpression('/^\s*fastcgi_pass\s+app:9000;/m', $config); } public function test_nginx_keeps_the_fastcgi_and_timeout_settings(): void { $config = $this->config(); $this->assertMatchesRegularExpression('/^\s*fastcgi_read_timeout\s+3600s;/m', $config); $this->assertMatchesRegularExpression('/^\s*fastcgi_send_timeout\s+3600s;/m', $config); $this->assertMatchesRegularExpression('/^\s*client_max_body_size\s+2048m;/m', $config); $this->assertMatchesRegularExpression('/^\s*fastcgi_param\s+HTTP_X_FORWARDED_PROTO\s+\$forwarded_proto;/m', $config); $this->assertStringContainsString('try_files $uri $uri/ /index.php?$query_string;', $config); $this->assertStringContainsString('location ~ /\.(?!well-known) {', $config); } public function test_alias_hosts_are_redirected_to_the_fixed_canonical_domain(): void { $config = $this->config(); $this->assertStringContainsString('if ($luf_canonical_redirect = 1) {', $config); $this->assertStringContainsString('return 301 https://lara-und-felix.de$request_uri;', $config, 'Fest verdrahtetes Ziel, Pfad und Query-String aus $request_uri.'); $this->assertSame(1, preg_match_all('/^\s*return\s+30\d\s/m', $config), 'Es gibt genau eine Weiterleitung.'); $this->assertSame(2, preg_match_all('/^\s*return\s+\d{3}\b/m', $config), 'Nur Weiterleitung und Sperre antworten selbst.'); $this->assertDoesNotMatchRegularExpression('/return\s+30\d\s+[^;]*\$(host|http_host|http_x_forwarded_host|scheme)/', $config, 'Das Ziel darf nicht aus Anfragewerten entstehen.'); } public function test_redirect_rules_cover_both_www_aliases_and_the_http_apex_only(): void { $rules = implode("\n", $this->redirectRules()); $this->assertCount(3, $this->redirectRules()); $this->assertStringContainsString('www\.lara-und-felix\.de', $rules); $this->assertStringContainsString('www\.laraundfelix\.de', $rules); $this->assertStringContainsString('\|\s*http\s*(,[^|]*)?\|lara-und-felix\.de', $rules, 'Der Apex wird nur bei ausdrücklichem X-Forwarded-Proto http weitergeleitet.'); $this->assertStringNotContainsString('dev.', $rules, 'dev.laraundfelix.de bleibt unverändert.'); $this->assertStringNotContainsString('|laraundfelix', $rules, 'laraundfelix.de ohne www übernimmt die STRATO-Weiterleitung.'); $this->assertStringContainsString('default 0;', implode("\n", $this->redirectMap())); } public function test_only_get_and_head_are_redirected(): void { foreach ($this->redirectRules() as $rule) { $this->assertStringContainsString('"~*^(GET|HEAD)\|', $rule, 'POST (zum Beispiel der Stripe-Webhook) darf kein 301 bekommen: '.$rule); } } /** * @return list Zeilen der map, die andere Methoden sperren (Wert 2) */ private function blockRules(): array { return array_values(array_filter($this->redirectMap(), fn (string $line) => str_ends_with($line, '" 2;'))); } public function test_other_methods_on_the_three_shop_hosts_end_at_nginx_when_proxy_says_http(): void { $config = $this->config(); $rules = $this->blockRules(); $this->assertCount(1, $rules); $rule = $rules[0]; $this->assertStringStartsWith('"~*^[^|]*\|', $rule, 'Jede Methode, die nicht vorher als GET/HEAD entschieden wurde.'); $this->assertStringContainsString('\|\s*http\s*(,[^|]*)?\|', $rule, 'Nur bei ausdrücklichem X-Forwarded-Proto http (erster Wert).'); foreach (['www\.lara-und-felix', 'www\.laraundfelix', '(www\.lara-und-felix|www\.laraundfelix|lara-und-felix)\.de'] as $host) { $this->assertStringContainsString($host, $rule); } $this->assertStringNotContainsString('dev', $rule, 'dev.laraundfelix.de bleibt unverändert.'); $this->assertStringNotContainsString('https', $rule); $this->assertMatchesRegularExpression('/^\s*if\s*\(\$luf_canonical_redirect = 2\)\s*\{\s*return\s+403;\s*\}/m', $config, 'Die Sperre antwortet in nginx, bevor die Anfrage Laravel erreicht.'); $this->assertDoesNotMatchRegularExpression('/\$luf_canonical_redirect = 2\)\s*\{[^}]*(fastcgi_pass|proxy_pass)/', $config); } public function test_block_rule_comes_after_acme_exception_and_the_redirects(): void { // nginx nimmt den ersten passenden Regex-Eintrag: Ausnahme, dann GET/HEAD-Weiterleitungen, zuletzt die Sperre. $values = array_map(fn (string $line) => rtrim(substr($line, (int) strrpos($line, ' ') + 1), ';'), $this->redirectMap()); $this->assertSame(['0', '0', '1', '1', '1', '2'], $values, 'Reihenfolge: default, ACME, drei Weiterleitungen, Sperre.'); } public function test_acme_challenge_is_excluded_before_any_redirect_rule(): void { $acme = null; $firstRedirect = null; foreach ($this->redirectMap() as $index => $line) { if ($acme === null && str_contains($line, '/\.well-known/acme-challenge/') && str_ends_with($line, '" 0;')) { $acme = $index; } if ($firstRedirect === null && (str_ends_with($line, '" 1;') || str_ends_with($line, '" 2;'))) { $firstRedirect = $index; } } $this->assertNotNull($acme, 'Die ACME-Ausnahme fehlt.'); $this->assertNotNull($firstRedirect); $this->assertLessThan($firstRedirect, $acme, 'nginx wertet map-Einträge der Reihe nach aus: die Ausnahme muss vorn stehen.'); } public function test_redirect_decision_uses_forwarded_headers_and_never_the_local_scheme(): void { $config = $this->config(); $this->assertStringContainsString('map "$request_method|$http_x_forwarded_proto|$luf_public_host|$uri" $luf_canonical_redirect {', $config); $this->assertStringContainsString('map $http_x_forwarded_host $luf_forwarded_host {', $config); $this->assertMatchesRegularExpression('/^\s*""\s+\$host;/m', $config, 'Ohne X-Forwarded-Host gilt der Host-Header.'); $this->assertStringNotContainsString('$scheme|', $config, 'Ohne X-Forwarded-Proto darf nie weitergeleitet werden (direkter Zugriff, Health-Check).'); } }