From b41c82c8e7252e883ffb1bbefa135536a683c0fb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20De=20Keersmaeker?=
 <francois.dekeersmaeker@uclouvain.be>
Date: Thu, 26 Dec 2024 17:53:24 +0100
Subject: [PATCH] HTTP: remove Path only if Request

---
 pcap_anonymize/app_layer/http.py | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/pcap_anonymize/app_layer/http.py b/pcap_anonymize/app_layer/http.py
index cb4babc..6839d77 100644
--- a/pcap_anonymize/app_layer/http.py
+++ b/pcap_anonymize/app_layer/http.py
@@ -70,17 +70,18 @@ def anonymize_http(http: HTTP) -> None:
         http (scapy.HTTP): HTTP layer to anonymize
     """
     # Remove request parameters
-    try:
-        path = http.getfieldval(HttpFields.PATH.value).decode(ENCODING)
-        http.setfieldval(HttpFields.PATH.value, path.split("?")[0].encode(ENCODING))
-    except AttributeError:
-        # HTTP packet does not contain the `Path` field
-        logger.warning(f"Field {HttpFields.PATH.value} not found in HTTP layer {http.summary()}")
-        pass
-    except UnicodeDecodeError:
-        # `Path` field is not encoded in UTF-8
-        logger.warning(f"Field {HttpFields.PATH.value} not UTF-8 encoded in HTTP layer {http.summary()}")
-        pass
+    if http.haslayer(HTTPRequest):
+        try:
+            path = http.getfieldval(HttpFields.PATH.value).decode(ENCODING)
+            http.setfieldval(HttpFields.PATH.value, path.split("?")[0].encode(ENCODING))
+        except AttributeError:
+            # HTTP packet does not contain the `Path` field
+            logger.warning(f"Field {HttpFields.PATH.value} not found in HTTP layer {http.summary()}")
+            pass
+        except UnicodeDecodeError:
+            # `Path` field is not encoded in UTF-8
+            logger.warning(f"Field {HttpFields.PATH.value} not UTF-8 encoded in HTTP layer {http.summary()}")
+            pass
 
     # Remove all fields other than Method and Path
     for field in http.fields.copy():
-- 
GitLab