Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Accept empty result from sd-devices during export for backwards compatibility #1596

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions securedrop_client/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def connect_signals(
self.run_printer_preflight, type=Qt.QueuedConnection
)

def _export_archive(cls, archive_path: str) -> ExportStatus:
def _export_archive(cls, archive_path: str) -> Optional[ExportStatus]:
gonzalo-bulnes marked this conversation as resolved.
Show resolved Hide resolved
"""
Make the subprocess call to send the archive to the Export VM, where the archive will be
processed.
Expand Down Expand Up @@ -152,7 +152,14 @@ def _export_archive(cls, archive_path: str) -> ExportStatus:
],
stderr=subprocess.STDOUT,
)
return ExportStatus(output.decode("utf-8").strip())
result = output.decode("utf-8").strip()

# No status is returned for successful `disk`, `printer-test`, and `print` calls.
# This will change in a future release of sd-export.
if result:
return ExportStatus(result)
else:
return None
gonzalo-bulnes marked this conversation as resolved.
Show resolved Hide resolved
except ValueError as e:
logger.debug(f"Export subprocess returned unexpected value: {e}")
raise ExportError(ExportStatus.UNEXPECTED_RETURN_STATUS)
Expand Down Expand Up @@ -241,7 +248,7 @@ def _run_usb_test(self, archive_dir: str) -> None:
"""
archive_path = self._create_archive(archive_dir, self.USB_TEST_FN, self.USB_TEST_METADATA)
status = self._export_archive(archive_path)
if status != ExportStatus.USB_CONNECTED:
if status and status != ExportStatus.USB_CONNECTED:
raise ExportError(status)

def _run_disk_test(self, archive_dir: str) -> None:
Expand All @@ -257,7 +264,7 @@ def _run_disk_test(self, archive_dir: str) -> None:
archive_path = self._create_archive(archive_dir, self.DISK_TEST_FN, self.DISK_TEST_METADATA)

status = self._export_archive(archive_path)
if status != ExportStatus.DISK_ENCRYPTED:
if status and status != ExportStatus.DISK_ENCRYPTED:
raise ExportError(status)

def _run_disk_export(self, archive_dir: str, filepaths: List[str], passphrase: str) -> None:
Expand Down
32 changes: 31 additions & 1 deletion tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def test__export_archive_with_evil_command(mocker):
Ensure shell command is shell-escaped.
"""
export = Export()
check_output = mocker.patch("subprocess.check_output", return_value=b"")
check_output = mocker.patch("subprocess.check_output", return_value=b"ERROR_FILE_NOT_FOUND")

with pytest.raises(ExportError, match="UNEXPECTED_RETURN_STATUS"):
export._export_archive("somefile; rm -rf ~")
Expand All @@ -426,3 +426,33 @@ def test__export_archive_with_evil_command(mocker):
],
stderr=-2,
)


def test__export_archive_success_on_empty_return_value(mocker):
"""
Ensure an error is not raised when qrexec call returns empty string,
(success state for `disk`, `print`, `printer-test`).

When export behaviour changes so that all success states return a status
string, this test will no longer pass and should be rewritten.
"""
export = Export()
check_output = mocker.patch("subprocess.check_output", return_value=b"")
gonzalo-bulnes marked this conversation as resolved.
Show resolved Hide resolved

result = export._export_archive("somefile.sd-export")

check_output.assert_called_once_with(
[
"qrexec-client-vm",
"--",
"sd-devices",
"qubes.OpenInVM",
"/usr/lib/qubes/qopen-in-vm",
"--view-only",
"--",
"somefile.sd-export",
],
stderr=-2,
)

assert result is None