From 341f474d3b18698a340cd2e0a2b7d443893dccec Mon Sep 17 00:00:00 2001 From: larabr <7375870+larabr@users.noreply.github.com> Date: Wed, 4 May 2022 21:38:59 +0200 Subject: [PATCH 1/4] Make sure asn1 is defined before setting it --- src/crypto/public_key/rsa.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/crypto/public_key/rsa.js b/src/crypto/public_key/rsa.js index 1c081316c..1b766b0d1 100644 --- a/src/crypto/public_key/rsa.js +++ b/src/crypto/public_key/rsa.js @@ -33,7 +33,7 @@ const nodeCrypto = util.getNodeCrypto(); const asn1 = nodeCrypto ? require('asn1.js') : undefined; /* eslint-disable no-invalid-this */ -const RSAPrivateKey = util.detectNode() ? asn1.define('RSAPrivateKey', function () { +const RSAPrivateKey = nodeCrypto ? asn1.define('RSAPrivateKey', function () { this.seq().obj( // used for native NodeJS crypto this.key('version').int(), // 0 this.key('modulus').int(), // n @@ -47,7 +47,7 @@ const RSAPrivateKey = util.detectNode() ? asn1.define('RSAPrivateKey', function ); }) : undefined; -const RSAPublicKey = util.detectNode() ? asn1.define('RSAPubliceKey', function () { +const RSAPublicKey = nodeCrypto ? asn1.define('RSAPubliceKey', function () { this.seq().obj( // used for native NodeJS crypto this.key('modulus').int(), // n this.key('publicExponent').int(), // e From 326e78a431db6285029a2105249f3964ec8f7515 Mon Sep 17 00:00:00 2001 From: larabr <7375870+larabr@users.noreply.github.com> Date: Mon, 9 May 2022 14:40:39 +0200 Subject: [PATCH 2/4] Check navigator first for hardware concurrency --- src/util.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/util.js b/src/util.js index 15b903bbc..68ce7200b 100644 --- a/src/util.js +++ b/src/util.js @@ -425,12 +425,12 @@ const util = { }, getHardwareConcurrency: function() { - if (util.detectNode()) { - const os = require('os'); - return os.cpus().length; + if (typeof navigator !== 'undefined') { + return navigator.hardwareConcurrency || 1; } - return (typeof navigator !== 'undefined' && navigator.hardwareConcurrency) || 1; + const os = require('os'); // if node + return os.cpus().length; }, isEmailAddress: function(data) { From 1e8659b41847d468e96043cc98c014abc843886e Mon Sep 17 00:00:00 2001 From: larabr <7375870+larabr@users.noreply.github.com> Date: Mon, 9 May 2022 15:58:36 +0200 Subject: [PATCH 3/4] Remove util.detectNode --- src/util.js | 8 -------- test/general/openpgp.js | 4 +++- test/general/streaming.js | 8 +++++--- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/util.js b/src/util.js index 68ce7200b..8e9f3724d 100644 --- a/src/util.js +++ b/src/util.js @@ -381,14 +381,6 @@ const util = { return typeof globalThis !== 'undefined' && globalThis.crypto && globalThis.crypto.subtle; }, - /** - * Detect Node.js runtime. - */ - detectNode: function() { - return typeof globalThis.process === 'object' && - typeof globalThis.process.versions === 'object'; - }, - /** * Detect native BigInt support */ diff --git a/test/general/openpgp.js b/test/general/openpgp.js index 3f5aadd8b..6fe34246c 100644 --- a/test/general/openpgp.js +++ b/test/general/openpgp.js @@ -17,6 +17,8 @@ chai.use(require('chai-as-promised')); const expect = chai.expect; +const detectNode = () => typeof globalThis.process === 'object' && typeof globalThis.process.versions === 'object'; + const pub_key = [ '-----BEGIN PGP PUBLIC KEY BLOCK-----', 'Version: GnuPG v2.0.19 (GNU/Linux)', @@ -2901,7 +2903,7 @@ aOU= expect(e.message).to.match(/Ascii armor integrity check failed/); expect(stepReached).to.equal( j === 0 ? 0 : - (openpgp.config.aeadChunkSizeByte === 0 && (j === 2 || util.detectNode() || util.getHardwareConcurrency() < 8)) || (!openpgp.config.aeadProtect && openpgp.config.allowUnauthenticatedStream) ? 2 : + (openpgp.config.aeadChunkSizeByte === 0 && (j === 2 || detectNode() || util.getHardwareConcurrency() < 8)) || (!openpgp.config.aeadProtect && openpgp.config.allowUnauthenticatedStream) ? 2 : 1 ); return; diff --git a/test/general/streaming.js b/test/general/streaming.js index 6839f1f4f..5af963afb 100644 --- a/test/general/streaming.js +++ b/test/general/streaming.js @@ -16,6 +16,8 @@ const stream = require('@openpgp/web-stream-tools'); const useNativeStream = (() => { try { new global.ReadableStream(); return true; } catch (e) { return false; } })(); // eslint-disable-line no-new const NodeReadableStream = useNativeStream ? undefined : require('stream').Readable; +const detectNode = () => typeof globalThis.process === 'object' && typeof globalThis.process.versions === 'object'; + const pub_key = [ '-----BEGIN PGP PUBLIC KEY BLOCK-----', 'Version: GnuPG v2.0.19 (GNU/Linux)', @@ -889,7 +891,7 @@ function tests() { it("Don't pull entire input stream when we're not pulling decrypted stream (AEAD)", async function() { let coresStub; - if (util.detectNode()) { + if (detectNode()) { coresStub = stub(require('os'), 'cpus'); coresStub.returns(new Array(2)); // Object.defineProperty(require('os'), 'cpus', { value: () => [,], configurable: true }); @@ -915,7 +917,7 @@ function tests() { await new Promise(resolve => setTimeout(resolve, 3000)); expect(i).to.be.lessThan(expectedType === 'web' ? 50 : 300); } finally { - if (util.detectNode()) { + if (detectNode()) { coresStub.restore(); } else { delete navigator.hardwareConcurrency; @@ -1012,7 +1014,7 @@ module.exports = () => describe('Streaming', function() { tests(); - if (util.detectNode()) { + if (detectNode()) { const fs = require('fs'); it('Node: Encrypt and decrypt text message roundtrip', async function() { From 89d4307e749bfb4ac7bbfff03513088c917b14d1 Mon Sep 17 00:00:00 2001 From: larabr Date: Wed, 11 May 2022 12:02:20 +0200 Subject: [PATCH 4/4] Update src/util.js Co-authored-by: Daniel Huigens --- src/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.js b/src/util.js index 8e9f3724d..f3a57e117 100644 --- a/src/util.js +++ b/src/util.js @@ -421,7 +421,7 @@ const util = { return navigator.hardwareConcurrency || 1; } - const os = require('os'); // if node + const os = require('os'); // Assume we're on Node.js. return os.cpus().length; },