You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2020/07/01 15:25:40 UTC

[skywalking-python] 01/01: [Core][Defect] Validate carrier before using it

This is an automated email from the ASF dual-hosted git repository.

kezhenxu94 pushed a commit to branch defense/carrier-validation
in repository https://gitbox.apache.org/repos/asf/skywalking-python.git

commit 43a1c2ce951e60c1d47557e25f354af90ce7e3bc
Author: kezhenxu94 <ke...@163.com>
AuthorDate: Wed Jul 1 23:25:25 2020 +0800

    [Core][Defect] Validate carrier before using it
---
 skywalking/trace/carrier/__init__.py | 14 +++++++++++++-
 skywalking/trace/context/__init__.py |  2 +-
 skywalking/trace/span/__init__.py    |  2 +-
 3 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/skywalking/trace/carrier/__init__.py b/skywalking/trace/carrier/__init__.py
index 0b95861..0634b83 100644
--- a/skywalking/trace/carrier/__init__.py
+++ b/skywalking/trace/carrier/__init__.py
@@ -74,14 +74,26 @@ class Carrier(CarrierItem):
         if not val:
             return
         parts = val.split('-')
+        if len(parts) != 8:
+            return
         self.trace_id = b64decode(parts[1])
         self.segment_id = b64decode(parts[2])
-        self.span_id = int(parts[3])
+        self.span_id = int(parts[3]) if parts[3].isnumeric() else -1
         self.service = b64decode(parts[4])
         self.service_instance = b64decode(parts[5])
         self.endpoint = b64decode(parts[6])
         self.client_address = b64decode(parts[7])
 
+    @property
+    def is_valid(self):
+        # type: () -> bool
+        return len(self.trace_id) > 0 and \
+               len(self.segment_id) > 0 and \
+               len(self.service) > 0 and \
+               len(self.service_instance) > 0 and \
+               len(self.endpoint) > 0 and \
+               len(self.client_address) > 0
+
     def __iter__(self):
         self.__iter_index = 0
         return self
diff --git a/skywalking/trace/context/__init__.py b/skywalking/trace/context/__init__.py
index dce8d68..190121f 100644
--- a/skywalking/trace/context/__init__.py
+++ b/skywalking/trace/context/__init__.py
@@ -55,7 +55,7 @@ class SpanContext(object):
         )
         span.op = op
 
-        if carrier is not None:
+        if carrier is not None and carrier.is_valid:
             span.extract(carrier=carrier)
 
         return span
diff --git a/skywalking/trace/span/__init__.py b/skywalking/trace/span/__init__.py
index e448424..529edd9 100644
--- a/skywalking/trace/span/__init__.py
+++ b/skywalking/trace/span/__init__.py
@@ -165,7 +165,7 @@ class EntrySpan(StackedSpan):
     def extract(self, carrier: 'Carrier') -> 'Span':
         Span.extract(self, carrier)
 
-        if carrier is None:
+        if carrier is None or not carrier.is_valid:
             return self
 
         ref = SegmentRef(carrier=carrier)