@@ -67,14 +67,31 @@ public function __construct(
67
67
68
68
if (isset ($ _COOKIE ['sid ' ]) === true ) {
69
69
self ::$ sid = htmlspecialchars ($ _COOKIE ['sid ' ]);
70
+
71
+ //Part 0 random string without session pw
72
+ //Part 1 remote adds + host with session pw
73
+ //Part 2 random string with session pw
70
74
$ testSession = explode ('- ' , self ::$ sid );
71
75
}
72
76
73
77
//Don't allow session ids from user.
74
78
if (is_array ($ testSession ) === true && count ($ testSession ) > 1 ) {
75
- $ testMD5 = hash ('sha1 ' , $ testSession [0 ] . $ this ->sessionpassword );
79
+ $ testSessionPw = hash ('sha1 ' , $ testSession [0 ] . $ this ->sessionpassword );
80
+
81
+ if ($ testSessionPw !== $ testSession [2 ]) {
82
+ error_log ("failed session pw test of tmp " );
83
+ self ::makeSID ();
84
+ }
85
+
86
+ //test remote host info
87
+ $ session_string = ! $ this ->request instanceof CliRequest
88
+ ? self ::get_client_ip () . $ _SERVER ['HTTP_HOST ' ]
89
+ : 'cli ' ;
90
+
91
+ $ testSessionHost = hash ('sha1 ' , $ session_string . $ this ->sessionpassword );
76
92
77
- if ($ testMD5 !== $ testSession [1 ]) {
93
+ if ($ testSessionHost !== $ testSession [1 ]) {
94
+ error_log ("failed ip and host check " );
78
95
self ::makeSID ();
79
96
}
80
97
} else {
@@ -89,11 +106,11 @@ public function __construct(
89
106
'leantime.core.httpkernel.handle.beforeSendResponse ' ,
90
107
fn ($ response ) => tap ($ response , fn (Response $ response ) => $ response ->headers ->setCookie (
91
108
Cookie::create ('sid ' )
92
- ->withValue (self ::$ sid )
93
- ->withExpires (time () + $ config ->sessionExpiration )
94
- ->withPath ('/ ' )
95
- ->withSameSite ('Lax ' )
96
- ->withSecure (true )
109
+ ->withValue (self ::$ sid )
110
+ ->withExpires (time () + $ config ->sessionExpiration )
111
+ ->withPath ('/ ' )
112
+ ->withSameSite ('Strict ' )
113
+ ->withSecure (true )
97
114
))
98
115
);
99
116
}
@@ -119,12 +136,12 @@ public static function getSID(): string
119
136
private function makeSID (): void
120
137
{
121
138
$ session_string = ! $ this ->request instanceof CliRequest
122
- ? $ _SERVER ['REMOTE_ADDR ' ]
139
+ ? self :: get_client_ip () . $ _SERVER ['HTTP_HOST ' ]
123
140
: 'cli ' ;
124
141
125
142
$ tmp = hash ('sha1 ' , mt_rand (32 , 32 ) . $ session_string . time ());
126
143
127
- self ::$ sid = $ tmp . '- ' . hash ('sha1 ' , $ tmp . $ this ->sessionpassword );
144
+ self ::$ sid = $ tmp . '- ' . hash ('sha1 ' , $ session_string . $ this -> sessionpassword ) . ' - ' . hash ( ' sha1 ' , $ tmp . $ this ->sessionpassword );
128
145
}
129
146
130
147
/**
@@ -143,12 +160,33 @@ public static function destroySession(): void
143
160
'leantime.core.httpkernel.handle.beforeSendResponse ' ,
144
161
fn ($ response ) => tap ($ response , fn (Response $ response ) => $ response ->headers ->setCookie (
145
162
Cookie::create ('sid ' )
146
- ->withValue ('' )
147
- ->withExpires (time () - 42000 )
148
- ->withPath ('/ ' )
149
- ->withSameSite ('Strict ' )
150
- ->withSecure (true )
163
+ ->withValue ('' )
164
+ ->withExpires (time () - 42000 )
165
+ ->withPath ('/ ' )
166
+ ->withSameSite ('Strict ' )
167
+ ->withSecure (true )
151
168
))
152
169
);
153
170
}
171
+
172
+ private static function get_client_ip ()
173
+ {
174
+ $ ipaddress = '' ;
175
+ if (getenv ('HTTP_CLIENT_IP ' )) {
176
+ $ ipaddress = getenv ('HTTP_CLIENT_IP ' );
177
+ } elseif (getenv ('HTTP_X_FORWARDED_FOR ' )) {
178
+ $ ipaddress = getenv ('HTTP_X_FORWARDED_FOR ' );
179
+ } elseif (getenv ('HTTP_X_FORWARDED ' )) {
180
+ $ ipaddress = getenv ('HTTP_X_FORWARDED ' );
181
+ } elseif (getenv ('HTTP_FORWARDED_FOR ' )) {
182
+ $ ipaddress = getenv ('HTTP_FORWARDED_FOR ' );
183
+ } elseif (getenv ('HTTP_FORWARDED ' )) {
184
+ $ ipaddress = getenv ('HTTP_FORWARDED ' );
185
+ } elseif (getenv ('REMOTE_ADDR ' )) {
186
+ $ ipaddress = getenv ('REMOTE_ADDR ' );
187
+ } else {
188
+ $ ipaddress = 'UNKNOWN ' ;
189
+ }
190
+ return $ ipaddress ;
191
+ }
154
192
}
0 commit comments