dcavalca / rpms / libdnf

Forked from rpms/libdnf 2 years ago
Clone

Blame SOURCES/0001-user-agent-Drop-the-whitelist.patch

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