Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion include/legacy/legacy_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions include/runtime/auth_runtime_composition.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion include/runtime/roblox_cookie_jar.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ class RobloxCookieJar {
std::vector<Cookie> 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 = {});

}
}
2 changes: 2 additions & 0 deletions include/services/browser_tracker_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 34 additions & 4 deletions src/runtime/roblox_cookie_jar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
15 changes: 15 additions & 0 deletions src/services/browser_tracker_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <thread>
#include <vector>

#include <ctime>

namespace mocktail {
namespace services {
namespace {
Expand All @@ -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());
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/browser_tracker_service_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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="),
Expand Down Expand Up @@ -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");
Expand Down
27 changes: 27 additions & 0 deletions tests/roblox_cookie_jar_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

}
}
}