Error out correctly when an invalid SSH agent socket provider is specified
Marco Ricci

Marco Ricci commited on 2026-02-08 13:40:06
Zeige 2 geänderte Dateien mit 44 Einfügungen und 0 Löschungen.


Issue the correct error message when an SSH agent socket provider is
specified via command-line options or via user configuration, but the
socket provider does not appear in the registry.

Previously, due to a technicality, this would be treated the same as the
`SSH_AUTH_SOCK` environment variable missing, and would issue the same
error message.  This is, of course, blatantly false, and has now been
corrected.
... ...
@@ -711,6 +711,34 @@ def prompt_for_selection(
711 711
     return 0
712 712
 
713 713
 
714
+def handle_nosuchprovidererror(
715
+    error_callback: Callable[..., NoReturn],
716
+    warning_callback: Callable[..., None],
717
+) -> Callable[[BaseExceptionGroup], NoReturn]:
718
+    """Generate a handler for socketprovider.NoSuchProviderException in try-except*.
719
+
720
+    Returns a function emitting a standard user-facing message.
721
+
722
+    """  # noqa: DOC201, E501
723
+    del warning_callback
724
+
725
+    def handle_nosuchprovidererror(excgroup: BaseExceptionGroup) -> NoReturn:
726
+        subset = excgroup.subgroup(socketprovider.NoSuchProviderError)
727
+        provider_name = (
728
+            ascii(subset.exceptions[0].args[0])
729
+            if subset is not None
730
+            else "(null)"
731
+        )
732
+        error_callback(
733
+            _msg.TranslatedString(
734
+                _msg.ErrMsgTemplate.UNKNOWN_SSH_AGENT_SOCKET_PROVIDER,
735
+                provider=provider_name,
736
+            )
737
+        )
738
+
739
+    return handle_nosuchprovidererror
740
+
741
+
714 742
 def handle_keyerror(
715 743
     error_callback: Callable[..., NoReturn],
716 744
     warning_callback: Callable[..., None],
... ...
@@ -886,6 +914,9 @@ def select_ssh_key(
886 914
         raise AssertionError()
887 915
 
888 916
     with exceptiongroup.catch({
917
+        socketprovider.NoSuchProviderError: handle_nosuchprovidererror(
918
+            error_callback, warning_callback
919
+        ),
889 920
         KeyError: handle_keyerror(error_callback, warning_callback),
890 921
         LookupError: handle_lookuperror,
891 922
         NotImplementedError: handle_notimplementederror(
... ...
@@ -1171,6 +1202,9 @@ def key_to_phrase(
1171 1202
     """
1172 1203
     key = base64.standard_b64decode(key)
1173 1204
     with exceptiongroup.catch({  # noqa: SIM117
1205
+        socketprovider.NoSuchProviderError: handle_nosuchprovidererror(
1206
+            error_callback, warning_callback
1207
+        ),
1174 1208
         KeyError: handle_keyerror(error_callback, warning_callback),
1175 1209
         NotImplementedError: handle_notimplementederror(
1176 1210
             error_callback, warning_callback
... ...
@@ -2361,6 +2361,16 @@ class ErrMsgTemplate(enum.Enum):
2361 2361
         "The requested SSH key is not loaded into the agent.",
2362 2362
     )
2363 2363
     """"""
2364
+    UNKNOWN_SSH_AGENT_SOCKET_PROVIDER = commented(
2365
+        "The provider is already put in ASCII quotes, and sanitized "
2366
+        "so that it is safe to emit to the terminal.",
2367
+    )(
2368
+        "Error message",
2369
+        "The SSH agent socket provider {provider} is not in "
2370
+        "derivepassphrase's provider registry.",
2371
+        flags="python-brace-format",
2372
+    )
2373
+    """"""
2364 2374
     USER_ABORTED_EDIT = commented(
2365 2375
         "The user requested to edit the notes for a service, "
2366 2376
         "but aborted the request mid-editing.",
2367 2377