Skip to content

Better error messages#645

Closed
DemiMarie wants to merge 4 commits intoQubesOS:mainfrom
DemiMarie:better-error-messages
Closed

Better error messages#645
DemiMarie wants to merge 4 commits intoQubesOS:mainfrom
DemiMarie:better-error-messages

Conversation

@DemiMarie
Copy link
Copy Markdown
Contributor

Use better exceptions for various client errors.

This is DRAFT and not ready yet for many reasons.

Previously qubesd would just terminate the connection.
If the destination of an Admin API method must be dom0 but it is not,
raise a useful exception instead of terminating the connection.
BackupCancelledError is the correct exception for this.
@ben-grande
Copy link
Copy Markdown
Contributor

Superseded by #751

First time seeing this PR though, I will revise it and add relevant things to #751.

Copy link
Copy Markdown
Contributor

@ben-grande ben-grande left a comment

Choose a reason for hiding this comment

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

Demi, this revision is not for you, it is for me, to remember what to do.

Comment thread qubes/api/__init__.py

#: argument
self.arg = arg.decode("ascii")
self.arg = arg.decode("ascii", "strict")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is already the default:

Was this intended to be explicit, enforce against upstream change of defaults?

Comment thread qubes/api/__init__.py
"""If the destination is not dom0, raise an exception."""
name = self.dest.name
if name != "dom0":
raise DestinationNotDom0Error(name)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I didn't make special exceptions for target error. It is using ProtocolError("Destination is not dom0") for example. I think it is fine. Same thing for the code above regarding prohibiting the use of argument.

Comment thread qubes/api/__init__.py
len(untrusted_payload),
)
if self.transport is not None:
self.send_exception(err)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This was added.

Comment thread qubes/api/admin.py
async def vm_volume_revert(self, untrusted_payload):
self.enforce(self.arg in self.dest.volumes.keys())
self._check_volume()
self.enforce(_snapshot_re.match(untrusted_payload))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copying the regex here so it makes it easier to read:

_snapshot_re = re.compile(rb"\A[A-Za-z0-9][0-9A-Za-z_.-]*\n?\Z")

The revision is in the payload. Later on, it is checked if the untrusted_revision is in the snapshots. I believe the regex you set was to allow a sanitized exception message. Although I am not logging the revision name, the check for correct revision in safe set was added:

self.enforce_arg(
    wants=self.dest.volumes.keys(),
    short_reason=self.EXC_ARG_NOT_IN_DEST_VOLUMES,
)
untrusted_revision = untrusted_payload.decode("ascii").strip()
del untrusted_payload
allowed_chars = string.ascii_letters + string.digits + "-_."
self.enforce(
    all(c in allowed_chars for c in untrusted_revision),
    reason="Revision name must be in safe set: " + allowed_chars,
)
revision = untrusted_revision
del untrusted_revision

Comment thread qubes/api/admin.py
"volatile",
"kernel",
]:
raise qubes.exc.UnknownVolumeError("<untrusted>")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is also done, but specifying the volumes that it is not, rather than what volume it is.

Comment thread qubes/api/admin.py
mode = allowed_values[untrusted_payload]
except KeyError:
raise qubes.exc.PermissionDenied()
raise qubes.exc.QubesAttachmentKindError("invalid attachment kind")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changed to ProtocolError("Invalid assignment mode"). Wouldn't hurt to add an specific exception. TODO.

Not related to this PR, but the docstring does not match what allowed_values expect of untrusted_payload.

Comment thread qubes/api/admin.py
raise qubes.exc.PermissionDenied(
"Backup profile {} does not exist".format(self.arg)
)
raise BackupProfileNotFoundError(self.arg)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Wouldn't hurt, good idea, TODO.

Comment thread qubes/api/admin.py
raise qubes.exc.PermissionDenied(
"Backup profile {} does not exist".format(self.arg)
)
raise BackupProfileNotFoundError(self.arg)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same, add, TODO.

Comment thread qubes/api/admin.py
snapshots = volume.revisions
self.enforce(untrusted_revision in snapshots)
if untrusted_revision not in snapshots:
raise qubes.exc.QubesNoSuchSnapshotError(untrusted_revision)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Done.

ben-grande added a commit to ben-grande/qubes-core-admin that referenced this pull request Feb 17, 2026
ben-grande added a commit to ben-grande/qubes-core-admin that referenced this pull request Feb 17, 2026
ben-grande added a commit to ben-grande/qubes-core-admin that referenced this pull request Mar 9, 2026
ben-grande added a commit to ben-grande/qubes-core-admin that referenced this pull request Mar 10, 2026
ben-grande added a commit to ben-grande/qubes-core-admin that referenced this pull request Mar 11, 2026
ben-grande added a commit to ben-grande/qubes-core-admin that referenced this pull request Mar 11, 2026
ben-grande added a commit to ben-grande/qubes-core-admin that referenced this pull request Mar 11, 2026
ben-grande added a commit to ben-grande/qubes-core-admin that referenced this pull request Mar 11, 2026
ben-grande added a commit to ben-grande/qubes-core-admin that referenced this pull request Mar 11, 2026
ben-grande added a commit to ben-grande/qubes-core-admin that referenced this pull request Mar 11, 2026
ben-grande added a commit to ben-grande/qubes-core-admin that referenced this pull request Mar 12, 2026
ben-grande added a commit to ben-grande/qubes-core-admin that referenced this pull request Mar 12, 2026
ben-grande added a commit to ben-grande/qubes-core-admin that referenced this pull request Mar 12, 2026
ben-grande added a commit to ben-grande/qubes-core-admin that referenced this pull request Mar 29, 2026
ben-grande added a commit to ben-grande/qubes-core-admin that referenced this pull request Apr 18, 2026
ben-grande added a commit to ben-grande/qubes-core-admin that referenced this pull request Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants