Skip to content

Conversation

@tylerchambers
Copy link
Contributor

If settings.json exists, but is unreadable, we should error instead of overwriting.

Fixes #22571

@tryphe
Copy link
Contributor

tryphe commented Jul 31, 2021

Concept ACK

@fanquake fanquake requested a review from ryanofsky August 2, 2021 01:53
Copy link
Contributor

@Zero-1729 Zero-1729 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 2b07126

Tested on macOS v11.4

Can confirm patch works as advertised in the op, running bitcoin-qt reports a file permission error if settings.json can't be read instead of overwriting the file (as is currently done on master).

On master

Running bitcoin-qt after changing the file ownership & permissions of settings.json to root & -rwx------ (via chmod 700 settings.json) respectively, opens without an error and overwrites settings.json.

After patch

However, running bitcoin-qt after changing file ownership to root and file permissions to -rwx------, the permissions error is reported, and the file is no longer overwritten:

Screenshot 2021-08-02 at 04 58 23

Screenshot 2021-08-02 at 04 58 05

@maflcko maflcko changed the title Util: error if settings.json exists, but is unreadable. Util: error if settings json exists, but is unreadable Aug 2, 2021
Copy link
Contributor

@shaavan shaavan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 2b07126
Compiled and Tested on Ubuntu 20.04

This PR adds the functionality to display an error message when the settings.json file exists but could not be read instead of simply going forward and overwriting it. This is done by returning False result (instead of True, as done by the master) when the file is there but could not be opened. Tested the PR by navigating to the .bitcoin folder followed by sudo chmod root:root settings.json and sudo chmod a-rwx settings.json as suggested here. The PR is successful in doing what is said in the description.
Here is the screenshot of the successful working of the PR.

ON MASTER (after running src/qt/bitcoin-qt)
Screenshot from 2021-08-04 20-46-18

ON PR (after running src/qt/bitcoin-qt)
Screenshot from 2021-08-04 20-27-30

I agree with the changes suggested by op. Because it might happen that the user might have added some custom changes to the settings.json file, the user could lose those changes if, for some reason, the file was not able to be read at the time of starting of bitcoin-qt. Also, the user should be aware of when the Client opened the settings file correctly and when it was overwritten because the settings file was not read. This PR can tackle both of these issues.

@jonatack
Copy link
Member

jonatack commented Aug 4, 2021

Approach ACK

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 2b07126

Tried on Windows because normally such things behave differently in Windows resulting in some issues. Changed the permissions for settings.json file and tried opening bitcoin-qt.

Master branch:
image

This created a file: settings.json.tmp:

image

PR branch:

image

nit: Couldn't find anything in debug.log. Maybe we can print this error in logs as well. Or do we require -debug=something for it?

Same error is printed if I try to run bitcoind:

Error: Failed loading settings file:
- E:\regtest\settings.json. Please check permissions.

Also tried running bitcoind and bitcoin-qt with file open in other programs (editors, defender etc.) but didn't see any errors or unexpected behavior.

I am not sure if there can be more reasons for is_open() returning false. 'Please check permissions' looks good enough for now as a hint in the error or most likely reason for the error.

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 2b07126. Thanks for the fix! Note that PR bitcoin-core/gui#379 will change the appearance of dialogs shown in screenshots above. So it could be interesting to test the two PRs together (but current testing seems more than sufficient)

