-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession.php
More file actions
298 lines (259 loc) · 7.26 KB
/
Copy pathSession.php
File metadata and controls
298 lines (259 loc) · 7.26 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
declare(strict_types=1);
namespace WebCodeFTP\Models;
use WebCodeFTP\Core\SecurityManager;
/**
* Secure Session Manager
*
* Handles session initialization, validation, and security.
* Prevents session hijacking, fixation, and other attacks.
*/
class Session
{
private bool $started = false;
public function __construct(
private array $config,
private SecurityManager $security
) {}
/**
* Start secure session with hardened configuration
*/
public function start(): void
{
if ($this->started || session_status() === PHP_SESSION_ACTIVE) {
return;
}
$sessionConfig = $this->config['security']['session'];
// Configure session parameters BEFORE starting session
ini_set('session.use_strict_mode', $sessionConfig['use_strict_mode'] ? '1' : '0');
ini_set('session.use_only_cookies', $sessionConfig['use_only_cookies'] ? '1' : '0');
ini_set('session.cookie_httponly', $sessionConfig['cookie_httponly'] ? '1' : '0');
ini_set('session.cookie_secure', $sessionConfig['cookie_secure'] ? '1' : '0');
ini_set('session.cookie_samesite', $sessionConfig['cookie_samesite']);
ini_set('session.cookie_lifetime', (string)$sessionConfig['lifetime']);
ini_set('session.gc_maxlifetime', (string)$sessionConfig['lifetime']);
// Use custom session name
session_name($sessionConfig['name']);
// Start session
session_start();
$this->started = true;
// Validate session
$this->validateSession();
}
/**
* Validate session to prevent hijacking
*/
private function validateSession(): void
{
// First-time session initialization
if (!isset($_SESSION['_initialized'])) {
$this->initializeSession();
return;
}
// Validate session fingerprint
if (!$this->validateFingerprint()) {
$this->destroy();
$this->initializeSession();
return;
}
// Check session timeout
if ($this->isExpired()) {
$this->destroy();
return;
}
// Update last activity time
$_SESSION['_last_activity'] = time();
}
/**
* Initialize new session with security markers
*/
private function initializeSession(): void
{
$_SESSION['_initialized'] = true;
$_SESSION['_created'] = time();
$_SESSION['_last_activity'] = time();
$_SESSION['_fingerprint'] = $this->generateFingerprint();
$_SESSION['_ip_address'] = $this->security->getClientIp();
}
/**
* Generate session fingerprint for validation
*
* @return string Session fingerprint hash
*/
private function generateFingerprint(): string
{
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$acceptLanguage = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '';
$acceptEncoding = $_SERVER['HTTP_ACCEPT_ENCODING'] ?? '';
// Create fingerprint from browser characteristics
return hash('sha256', $userAgent . $acceptLanguage . $acceptEncoding);
}
/**
* Validate session fingerprint
*
* @return bool True if fingerprint matches
*/
private function validateFingerprint(): bool
{
if (!isset($_SESSION['_fingerprint'])) {
return false;
}
$currentFingerprint = $this->generateFingerprint();
return hash_equals($_SESSION['_fingerprint'], $currentFingerprint);
}
/**
* Check if session has expired
*
* @return bool True if expired
*/
private function isExpired(): bool
{
if (!isset($_SESSION['_last_activity'])) {
return true;
}
$lifetime = $this->config['security']['session']['lifetime'];
return (time() - $_SESSION['_last_activity']) > $lifetime;
}
/**
* Regenerate session ID (prevent fixation)
*/
public function regenerate(): void
{
if ($this->started) {
session_regenerate_id(true);
$_SESSION['_last_regeneration'] = time();
}
}
/**
* Destroy session completely
*/
public function destroy(): void
{
if ($this->started) {
$_SESSION = [];
// Delete session cookie
$params = session_get_cookie_params();
setcookie(
session_name(),
'',
time() - 42000,
$params['path'],
$params['domain'],
$params['secure'],
$params['httponly']
);
session_destroy();
$this->started = false;
}
}
/**
* Set session value
*
* @param string $key Session key
* @param mixed $value Value to store
*/
public function set(string $key, mixed $value): void
{
$_SESSION[$key] = $value;
}
/**
* Get session value
*
* @param string $key Session key
* @param mixed $default Default value if not set
* @return mixed Session value
*/
public function get(string $key, mixed $default = null): mixed
{
return $_SESSION[$key] ?? $default;
}
/**
* Check if session key exists
*
* @param string $key Session key
* @return bool
*/
public function has(string $key): bool
{
return isset($_SESSION[$key]);
}
/**
* Remove session key
*
* @param string $key Session key
*/
public function remove(string $key): void
{
unset($_SESSION[$key]);
}
/**
* Check if user is authenticated
*
* @return bool
*/
public function isAuthenticated(): bool
{
return $this->get('authenticated', false) === true;
}
/**
* Mark user as authenticated
*/
public function authenticate(): void
{
// Regenerate session ID on authentication (prevent session fixation)
if ($this->config['security']['session']['regenerate_on_login']) {
$this->regenerate();
}
$this->set('authenticated', true);
$this->set('auth_time', time());
}
/**
* Mark user as unauthenticated
*/
public function unauthenticate(): void
{
$this->destroy();
}
/**
* Get session ID
*
* @return string
*/
public function getId(): string
{
return session_id();
}
/**
* Flash message - store for one request
*
* @param string $key Flash key
* @param mixed $value Flash value
*/
public function flash(string $key, mixed $value): void
{
$_SESSION['_flash'][$key] = $value;
}
/**
* Get and remove flash message
*
* @param string $key Flash key
* @param mixed $default Default value
* @return mixed Flash value
*/
public function getFlash(string $key, mixed $default = null): mixed
{
$value = $_SESSION['_flash'][$key] ?? $default;
unset($_SESSION['_flash'][$key]);
return $value;
}
/**
* Check if flash message exists
*
* @param string $key Flash key
* @return bool
*/
public function hasFlash(string $key): bool
{
return isset($_SESSION['_flash'][$key]);
}
}