This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: better error for missing web crypto
This PR simply detects missing web crypto and throws an error with an appropriate message. This is a stepping stone that will help users understand the problem until we have time to do a refactor of this module and of all the modules that use it to enable optionally passing your own crypto implementation. refs #149 refs #150 refs #105 refs ipfs/js-ipfs#2153 refs ipfs/js-ipfs#2017 License: MIT Signed-off-by: Alan Shaw <[email protected]>
- Loading branch information
Showing
6 changed files
with
123 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,24 @@ | ||
/* global self */ | ||
/* eslint-env browser */ | ||
|
||
'use strict' | ||
|
||
module.exports = self.crypto || self.msCrypto | ||
// Check native crypto exists and is enabled (In insecure context `self.crypto` | ||
// exists but `self.crypto.subtle` does not). | ||
exports.get = (win = self) => { | ||
const nativeCrypto = win.crypto || win.msCrypto | ||
|
||
if (!nativeCrypto || !nativeCrypto.subtle) { | ||
throw Object.assign( | ||
new Error( | ||
'Missing Web Crypto API. ' + | ||
'The most likely cause of this error is that this page is being accessed ' + | ||
'from an insecure context (i.e. not HTTPS). For more information and ' + | ||
'possible resolutions see ' + | ||
'https://github.com/libp2p/js-libp2p-crypto/blob/master/README.md#web-crypto-api' | ||
), | ||
{ code: 'ERR_MISSING_WEB_CRYPTO' } | ||
) | ||
} | ||
|
||
return nativeCrypto | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const chai = require('chai') | ||
const dirtyChai = require('dirty-chai') | ||
const expect = chai.expect | ||
chai.use(dirtyChai) | ||
const crypto = require('../') | ||
const webcrypto = require('../src/webcrypto') | ||
|
||
async function expectMissingWebCrypto (fn) { | ||
try { | ||
await fn() | ||
} catch (err) { | ||
expect(err.code).to.equal('ERR_MISSING_WEB_CRYPTO') | ||
return | ||
} | ||
throw new Error('Expected missing web crypto error') | ||
} | ||
|
||
describe('Missing web crypto', () => { | ||
let webcryptoGet | ||
let rsaPrivateKey | ||
|
||
before(async () => { | ||
rsaPrivateKey = await crypto.keys.generateKeyPair('RSA', 512) | ||
}) | ||
|
||
before(() => { | ||
webcryptoGet = webcrypto.get | ||
webcrypto.get = () => webcryptoGet({}) | ||
}) | ||
|
||
after(() => { | ||
webcrypto.get = webcryptoGet | ||
}) | ||
|
||
it('should error for hmac create when web crypto is missing', () => { | ||
return expectMissingWebCrypto(() => crypto.hmac.create('SHA256', Buffer.from('secret'))) | ||
}) | ||
|
||
it('should error for generate ephemeral key pair when web crypto is missing', () => { | ||
return expectMissingWebCrypto(() => crypto.keys.generateEphemeralKeyPair('P-256')) | ||
}) | ||
|
||
it('should error for generate rsa key pair when web crypto is missing', () => { | ||
return expectMissingWebCrypto(() => crypto.keys.generateKeyPair('rsa', 256)) | ||
}) | ||
|
||
it('should error for unmarshal RSA private key when web crypto is missing', () => { | ||
return expectMissingWebCrypto(() => crypto.keys.unmarshalPrivateKey(crypto.keys.marshalPrivateKey(rsaPrivateKey))) | ||
}) | ||
|
||
it('should error for sign RSA private key when web crypto is missing', () => { | ||
return expectMissingWebCrypto(() => rsaPrivateKey.sign(Buffer.from('test'))) | ||
}) | ||
|
||
it('should error for verify RSA public key when web crypto is missing', () => { | ||
return expectMissingWebCrypto(() => rsaPrivateKey.public.verify(Buffer.from('test'), Buffer.from('test'))) | ||
}) | ||
}) |