Skip to contentSkip to conversation

cve: Fix escaping of CPE

PATCH New Benjamin Robin 1 participant 5 messages Apr 10

lore
Share & tools
lore
#0 lore raw

cover cve: Fix escaping of CPE

This series tries to fix the regression introduced by commit 9dd9c0038907 ("cve_check: Escape special characters in CPE 2.3 strings")

CVE_PRODUCT was already escaped. Now it no longer needs to be escaped, otherwise double escaping is realized.

Also add a patch in this series that try to handle the transition between the previous behavior and the new one.

---
Benjamin Robin (4):
cve_check: Improve escaping of special characters in CPE 2.3
cve_check: do not break old CVE_PRODUCT with escaped + gtk+: Remove escaping of the plus sign in CVE_PRODUCT
webkitgtk: Remove escaping of the plus sign in CVE_PRODUCT

 meta/lib/oe/cve_check.py                     | 29 ++++++++++++++--------------
 meta/recipes-gnome/gtk+/gtk+3_3.24.51.bb     |  2 +-
 meta/recipes-sato/webkit/webkitgtk_2.50.6.bb |  2 +-
 3 files changed, 16 insertions(+), 17 deletions(-)

---
base-commit: 853baa702e3275fc8b4dc492a93d9a00c8cf5c0f
change-id: 20260410-fix-cpe-escaping-5bc9a67a8d13

Best regards,

Signature · 1 line
Benjamin Robin <[email protected]>
#1 lore raw

PATCH 1/4 cve_check: Improve escaping of special characters in CPE 2.3

According to the NISTIR 7695 specification [1], multiple characters require escaping when using formatted strings (e.g., cpe:2.3:...), which use backslash escaping. In "Figure 6-3. ABNF for Formatted String Binding"", the characters that need escaping are referenced by "escape", "special", and "punc". More characters must be escaped than just \, ?, *, :, and +.

Additionally, use maketrans() with translate(), which is more efficient than a simple replace().

[1] https://nvlpubs.nist.gov/nistpubs/legacy/ir/nistir7695.pdf

---
 meta/lib/oe/cve_check.py | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)
meta/lib/oe/cve_check.py +12 -16
index 65557435149a..22b5062c977c 100644
@@ -205,33 +205,29 @@ def get_patched_cves(d):
     return patched_cves
 
 
+_CPE23_ENCODE_TRANS_TABLE = str.maketrans(
+    {c: f"\\{c}" for c in [
+        "\\", "!", '"', "#", "$", "%", "&", "'", "(", ")", "+", ",", "/", ":", ";",
+        "<", "=", ">", "@", "[", "]", "^", "`", "{", "|", "}", "~", "?", "*"
+    ]}
+)
+
+
 def cpe_escape(value):
-    r"""
+    """
     Escape special characters for CPE 2.3 formatted string binding.
 
     CPE 2.3 formatted string binding (cpe:2.3:...) uses backslash escaping
     for special meta-characters, NOT percent-encoding. Percent-encoding is
     only used in the URI binding (cpe:/...).
 
-    According to NISTIR 7695, these characters need escaping:
-    - Backslash (\) -> \\
-    - Question mark (?) -> \?
-    - Asterisk (*) -> \*
-    - Colon (:) -> \:
-    - Plus (+) -> \+ (required by some SBOM validators)
+    According to NISTIR 7695, various characters referenced in the "Figure 6-3.
+    ABNF for Formatted String Binding" need escaping: escape, special and punc.
     """
     if not value:
         return value
 
-    # Escape special meta-characters for CPE 2.3 formatted string binding
-    # Order matters: escape backslash first to avoid double-escaping
-    result = value.replace('\\', '\\\\')
-    result = result.replace('?', '\\?')
-    result = result.replace('*', '\\*')
-    result = result.replace(':', '\\:')
-    result = result.replace('+', '\\+')
-
-    return result
+    return value.translate(_CPE23_ENCODE_TRANS_TABLE)
 
 
 def get_cpe_ids(cve_product, version):
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/4 cve_check: do not break old CVE_PRODUCT with escaped +

In reply to Benjamin Robin

For now, until all layer are fixed, replace already escaped plus (+) with a simple + before doing the escaping.

---
 meta/lib/oe/cve_check.py | 3 +++
 1 file changed, 3 insertions(+)
meta/lib/oe/cve_check.py +3 -0
index 22b5062c977c..e6104a279d5e 100644
@@ -227,6 +227,9 @@ def cpe_escape(value):
     if not value:
         return value
 
+    # Do not break compatibility
+    value = value.replace("\\+", "+")
+
     return value.translate(_CPE23_ENCODE_TRANS_TABLE)
 
 
Signature · 1 line
2.53.0
#3 lore raw

PATCH 3/4 gtk+: Remove escaping of the plus sign in `CVE_PRODUCT`

In reply to Benjamin Robin

The + character is now properly escaped by cve_check.cpe_escape()

---
 meta/recipes-gnome/gtk+/gtk+3_3.24.51.bb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
meta/recipes-gnome/gtk+/gtk+3_3.24.51.bb +1 -1
index 35f99b2de0c1..51a293c1d8ee 100644
@@ -9,7 +9,7 @@ LIC_FILES_CHKSUM = "file://COPYING;md5=5f30f0716dfdd0d91eb439ebec522ec2 \
                     file://gdk/gdk.h;endline=25;md5=c920ce39dc88c6f06d3e7c50e08086f2 \
                     file://tests/testgtk.c;endline=25;md5=cb732daee1d82af7a2bf953cf3cf26f1"
 
-CVE_PRODUCT = "gnome:gtk gtk:gtk\+"
+CVE_PRODUCT = "gnome:gtk gtk:gtk+"
 
 DEPENDS = "glib-2.0 cairo pango atk jpeg libpng gdk-pixbuf gdk-pixbuf-native"
 
Signature · 1 line
2.53.0
#4 lore raw

PATCH 4/4 webkitgtk: Remove escaping of the plus sign in `CVE_PRODUCT`

In reply to Benjamin Robin

The + character is now properly escaped by cve_check.cpe_escape()

---
 meta/recipes-sato/webkit/webkitgtk_2.50.6.bb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
meta/recipes-sato/webkit/webkitgtk_2.50.6.bb +1 -1
index 941cbe930416..5d2c970b29e3 100644
@@ -25,7 +25,7 @@ inherit cmake pkgconfig gobject-introspection perlnative features_check upstream
 ANY_OF_DISTRO_FEATURES = "${GTK3DISTROFEATURES}"
 REQUIRED_DISTRO_FEATURES = "opengl"
 
-CVE_PRODUCT = "webkitgtk webkitgtk\+"
+CVE_PRODUCT = "webkitgtk webkitgtk+"
 
 DEPENDS += " \
           ruby-native \
Signature · 1 line
2.53.0