chantra / rpms / libdnf

Forked from rpms/libdnf 2 years ago
Clone
c43b0c
From 1dffef87fc2f07763f64eeabc1ea891e68d23541 Mon Sep 17 00:00:00 2001
c43b0c
From: Michal Domonkos <mdomonko@redhat.com>
c43b0c
Date: Tue, 26 Nov 2019 13:05:49 +0100
c43b0c
Subject: [PATCH] [user-agent] Drop the whitelist
c43b0c
c43b0c
 - Stop checking os-release(5) data against a hard-coded whitelist and
c43b0c
   just use them as they are, to avoid a maintenance burden in the
c43b0c
   future (see [1] for details)
c43b0c
c43b0c
 - Clean up the getUserAgent() function a bit
c43b0c
c43b0c
Note that, by removing the whitelist, there's a risk of leaking a
c43b0c
"unique" value from the os-release file now, but a rather small one.
c43b0c
c43b0c
[1] https://github.com/rpm-software-management/libdnf/pull/851
c43b0c
---
c43b0c
 libdnf/utils/os-release.cpp | 58 ++++++++++++++++++++--------------------------------------
c43b0c
 libdnf/utils/os-release.hpp |  7 ++-----
c43b0c
 2 files changed, 22 insertions(+), 43 deletions(-)
