diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b75dc76f..9f448a83e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ Release notes on GitHub are taken from the matching section here. ### Fixed +- Blocking and unblocking players works when Nightcap has no saved `RBXEventTrackerV2` cookie. Roblox refused + those requests with "BrowserTrackerID is missing or invalid"; Nightcap now sends its browser tracker. - On FreeBSD, Roblox downloads no longer fail with "error adding trust anchors" in a Fedora userspace. - On FreeBSD, sound plays through `/dev/dsp` without an `asound.conf`, once `alsa-plugins-oss` is installed in the Linux userspace. An existing `~/.asoundrc` or `/etc/asound.conf` is still used. diff --git a/include/legacy/legacy_runtime.h b/include/legacy/legacy_runtime.h index 5097e59ec..fd376a6c2 100644 --- a/include/legacy/legacy_runtime.h +++ b/include/legacy/legacy_runtime.h @@ -27,7 +27,8 @@ class RuntimeDependencies final { account_identity_(std::move(composition.account_identity)), roblox_credential_(std::move(composition.credential)), startup_cookies_(runtime::CombineRobloxStartupCookies( - roblox_credential_.view(), composition.saved_cookies.view())), + roblox_credential_.view(), composition.saved_cookies.view(), + composition.browser_tracker)), clear_persisted_web_view_cookie_( composition.rejected_credential_retired || composition.clear_web_view_cookie), diff --git a/include/runtime/auth_runtime_composition.h b/include/runtime/auth_runtime_composition.h index b161161d1..c65ea281f 100644 --- a/include/runtime/auth_runtime_composition.h +++ b/include/runtime/auth_runtime_composition.h @@ -83,6 +83,9 @@ struct AuthRuntimeComposition { SecureRobloxCredential credential; // Other roblox.com cookies saved from earlier runs, as a Cookie header. SecureRobloxCredential saved_cookies; + // RBXEventTrackerV2 from the BrowserTracker bootstrap, sent when neither + // the credential nor the saved cookies carry one. + std::string browser_tracker; long http_status = 0; bool rejected_credential_retired = false; bool clear_web_view_cookie = false; diff --git a/include/runtime/roblox_cookie_jar.h b/include/runtime/roblox_cookie_jar.h index 0fb411caa..c3bfd640e 100644 --- a/include/runtime/roblox_cookie_jar.h +++ b/include/runtime/roblox_cookie_jar.h @@ -50,8 +50,11 @@ class RobloxCookieJar { std::vector cookies_; }; +// Roblox's servers read the BrowserTrackerID from RBXEventTrackerV2, so +// `browser_tracker` (that cookie's value) is added when neither header has one. std::string CombineRobloxStartupCookies(std::string_view credential, - std::string_view jar_header); + std::string_view jar_header, + std::string_view browser_tracker = {}); } } diff --git a/include/services/browser_tracker_service.h b/include/services/browser_tracker_service.h index f8bf4fd31..7bca8253c 100644 --- a/include/services/browser_tracker_service.h +++ b/include/services/browser_tracker_service.h @@ -14,6 +14,8 @@ enum class BrowserTrackerStatus { kExisting, kCreated }; struct BrowserTrackerResult { BrowserTrackerStatus status = BrowserTrackerStatus::kExisting; std::string browser_tracker_id; + // The RBXEventTrackerV2 value Roblox issued, when the cookie file has one. + std::string tracker_cookie; long http_status = 0; std::string error; diff --git a/src/main.cc b/src/main.cc index 3cb8de1f3..58cb5e6a6 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1409,6 +1409,7 @@ int main(int argc, char* argv[]) { PromptFirstLaunchSignIn(environment, paths, auth_service, http_client, &composition); } + composition.browser_tracker = browser_tracker_result.tracker_cookie; if (!composition) { std::cerr << "[FATAL] Typed Roblox authentication preflight failed: " << composition.error; diff --git a/src/runtime/roblox_cookie_jar.cc b/src/runtime/roblox_cookie_jar.cc index bd82d33b7..ffc27b714 100644 --- a/src/runtime/roblox_cookie_jar.cc +++ b/src/runtime/roblox_cookie_jar.cc @@ -489,13 +489,43 @@ std::string RobloxCookieJar::StartupHeader(std::int64_t now) const { return header; } +namespace { + +bool HeaderHasCookie(std::string_view header, std::string_view name) { + std::size_t begin = 0; + while (begin < header.size()) { + while (begin < header.size() && (header[begin] == ';' || header[begin] == ' ')) + ++begin; + const std::size_t end = std::min(header.find(';', begin), header.size()); + const std::string_view segment = header.substr(begin, end - begin); + if (segment.size() > name.size() && segment.substr(0, name.size()) == name && + segment[name.size()] == '=') { + return true; + } + begin = end; + } + return false; +} + +void AppendCookies(std::string* combined, std::string_view cookies) { + if (!combined->empty() && !cookies.empty()) { + *combined += "; "; + } + *combined += cookies; +} + +} // namespace + std::string CombineRobloxStartupCookies(std::string_view credential, - std::string_view jar_header) { + std::string_view jar_header, + std::string_view browser_tracker) { + constexpr std::string_view kTracker = "RBXEventTrackerV2"; std::string combined(credential); - if (!combined.empty() && !jar_header.empty()) { - combined += "; "; + AppendCookies(&combined, jar_header); + if (!browser_tracker.empty() && !HeaderHasCookie(combined, kTracker)) { + AppendCookies(&combined, std::string(kTracker) + "=" + + std::string(browser_tracker)); } - combined += jar_header; return combined; } diff --git a/src/services/browser_tracker_service.cc b/src/services/browser_tracker_service.cc index 311322da6..17b411164 100644 --- a/src/services/browser_tracker_service.cc +++ b/src/services/browser_tracker_service.cc @@ -17,6 +17,8 @@ #include #include +#include + namespace mocktail { namespace services { namespace { @@ -37,6 +39,16 @@ bool IsValidId(const std::string& value) { conversion.ptr == value.data() + value.size() && parsed != 0; } +// The RBXEventTrackerV2 value Roblox issues, rebuilt for a stored ID. +std::string TrackerCookieFor(const std::string& id) { + const std::time_t now = std::time(nullptr); + std::tm utc = {}; + gmtime_r(&now, &utc); + char date[32]; + std::strftime(date, sizeof(date), "%m/%d/%Y %H:%M:%S", &utc); + return std::string("CreateDate=") + date + "&rbxuid=&browserid=" + id; +} + std::string SuggestedId() { std::random_device entropy; std::mt19937_64 random(entropy()); @@ -605,6 +617,7 @@ BrowserTrackerResult BrowserTrackerService::EnsureInitialized( } if (!authoritative_id.empty()) { result.browser_tracker_id = authoritative_id; + result.tracker_cookie = tracker; if (stored_id == authoritative_id) { close(lock); return result; @@ -617,6 +630,7 @@ BrowserTrackerResult BrowserTrackerService::EnsureInitialized( } if (!stored_id.empty()) { result.browser_tracker_id = stored_id; + result.tracker_cookie = TrackerCookieFor(stored_id); close(lock); return result; } @@ -684,6 +698,7 @@ BrowserTrackerResult BrowserTrackerService::EnsureInitialized( AtomicWrite(app_storage_file, storage, &result.error)) { result.status = BrowserTrackerStatus::kCreated; result.browser_tracker_id = std::move(authoritative_id); + result.tracker_cookie = tracker; } } } diff --git a/tests/browser_tracker_service_test.cc b/tests/browser_tracker_service_test.cc index 1fd9f1b19..5587b8970 100644 --- a/tests/browser_tracker_service_test.cc +++ b/tests/browser_tracker_service_test.cc @@ -80,6 +80,7 @@ TEST_F(BrowserTrackerServiceTest, ReusesValidStoredIdentityWithoutNetwork) { ASSERT_TRUE(result) << result.error; EXPECT_EQ(result.status, BrowserTrackerStatus::kExisting); EXPECT_EQ(result.browser_tracker_id, "123456"); + EXPECT_EQ(result.tracker_cookie, "CreateDate=1&rbxid=2&browserid=123456"); EXPECT_EQ(http.post_count, 0); } @@ -128,6 +129,8 @@ TEST_F(BrowserTrackerServiceTest, InitializesAndAtomicallyPreservesStorage) { ASSERT_TRUE(result) << result.error; EXPECT_EQ(result.status, BrowserTrackerStatus::kCreated); EXPECT_EQ(result.browser_tracker_id, "987654321"); + EXPECT_EQ(result.tracker_cookie, + "CreateDate=1&rbxid=2&browserid=987654321"); EXPECT_EQ(http.post_count, 1); EXPECT_EQ(http.last_body, "{}"); EXPECT_NE(http.last_request.url.find("suggestedBrowserTrackerId="), @@ -177,6 +180,9 @@ TEST_F(BrowserTrackerServiceTest, ASSERT_TRUE(result) << result.error; EXPECT_EQ(result.status, BrowserTrackerStatus::kExisting); EXPECT_EQ(result.browser_tracker_id, "1234567890123456"); + EXPECT_EQ(result.tracker_cookie.rfind("CreateDate=", 0), 0u); + EXPECT_NE(result.tracker_cookie.find("&rbxuid=&browserid=1234567890123456"), + std::string::npos); EXPECT_EQ(http.post_count, 0); EXPECT_EQ(ReadFile(Storage()), storage); EXPECT_EQ(ReadFile(Cookies()), ".ROBLOSECURITY=secret"); diff --git a/tests/roblox_cookie_jar_test.cc b/tests/roblox_cookie_jar_test.cc index ccb7759a0..1b5673422 100644 --- a/tests/roblox_cookie_jar_test.cc +++ b/tests/roblox_cookie_jar_test.cc @@ -143,6 +143,33 @@ TEST(RobloxCookieJarTest, CombinesCredentialAndJar) { EXPECT_EQ(CombineRobloxStartupCookies("", "a=1"), "a=1"); } +TEST(RobloxCookieJarTest, AddsTheBrowserTrackerWhenNeitherHasOne) { + EXPECT_EQ(CombineRobloxStartupCookies(".ROBLOSECURITY=x", "a=1", + "browserid=123"), + ".ROBLOSECURITY=x; a=1; RBXEventTrackerV2=browserid=123"); + EXPECT_EQ(CombineRobloxStartupCookies(".ROBLOSECURITY=x", "", "browserid=123"), + ".ROBLOSECURITY=x; RBXEventTrackerV2=browserid=123"); +} + +TEST(RobloxCookieJarTest, KeepsABrowserTrackerAlreadySent) { + EXPECT_EQ(CombineRobloxStartupCookies( + ".ROBLOSECURITY=x", "RBXEventTrackerV2=browserid=1; a=1", + "browserid=2"), + ".ROBLOSECURITY=x; RBXEventTrackerV2=browserid=1; a=1"); + EXPECT_EQ(CombineRobloxStartupCookies( + ".ROBLOSECURITY=x; RBXEventTrackerV2=browserid=1", "", + "browserid=2"), + ".ROBLOSECURITY=x; RBXEventTrackerV2=browserid=1"); +} + +TEST(RobloxCookieJarTest, MatchesOnlyTheWholeTrackerName) { + EXPECT_EQ(CombineRobloxStartupCookies(".ROBLOSECURITY=x", + "XRBXEventTrackerV2=1", + "browserid=2"), + ".ROBLOSECURITY=x; XRBXEventTrackerV2=1; " + "RBXEventTrackerV2=browserid=2"); +} + } } }