You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2022/07/16 23:02:44 UTC

[skywalking-nodejs] branch master updated: Fix sw header is not validated (#90)

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

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-nodejs.git


The following commit(s) were added to refs/heads/master by this push:
     new 75afb3e  Fix sw header is not validated (#90)
75afb3e is described below

commit 75afb3ec7a40263ce9317d9e535ce46b296b546a
Author: kezhenxu94 <ke...@apache.org>
AuthorDate: Sun Jul 17 07:02:39 2022 +0800

    Fix sw header is not validated (#90)
---
 package.json                        |  2 +-
 src/trace/context/ContextCarrier.ts | 47 ++++++++++++++++++++++---------------
 2 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/package.json b/package.json
index 783efa0..add81fb 100644
--- a/package.json
+++ b/package.json
@@ -11,7 +11,7 @@
     "generate-source": "scripts/protoc.sh",
     "build": "npm run clean && npm run prepare && tsc --build src && OUT_DIR=lib/proto/ scripts/protoc.sh",
     "lint": "eslint src/**/*.ts",
-    "lint:fix": "eslint --fix src/**/*.ts",
+    "lint:fix": "prettier --write \"src/**/*.ts\"",
     "lint-staged": "lint-staged",
     "test": "DEBUG=testcontainers* jest",
     "format": "prettier --write \"src/**/*.ts\"",
diff --git a/src/trace/context/ContextCarrier.ts b/src/trace/context/ContextCarrier.ts
index abe06a1..f5ad3b8 100644
--- a/src/trace/context/ContextCarrier.ts
+++ b/src/trace/context/ContextCarrier.ts
@@ -44,20 +44,28 @@ export class ContextCarrier extends CarrierItem {
   };
 
   get value(): string {
-    return [
-      '1',
-      this.encode(this.traceId?.toString() || ''),
-      this.encode(this.segmentId?.toString() || ''),
-      this.spanId?.toString(),
-      this.encode(this.service || ''),
-      this.encode(this.serviceInstance || ''),
-      this.encode(this.endpoint || ''),
-      this.encode(this.clientAddress || ''),
-    ].join('-');
+    return this.isValid()
+      ? [
+          '1',
+          this.encode(this.traceId?.toString() || ''),
+          this.encode(this.segmentId?.toString() || ''),
+          this.spanId?.toString(),
+          this.encode(this.service || ''),
+          this.encode(this.serviceInstance || ''),
+          this.encode(this.endpoint || ''),
+          this.encode(this.clientAddress || ''),
+        ].join('-')
+      : '';
   }
 
   set value(val) {
+    if (!val) {
+      return;
+    }
     const parts = val.split('-');
+    if (parts.length != 8) {
+      return;
+    }
     this.traceId = new ID(this.decode(parts[1]));
     this.segmentId = new ID(this.decode(parts[2]));
     this.spanId = Number.parseInt(parts[3], 10);
@@ -70,22 +78,23 @@ export class ContextCarrier extends CarrierItem {
   isValid(): boolean {
     return Boolean(
       this.traceId?.rawId &&
-      this.segmentId?.rawId &&
-      this.spanId !== undefined &&
-      !isNaN(this.spanId) &&
-      this.service &&
-      this.endpoint &&
-      this.clientAddress !== undefined
+        this.segmentId?.rawId &&
+        this.spanId !== undefined &&
+        !isNaN(this.spanId) &&
+        this.service &&
+        this.endpoint &&
+        this.clientAddress !== undefined,
     );
   }
 
   public static from(map: { [key: string]: string }): ContextCarrier | undefined {
-    if (!map.hasOwnProperty('sw8'))
-      return;
+    if (!Object.prototype.hasOwnProperty.call(map, 'sw8')) return;
 
     const carrier = new ContextCarrier();
 
-    carrier.items.filter((item) => map.hasOwnProperty(item.key)).forEach((item) => (item.value = map[item.key]));
+    carrier.items
+      .filter((item) => Object.prototype.hasOwnProperty.call(map, item.key))
+      .forEach((item) => (item.value = map[item.key]));
 
     return carrier;
   }