Check write access when choosing a data directory in settings

Don't let an unwritable folder be selected as the data directory, which
would cause Zotero to fail to start after the restart.

https://forums.zotero.org/discussion/134510/
This commit is contained in:
Dan Stillman 2026-10-29 12:24:24 -04:00
parent 5f1a304e52
commit 03420a4e7a
3 changed files with 70 additions and 0 deletions

View file

@ -301,6 +301,39 @@ Zotero.DataDirectory = {
},
/**
* Check that a directory can be created and written to, and show an error if not
*
* @param {String} dir
* @return {Promise<Boolean>}
*/
_checkWriteAccess: async function (dir) {
var existed = false;
try {
existed = await IOUtils.exists(dir);
await Zotero.File.createDirectoryIfMissingAsync(dir);
let testFile = PathUtils.join(dir, '.zotero-write-test');
await IOUtils.writeUTF8(testFile, '');
await IOUtils.remove(testFile);
}
catch (e) {
Zotero.logError(e);
Services.prompt.alert(
null,
Zotero.getString('general.accessDenied'),
Zotero.ftl.formatValueSync('data-dir-not-writable', { path: dir })
+ "\n\n"
+ Zotero.ftl.formatValueSync(
'data-dir-check-parent-write-access',
{ path: existed ? dir : PathUtils.parent(dir) }
)
);
return false;
}
return true;
},
/**
* @return {String|false} - New path or false if not changed
*/
@ -310,6 +343,9 @@ Zotero.DataDirectory = {
var newPath;
if (useHomeDir) {
if (!await this._checkWriteAccess(this.defaultDir)) {
return false;
}
let changed = this.set(this.defaultDir);
if (!changed) {
return false;
@ -356,6 +392,10 @@ Zotero.DataDirectory = {
continue;
}
if (!await this._checkWriteAccess(file.path)) {
continue;
}
// In a cloud storage folder (Dropbox, etc.)
if (Zotero.File.isCloudStorageFolder(file.path)) {
dialogTitle = Zotero.getString('general.warning');

View file

@ -1192,6 +1192,7 @@ plugins-blocked-plugin =
.message = This plugin has been disabled by { -app-name }.
data-dir-unsupported-storage = This can happen if the { -app-name } data directory is in a cloud storage folder (OneDrive, Dropbox, etc.) or on a network share.
data-dir-not-writable = { -app-name } can’t write to { $path }.
data-dir-check-parent-write-access = Make sure you have write access to { $path } and that security software isn’t preventing { -app-name } from writing to the disk.
login-manager-reset = { -app-name } was unable to read your saved login information, so it has been reset. Please log in again in the { preferences-pane-account } pane of the { -app-name } settings.

View file

@ -194,6 +194,35 @@ describe("Zotero.DataDirectory", function () {
});
});
describe("#choose()", function () {
it("should show an error and leave the data directory unchanged if the default location isn't writable", async function () {
if (Zotero.isWin) {
this.skip();
}
var parentDir = await getTempDirectory();
var defaultDir = PathUtils.join(parentDir, 'Zotero');
var defaultDirStub = sinon.stub(Zotero.DataDirectory, 'defaultDir').get(() => defaultDir);
var promptService = Services.prompt;
var alertStub = sinon.stub();
var confirmStub = sinon.stub();
Services.prompt = { alert: alertStub, confirmEx: confirmStub };
await IOUtils.setPermissions(parentDir, 0o555);
try {
assert.isFalse(await Zotero.DataDirectory.choose(false, true));
assert.isTrue(alertStub.calledOnce);
assert.include(alertStub.firstCall.args[2], defaultDir);
assert.isFalse(confirmStub.called);
assert.isFalse(stubs.setDataDir.called);
}
finally {
Services.prompt = promptService;
defaultDirStub.restore();
await IOUtils.setPermissions(parentDir, 0o755);
}
});
});
describe("#checkForMigration()", function () {
let fileMoveStub;