RPC: Delete cookie file before replacing it

Unclear if this is the best thing to do, but due to v26.1.knots20240325 creating it read-only, it is somewhat necessary for now
This commit is contained in:
Luke Dashjr 2024-05-11 15:54:24 +00:00
parent c04e29bc3a
commit 6cd206d331
2 changed files with 17 additions and 0 deletions

View File

@ -106,6 +106,11 @@ bool GenerateAuthCookie(std::string* cookie_out, const std::pair<std::optional<f
file.close(); file.close();
fs::path filepath = GetAuthCookieFile(false); fs::path filepath = GetAuthCookieFile(false);
try {
fs::remove(filepath);
} catch (const fs::filesystem_error& e) {
// ignore
}
if (!RenameOver(filepath_tmp, filepath)) { if (!RenameOver(filepath_tmp, filepath)) {
LogInfo("Unable to rename cookie authentication file %s to %s\n", fs::PathToString(filepath_tmp), fs::PathToString(filepath)); LogInfo("Unable to rename cookie authentication file %s to %s\n", fs::PathToString(filepath_tmp), fs::PathToString(filepath));
return false; return false;

View File

@ -257,6 +257,18 @@ class HTTPBasicsTest(BitcoinTestFramework):
(self.nodes[0].chain_path / ".cookie.tmp").mkdir() (self.nodes[0].chain_path / ".cookie.tmp").mkdir()
(self.nodes[0].chain_path / ".cookie.tmp/subdir").mkdir() (self.nodes[0].chain_path / ".cookie.tmp/subdir").mkdir()
self.nodes[0].assert_start_raises_init_error(expected_msg=init_error) self.nodes[0].assert_start_raises_init_error(expected_msg=init_error)
(self.nodes[0].chain_path / ".cookie.tmp/subdir").rmdir()
(self.nodes[0].chain_path / ".cookie.tmp").rmdir()
(self.nodes[0].chain_path / ".cookie").mkdir()
(self.nodes[0].chain_path / ".cookie/subdir").mkdir()
self.nodes[0].assert_start_raises_init_error(expected_msg=init_error)
(self.nodes[0].chain_path / ".cookie/subdir").rmdir()
(self.nodes[0].chain_path / ".cookie").rmdir()
self.log.info('Check that a non-writable cookie file will get replaced gracefully')
(self.nodes[0].chain_path / ".cookie").mkdir(mode=1)
self.restart_node(0)
self.test_rpccookieperms() self.test_rpccookieperms()