Better error checking when saving a temporary key to ensure file was written successfully and the server is the exclusive mode

This commit is contained in:
Alex Bilbie 2017-06-16 16:59:29 +01:00
parent 2f8de3d230
commit 63530443fe

View File

@ -74,7 +74,8 @@ class CryptKey
*/ */
private function saveKeyToFile($key) private function saveKeyToFile($key)
{ {
$keyPath = sys_get_temp_dir() . '/' . sha1($key) . '.key'; $tmpDir = sys_get_temp_dir();
$keyPath = $tmpDir . '/' . sha1($key) . '.key';
if (!file_exists($keyPath) && !touch($keyPath)) { if (!file_exists($keyPath) && !touch($keyPath)) {
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
@ -82,7 +83,17 @@ class CryptKey
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
} }
file_put_contents($keyPath, $key); if (file_put_contents($keyPath, $key) === false) {
// @codeCoverageIgnoreStart
throw new \RuntimeException('Unable to write key file to temporary directory "%s"', $tmpDir);
// @codeCoverageIgnoreEnd
}
if (chmod($keyPath, 0600) === false) {
// @codeCoverageIgnoreStart
throw new \RuntimeException('The key file "%s" file mode could not be changed with chmod to 600', $keyPath);
// @codeCoverageIgnoreEnd
}
return 'file://' . $keyPath; return 'file://' . $keyPath;
} }