Procházet zdrojové kódy

Detect invalid content (#458) (#748)

Lutz Roeder před 4 roky
rodič
revize
b66fe09675
5 změnil soubory, kde provedl 32 přidání a 13 odebrání
  1. 7 4
      source/onnx.js
  2. 3 0
      source/protobuf.js
  3. 7 4
      source/tf.js
  4. 8 5
      source/view.js
  5. 7 0
      test/models.json

+ 7 - 4
source/onnx.js

@@ -24,19 +24,22 @@ onnx.ModelFactory = class {
                     for (const pair of schema) {
                         const key = pair[0];
                         const inner = pair[1];
-                        if (tags[key] === undefined) {
+                        const value = tags[key];
+                        if (value === undefined) {
                             continue;
                         }
-                        else if (inner === false) {
+                        if (inner === false) {
                             return false;
                         }
                         if (Array.isArray(inner)) {
-                            const value = tags[key];
                             if (typeof value !== 'object' || !match(value, inner)) {
                                 return false;
                             }
                         }
-                        else if (inner !== tags[key]) {
+                        else if (inner !== value) {
+                            if (inner === 2 && !Array.isArray(value) && Object(value) === (value) && Object.keys(value).length === 0) {
+                                return true;
+                            }
                             return false;
                         }
                     }

+ 3 - 0
source/protobuf.js

@@ -68,6 +68,9 @@ protobuf.BinaryReader = class {
                 if (length === undefined) {
                     return undefined;
                 }
+                if (length === 0) {
+                    // return 2;
+                }
                 const end = this.position + length;
                 if (end > max) {
                     return undefined;

+ 7 - 4
source/tf.js

@@ -50,19 +50,22 @@ tf.ModelFactory = class {
                         for (const pair of schema) {
                             const key = pair[0];
                             const inner = pair[1];
-                            if (tags[key] === undefined) {
+                            const value = tags[key];
+                            if (value === undefined) {
                                 continue;
                             }
-                            else if (inner === false) {
+                            if (inner === false) {
                                 return false;
                             }
                             if (Array.isArray(inner)) {
-                                const value = tags[key];
                                 if (typeof value !== 'object' || !match(value, inner)) {
                                     return false;
                                 }
                             }
-                            else if (inner !== tags[key]) {
+                            else if (inner !== value) {
+                                if (inner === 2 && !Array.isArray(value) && Object(value) === (value) && Object.keys(value).length === 0) {
+                                    return true;
+                                }
                                 return false;
                             }
                         }

+ 8 - 5
source/view.js

@@ -1640,8 +1640,8 @@ view.ModelFactoryService = class {
             const tags = context.tags('pb+');
             if (Object.keys(tags).length > 0) {
                 const formats = [
-                    { name: 'mediapipe.BoxDetectorIndex data', tags: [[1,[[1,[[1,[[1,5],[2,5],[3,5],[4,5],[6,0],[7,5],[8,5],[10,5],[11,0],[12,0]]],[2,5],[3,[]]]],[2,false],[3,false],[4,false],[5,false]]],[2,false],[3,false]] },
                     { name: 'sentencepiece.ModelProto data', tags: [[1,[[1,2],[2,5],[3,0]]],[2,[[1,2],[2,2],[3,0],[4,0],[5,2],[6,0],[7,2],[10,5],[16,0],[40,0],[41,0],[42,0],[43,0]]],[3,[]],[4,[]],[5,[]]] },
+                    { name: 'mediapipe.BoxDetectorIndex data', tags: [[1,[[1,[[1,[[1,5],[2,5],[3,5],[4,5],[6,0],[7,5],[8,5],[10,5],[11,0],[12,0]]],[2,5],[3,[]]]],[2,false],[3,false],[4,false],[5,false]]],[2,false],[3,false]] },
                     { name: 'third_party.tensorflow.python.keras.protobuf.SavedMetadata data', tags: [[1,[[1,[[1,0],[2,0]]],[2,0],[3,2],[4,2],[5,2]]]] },
                     { name: 'pblczero.Net data', tags: [[1,5],[2,2],[3,[[1,0],[2,0],[3,0]],[10,[[1,[]],[2,[]],[3,[]],[4,[]],[5,[]],[6,[]]]],[11,[]]]] } // https://github.com/LeelaChessZero/lczero-common/blob/master/proto/net.proto
                 ];
@@ -1649,19 +1649,22 @@ view.ModelFactoryService = class {
                     for (const pair of schema) {
                         const key = pair[0];
                         const inner = pair[1];
-                        if (tags[key] === undefined) {
+                        const value = tags[key];
+                        if (value === undefined) {
                             continue;
                         }
-                        else if (inner === false) {
+                        if (inner === false) {
                             return false;
                         }
                         if (Array.isArray(inner)) {
-                            const value = tags[key];
                             if (typeof value !== 'object' || !match(value, inner)) {
                                 return false;
                             }
                         }
-                        else if (inner !== tags[key]) {
+                        else if (inner !== value) {
+                            if (inner === 2 && !Array.isArray(value) && Object(value) === (value) && Object.keys(value).length === 0) {
+                                return true;
+                            }
                             return false;
                         }
                     }

+ 7 - 0
test/models.json

@@ -124,6 +124,13 @@
     "error":  "Invalid file content. File contains Darkflow metadata.",
     "link":   "https://github.com/lutzroeder/netron/issues/458"
   },
+  {
+    "type":   "_",
+    "target": "universal_encoder_8k_spm.model.model",
+    "source": "https://github.com/lutzroeder/netron/files/7232403/universal_encoder_8k_spm.model.zip[universal_encoder_8k_spm.model]",
+    "error":  "Invalid file content. File contains sentencepiece.ModelProto data.",
+    "link":   "https://github.com/lutzroeder/netron/issues/748"
+  },
   {
     "type":   "_",
     "target": "zip_invalid_archive.zip",