From b51b8ad341d625ac6915fd424faf2a6d65bda787 Mon Sep 17 00:00:00 2001
From: Sanjana Rajan <srajan1@stanford.edu>
Date: Wed, 5 Apr 2017 07:54:08 -0700
Subject: [PATCH 1/2] add tolerant mode which suppresses errors in reading
 non-data packets

---
 src/config/config.js     |  1 +
 src/packet/compressed.js |  2 +-
 src/packet/packetlist.js |  4 ++++
 test/general/key.js      | 12 +++++++++++-
 4 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/src/config/config.js b/src/config/config.js
index 6a070b76..976128d0 100644
--- a/src/config/config.js
+++ b/src/config/config.js
@@ -44,6 +44,7 @@ export default {
   use_native: true, // use native node.js crypto and Web Crypto apis (if available)
   zero_copy: false, // use transferable objects between the Web Worker and main thread
   debug: false,
+  tolerant: false, // ignore unsupported/unrecognizable packets instead of throwing an error
   show_version: true,
   show_comment: true,
   versionstring: "OpenPGP.js VERSION",
diff --git a/src/packet/compressed.js b/src/packet/compressed.js
index e6db0134..619a87da 100644
--- a/src/packet/compressed.js
+++ b/src/packet/compressed.js
@@ -120,7 +120,7 @@ Compressed.prototype.decompress = function () {
       throw new Error('Compression algorithm BZip2 [BZ2] is not implemented.');
 
     default:
-      throw new Error("Compression algorithm unknown :" + this.alogrithm);
+      throw new Error("Compression algorithm unknown :" + this.algorithm);
   }
 
   this.packets.read(decompressed);
diff --git a/src/packet/packetlist.js b/src/packet/packetlist.js
index 460821c2..01d5c0cd 100644
--- a/src/packet/packetlist.js
+++ b/src/packet/packetlist.js
@@ -15,6 +15,7 @@ import util from '../util';
 import packetParser from './packet.js';
 import * as packets from './all_packets.js';
 import enums from '../enums.js';
+import config from '../config';
 
 /**
  * @constructor
@@ -44,6 +45,9 @@ Packetlist.prototype.read = function (bytes) {
       pushed = true;
       packet.read(parsed.packet);
     } catch(e) {
+      if (!config.tolerant || parsed.tag == enums.packet.symmetricallyEncrypted || parsed.tag == enums.packet.literal || parsed.tag == enums.packet.compressed) {
+        throw e;
+      }
       if (pushed) {
         this.pop(); // drop unsupported packet
       }
diff --git a/test/general/key.js b/test/general/key.js
index a04da7a4..16e0610b 100644
--- a/test/general/key.js
+++ b/test/general/key.js
@@ -527,7 +527,8 @@ describe('Key', function() {
     '=Q/kB',
     '-----END PGP PUBLIC KEY BLOCK-----'].join('\n');
 
-  it('Parsing armored text with RSA key and ECC subkey', function(done) {
+  it('Parsing armored text with RSA key and ECC subkey in tolerant mode', function(done) {
+    openpgp.config.tolerant = true;
     var pubKeys = openpgp.key.readArmored(rsa_ecc_pub);
     expect(pubKeys).to.exist;
     expect(pubKeys.err).to.not.exist;
@@ -536,6 +537,15 @@ describe('Key', function() {
     done();
   });
 
+  it('Parsing armored text with RSA key and ECC subkey in non-tolerant mode', function(done) {
+    openpgp.config.tolerant = false;
+    var pubKeys = openpgp.key.readArmored(rsa_ecc_pub);
+    expect(pubKeys).to.exist;
+    expect(pubKeys.err).to.exist;
+    done();
+  });
+
+
   var multi_uid_key =
     ['-----BEGIN PGP PUBLIC KEY BLOCK-----',
     'Version: GnuPG v1',

From c3958b02703f85f9e653f4d0249afe18bb68152d Mon Sep 17 00:00:00 2001
From: Sanjana Rajan <srajan1@stanford.edu>
Date: Wed, 5 Apr 2017 16:36:29 -0700
Subject: [PATCH 2/2] tolerant default to true

---
 src/config/config.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/config/config.js b/src/config/config.js
index 976128d0..fd37baba 100644
--- a/src/config/config.js
+++ b/src/config/config.js
@@ -44,7 +44,7 @@ export default {
   use_native: true, // use native node.js crypto and Web Crypto apis (if available)
   zero_copy: false, // use transferable objects between the Web Worker and main thread
   debug: false,
-  tolerant: false, // ignore unsupported/unrecognizable packets instead of throwing an error
+  tolerant: true, // ignore unsupported/unrecognizable packets instead of throwing an error
   show_version: true,
   show_comment: true,
   versionstring: "OpenPGP.js VERSION",