file.open(path);
if (!file.is_open()) return true; // Ok for file not to exist.
if (!file.is_open()) {
errors.emplace_back(strprintf("%s. Please check permissions.", path.string()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit "error if settings.json exists, but is unreadable" (2b07126)

Indent should be 4 instead of 2 spaces

Also this text is pretty inconsistent with the other errors below. The other errors mention what problem happened at the path, while this just prints the path with no description of the failure, only a request to check permissions. I didn't really follow the previous review discussion, but for clarity and consistency with other errors below I'd consider changing to something like:

errors.emplace_back(strprintf("Failed to open settings file %s for reading (may be inaccessible due to permissions)", path.string()));

@Zero-1729
Copy link
Contributor

Zero-1729 commented Aug 5, 2021

Following the comment above, I was curious to see how the two PRs would look once both are merged. So I decided to rebase this PR atop bitcoin-core/gui#379 and test.

I also tested with @ryanofsky's suggestion above (result added below). After seeing the errors side-by-side, @tylerchambers I'll also recommend considering including the suggestion.

Result

Tested on macOS v11.4

As I did in my previous test, I ran bitcoin-qt after changing file ownership to root and file permissions to -rwx------, then got the following cli error and GUI prompt dialog:

$ src/qt/bitcoin-qt                                                   
Error: Settings file could not be read:
- /Users/abubakar/Library/Application Support/Bitcoin/settings.json. Please check permissions.

Note: Now the settings.json is only overwritten if the user explicitly clicks reset, else it remains untouched. IMHO, the new GUI prompt is a lot sleeker and less noisy.

Screenshot 2021-08-05 at 23 33 05

After opening the details:

Screenshot 2021-08-05 at 23 12 27

With @ryanofsky's suggestion above

$ src/qt/bitcoin-qt
Error: Settings file could not be read:
- Failed to open settings file /Users/abubakar/Library/Application Support/Bitcoin/settings.json for reading (may be inaccessible due to permissions)

Screenshot 2021-08-05 at 23 24 44

I am really glad to see how these two PRs work well to tackle this issue! 🍾

@tryphe
Copy link
Contributor

tryphe commented Aug 25, 2021

Quick summary to aid review: this PR is a non-gui version of bitcoin-core/gui#379 (merged)

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 2b07126 📁

Tested by first removing permissions via chmod 000 ~/.bitcoin/settings.json and then starting bitcoind. In master, the file is overwritten, in the PR branch, the introduced error message appears and bitcoind exits with error code 1.

@maflcko maflcko merged commit 91e07cc into bitcoin:master Sep 5, 2021
@tylerchambers tylerchambers deleted the fix-22571 branch September 6, 2021 20:32
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Sep 7, 2021
…adable

2b07126 error if settings.json exists, but is unreadable (Tyler Chambers)

Pull request description:

  If settings.json exists, but is unreadable, we should error instead of overwriting.

  Fixes bitcoin#22571

ACKs for top commit:
  Zero-1729:
    tACK 2b07126
  ShaMan239:
    tACK 2b07126
  prayank23:
    tACK bitcoin@2b07126
  ryanofsky:
    Code review ACK 2b07126. Thanks for the fix! Note that PR   bitcoin-core/gui#379 will change the appearance of dialogs shown in screenshots above. So it could be interesting to test the two PRs together (but current testing seems more than sufficient)
  theStack:
    ACK 2b07126 📁

Tree-SHA512: 6f7f96ce8a13213d0335198a2245d127264495c877105058d1503252435915b332a6e55068ac21088f4c0c017d564689f4956213328d5bdee81d73711efc5511
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Oct 10, 2021
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 10, 2022
Summary: This is a backport of [[bitcoin/bitcoin#22591 | core#22591]]

Test Plan:
`ninja all check-all`

```
$ sudo chmod a-rwx /bitcoinddata/settings.json
$ src/bitcoind
Error: Failed loading settings file:
- /bitcoinddata/settings.json. Please check permissions.
```

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10783
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Sep 3, 2022
…adable

2b07126 error if settings.json exists, but is unreadable (Tyler Chambers)

Pull request description:

  If settings.json exists, but is unreadable, we should error instead of overwriting.

  Fixes bitcoin#22571

ACKs for top commit:
  Zero-1729:
    tACK 2b07126
  ShaMan239:
    tACK 2b07126
  prayank23:
    tACK bitcoin@2b07126
  ryanofsky:
    Code review ACK 2b07126. Thanks for the fix! Note that PR   bitcoin-core/gui#379 will change the appearance of dialogs shown in screenshots above. So it could be interesting to test the two PRs together (but current testing seems more than sufficient)
  theStack:
    ACK 2b07126 📁

Tree-SHA512: 6f7f96ce8a13213d0335198a2245d127264495c877105058d1503252435915b332a6e55068ac21088f4c0c017d564689f4956213328d5bdee81d73711efc5511
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

settings.json overwritten if missing read permissions

9 participants