Skip to content
Open
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
21 changes: 18 additions & 3 deletions packages/google-auth/google/oauth2/webauthn_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,24 @@ def _call_plugin(self, cmd: str, input_json: str) -> str:
request = length_bytes_le + input_json.encode()

# Call plugin
process_result = subprocess.run(
[cmd], input=request, capture_output=True, check=True
)
process_result = subprocess.run([cmd], input=request, capture_output=True)

if process_result.returncode != 0:
stdout_bytes = process_result.stdout
if (
len(stdout_bytes) >= 4
and struct.unpack("<I", stdout_bytes[:4])[0] == len(stdout_bytes) - 4
):
stdout_bytes = stdout_bytes[4:]

error_msg = "Command '{}' returned non-zero exit status {}.\nStdout: {}\nStderr: {}".format(
cmd,
process_result.returncode,
stdout_bytes.decode(errors="replace").strip(),
process_result.stderr.decode(errors="replace").strip(),
)

raise exceptions.GoogleAuthError(error_msg)

# Check length of response
response_len_le = process_result.stdout[:4]
Expand Down
21 changes: 21 additions & 0 deletions packages/google-auth/tests/oauth2/test_webauthn_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_malformated_get_assertion_response(os_get_stub, subprocess_run_stub):
response_len = struct.pack("<I", 5)
response = "1234567890"
mock_response = mock.Mock()
mock_response.returncode = 0
mock_response.stdout = response_len + response.encode()
subprocess_run_stub.return_value = mock_response

Expand All @@ -68,6 +69,7 @@ def test_failure_get_assertion(os_get_stub, subprocess_run_stub):

# process returns get response in json
mock_response = mock.Mock()
mock_response.returncode = 0
mock_response.stdout = response_len + response_json
subprocess_run_stub.return_value = mock_response

Expand Down Expand Up @@ -96,6 +98,7 @@ def test_success_get_assertion(os_get_stub, subprocess_run_stub):

# process returns get response in json
mock_response = mock.Mock()
mock_response.returncode = 0
mock_response.stdout = valid_plugin_response_len + valid_plugin_response_json
subprocess_run_stub.return_value = mock_response

Expand Down Expand Up @@ -146,3 +149,21 @@ def test_success_get_assertion(os_get_stub, subprocess_run_stub):
assert (
got_response.response.user_handle == success_response["response"]["userHandle"]
)


def test_plugin_nonzero_exit(os_get_stub, subprocess_run_stub):
response_json = b"detailed_gnubby_internal_crash_log"
response_len = struct.pack("<I", len(response_json))

mock_response = mock.Mock()
mock_response.returncode = 1
mock_response.stdout = response_len + response_json
mock_response.stderr = b"stderr dump"
subprocess_run_stub.return_value = mock_response

test_handler = webauthn_handler.PluginHandler()
with pytest.raises(exceptions.GoogleAuthError) as excinfo:
test_handler.get(GET_ASSERTION_REQUEST)

assert "returned non-zero exit status 1" in str(excinfo.value)
assert "detailed_gnubby_internal_crash_log" in str(excinfo.value)
Loading