1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CWP\Core\Control; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPRequest; |
6
|
|
|
use SilverStripe\Control\Middleware\HTTPMiddleware; |
7
|
|
|
use SilverStripe\Core\Config\Configurable; |
8
|
|
|
use SilverStripe\Core\Environment; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Initialises CWP-specific configuration settings, to avoid _config.php. |
12
|
|
|
*/ |
13
|
|
|
class InitialisationMiddleware implements HTTPMiddleware |
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 (short max-age, excluding dev environments): |
56
|
|
|
* ```yml |
57
|
|
|
* --- |
58
|
|
|
* Name: appsecurity |
59
|
|
|
* After: '#cwpsecurity' |
60
|
|
|
* Except: |
61
|
|
|
* environment: dev |
62
|
|
|
* --- |
63
|
|
|
* CWP\Core\Control\InitialisationMiddleware: |
64
|
|
|
* strict_transport_security: 'max-age=300' |
65
|
|
|
* SilverStripe\Core\Injector\Injector: |
66
|
|
|
* SilverStripe\Control\Middleware\CanonicalURLMiddleware: |
67
|
|
|
* properties: |
68
|
|
|
* ForceSSL: true |
69
|
|
|
* ForceSSLPatterns: null |
70
|
|
|
* ``` |
71
|
|
|
* |
72
|
|
|
* Note: This is enabled by default in `cwp/installer` starting with 2.4.x, |
73
|
|
|
* see `app/_config/security.yml`. |
74
|
|
|
* |
75
|
|
|
* @see https://www.cwp.govt.nz/developer-docs/en/2/working_with_projects/security/ |
76
|
|
|
* @config |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
private static $strict_transport_security = null; |
|
|
|
|
80
|
|
|
|
81
|
|
|
public function process(HTTPRequest $request, callable $delegate) |
82
|
|
|
{ |
83
|
|
|
if ($this->config()->get('egress_proxy_default_enabled')) { |
84
|
|
|
$this->configureEgressProxy(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->configureProxyDomainExclusions(); |
88
|
|
|
|
89
|
|
|
$response = $delegate($request); |
90
|
|
|
|
91
|
|
|
if ($this->config()->get('xss_protection_enabled') && $response) { |
92
|
|
|
$response->addHeader('X-XSS-Protection', '1; mode=block'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$hsts = $this->config()->get('strict_transport_security'); |
96
|
|
|
if ($hsts && $response) { |
97
|
|
|
$response->addHeader('Strict-Transport-Security', $hsts); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $response; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* If the outbound egress proxy details have been defined in environment variables, configure the proxy |
105
|
|
|
* variables that are used to configure it. |
106
|
|
|
*/ |
107
|
|
|
protected function configureEgressProxy() |
108
|
|
|
{ |
109
|
|
|
if (!Environment::getEnv('SS_OUTBOUND_PROXY') |
110
|
|
|
|| !Environment::getEnv('SS_OUTBOUND_PROXY_PORT') |
111
|
|
|
) { |
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$proxy = Environment::getEnv('SS_OUTBOUND_PROXY'); |
116
|
|
|
$proxyPort = Environment::getEnv('SS_OUTBOUND_PROXY_PORT'); |
117
|
|
|
|
118
|
|
|
/* |
119
|
|
|
* This sets the environment variables so they are available in |
120
|
|
|
* external calls executed by exec() such as curl. |
121
|
|
|
* Environment::setEnv() would only availabe in context of SilverStripe. |
122
|
|
|
* Environment::getEnv() will fallback to getenv() and will therefore |
123
|
|
|
* fetch the variables |
124
|
|
|
*/ |
125
|
|
|
putenv('http_proxy=' . $proxy . ':' . $proxyPort); |
126
|
|
|
putenv('https_proxy=' . $proxy . ':' . $proxyPort); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Configure any domains that should be excluded from egress proxy rules and provide them to the environment |
131
|
|
|
*/ |
132
|
|
|
protected function configureProxyDomainExclusions() |
133
|
|
|
{ |
134
|
|
|
$noProxy = $this->config()->get('egress_proxy_exclude_domains'); |
135
|
|
|
if (empty($noProxy)) { |
136
|
|
|
return; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (!is_array($noProxy)) { |
140
|
|
|
$noProxy = [$noProxy]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// Merge with exsiting if needed. |
144
|
|
|
if (Environment::getEnv('NO_PROXY')) { |
145
|
|
|
$noProxy = array_merge(explode(',', Environment::getEnv('NO_PROXY')), $noProxy); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/* |
149
|
|
|
* Set the environment varial for NO_PROXY the same way the |
150
|
|
|
* proxy variables are set above |
151
|
|
|
*/ |
152
|
|
|
putenv('NO_PROXY=' . implode(',', array_unique($noProxy))); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|