Repository navigation
Expand file tree
/
Copy pathconfig.php
More file actions
219 lines (181 loc) · 4.39 KB
/
Copy pathconfig.php
File metadata and controls
219 lines (181 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
declare(strict_types=1);
require_once __DIR__ . '/urls.php';
require_once __DIR__ . '/mysql-session-handler.php';
const APPLICATION_SESSION_LIFETIME = 60 * 60 * 24 * 30;
mysqli_report(
MYSQLI_REPORT_ERROR |
MYSQLI_REPORT_STRICT
);
/*
* Retrieve and validate the database configuration before starting the
* session because PHP sessions are stored in that database.
*/
$databaseEnvironment = [];
foreach (
[
'MYSQL_HOST',
'MYSQL_PORT',
'MYSQL_DATABASE',
'MYSQL_USER',
'MYSQL_PASSWORD',
] as $environmentName
) {
$value = getenv($environmentName);
if (
!is_string($value) ||
trim($value) === ''
) {
error_log(
sprintf(
'%s is not configured.',
$environmentName
)
);
http_response_code(503);
exit(
'The service is temporarily unavailable.'
);
}
$databaseEnvironment[$environmentName] =
trim($value);
}
$host = $databaseEnvironment['MYSQL_HOST'];
$user = $databaseEnvironment['MYSQL_USER'];
$pass = $databaseEnvironment['MYSQL_PASSWORD'];
$dbname = $databaseEnvironment['MYSQL_DATABASE'];
$portValue =
$databaseEnvironment['MYSQL_PORT'];
if (
filter_var(
$portValue,
FILTER_VALIDATE_INT,
[
'options' => [
'min_range' => 1,
'max_range' => 65535,
],
]
) === false
) {
error_log(
'MYSQL_PORT is not a valid TCP port.'
);
http_response_code(503);
exit(
'The service is temporarily unavailable.'
);
}
$port = (int) $portValue;
$config_base_host =
getenv('WEBSITE_HOSTNAME');
if (
!is_string($config_base_host) ||
trim($config_base_host) === ''
) {
error_log(
'WEBSITE_HOSTNAME is not configured.'
);
http_response_code(503);
exit(
'The service is temporarily unavailable.'
);
}
$config_redirect_uri = applicationHttpsUrl(
trim($config_base_host),
'/token.php'
);
try {
/*
* PHP owns this connection through the session handler. It remains
* independent of $config_conn so application code can close its own
* connection before request shutdown.
*/
$sessionConnection = mysqli_connect(
$host,
$user,
$pass,
$dbname,
$port
);
mysqli_set_charset(
$sessionConnection,
'utf8mb4'
);
$sessionHandler = new MySqlSessionHandler(
$sessionConnection
);
session_set_save_handler(
$sessionHandler,
true
);
ini_set(
'session.gc_maxlifetime',
(string) APPLICATION_SESSION_LIFETIME
);
session_set_cookie_params([
'lifetime' =>
APPLICATION_SESSION_LIFETIME,
'path' => '/',
'secure' => true,
'httponly' => true,
'samesite' => 'Lax',
]);
session_start();
/*
* This is the application's normal database connection.
*/
$config_conn = mysqli_connect(
$host,
$user,
$pass,
$dbname,
$port
);
mysqli_set_charset(
$config_conn,
'utf8mb4'
);
} catch (mysqli_sql_exception $exception) {
error_log(
'Database or session initialisation failed: ' .
$exception->getMessage()
);
http_response_code(503);
exit(
'The service is temporarily unavailable.'
);
}
$cspNonce = base64_encode(
random_bytes(18)
);
$csp = implode('; ', [
"default-src 'self'",
"base-uri 'self'",
"object-src 'none'",
"form-action 'self'",
"script-src 'self' 'nonce-{$cspNonce}'",
"style-src 'self'",
"img-src 'self' data: blob:",
"font-src 'self'",
"connect-src 'self'",
"manifest-src 'self'",
"worker-src 'none'",
"frame-ancestors 'none'",
"frame-src 'none'",
"upgrade-insecure-requests",
]);
// Uncomment the following line to enable reporting of CSP violations to a specified endpoint
// $csp .= "; report-uri /csp-report-endpoint.php";
// header("Content-Security-Policy-Report-Only: {$csp}");
// Comment the line below to disable enforcement of the CSP if you uncomment the report-uri line above
header(
"Content-Security-Policy: {$csp}"
);
header(
'X-Content-Type-Options: nosniff'
);
header(
'Referrer-Policy: strict-origin-when-cross-origin'
);
include_once __DIR__ . '/functions.php';