c43b0c
c43b0c
diff --git a/libdnf/utils/os-release.cpp b/libdnf/utils/os-release.cpp
c43b0c
index 57be110..1d8a95b 100644
c43b0c
--- a/libdnf/utils/os-release.cpp
c43b0c
+++ b/libdnf/utils/os-release.cpp
c43b0c
@@ -36,17 +36,8 @@
c43b0c
 namespace libdnf {
c43b0c
 
c43b0c
 // sorted by precedence (see os-release(5) for details)
c43b0c
-static const std::array<const std::string, 2> paths = {"/etc/os-release", "/usr/lib/os-release"};
c43b0c
-// whitelists used for sanity-checking the os-release data when constructing a
c43b0c
-// User-Agent string (to avoid reporting rare systems or platforms that could
c43b0c
-// be tracked)
c43b0c
-static const std::map<std::string, std::vector<std::string>> distros = {
c43b0c
-    // taken from the {fedora,generic}-release.spec files
c43b0c
-    { "Fedora", { "cinnamon", "cloud", "container", "coreos", "generic", "iot",
c43b0c
-                  "kde", "matecompiz", "server", "silverblue", "snappy", "soas",
c43b0c
-                  "workstation", "xfce" } },
c43b0c
-};
c43b0c
-std::array<const std::string, 1> canons = { "Linux" };
c43b0c
+static const std::array<const std::string, 2>
c43b0c
+paths = {"/etc/os-release", "/usr/lib/os-release"};
c43b0c
 
c43b0c
 std::map<std::string, std::string> getOsReleaseData()
c43b0c
 {
c43b0c
@@ -118,47 +109,38 @@ std::string getUserAgent(const std::map<std::string, std::string> & osReleaseDat
c43b0c
 {
c43b0c
     std::ostringstream oss;
c43b0c
     auto logger(Log::getLogger());
c43b0c
-    std::string msg = "os-release: falling back to basic User-Agent";
c43b0c
 
c43b0c
-    // start with the basic libdnf string
c43b0c
     oss << USER_AGENT;
c43b0c
+    std::string fallback = oss.str();
c43b0c
 
c43b0c
-    // mandatory OS data (bail out if missing or unknown)
c43b0c
     if (!osReleaseData.count("NAME") || !osReleaseData.count("VERSION_ID")) {
c43b0c
-        logger->debug(tfm::format("%s: missing NAME or VERSION_ID", msg));
c43b0c
-        return oss.str();
c43b0c
+        logger->debug(tfm::format(
c43b0c
+            "User-Agent: falling back to '%s': missing NAME or VERSION_ID",
c43b0c
+            fallback
c43b0c
+        ));
c43b0c
+        return fallback;
c43b0c
     }
c43b0c
     std::string name = osReleaseData.at("NAME");
c43b0c
     std::string version = osReleaseData.at("VERSION_ID");
c43b0c
-    if (!distros.count(name)) {
c43b0c
-        logger->debug(tfm::format("%s: distro %s not whitelisted", msg, name));
c43b0c
-        return oss.str();
c43b0c
-    }
c43b0c
+    std::string variant = "generic";
c43b0c
+    if (osReleaseData.count("VARIANT_ID"))
c43b0c
+        variant = osReleaseData.at("VARIANT_ID");
c43b0c
 
c43b0c
-    // mandatory platform data from RPM (bail out if missing or unknown)
c43b0c
     std::string canon = getCanonOs();
c43b0c
     std::string arch = getBaseArch();
c43b0c
-    if (canon.empty() || arch.empty()
c43b0c
-        || std::find(canons.begin(), canons.end(), canon) == canons.end()) {
c43b0c
-        logger->debug(tfm::format("%s: could not detect canonical OS or basearch", msg));
c43b0c
-        return oss.str();
c43b0c
-    }
c43b0c
-
c43b0c
-    // optional OS data (use fallback values if missing or unknown)
c43b0c
-    std::string variant = "generic";
c43b0c
-    auto list = distros.at(name);
c43b0c
-    if (osReleaseData.count("VARIANT_ID")) {
c43b0c
-        std::string value = osReleaseData.at("VARIANT_ID");
c43b0c
-        if (std::find(list.begin(), list.end(), value) != list.end())
c43b0c
-            variant = value;
c43b0c
+    if (canon.empty() || arch.empty()) {
c43b0c
+        logger->debug(tfm::format(
c43b0c
+            "User-Agent: falling back to '%s': could not detect OS or basearch",
c43b0c
+            fallback
c43b0c
+        ));
c43b0c
+        return fallback;
c43b0c
     }
c43b0c
 
c43b0c
-    // good to go!
c43b0c
-    oss << " (" << name << " " << version << "; " << variant << "; "
c43b0c
-        << canon << "." << arch << ")";
c43b0c
+    oss << " (" << name << " " << version << "; " << variant << "; " << canon
c43b0c
+        << "." << arch << ")";
c43b0c
 
c43b0c
     std::string result = oss.str();
c43b0c
-    logger->debug(tfm::format("os-release: User-Agent constructed: %s", result));
c43b0c
+    logger->debug(tfm::format("User-Agent: constructed: '%s'", result));
c43b0c
     return result;
c43b0c
 }
c43b0c
 
c43b0c
diff --git a/libdnf/utils/os-release.hpp b/libdnf/utils/os-release.hpp
c43b0c
index ef4d14f..e7b24a7 100644
c43b0c
--- a/libdnf/utils/os-release.hpp
c43b0c
+++ b/libdnf/utils/os-release.hpp
c43b0c
@@ -50,11 +50,8 @@ getOsReleaseData();
c43b0c
  *   libdnf (NAME VERSION_ID; VARIANT_ID; OS.BASEARCH)
c43b0c
  *
c43b0c
  * where NAME, VERSION_ID and VARIANT_ID are OS identifiers read from the
c43b0c
- * passed os-release data, and OS and BASEARCH (if found) are the canonical OS
c43b0c
- * name and base architecture, respectively, detected using RPM.
c43b0c
- *
c43b0c
- * Note that the OS part (enclosed in parentheses) will only be included for
c43b0c
- * whitelisted values.
c43b0c
+ * passed os-release data, and OS and BASEARCH are the canonical OS name and
c43b0c
+ * base architecture, respectively, detected using RPM.
c43b0c
  *
c43b0c
  * @param  osReleaseData a map containing os-release data (will be loaded from
c43b0c
  *                       disk if not specified)
c43b0c
--
c43b0c
libgit2 0.28.2
c43b0c