|
39 | 39 | define('TRASH_DIR', UPLOAD_DIR . 'trash/'); |
40 | 40 | define('TIMEZONE', 'America/New_York'); |
41 | 41 | define('DATE_TIME_FORMAT','m/d/y h:iA'); |
42 | | -define('TOTAL_UPLOAD_SIZE','5G'); |
| 42 | +define('TOTAL_UPLOAD_SIZE', '5G'); |
43 | 43 | define('REGEX_FOLDER_NAME','/^(?!^(?:CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])$)(?!.*[. ]$)(?:[^<>:"\/\\\\|?*\x00-\x1F]{1,255})(?:[\/\\\\][^<>:"\/\\\\|?*\x00-\x1F]{1,255})*$/xu'); |
44 | 44 | define('PATTERN_FOLDER_NAME','[\p{L}\p{N}_\-\s\/\\\\]+'); |
45 | 45 | define('REGEX_FILE_NAME', '/^[^\x00-\x1F\/\\\\]{1,255}$/u'); |
@@ -191,14 +191,100 @@ function decryptData($encryptedData, $encryptionKey) |
191 | 191 | return openssl_decrypt($ct, $cipher, $encryptionKey, OPENSSL_RAW_DATA, $iv); |
192 | 192 | } |
193 | 193 |
|
194 | | -// Load encryption key |
195 | | -$envKey = getenv('PERSISTENT_TOKENS_KEY'); |
196 | | -if ($envKey === false || $envKey === '') { |
197 | | - $encryptionKey = 'default_please_change_this_key'; |
198 | | - error_log('WARNING: Using default encryption key. Please set PERSISTENT_TOKENS_KEY in your environment.'); |
199 | | -} else { |
200 | | - $encryptionKey = $envKey; |
| 194 | +function fr_get_persistent_tokens_key_file_path(): string |
| 195 | +{ |
| 196 | + return rtrim((string)META_DIR, "/\\") . DIRECTORY_SEPARATOR . 'persistent_tokens.key'; |
| 197 | +} |
| 198 | + |
| 199 | +function fr_resolve_persistent_tokens_key(): array |
| 200 | +{ |
| 201 | + static $resolved = null; |
| 202 | + if (is_array($resolved)) { |
| 203 | + return $resolved; |
| 204 | + } |
| 205 | + |
| 206 | + $defaultKey = 'default_please_change_this_key'; |
| 207 | + $publishedPlaceholders = [$defaultKey, 'please_change_this_@@']; |
| 208 | + |
| 209 | + $envKeyRaw = getenv('PERSISTENT_TOKENS_KEY'); |
| 210 | + $envKey = trim($envKeyRaw === false ? '' : (string)$envKeyRaw); |
| 211 | + |
| 212 | + $sourceHintRaw = getenv('PERSISTENT_TOKENS_KEY_SOURCE'); |
| 213 | + $sourceHint = trim($sourceHintRaw === false ? '' : (string)$sourceHintRaw); |
| 214 | + |
| 215 | + $keyFile = fr_get_persistent_tokens_key_file_path(); |
| 216 | + $fileKey = ''; |
| 217 | + if (is_file($keyFile)) { |
| 218 | + $raw = @file_get_contents($keyFile); |
| 219 | + if ($raw !== false) { |
| 220 | + $fileKey = trim((string)$raw); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + $source = 'legacy_default'; |
| 225 | + $key = $defaultKey; |
| 226 | + if ($envKey !== '') { |
| 227 | + $key = $envKey; |
| 228 | + if (in_array($sourceHint, ['env', 'file', 'generated_file', 'legacy_default'], true)) { |
| 229 | + $source = $sourceHint; |
| 230 | + } else { |
| 231 | + $source = 'env'; |
| 232 | + } |
| 233 | + } elseif ($fileKey !== '') { |
| 234 | + $key = $fileKey; |
| 235 | + $source = 'file'; |
| 236 | + } |
| 237 | + |
| 238 | + $usesPublishedPlaceholder = in_array($key, $publishedPlaceholders, true); |
| 239 | + $usesLegacyDefault = ($source === 'legacy_default'); |
| 240 | + $autoGenerated = ($source === 'generated_file'); |
| 241 | + $needsAttention = $usesLegacyDefault || $usesPublishedPlaceholder; |
| 242 | + |
| 243 | + $warning = ''; |
| 244 | + $recommendedAction = ''; |
| 245 | + if ($usesLegacyDefault) { |
| 246 | + $warning = 'FileRise is using the legacy built-in persistent tokens key because no explicit key is configured.'; |
| 247 | + $recommendedAction = 'Set a unique key for new installs. For existing installs, plan a controlled rotation because changing the key can invalidate remember-me tokens and break decryption of stored secrets until they are re-encrypted.'; |
| 248 | + } elseif ($usesPublishedPlaceholder) { |
| 249 | + $warning = 'FileRise is using a published placeholder persistent tokens key value.'; |
| 250 | + $recommendedAction = 'Replace it with a unique key. For existing installs, rotate carefully because changing the key can invalidate remember-me tokens and break decryption of stored secrets until they are re-encrypted.'; |
| 251 | + } elseif ($autoGenerated) { |
| 252 | + $recommendedAction = 'This Docker install auto-generated a key and stored it on disk. Back up metadata/persistent_tokens.key or set PERSISTENT_TOKENS_KEY explicitly before migrating the instance.'; |
| 253 | + } |
| 254 | + |
| 255 | + if ($needsAttention) { |
| 256 | + error_log('WARNING: ' . $warning); |
| 257 | + } |
| 258 | + |
| 259 | + $resolved = [ |
| 260 | + 'key' => $key, |
| 261 | + 'source' => $source, |
| 262 | + 'usesPublishedPlaceholder' => $usesPublishedPlaceholder, |
| 263 | + 'usesLegacyDefault' => $usesLegacyDefault, |
| 264 | + 'autoGenerated' => $autoGenerated, |
| 265 | + 'needsAttention' => $needsAttention, |
| 266 | + 'warning' => $warning, |
| 267 | + 'recommendedAction' => $recommendedAction, |
| 268 | + 'keyFilePresent' => ($fileKey !== ''), |
| 269 | + ]; |
| 270 | + |
| 271 | + return $resolved; |
| 272 | +} |
| 273 | + |
| 274 | +function fr_load_persistent_tokens_key(): string |
| 275 | +{ |
| 276 | + $resolved = fr_resolve_persistent_tokens_key(); |
| 277 | + $key = (string)($resolved['key'] ?? ''); |
| 278 | + return $key; |
| 279 | +} |
| 280 | + |
| 281 | +function fr_get_persistent_tokens_key_status(): array |
| 282 | +{ |
| 283 | + $resolved = fr_resolve_persistent_tokens_key(); |
| 284 | + unset($resolved['key']); |
| 285 | + return $resolved; |
201 | 286 | } |
| 287 | +$encryptionKey = fr_load_persistent_tokens_key(); |
202 | 288 | // Ensure encryption key is always available via $GLOBALS, even when this file |
203 | 289 | // is required from function scope (e.g. API helper bootstrap wrappers). |
204 | 290 | $GLOBALS['encryptionKey'] = $encryptionKey; |
@@ -342,7 +428,8 @@ function loadUserPermissions($username) |
342 | 428 | if (file_exists($adminConfigFile)) { |
343 | 429 | $encrypted = file_get_contents($adminConfigFile); |
344 | 430 | $decrypted = decryptData($encrypted, $encryptionKey); |
345 | | - $adminCfg = json_decode($decrypted, true) ?: []; |
| 431 | + $json = ($decrypted !== false) ? $decrypted : $encrypted; |
| 432 | + $adminCfg = is_string($json) ? (json_decode($json, true) ?: []) : []; |
346 | 433 |
|
347 | 434 | $loginOpts = $adminCfg['loginOptions'] ?? []; |
348 | 435 |
|
|
0 commit comments