@@ -22,6 +22,16 @@ class Session
2222 /** Default file lifetime */
2323 private const DEFAULT_FILE_LIFETIME = 3 * Nette \Utils \DateTime::HOUR ;
2424
25+ /** @var array default configuration */
26+ private const SECURITY_OPTIONS = [
27+ 'referer_check ' => '' , // must be disabled because PHP implementation is invalid
28+ 'use_cookies ' => 1 , // must be enabled to prevent Session Hijacking and Fixation
29+ 'use_only_cookies ' => 1 , // must be enabled to prevent Session Fixation
30+ 'use_trans_sid ' => 0 , // must be disabled to prevent Session Hijacking and Fixation
31+ 'use_strict_mode ' => 1 , // must be enabled to prevent Session Fixation
32+ 'cookie_httponly ' => true , // must be enabled to prevent Session Hijacking
33+ ];
34+
2535 /** @var bool has been session ID regenerated? */
2636 private $ regenerated = false ;
2737
@@ -30,18 +40,7 @@ class Session
3040
3141 /** @var array default configuration */
3242 private $ options = [
33- // security
34- 'referer_check ' => '' , // must be disabled because PHP implementation is invalid
35- 'use_cookies ' => 1 , // must be enabled to prevent Session Hijacking and Fixation
36- 'use_only_cookies ' => 1 , // must be enabled to prevent Session Fixation
37- 'use_trans_sid ' => 0 , // must be disabled to prevent Session Hijacking and Fixation
38- 'use_strict_mode ' => 1 , // must be enabled to prevent Session Fixation
39-
40- // cookies
4143 'cookie_lifetime ' => 0 , // until the browser is closed
42- 'cookie_httponly ' => true , // must be enabled to prevent Session Hijacking
43-
44- // other
4544 'gc_maxlifetime ' => self ::DEFAULT_FILE_LIFETIME , // 3 hours
4645 ];
4746
@@ -73,19 +72,20 @@ public function start(): void
7372 {
7473 if (session_status () === PHP_SESSION_ACTIVE ) {
7574 if (!$ this ->started ) {
75+ $ this ->configure (self ::SECURITY_OPTIONS );
7676 $ this ->initialize ();
7777 }
7878 return ;
7979 }
8080
81- $ this ->configure ($ this ->options );
81+ $ this ->configure (self :: SECURITY_OPTIONS + $ this ->options );
8282
83- if (!session_id ()) {
83+ if (!session_id ()) { // session is started for first time
8484 $ id = $ this ->request ->getCookie (session_name ());
8585 $ id = is_string ($ id ) && preg_match ('#^[0-9a-zA-Z,-]{22,256}\z#i ' , $ id )
8686 ? $ id
8787 : session_create_id ();
88- session_id ($ id );
88+ session_id ($ id ); // causes resend of a cookie
8989 }
9090
9191 try {
@@ -123,7 +123,7 @@ private function initialize(): void
123123 // regenerate empty session
124124 if (empty ($ nf ['Time ' ])) {
125125 $ nf ['Time ' ] = time ();
126- $ this ->regenerateId ();
126+ $ this ->regenerateId (); // ensures that the session was created in strict mode (see use_strict_mode)
127127 }
128128
129129 // process meta metadata
0 commit comments