From a7a719261e868ad6813fc5f7536d257e89175e7c Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sat, 20 Jul 2024 10:27:04 +0200 Subject: [PATCH] Minimize data sent in the default user agent (#14851) --- src/httpfetch.cpp | 9 ++++++++- src/porting.cpp | 32 ++++++++++++++++++++++++++++---- src/porting.h | 2 +- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/httpfetch.cpp b/src/httpfetch.cpp index 67b60df45..771e539e5 100644 --- a/src/httpfetch.cpp +++ b/src/httpfetch.cpp @@ -40,10 +40,17 @@ static std::unordered_map> g_httpfetch_results; static PcgRandom g_callerid_randomness; +static std::string default_user_agent() +{ + std::string ret(PROJECT_NAME_C "/"); + ret.append(g_version_string).append(" (").append(porting::get_sysinfo()).append(")"); + return ret; +} + HTTPFetchRequest::HTTPFetchRequest() : timeout(g_settings->getS32("curl_timeout")), connect_timeout(10 * 1000), - useragent(std::string(PROJECT_NAME_C "/") + g_version_hash + " (" + porting::get_sysinfo() + ")") + useragent(default_user_agent()) { timeout = std::max(timeout, MIN_HTTPFETCH_TIMEOUT_INTERACTIVE); } diff --git a/src/porting.cpp b/src/porting.cpp index a7601b101..da972926a 100644 --- a/src/porting.cpp +++ b/src/porting.cpp @@ -50,6 +50,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #endif #if defined(__ANDROID__) #include "porting_android.h" + #include #endif #if defined(__APPLE__) #include @@ -205,7 +206,10 @@ bool detectMSVCBuildDir(const std::string &path) return (!removeStringEnd(path, ends).empty()); } -std::string get_sysinfo() +// Note that the system info is sent in every HTTP request, so keep it reasonably +// privacy-conserving while ideally still being meaningful. + +static std::string detectSystemInfo() { #ifdef _WIN32 std::ostringstream oss; @@ -251,14 +255,34 @@ std::string get_sysinfo() delete[] filePath; return oss.str(); -#else +#elif defined(__ANDROID__) + std::ostringstream oss; struct utsname osinfo; uname(&osinfo); - return std::string(osinfo.sysname) + "/" - + osinfo.release + " " + osinfo.machine; + int api = android_get_device_api_level(); + + oss << "Android/" << api << " " << osinfo.machine; + return oss.str(); +#else /* POSIX */ + struct utsname osinfo; + uname(&osinfo); + + std::string_view release(osinfo.release); + // cut off anything but the primary version number + release = release.substr(0, release.find_first_not_of("0123456789.")); + + std::string ret = osinfo.sysname; + ret.append("/").append(release).append(" ").append(osinfo.machine); + return ret; #endif } +const std::string &get_sysinfo() +{ + static std::string ret = detectSystemInfo(); + return ret; +} + bool getCurrentWorkingDir(char *buf, size_t len) { diff --git a/src/porting.h b/src/porting.h index b5e23c1f9..b7a71bce2 100644 --- a/src/porting.h +++ b/src/porting.h @@ -138,7 +138,7 @@ void initializePaths(); Return system information e.g. "Linux/3.12.7 x86_64" */ -std::string get_sysinfo(); +const std::string &get_sysinfo(); // Monotonic timer