web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

TypeError: Cannot read properties of undefined (reading 'importKey')

Open Muhammad-Altabba opened this issue 2 years ago • 2 comments

Description

Unclear error is thrown when calling encrypt from web3-eth-accounts in an in-secured environment. This could happen when using the library with a remote website with http (and not https). However, I checked this using Jest with jsdom; And in this case it is also an in-secured environment. So, this issue was detected using jsdom but it applies to other scenarios (remote http websites).

It also worth noting that it could be implemented for jsdom later, and there are polyfiles for it, see this issue for more: https://github.com/jsdom/jsdom/issues/1612

Expected behavior

To throw a meaningful error in case some functionality is not available.

Actual behavior

Throws: TypeError: Cannot read properties of undefined (reading 'importKey')

Steps to reproduce the behavior

  1. At test/unit/account.test.ts inside the package web3-eth-accounts:
  • Add this to the very first line of the file (before the existing comment):
/**
 * @jest-environment jsdom
 */
  • Add polyfills for TextEncoder before the imports:
import { TextEncoder } from 'util';
global.TextEncoder = TextEncoder;
  • Search for describe('encrypt', () => { and replace with: describe.only('encrypt', () => {
  1. cd packages/web3-eth-accounts && yarn test account.test.ts

Logs

FAIL  test/unit/account.test.ts (7.943 s)
 ● accounts › encrypt › valid cases › [
 '0x67f476289210e3bef3c1c75e4de993ff0a00663df00def84e73aa7411eac18a6',
  '123',
  [Object]
]

    TypeError: Cannot read properties of undefined (reading 'importKey')

      552 |     }
      553 |
    > 554 |     const cipher = await createCipheriv(
          |                          ^
      555 |             privateKeyUint8Array,
      556 |             derivedKey.slice(0, 16),
      557 |             initializationVector,

      at getBrowserKey (node_modules/ethereum-cryptography/aes.js:33:42)
      at encrypt (node_modules/ethereum-cryptography/aes.js:41:36)
      at src/account.ts:554:23
      at src/account.ts:5226:39
      at Object.<anonymous>.__awaiter (src/account.ts:5169:10)
      at encrypt (src/account.ts:487:24)
      at test/unit/account.test.ts:169:33
      at test/unit/account.test.ts:8:71
      at Object.<anonymous>.__awaiter (test/unit/account.test.ts:4:12)
      at test/unit/account.test.ts:168:60

Muhammad-Altabba avatar Oct 06 '23 06:10 Muhammad-Altabba

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

github-actions[bot] avatar Dec 06 '23 00:12 github-actions[bot]

So I suggest just to give the user a clear error message indicating that encrypt does not work inside unsecured environment. That is when he browse a remote http website.

Muhammad-Altabba avatar Dec 06 '23 03:12 Muhammad-Altabba

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

github-actions[bot] avatar Feb 21 '24 00:02 github-actions[bot]

So I suggest just to give the user a clear error message indicating that encrypt does not work inside unsecured environment. That is when he browse a remote http website.

I agree with you

HenriqueVazquez avatar Feb 22 '24 14:02 HenriqueVazquez

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

github-actions[bot] avatar Apr 23 '24 00:04 github-actions[bot]