forked from laysense/laysite
Address LaySite 1.4.5 editor and release review suggestions
This commit is contained in:
@@ -44,8 +44,12 @@ final class SiteSecrets
|
||||
private static function key(bool $create = false): string
|
||||
{
|
||||
$path = Database::path() . '.key';
|
||||
clearstatcache(true, $path);
|
||||
if (is_link($path)) {
|
||||
throw new RuntimeException('密钥文件不能使用符号链接。');
|
||||
throw new SiteKeyFileError('密钥文件不能使用符号链接。');
|
||||
}
|
||||
if (!$create && !file_exists($path)) {
|
||||
throw new RuntimeException('站点密钥文件缺失。');
|
||||
}
|
||||
$mask = umask(0077);
|
||||
try {
|
||||
@@ -54,22 +58,22 @@ final class SiteSecrets
|
||||
umask($mask);
|
||||
}
|
||||
if ($file === false) {
|
||||
throw new RuntimeException('无法打开站点密钥文件。');
|
||||
throw new SiteKeyFileError('无法打开站点密钥文件。');
|
||||
}
|
||||
try {
|
||||
if (!flock($file, $create ? LOCK_EX : LOCK_SH) || (fstat($file)['mode'] & 0077) !== 0) {
|
||||
throw new RuntimeException('站点密钥文件需能上锁,并且只能由运行用户访问。');
|
||||
throw new SiteKeyFileError('站点密钥文件需能上锁,并且只能由运行用户访问。');
|
||||
}
|
||||
$key = stream_get_contents($file, self::KEY_BYTES + 1);
|
||||
if ($create && $key === '') {
|
||||
$key = random_bytes(self::KEY_BYTES);
|
||||
if (fwrite($file, $key) !== self::KEY_BYTES || !fflush($file)) {
|
||||
ftruncate($file, 0);
|
||||
throw new RuntimeException('无法写入站点密钥文件。');
|
||||
throw new SiteKeyFileError('无法写入站点密钥文件。');
|
||||
}
|
||||
}
|
||||
if (!is_string($key) || strlen($key) !== self::KEY_BYTES) {
|
||||
throw new RuntimeException('站点密钥文件格式无效。');
|
||||
throw new SiteKeyFileError('站点密钥文件格式无效。');
|
||||
}
|
||||
|
||||
return $key;
|
||||
|
||||
Reference in New Issue
Block a user