From ffd1212170d2666e237dbddd2aa45b0edba8c7a2 Mon Sep 17 00:00:00 2001 From: Dave Lane <42013603+ReenigneArcher@users.noreply.github.com> Date: Thu, 10 Sep 2026 21:57:23 -0400 Subject: [PATCH] fix(api): return the final result of pairing (#5680) --- src/confighttp.cpp | 6 +- src/nvhttp.cpp | 110 ++++++++++++++++++++++++------- src/nvhttp.h | 26 +++++++- tests/unit/test_confighttp.cpp | 22 +++++++ tests/unit/test_http_pairing.cpp | 44 +++++++++++++ 5 files changed, 183 insertions(+), 25 deletions(-) diff --git a/src/confighttp.cpp b/src/confighttp.cpp index 84a55ff98..9bd3cbc22 100644 --- a/src/confighttp.cpp +++ b/src/confighttp.cpp @@ -1775,7 +1775,11 @@ namespace confighttp { } /** - * @brief Send a PIN code to the explicitly selected pairing request. + * @brief Submit a PIN and return whether the selected client completes pairing. + * + * The request remains open for up to the configured `ping_timeout` while + * Moonlight completes the cryptographic handshake. A wrong PIN, protocol + * failure, cancellation, or timeout returns `{"status":false}`. * The body for the post request should be JSON serialized in the following format: * @code{.json} * { diff --git a/src/nvhttp.cpp b/src/nvhttp.cpp index 2ce99ab7e..8e33cad8a 100644 --- a/src/nvhttp.cpp +++ b/src/nvhttp.cpp @@ -552,6 +552,23 @@ namespace nvhttp { return true; } + /** + * @brief Publish the final result of a pairing handshake. + * + * @param sess Pairing session whose waiter should be notified. + * @param success Whether the client completed the authenticated handshake. + */ + void complete_pairing(pair_session_t &sess, const bool success) { + { + std::lock_guard lock {sess.completion->mutex}; + if (sess.completion->result.has_value()) { + return; + } + sess.completion->result = success; + } + sess.completion->condition.notify_all(); + } + /** * @brief Expire stale pairing sessions while the session mutex is held. * @@ -569,6 +586,7 @@ namespace nvhttp { tree.put("root..status_code", 408); tree.put("root..status_message", "Pairing session expired"); write_pairing_response(it->second, tree); + complete_pairing(it->second, false); it = map_id_sess.erase(it); } } @@ -666,13 +684,17 @@ namespace nvhttp { tree.put("root..status_code", 400); tree.put("root..status_message", "Pairing request cancelled by operator"); write_pairing_response(sess_it->second, tree); + complete_pairing(sess_it->second, false); map_id_sess.erase(sess_it); return true; } void remove_session(const pair_session_t &sess) { std::scoped_lock lock {map_id_sess_mutex()}; - map_id_sess.erase(sess.client.uniqueID); + if (const auto sess_it = map_id_sess.find(sess.client.uniqueID); sess_it != map_id_sess.end()) { + complete_pairing(sess_it->second, false); + map_id_sess.erase(sess_it); + } } /** @@ -687,6 +709,7 @@ namespace nvhttp { tree.put("root..status_code", 400); tree.put("root..status_message", status_msg); sess.failed = true; + complete_pairing(sess, false); } /** @@ -849,14 +872,15 @@ namespace nvhttp { // if hash not correct, probably MITM bool same_hash = hash.size() == sess.clienthash.size() && std::equal(hash.begin(), hash.end(), sess.clienthash.begin()); auto verify = crypto::verify256(crypto::x509(client.cert), secret, sign); + bool paired = false; if (same_hash && verify) { // The client is now successfully paired and will be authorized to connect - tree.put("root.paired", add_authorized_client(client.name, std::move(client.cert)).empty() ? 0 : 1); - } else { - tree.put("root.paired", 0); + paired = !add_authorized_client(client.name, std::move(client.cert)).empty(); } + tree.put("root.paired", paired ? 1 : 0); tree.put("root..status_code", 200); + complete_pairing(sess, paired); } template @@ -1085,28 +1109,54 @@ namespace nvhttp { return false; } + std::shared_ptr completion; + auto completion_deadline = std::chrono::steady_clock::time_point::min(); + { + std::scoped_lock lock {map_id_sess_mutex()}; + const auto now = std::chrono::steady_clock::now(); + expire_pair_sessions_unlocked(now); + const auto sess_it = std::ranges::find_if(map_id_sess, [&](const auto &entry) { + return entry.second.last_phase == PAIR_PHASE::NONE && entry.second.async_insert_pin.id == pairing_id; + }); + if (sess_it == map_id_sess.end()) { + return false; + } + + auto &sess = sess_it->second; + pt::ptree tree; + getservercert(sess, tree, pin); + if (!sess.failed) { + sess.client.name = std::move(name); + } + + if (!write_pairing_response(sess, tree) || sess.failed) { + complete_pairing(sess, false); + map_id_sess.erase(sess_it); + return false; + } + + completion = sess.completion; + completion_deadline = std::min(sess.async_insert_pin.expires_at, now + config::stream.ping_timeout); + } + + { + std::unique_lock lock {completion->mutex}; + if (completion->condition.wait_until(lock, completion_deadline, [&completion]() { + return completion->result.has_value(); + })) { + return *completion->result; + } + } + std::scoped_lock lock {map_id_sess_mutex()}; - expire_pair_sessions_unlocked(std::chrono::steady_clock::now()); - const auto sess_it = std::ranges::find_if(map_id_sess, [&](const auto &entry) { - return entry.second.last_phase == PAIR_PHASE::NONE && entry.second.async_insert_pin.id == pairing_id; - }); - if (sess_it == map_id_sess.end()) { - return false; - } - - auto &sess = sess_it->second; - pt::ptree tree; - getservercert(sess, tree, pin); - if (!sess.failed) { - sess.client.name = std::move(name); - } - - const bool response_written = write_pairing_response(sess, tree); - const bool success = response_written && !sess.failed; - if (!response_written || sess.failed) { + if (const auto sess_it = std::ranges::find_if(map_id_sess, [&](const auto &entry) { + return entry.second.async_insert_pin.id == pairing_id && entry.second.completion == completion; + }); + sess_it != map_id_sess.end()) { + complete_pairing(sess_it->second, false); map_id_sess.erase(sess_it); } - return success; + return false; } /** @@ -1792,6 +1842,20 @@ namespace nvhttp { return verify_client_certificate(certificate.get()) == nullptr; } + bool complete_pairing(const std::string_view pairing_id, const bool success) { + std::scoped_lock lock {map_id_sess_mutex()}; + const auto sess_it = std::ranges::find_if(map_id_sess, [&](const auto &entry) { + return entry.second.async_insert_pin.id == pairing_id; + }); + if (sess_it == map_id_sess.end()) { + return false; + } + + nvhttp::complete_pairing(sess_it->second, success); + map_id_sess.erase(sess_it); + return true; + } + void reload_client_state() { load_state(); } diff --git a/src/nvhttp.h b/src/nvhttp.h index cd5ed7d27..fe7e2c670 100644 --- a/src/nvhttp.h +++ b/src/nvhttp.h @@ -7,7 +7,11 @@ // standard includes #include +#include #include +#include +#include +#include #include #include #include @@ -115,6 +119,15 @@ namespace nvhttp { CLIENTPAIRINGSECRET ///< Sunshine is in the client pairing secret phase }; + /** + * @brief Shared result used to return the completed pairing outcome to a PIN submitter. + */ + struct pairing_completion_t { + std::condition_variable condition; ///< Wakes the REST request when pairing finishes. + std::mutex mutex; ///< Protects the completion result. + std::optional result; ///< Final pairing result, or no value while the handshake is pending. + }; + /** * @brief Pairing handshake state exchanged with a Moonlight client. */ @@ -130,6 +143,7 @@ namespace nvhttp { std::string serversecret = {}; ///< Server pairing secret. std::string serverchallenge = {}; ///< Server challenge sent during pairing. + std::shared_ptr completion = std::make_shared(); ///< Result shared with the REST request waiting for the handshake. struct { util::Either< @@ -297,7 +311,8 @@ namespace nvhttp { * @param pairing_id Unguessable identifier of the pairing request to approve. * @param pin The user supplied pin. * @param name The user supplied name. - * @return `true` if the pin is correct, `false` otherwise. + * @return `true` if Moonlight proves the PIN by completing the handshake, `false` otherwise. + * @note The handshake wait uses the configured `ping_timeout` and never exceeds the pairing session deadline. * @examples * bool pin_status = nvhttp::pin("0123456789abcdef0123456789abcdef", "1234", "laptop"); * @examples_end @@ -386,6 +401,15 @@ namespace nvhttp { */ bool authorize_client_certificate(std::string_view cert); + /** + * @brief Complete and remove a pairing session without running the protocol phases. + * + * @param pairing_id Operator approval identifier of the test session. + * @param success Pairing result delivered to the waiting PIN submitter. + * @return `true` when the session was found and completed. + */ + bool complete_pairing(std::string_view pairing_id, bool success); + /** * @brief Reload paired-client authorization state from the configured state file. */ diff --git a/tests/unit/test_confighttp.cpp b/tests/unit/test_confighttp.cpp index 14e37ddba..fa5f2e279 100644 --- a/tests/unit/test_confighttp.cpp +++ b/tests/unit/test_confighttp.cpp @@ -652,6 +652,28 @@ TEST_F(ConfigHttpTest, PairingRestApiCancelsOnlyExplicitRequest) { EXPECT_TRUE(nvhttp::get_pending_pairings().empty()); } +TEST_F(ConfigHttpTest, PairingRestApiReportsIncompleteHandshakeAsFailure) { + const std::string pairing_id = insert_pending_pairing(); + SimpleWeb::CaseInsensitiveMultimap headers; + headers.emplace("Authorization", create_auth_header("testuser", "testpass")); + headers.emplace("Content-Type", "application/json"); + + const auto response = client->request( + "POST", + "/pairing-test", + nlohmann::json { + {"pairing_id", pairing_id}, + {"pin", "9875"}, + {"name", "Client"}, + } + .dump(), + headers + ); + ASSERT_EQ(response->status_code, "200 OK"); + EXPECT_FALSE(nlohmann::json::parse(response->content.string()).at("status").get()); + EXPECT_TRUE(nvhttp::get_pending_pairings().empty()); +} + // Test: confighttp::authenticate() rejects requests without auth header TEST_F(ConfigHttpTest, AuthenticateRejectsNoAuth) { const auto response = client->request("GET", "/auth-test"); diff --git a/tests/unit/test_http_pairing.cpp b/tests/unit/test_http_pairing.cpp index 4dab340db..708220d3a 100644 --- a/tests/unit/test_http_pairing.cpp +++ b/tests/unit/test_http_pairing.cpp @@ -151,6 +151,8 @@ TEST_P(PairingTest, Run) { auto input_client_cert = input.session->client.cert; // Will be moved clientpairingsecret(*input.session, tree, input.client_pairing_secret); ASSERT_EQ(tree.get("root.paired") == 1, expected.phase_4_success); + ASSERT_TRUE(input.session->completion->result.has_value()); + EXPECT_EQ(*input.session->completion->result, expected.phase_4_success); if (expected.phase_4_success) { ASSERT_TRUE(nvhttp::test_support::authorize_client_certificate(input_client_cert)); @@ -416,6 +418,7 @@ namespace { BaseTest::SetUp(); expire_pair_sessions(std::chrono::steady_clock::time_point::max()); original_pin_stdin_ = config::sunshine.flags[config::flag::PIN_STDIN]; + original_ping_timeout_ = config::stream.ping_timeout; config::sunshine.flags[config::flag::PIN_STDIN] = false; server_ = std::make_unique>(); @@ -453,6 +456,7 @@ namespace { client_.reset(); server_.reset(); config::sunshine.flags[config::flag::PIN_STDIN] = original_pin_stdin_; + config::stream.ping_timeout = original_ping_timeout_; BaseTest::TearDown(); } @@ -503,6 +507,7 @@ namespace { std::jthread server_thread_; ///< Thread running the local server event loop. std::atomic port_ {0}; ///< Ephemeral port assigned to the local server. bool original_pin_stdin_; ///< Console-PIN flag restored after each test. + std::chrono::milliseconds original_ping_timeout_; ///< Configured client timeout restored after each test. }; } // namespace @@ -529,6 +534,45 @@ TEST_F(PairingHttpHandlerTest, WebApprovalRequestRemainsPendingUntilCancelled) { EXPECT_NE(response.get().find("cancelled by operator"), std::string::npos); } +TEST_F(PairingHttpHandlerTest, PinReturnsCompletedHandshakeResult) { + for (const bool expected_result : {false, true}) { + const auto unique_id = expected_result ? "successful-result"sv : "failed-result"sv; + std::packaged_task request_task {[this, unique_id]() { + return request(server_certificate_target(unique_id)); + }}; + auto client_response = request_task.get_future(); + std::jthread request_thread {std::move(request_task)}; + + const auto pairing_id = wait_for_pending_pairing(); + ASSERT_FALSE(pairing_id.empty()); + config::stream.ping_timeout = std::chrono::seconds {2}; + std::packaged_task pin_task {[&pairing_id]() { + return pin(pairing_id, "5338", "Test client"); + }}; + auto pin_result = pin_task.get_future(); + std::jthread pin_thread {std::move(pin_task)}; + + EXPECT_NE(client_response.get().find("status_code=\"200\""), std::string::npos); + EXPECT_TRUE(nvhttp::test_support::complete_pairing(pairing_id, expected_result)); + EXPECT_EQ(pin_result.get(), expected_result); + } +} + +TEST_F(PairingHttpHandlerTest, PinReturnsFalseWhenClientDoesNotCompleteHandshake) { + std::packaged_task request_task {[this]() { + return request(server_certificate_target("wrong-pin")); + }}; + auto client_response = request_task.get_future(); + std::jthread request_thread {std::move(request_task)}; + + const auto pairing_id = wait_for_pending_pairing(); + ASSERT_FALSE(pairing_id.empty()); + config::stream.ping_timeout = std::chrono::milliseconds {50}; + EXPECT_FALSE(pin(pairing_id, "0000", "Test client")); + EXPECT_NE(client_response.get().find("status_code=\"200\""), std::string::npos); + EXPECT_TRUE(get_pending_pairings().empty()); +} + TEST_F(PairingHttpHandlerTest, DuplicateAndCapacityErrorsReturnImmediately) { std::string pairing_id; ASSERT_EQ(insert_pair_session(pending_session("duplicate", "Original", "192.0.2.10"), pairing_id), pair_session_insert_e::ADDED);