Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: fix correctness issues in reading #44230

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions lib/private/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,22 +215,39 @@ private function readData() {

// Include file and merge config
foreach ($configFiles as $file) {
$fileExistsAndIsReadable = file_exists($file) && is_readable($file);
$filePointer = $fileExistsAndIsReadable ? fopen($file, 'r') : false;
if ($file === $this->configFilePath &&
$filePointer === false) {
// Opening the main config might not be possible, e.g. if the wrong
// permissions are set (likely on a new installation)
continue;
unset($CONFIG);

// Invalidate opcache (only if the timestamp changed)
if (function_exists('opcache_invalidate')) {
opcache_invalidate($file, false);
}

$filePointer = @fopen($file, 'r');
if ($filePointer === false) {
emoral435 marked this conversation as resolved.
Show resolved Hide resolved
// e.g. wrong permissions are set
if ($file === $this->configFilePath) {
// opening the main config file might not be possible
// (likely on a new installation)
continue;
}

http_response_code(500);
die(sprintf('FATAL: Could not open the config file %s', $file));
}

// Try to acquire a file lock
if (!flock($filePointer, LOCK_SH)) {
throw new \Exception(sprintf('Could not acquire a shared lock on the config file %s', $file));
}

unset($CONFIG);
include $file;
try {
include $file;
} finally {
// Close the file pointer and release the lock
flock($filePointer, LOCK_UN);
Altahrim marked this conversation as resolved.
Show resolved Hide resolved
fclose($filePointer);
}

if (!defined('PHPUNIT_RUN') && headers_sent()) {
// syntax issues in the config file like leading spaces causing PHP to send output
$errorMessage = sprintf('Config file has leading content, please remove everything before "<?php" in %s', basename($file));
Expand All @@ -242,10 +259,6 @@ private function readData() {
if (isset($CONFIG) && is_array($CONFIG)) {
$this->cache = array_merge($this->cache, $CONFIG);
}

// Close the file pointer and release the lock
flock($filePointer, LOCK_UN);
fclose($filePointer);
}

$this->envCache = getenv();
Expand Down
Loading