From df5c5fe836e693910cfceb3d75e384ab02002a7f Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Wed, 10 Jul 2019 16:47:54 -0500 Subject: [PATCH 1/4] Expands exception hierarchy --- src/Exceptions/MismatchedSchemeException.php | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/Exceptions/MismatchedSchemeException.php diff --git a/src/Exceptions/MismatchedSchemeException.php b/src/Exceptions/MismatchedSchemeException.php new file mode 100644 index 0000000..f337e38 --- /dev/null +++ b/src/Exceptions/MismatchedSchemeException.php @@ -0,0 +1,7 @@ + Date: Wed, 10 Jul 2019 16:48:42 -0500 Subject: [PATCH 2/4] Exception on mismatched schemes of the same host --- src/ApacheModRewriteGenerator.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/ApacheModRewriteGenerator.php b/src/ApacheModRewriteGenerator.php index 45f3f1b..8f7fba3 100644 --- a/src/ApacheModRewriteGenerator.php +++ b/src/ApacheModRewriteGenerator.php @@ -3,6 +3,7 @@ namespace donatj\RewriteGenerator; use donatj\RewriteGenerator\Exceptions\AmbiguousRelativeHostException; +use donatj\RewriteGenerator\Exceptions\MismatchedSchemeException; use donatj\RewriteGenerator\Exceptions\UnhandledUrlException; use InvalidArgumentException; @@ -29,7 +30,8 @@ public function generateRewrite( string $from, string $to, int $type ) : string ); } - $toScheme = $parsedTo['scheme'] ?? ''; + $fromScheme = $parsedFrom['scheme'] ?? ''; + $toScheme = $parsedTo['scheme'] ?? ''; $fromHost = $parsedFrom['host'] ?? ''; $toHost = $parsedTo['host'] ?? ''; @@ -47,6 +49,11 @@ public function generateRewrite( string $from, string $to, int $type ) : string 'Unclear relative host. When the "FROM" URI specifies a HOST the "TO" MUST specify a HOST as well.' ); } + + if( ($fromScheme && $toScheme) && $fromScheme !== $toScheme && $toHost && $fromHost === $toHost ) { + throw new MismatchedSchemeException('Scheme specified on "FROM" of "' . $fromScheme . '" and "TO" of "' . $toScheme . '" do not match.'); + } + if( $toHost && $fromHost !== $toHost ) { $output .= 'RewriteCond %{HTTP_HOST} ^' . preg_quote($fromHost, ' ') . '$'; $output .= "\n"; From 86e45c058a73b3b3bb23634f40c5e8724acd894e Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Wed, 10 Jul 2019 16:49:00 -0500 Subject: [PATCH 3/4] Add tests to support scheme error --- tests/tests/ApacheIntegrationTest.php | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/tests/ApacheIntegrationTest.php b/tests/tests/ApacheIntegrationTest.php index d24fa6e..8048a8d 100644 --- a/tests/tests/ApacheIntegrationTest.php +++ b/tests/tests/ApacheIntegrationTest.php @@ -131,6 +131,27 @@ public function exampleProvider() : Generator { , ]; + // Assure Redirects across Schemes do not error. + yield [ + <<<'TAG' +http://foo.bar/baz https://baz.qux/quux +TAG + , + <<<'TAG' +# 301 --- http://foo.bar/baz => https://baz.qux/quux +RewriteCond %{HTTP_HOST} ^foo\.bar$ +RewriteRule ^baz$ https://baz.qux/quux? [L,R=301] + +TAG + , + <<<'TAG' +# Rewrite --- http://foo.bar/baz => https://baz.qux/quux +RewriteCond %{HTTP_HOST} ^foo\.bar$ +RewriteRule ^baz$ https://baz.qux/quux?&%{QUERY_STRING} + +TAG + , + ]; } /** @@ -204,6 +225,22 @@ public function failureProvider() : Generator { , 3, ]; + // Ensure an error is thrown if the HOST is the same but the SCHEME varies + yield [ + <<<'TAG' +http://foo.bar/baz https://foo.bar/baz +TAG + , + <<<'TAG' +# WARNING: Input contained 1 error(s) + +# 301 --- http://foo.bar/baz => https://foo.bar/baz +# ERROR: Scheme specified on "FROM" of "http" and "TO" of "https" do not match.: http://foo.bar/baz https://foo.bar/baz + +TAG + , 1, + ]; + yield [ <<<'TAG' foo.html#funk bar.html From adf857c33abe0a47e7e110db0720e40ea96ed4fc Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Wed, 10 Jul 2019 16:53:20 -0500 Subject: [PATCH 4/4] Small if statment cleanup --- src/ApacheModRewriteGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ApacheModRewriteGenerator.php b/src/ApacheModRewriteGenerator.php index 8f7fba3..dc2ebe4 100644 --- a/src/ApacheModRewriteGenerator.php +++ b/src/ApacheModRewriteGenerator.php @@ -50,7 +50,7 @@ public function generateRewrite( string $from, string $to, int $type ) : string ); } - if( ($fromScheme && $toScheme) && $fromScheme !== $toScheme && $toHost && $fromHost === $toHost ) { + if( $fromScheme && $toScheme && $toHost && $fromScheme !== $toScheme && $fromHost === $toHost ) { throw new MismatchedSchemeException('Scheme specified on "FROM" of "' . $fromScheme . '" and "TO" of "' . $toScheme . '" do not match.'); }