Skip to content

Commit

Permalink
permissions: Gracefully handle malformed overrides files
Browse files Browse the repository at this point in the history
Overrides files are not owned by Flatseal, therefore
there's no guarantees on its contents.

Closes #558
  • Loading branch information
tchx84 committed Aug 31, 2023
1 parent 7950913 commit 066d31d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/models/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ var FlatpakPermissionsModel = GObject.registerClass({
param_types: [GObject.TYPE_BOOLEAN, GObject.TYPE_BOOLEAN],
},
reset: {},
failed: {},
},
}, class FlatpakPermissionsModel extends GObject.Object {
_init() {
Expand Down Expand Up @@ -122,12 +123,19 @@ var FlatpakPermissionsModel = GObject.registerClass({
return GLib.build_filenamev([this._getBaseOverridesPath(), this._appId]);
}

static _loadPermissionsForPath(path, overrides, global) {
_loadPermissionsForPath(path, overrides, global) {
if (GLib.access(path, 0) !== 0)
return;

const keyFile = new GLib.KeyFile();
keyFile.load_from_file(path, 0);

try {
keyFile.load_from_file(path, 0);
} catch (err) {
logError(err, `Could not load ${path}`);
this.emit('failed');
return;
}

const [groups] = keyFile.get_groups();

Expand Down Expand Up @@ -178,20 +186,20 @@ var FlatpakPermissionsModel = GObject.registerClass({
if (isGlobalOverride(this._appId))
return;

this.constructor._loadPermissionsForPath(
this._loadPermissionsForPath(
this._applications.getMetadataPathForAppId(this._appId), false, false);
}

_loadGlobalOverrides() {
if (isGlobalOverride(this._appId))
return;

this.constructor._loadPermissionsForPath(
this._loadPermissionsForPath(
this._getGlobalOverridesPath(), true, true);
}

_loadOverrides() {
this.constructor._loadPermissionsForPath(
this._loadPermissionsForPath(
this._getOverridesPath(), true, false);
}

Expand Down
22 changes: 21 additions & 1 deletion src/widgets/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,14 @@ var FlatsealWindow = GObject.registerClass({
this._toast.button_label = _('_Undo');
this._toast.timeout = 3;
this._toast.connect('button-clicked', this._undoReset.bind(this));
this._permissions.connect('reset', this._showToast.bind(this));
this._resetHandlerId = this._permissions.connect('reset', this._showToast.bind(this));

this._failedToast = new Adw.Toast();
this._failedToast.title = _('Can\'t load overrides due to wrong contents');
this._failedToast.button_label = _('_Reset');
this._failedToast.timeout = null;
this._failedToast.connect('button-clicked', this._doReset.bind(this));
this._permissions.connect('failed', this._showFailedToast.bind(this));

this._contentLeaflet.bind_property(
'folded', this._backButton, 'visible', _bindReadFlags);
Expand Down Expand Up @@ -275,6 +282,7 @@ var FlatsealWindow = GObject.registerClass({
}

_updatePermissions() {
this._failedToast.dismiss();
const row = this._applicationsListBox.get_selected_row();
this._permissions.appId = row.appId;
this._permissionsTitle.title = row.appName;
Expand Down Expand Up @@ -339,6 +347,18 @@ var FlatsealWindow = GObject.registerClass({
this._toastOverlay.add_toast(this._toast);
}

_showFailedToast() {
this._toastOverlay.add_toast(this._failedToast);
}

_doReset() {
GObject.signal_handler_block(this._permissions, this._resetHandlerId);

this._permissions.reset();

GObject.signal_handler_unblock(this._permissions, this._resetHandlerId);
}

_undoReset() {
this._permissions.undo();
}
Expand Down

0 comments on commit 066d31d

Please sign in to comment.