@@ -10,120 +10,120 @@ |
||
| 10 | 10 | |
| 11 | 11 | class InitialisationMiddlewareTest extends FunctionalTest |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * @var HTTPRequest |
|
| 15 | - */ |
|
| 16 | - protected $request; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @var InitialisationMiddleware |
|
| 20 | - */ |
|
| 21 | - protected $middleware; |
|
| 22 | - |
|
| 23 | - protected $usesDatabase = true; |
|
| 24 | - |
|
| 25 | - protected function setUp() |
|
| 26 | - { |
|
| 27 | - parent::setUp(); |
|
| 28 | - |
|
| 29 | - $this->request = new HTTPRequest('GET', '/'); |
|
| 30 | - $this->middleware = new InitialisationMiddleware(); |
|
| 31 | - |
|
| 32 | - Environment::setEnv('SS_OUTBOUND_PROXY', ''); |
|
| 33 | - Environment::setEnv('SS_OUTBOUND_PROXY_PORT', ''); |
|
| 34 | - putenv('NO_PROXY='); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - public function testDoNotConfigureProxyIfNoEnvironmentVarsAreSet() |
|
| 38 | - { |
|
| 39 | - $this->runMiddleware(); |
|
| 40 | - |
|
| 41 | - $this->assertEmpty( |
|
| 42 | - Environment::getEnv('http_proxy'), |
|
| 43 | - 'Proxy information is not set if no outbound proxy is configured' |
|
| 44 | - ); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public function testConfigureEgressProxyWhenVarsAreSet() |
|
| 48 | - { |
|
| 49 | - Environment::setEnv('SS_OUTBOUND_PROXY', 'http://example.com'); |
|
| 50 | - Environment::setEnv('SS_OUTBOUND_PROXY_PORT', '8024'); |
|
| 51 | - |
|
| 52 | - $this->runMiddleware(); |
|
| 53 | - |
|
| 54 | - $this->assertEquals( |
|
| 55 | - 'http://example.com:8024', |
|
| 56 | - Environment::getEnv('http_proxy'), |
|
| 57 | - 'Proxy is configured with proxy and port' |
|
| 58 | - ); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - public function testDoNotConfigureProxyDomainExclusionsWhenNoneAreDefined() |
|
| 62 | - { |
|
| 63 | - Config::modify()->remove(InitialisationMiddleware::class, 'egress_proxy_exclude_domains'); |
|
| 64 | - |
|
| 65 | - $this->runMiddleware(); |
|
| 66 | - |
|
| 67 | - $this->assertSame( |
|
| 68 | - '', |
|
| 69 | - Environment::getEnv('NO_PROXY'), |
|
| 70 | - 'No domain exclusions are set when none are defined' |
|
| 71 | - ); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - public function testConfigureEgressProxyDomainExclusions() |
|
| 75 | - { |
|
| 76 | - Config::modify()->set( |
|
| 77 | - InitialisationMiddleware::class, |
|
| 78 | - 'egress_proxy_exclude_domains', |
|
| 79 | - 'example.com' |
|
| 80 | - ); |
|
| 81 | - |
|
| 82 | - putenv('NO_PROXY=foo.com,bar.com'); |
|
| 83 | - $this->runMiddleware(); |
|
| 84 | - |
|
| 85 | - $this->assertSame( |
|
| 86 | - 'foo.com,bar.com,example.com', |
|
| 87 | - Environment::getEnv('NO_PROXY'), |
|
| 88 | - 'Domain exclusions are combined with existing values and configuration settings' |
|
| 89 | - ); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - public function testSecurityHeadersAddedByDefault() |
|
| 93 | - { |
|
| 94 | - $response = $this->get('Security/login'); |
|
| 95 | - $this->assertArrayHasKey('x-xss-protection', $response->getHeaders()); |
|
| 96 | - $this->assertSame('1; mode=block', $response->getHeader('x-xss-protection')); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - public function testXSSProtectionHeaderNotAdded() |
|
| 100 | - { |
|
| 101 | - Config::modify()->set(InitialisationMiddleware::class, 'xss_protection_enabled', false); |
|
| 102 | - $response = $this->get('Security/login'); |
|
| 103 | - $this->assertArrayNotHasKey('x-xss-protection', $response->getHeaders()); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - public function testHstsNotAddedByDefault() |
|
| 107 | - { |
|
| 108 | - Config::modify()->remove(InitialisationMiddleware::class, 'strict_transport_security'); |
|
| 109 | - $response = $this->get('Security/login'); |
|
| 110 | - $this->assertArrayNotHasKey('strict-transport-security', $response->getHeaders()); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - public function testHstsAddedWhenConfigured() |
|
| 114 | - { |
|
| 115 | - Config::modify()->update(InitialisationMiddleware::class, 'strict_transport_security', 'max-age=1'); |
|
| 116 | - $response = $this->get('Security/login'); |
|
| 117 | - $this->assertArrayHasKey('strict-transport-security', $response->getHeaders()); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Runs the middleware with a stubbed delegate |
|
| 122 | - */ |
|
| 123 | - protected function runMiddleware() |
|
| 124 | - { |
|
| 125 | - $this->middleware->process($this->request, function () { |
|
| 126 | - // no op |
|
| 127 | - }); |
|
| 128 | - } |
|
| 13 | + /** |
|
| 14 | + * @var HTTPRequest |
|
| 15 | + */ |
|
| 16 | + protected $request; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @var InitialisationMiddleware |
|
| 20 | + */ |
|
| 21 | + protected $middleware; |
|
| 22 | + |
|
| 23 | + protected $usesDatabase = true; |
|
| 24 | + |
|
| 25 | + protected function setUp() |
|
| 26 | + { |
|
| 27 | + parent::setUp(); |
|
| 28 | + |
|
| 29 | + $this->request = new HTTPRequest('GET', '/'); |
|
| 30 | + $this->middleware = new InitialisationMiddleware(); |
|
| 31 | + |
|
| 32 | + Environment::setEnv('SS_OUTBOUND_PROXY', ''); |
|
| 33 | + Environment::setEnv('SS_OUTBOUND_PROXY_PORT', ''); |
|
| 34 | + putenv('NO_PROXY='); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + public function testDoNotConfigureProxyIfNoEnvironmentVarsAreSet() |
|
| 38 | + { |
|
| 39 | + $this->runMiddleware(); |
|
| 40 | + |
|
| 41 | + $this->assertEmpty( |
|
| 42 | + Environment::getEnv('http_proxy'), |
|
| 43 | + 'Proxy information is not set if no outbound proxy is configured' |
|
| 44 | + ); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public function testConfigureEgressProxyWhenVarsAreSet() |
|
| 48 | + { |
|
| 49 | + Environment::setEnv('SS_OUTBOUND_PROXY', 'http://example.com'); |
|
| 50 | + Environment::setEnv('SS_OUTBOUND_PROXY_PORT', '8024'); |
|
| 51 | + |
|
| 52 | + $this->runMiddleware(); |
|
| 53 | + |
|
| 54 | + $this->assertEquals( |
|
| 55 | + 'http://example.com:8024', |
|
| 56 | + Environment::getEnv('http_proxy'), |
|
| 57 | + 'Proxy is configured with proxy and port' |
|
| 58 | + ); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + public function testDoNotConfigureProxyDomainExclusionsWhenNoneAreDefined() |
|
| 62 | + { |
|
| 63 | + Config::modify()->remove(InitialisationMiddleware::class, 'egress_proxy_exclude_domains'); |
|
| 64 | + |
|
| 65 | + $this->runMiddleware(); |
|
| 66 | + |
|
| 67 | + $this->assertSame( |
|
| 68 | + '', |
|
| 69 | + Environment::getEnv('NO_PROXY'), |
|
| 70 | + 'No domain exclusions are set when none are defined' |
|
| 71 | + ); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + public function testConfigureEgressProxyDomainExclusions() |
|
| 75 | + { |
|
| 76 | + Config::modify()->set( |
|
| 77 | + InitialisationMiddleware::class, |
|
| 78 | + 'egress_proxy_exclude_domains', |
|
| 79 | + 'example.com' |
|
| 80 | + ); |
|
| 81 | + |
|
| 82 | + putenv('NO_PROXY=foo.com,bar.com'); |
|
| 83 | + $this->runMiddleware(); |
|
| 84 | + |
|
| 85 | + $this->assertSame( |
|
| 86 | + 'foo.com,bar.com,example.com', |
|
| 87 | + Environment::getEnv('NO_PROXY'), |
|
| 88 | + 'Domain exclusions are combined with existing values and configuration settings' |
|
| 89 | + ); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + public function testSecurityHeadersAddedByDefault() |
|
| 93 | + { |
|
| 94 | + $response = $this->get('Security/login'); |
|
| 95 | + $this->assertArrayHasKey('x-xss-protection', $response->getHeaders()); |
|
| 96 | + $this->assertSame('1; mode=block', $response->getHeader('x-xss-protection')); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + public function testXSSProtectionHeaderNotAdded() |
|
| 100 | + { |
|
| 101 | + Config::modify()->set(InitialisationMiddleware::class, 'xss_protection_enabled', false); |
|
| 102 | + $response = $this->get('Security/login'); |
|
| 103 | + $this->assertArrayNotHasKey('x-xss-protection', $response->getHeaders()); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + public function testHstsNotAddedByDefault() |
|
| 107 | + { |
|
| 108 | + Config::modify()->remove(InitialisationMiddleware::class, 'strict_transport_security'); |
|
| 109 | + $response = $this->get('Security/login'); |
|
| 110 | + $this->assertArrayNotHasKey('strict-transport-security', $response->getHeaders()); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + public function testHstsAddedWhenConfigured() |
|
| 114 | + { |
|
| 115 | + Config::modify()->update(InitialisationMiddleware::class, 'strict_transport_security', 'max-age=1'); |
|
| 116 | + $response = $this->get('Security/login'); |
|
| 117 | + $this->assertArrayHasKey('strict-transport-security', $response->getHeaders()); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Runs the middleware with a stubbed delegate |
|
| 122 | + */ |
|
| 123 | + protected function runMiddleware() |
|
| 124 | + { |
|
| 125 | + $this->middleware->process($this->request, function () { |
|
| 126 | + // no op |
|
| 127 | + }); |
|
| 128 | + } |
|
| 129 | 129 | } |
@@ -12,125 +12,125 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class InitialisationMiddleware implements HTTPMiddleware |
| 14 | 14 | { |
| 15 | - use Configurable; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * Disable the automatically added 'X-XSS-Protection' header that is added to all responses. This should be left |
|
| 19 | - * alone in most circumstances to include the header. Refer to Mozilla Developer Network for more information: |
|
| 20 | - * https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection |
|
| 21 | - * |
|
| 22 | - * @config |
|
| 23 | - * @var bool |
|
| 24 | - */ |
|
| 25 | - private static $xss_protection_enabled = true; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Enable egress proxy. This works on the principle of setting http(s)_proxy environment variables, |
|
| 29 | - * which will be automatically picked up by curl. This means RestfulService and raw curl |
|
| 30 | - * requests should work out of the box. Stream-based requests need extra manual configuration. |
|
| 31 | - * Refer to https://www.cwp.govt.nz/guides/core-technical-documentation/common-web-platform-core/en/how-tos/external_http_requests_with_proxy |
|
| 32 | - * |
|
| 33 | - * @config |
|
| 34 | - * @var bool |
|
| 35 | - */ |
|
| 36 | - private static $egress_proxy_default_enabled = true; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Configure the list of domains to bypass proxy by setting the NO_PROXY environment variable. |
|
| 40 | - * 'services.cwp.govt.nz' needs to be present for Solr and Docvert internal CWP integration. |
|
| 41 | - * 'localhost' is necessary for accessing services on the same instance such as tika-server for text extraction. |
|
| 42 | - * |
|
| 43 | - * @config |
|
| 44 | - * @var string[] |
|
| 45 | - */ |
|
| 46 | - private static $egress_proxy_exclude_domains = [ |
|
| 47 | - 'services.cwp.govt.nz', |
|
| 48 | - 'localhost', |
|
| 49 | - ]; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Provide a value for the HTTP Strict Transport Security header. |
|
| 53 | - * This header is only respected if you also redirect to SSL. |
|
| 54 | - * |
|
| 55 | - * Example configuration: |
|
| 56 | - * |
|
| 57 | - * |
|
| 58 | - * @config |
|
| 59 | - * @var string |
|
| 60 | - */ |
|
| 61 | - private static $strict_transport_security = null; |
|
| 62 | - |
|
| 63 | - public function process(HTTPRequest $request, callable $delegate) |
|
| 64 | - { |
|
| 65 | - if ($this->config()->get('egress_proxy_default_enabled')) { |
|
| 66 | - $this->configureEgressProxy(); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - $this->configureProxyDomainExclusions(); |
|
| 70 | - |
|
| 71 | - $response = $delegate($request); |
|
| 72 | - |
|
| 73 | - if ($this->config()->get('xss_protection_enabled') && $response) { |
|
| 74 | - $response->addHeader('X-XSS-Protection', '1; mode=block'); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - $hsts = $this->config()->get('strict_transport_security'); |
|
| 78 | - if ($hsts && $response) { |
|
| 79 | - $response->addHeader('Strict-Transport-Security', $hsts); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - return $response; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * If the outbound egress proxy details have been defined in environment variables, configure the proxy |
|
| 87 | - * variables that are used to configure it. |
|
| 88 | - */ |
|
| 89 | - protected function configureEgressProxy() |
|
| 90 | - { |
|
| 91 | - if (!Environment::getEnv('SS_OUTBOUND_PROXY') |
|
| 92 | - || !Environment::getEnv('SS_OUTBOUND_PROXY_PORT') |
|
| 93 | - ) { |
|
| 94 | - return; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - $proxy = Environment::getEnv('SS_OUTBOUND_PROXY'); |
|
| 98 | - $proxyPort = Environment::getEnv('SS_OUTBOUND_PROXY_PORT'); |
|
| 99 | - |
|
| 100 | - /* |
|
| 15 | + use Configurable; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * Disable the automatically added 'X-XSS-Protection' header that is added to all responses. This should be left |
|
| 19 | + * alone in most circumstances to include the header. Refer to Mozilla Developer Network for more information: |
|
| 20 | + * https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection |
|
| 21 | + * |
|
| 22 | + * @config |
|
| 23 | + * @var bool |
|
| 24 | + */ |
|
| 25 | + private static $xss_protection_enabled = true; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Enable egress proxy. This works on the principle of setting http(s)_proxy environment variables, |
|
| 29 | + * which will be automatically picked up by curl. This means RestfulService and raw curl |
|
| 30 | + * requests should work out of the box. Stream-based requests need extra manual configuration. |
|
| 31 | + * Refer to https://www.cwp.govt.nz/guides/core-technical-documentation/common-web-platform-core/en/how-tos/external_http_requests_with_proxy |
|
| 32 | + * |
|
| 33 | + * @config |
|
| 34 | + * @var bool |
|
| 35 | + */ |
|
| 36 | + private static $egress_proxy_default_enabled = true; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Configure the list of domains to bypass proxy by setting the NO_PROXY environment variable. |
|
| 40 | + * 'services.cwp.govt.nz' needs to be present for Solr and Docvert internal CWP integration. |
|
| 41 | + * 'localhost' is necessary for accessing services on the same instance such as tika-server for text extraction. |
|
| 42 | + * |
|
| 43 | + * @config |
|
| 44 | + * @var string[] |
|
| 45 | + */ |
|
| 46 | + private static $egress_proxy_exclude_domains = [ |
|
| 47 | + 'services.cwp.govt.nz', |
|
| 48 | + 'localhost', |
|
| 49 | + ]; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Provide a value for the HTTP Strict Transport Security header. |
|
| 53 | + * This header is only respected if you also redirect to SSL. |
|
| 54 | + * |
|
| 55 | + * Example configuration: |
|
| 56 | + * |
|
| 57 | + * |
|
| 58 | + * @config |
|
| 59 | + * @var string |
|
| 60 | + */ |
|
| 61 | + private static $strict_transport_security = null; |
|
| 62 | + |
|
| 63 | + public function process(HTTPRequest $request, callable $delegate) |
|
| 64 | + { |
|
| 65 | + if ($this->config()->get('egress_proxy_default_enabled')) { |
|
| 66 | + $this->configureEgressProxy(); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + $this->configureProxyDomainExclusions(); |
|
| 70 | + |
|
| 71 | + $response = $delegate($request); |
|
| 72 | + |
|
| 73 | + if ($this->config()->get('xss_protection_enabled') && $response) { |
|
| 74 | + $response->addHeader('X-XSS-Protection', '1; mode=block'); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + $hsts = $this->config()->get('strict_transport_security'); |
|
| 78 | + if ($hsts && $response) { |
|
| 79 | + $response->addHeader('Strict-Transport-Security', $hsts); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + return $response; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * If the outbound egress proxy details have been defined in environment variables, configure the proxy |
|
| 87 | + * variables that are used to configure it. |
|
| 88 | + */ |
|
| 89 | + protected function configureEgressProxy() |
|
| 90 | + { |
|
| 91 | + if (!Environment::getEnv('SS_OUTBOUND_PROXY') |
|
| 92 | + || !Environment::getEnv('SS_OUTBOUND_PROXY_PORT') |
|
| 93 | + ) { |
|
| 94 | + return; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + $proxy = Environment::getEnv('SS_OUTBOUND_PROXY'); |
|
| 98 | + $proxyPort = Environment::getEnv('SS_OUTBOUND_PROXY_PORT'); |
|
| 99 | + |
|
| 100 | + /* |
|
| 101 | 101 | * This sets the environment variables so they are available in |
| 102 | 102 | * external calls executed by exec() such as curl. |
| 103 | 103 | * Environment::setEnv() would only availabe in context of SilverStripe. |
| 104 | 104 | * Environment::getEnv() will fallback to getenv() and will therefore |
| 105 | 105 | * fetch the variables |
| 106 | 106 | */ |
| 107 | - putenv('http_proxy=' . $proxy . ':' . $proxyPort); |
|
| 108 | - putenv('https_proxy=' . $proxy . ':' . $proxyPort); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Configure any domains that should be excluded from egress proxy rules and provide them to the environment |
|
| 113 | - */ |
|
| 114 | - protected function configureProxyDomainExclusions() |
|
| 115 | - { |
|
| 116 | - $noProxy = $this->config()->get('egress_proxy_exclude_domains'); |
|
| 117 | - if (empty($noProxy)) { |
|
| 118 | - return; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - if (!is_array($noProxy)) { |
|
| 122 | - $noProxy = [$noProxy]; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // Merge with exsiting if needed. |
|
| 126 | - if (Environment::getEnv('NO_PROXY')) { |
|
| 127 | - $noProxy = array_merge(explode(',', Environment::getEnv('NO_PROXY')), $noProxy); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /* |
|
| 107 | + putenv('http_proxy=' . $proxy . ':' . $proxyPort); |
|
| 108 | + putenv('https_proxy=' . $proxy . ':' . $proxyPort); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Configure any domains that should be excluded from egress proxy rules and provide them to the environment |
|
| 113 | + */ |
|
| 114 | + protected function configureProxyDomainExclusions() |
|
| 115 | + { |
|
| 116 | + $noProxy = $this->config()->get('egress_proxy_exclude_domains'); |
|
| 117 | + if (empty($noProxy)) { |
|
| 118 | + return; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + if (!is_array($noProxy)) { |
|
| 122 | + $noProxy = [$noProxy]; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // Merge with exsiting if needed. |
|
| 126 | + if (Environment::getEnv('NO_PROXY')) { |
|
| 127 | + $noProxy = array_merge(explode(',', Environment::getEnv('NO_PROXY')), $noProxy); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /* |
|
| 131 | 131 | * Set the environment varial for NO_PROXY the same way the |
| 132 | 132 | * proxy variables are set above |
| 133 | 133 | */ |
| 134 | - putenv('NO_PROXY=' . implode(',', array_unique($noProxy))); |
|
| 135 | - } |
|
| 134 | + putenv('NO_PROXY=' . implode(',', array_unique($noProxy))); |
|
| 135 | + } |
|
| 136 | 136 | } |