@@ -12,97 +12,97 @@ |
||
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 | - public function process(HTTPRequest $request, callable $delegate) |
|
52 | - { |
|
53 | - if ($this->config()->get('egress_proxy_default_enabled')) { |
|
54 | - $this->configureEgressProxy(); |
|
55 | - } |
|
56 | - |
|
57 | - $this->configureProxyDomainExclusions(); |
|
58 | - |
|
59 | - $response = $delegate($request); |
|
60 | - |
|
61 | - if ($this->config()->get('xss_protection_enabled') && $response) { |
|
62 | - $response->addHeader('X-XSS-Protection', '1; mode=block'); |
|
63 | - } |
|
64 | - |
|
65 | - return $response; |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * If the outbound egress proxy details have been defined in environment variables, configure the proxy |
|
70 | - * variables that are used to configure it. |
|
71 | - */ |
|
72 | - protected function configureEgressProxy() |
|
73 | - { |
|
74 | - if (!Environment::getEnv('SS_OUTBOUND_PROXY') |
|
75 | - || !Environment::getEnv('SS_OUTBOUND_PROXY_PORT') |
|
76 | - ) { |
|
77 | - return; |
|
78 | - } |
|
79 | - |
|
80 | - $proxy = Environment::getEnv('SS_OUTBOUND_PROXY'); |
|
81 | - $proxyPort = Environment::getEnv('SS_OUTBOUND_PROXY_PORT'); |
|
82 | - |
|
83 | - Environment::setEnv('http_proxy', $proxy . ':' . $proxyPort); |
|
84 | - Environment::setEnv('https_proxy', $proxy . ':' . $proxyPort); |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Configure any domains that should be excluded from egress proxy rules and provide them to the environment |
|
89 | - */ |
|
90 | - protected function configureProxyDomainExclusions() |
|
91 | - { |
|
92 | - $noProxy = $this->config()->get('egress_proxy_exclude_domains'); |
|
93 | - if (empty($noProxy)) { |
|
94 | - return; |
|
95 | - } |
|
96 | - |
|
97 | - if (!is_array($noProxy)) { |
|
98 | - $noProxy = [$noProxy]; |
|
99 | - } |
|
100 | - |
|
101 | - // Merge with exsiting if needed. |
|
102 | - if (Environment::getEnv('NO_PROXY')) { |
|
103 | - $noProxy = array_merge(explode(',', Environment::getEnv('NO_PROXY')), $noProxy); |
|
104 | - } |
|
105 | - |
|
106 | - Environment::setEnv('NO_PROXY', implode(',', array_unique($noProxy))); |
|
107 | - } |
|
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 | + public function process(HTTPRequest $request, callable $delegate) |
|
52 | + { |
|
53 | + if ($this->config()->get('egress_proxy_default_enabled')) { |
|
54 | + $this->configureEgressProxy(); |
|
55 | + } |
|
56 | + |
|
57 | + $this->configureProxyDomainExclusions(); |
|
58 | + |
|
59 | + $response = $delegate($request); |
|
60 | + |
|
61 | + if ($this->config()->get('xss_protection_enabled') && $response) { |
|
62 | + $response->addHeader('X-XSS-Protection', '1; mode=block'); |
|
63 | + } |
|
64 | + |
|
65 | + return $response; |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * If the outbound egress proxy details have been defined in environment variables, configure the proxy |
|
70 | + * variables that are used to configure it. |
|
71 | + */ |
|
72 | + protected function configureEgressProxy() |
|
73 | + { |
|
74 | + if (!Environment::getEnv('SS_OUTBOUND_PROXY') |
|
75 | + || !Environment::getEnv('SS_OUTBOUND_PROXY_PORT') |
|
76 | + ) { |
|
77 | + return; |
|
78 | + } |
|
79 | + |
|
80 | + $proxy = Environment::getEnv('SS_OUTBOUND_PROXY'); |
|
81 | + $proxyPort = Environment::getEnv('SS_OUTBOUND_PROXY_PORT'); |
|
82 | + |
|
83 | + Environment::setEnv('http_proxy', $proxy . ':' . $proxyPort); |
|
84 | + Environment::setEnv('https_proxy', $proxy . ':' . $proxyPort); |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Configure any domains that should be excluded from egress proxy rules and provide them to the environment |
|
89 | + */ |
|
90 | + protected function configureProxyDomainExclusions() |
|
91 | + { |
|
92 | + $noProxy = $this->config()->get('egress_proxy_exclude_domains'); |
|
93 | + if (empty($noProxy)) { |
|
94 | + return; |
|
95 | + } |
|
96 | + |
|
97 | + if (!is_array($noProxy)) { |
|
98 | + $noProxy = [$noProxy]; |
|
99 | + } |
|
100 | + |
|
101 | + // Merge with exsiting if needed. |
|
102 | + if (Environment::getEnv('NO_PROXY')) { |
|
103 | + $noProxy = array_merge(explode(',', Environment::getEnv('NO_PROXY')), $noProxy); |
|
104 | + } |
|
105 | + |
|
106 | + Environment::setEnv('NO_PROXY', implode(',', array_unique($noProxy))); |
|
107 | + } |
|
108 | 108 | } |