Skip to content

Troubles loading crypto-js #199

Closed
Closed

Description

@Smawexi

Issue type

Bug

How did you install PythonMonkey?

None

OS platform and distribution

No response

Python version (python --version)

No response

PythonMonkey version (pip show pythonmonkey)

No response

Bug Description

When importing algorithm libraries, globalThis does not have the hasOwnProperty function

Standalone code to reproduce the issue

No response

Relevant log output or backtrace

No response

Additional info if applicable

No response

What branch of PythonMonkey were you developing on? (If applicable)

No response

Activity

  1. Smawexi commented on Dec 9, 2023

    @Smawexi
    Author
    import pythonmonkey as pm  
    cryptojs = pm.require('./node_modules/crypto-js')  
    temp = '123456'  
    key = cryptojs['enc']['Utf8']['parse'](temp)  
    print(cryptojs['AES']['encrypt'](key, key, {  
        'iv': key,  
        'mode': cryptojs['mode']['CBC']  
    }))  
    

    When executing this code, the error is reported as follows:
    Traceback (most recent call last):
    File "D:\Test\example\test\demo.py", line 35, in
    print(cryptojs['AES']['encrypt'](key, key, {
    pythonmonkey.SpiderMonkeyError: Error in file evaluate, on line 209:
    TypeError: properties.hasOwnProperty is not a function
    Stack Trace:
    mixIn@evaluate:209:37
    extend@evaluate:143:30
    encrypt@evaluate:681:29
    encrypt@evaluate:201:59


    This should be because its prototype object has the hasOwnProperty attribute

  2. philippedistributive commented on Feb 2, 2024

    @philippedistributive
    Contributor

    I get

    import pythonmonkey as pm
    cryptojs = pm.require('crypto-js')
    temp = '123456'
    key = cryptojs['enc']['Utf8'] -> no 'parse'
    print(cryptojs['AES']['encrypt'](key, key, {
    ... 'iv': key,
    ... 'mode': cryptojs['mode']['CBC']
    ... }))
    Traceback (most recent call last):
    File "", line 1, in
    pythonmonkey.SpiderMonkeyError: Error in file evaluate, on line 335:
    RangeError: invalid array length
    Stack Trace:
    clamp@evaluate:335:34
    concat@evaluate:300:19
    pad@evaluate:417:19
    _doFinalize@evaluate:498:26
    finalize@evaluate:163:44
    encrypt@evaluate:685:41
    encrypt@evaluate:201:59

  3. wiwichips commented on Feb 12, 2024

    @wiwichips
    Collaborator

    Thanks @Smawexi , On my machine I get a Segmentation Fault when exeucuting your code:

    @philippedistributive , the code snippet mentioned in the comment by @Smawexi is formatted weirdly since GitHub's markdown renderer thinks the code is Markdown and not Python. As a result if you copy the example you'll get a syntax error, and if you fix the syntax error, you'll get the "Invalid Array Length" error. -- here is the intended source code sent in the above snippet (seen by pressing "edit" on the message):

    repro.py

    import pythonmonkey as pm
    cryptojs = pm.require('./node_modules/crypto-js')
    temp = '123456'
    key = cryptojs['enc']['Utf8']['parse'](temp)
    print(cryptojs['AES']['encrypt'](key, key, {
        'iv': key,
        'mode': cryptojs['mode']['CBC']
    }))

    Which results in

    Segmentation fault (core dumped)
    

    and takes 1300ms seconds to run (on my machine)...


    Running the same code using NodeJS (except replacing pm.require with require and replacing print with console.log) results in this output which takes 48ms on my box:

    {
      init: [Function (anonymous)],
      '$super': {
        init: [Function: init],
        toString: [Function: toString],
        '$super': {
          extend: [Function: extend],
          create: [Function: create],
          init: [Function: init],
          mixIn: [Function: mixIn],
          clone: [Function: clone]
        }
      },
      ciphertext: {
        words: [ -1919796327, -121739567, 1807892019, -115437076 ],
        sigBytes: 16
      },
      key: { words: [ 825373492, 892731392 ], sigBytes: 6 },
      iv: { words: [ 825373492, 892731392 ], sigBytes: 6 },
      algorithm: {
        _doReset: [Function: _doReset],
        encryptBlock: [Function: encryptBlock],
        decryptBlock: [Function: decryptBlock],
        _doCryptBlock: [Function: _doCryptBlock],
        keySize: 8,
        init: [Function (anonymous)],
        '$super': {
          cfg: [Object],
          reset: [Function: reset],
          _doProcessBlock: [Function: _doProcessBlock],
          _doFinalize: [Function: _doFinalize],
          blockSize: 4,
          init: [Function (anonymous)],
          '$super': [Object]
        }
      },
      mode: <ref *1> {
        init: [Function (anonymous)],
        '$super': {
          createEncryptor: [Function: createEncryptor],
          createDecryptor: [Function: createDecryptor],
          init: [Function: init],
          '$super': [Object]
        },
        Encryptor: {
          processBlock: [Function: processBlock],
          init: [Function (anonymous)],
          '$super': [Circular *1]
        },
        Decryptor: {
          processBlock: [Function: processBlock],
          init: [Function (anonymous)],
          '$super': [Circular *1]
        }
      },
      padding: { pad: [Function: pad], unpad: [Function: unpad] },
      blockSize: 4,
      formatter: { stringify: [Function: stringify], parse: [Function: parse] }
    }
    
    real  0m0.048s
    user  0m0.027s
    sys   0m0.024s
  4. Smawexi commented on Feb 12, 2024

    @Smawexi
    Author

    Now when I execute the following code, strange errors occur:

    import pythonmonkey as pm
    
    print(pm.__version__)
    cryptojs = pm.require('./node_modules/crypto-js')
    print(cryptojs)
    

    The output is:
    0.3.0

    Process finished with exit code -1073741795 (0xC000001D)


    platform: windows10
    python version: 3.9.13
    pythonmonkey: 0.3.0

  5. philippedistributive commented on Feb 12, 2024

    @philippedistributive
    Contributor

    @Smawexi this latest error already has a fix pending release

  6. philippedistributive commented on Feb 12, 2024

    @philippedistributive
    Contributor

    And so does the initially reported one. Printing the crytop-js objects goes beyond our capabilities and we fail with:

    RecursionError: maximum recursion depth exceeded while getting the repr of an object

    Note that this is the python runtime behavior which of course is different from Node

    Indeed the circularity comes out here:
    '$super': [Circular *1]

  7. philippedistributive commented on Feb 12, 2024

    @philippedistributive
    Contributor

    When I try console.log I get:

    pm.eval('console.log')(cryptojs)
    { lib:
    { Base:
    { extend: [Function: extend],
    create: [Function: create],
    init: [Function: init],
    mixIn: [Function: mixIn],
    clone: [Function: clone] },
    WordArray:
    { init: [Function],
    toString: [Function: toString],
    concat: [Function: concat],
    clamp: [Function: clamp],
    clone: [Function: clone],
    random: [Function: random],
    '$super': [Object] },
    BufferedBlockAlgorithm:
    { reset: [Function: reset],
    _append: [Function: _append],
    _process: [Function: _process],
    clone: [Function: clone],
    _minBufferSize: 0,
    init: [Function],
    '$super': [Object] },
    Hasher:
    { cfg: [Object],
    init: [Function: init],
    reset: [Function: reset],
    update: [Function: update],
    finalize: [Function: finalize],
    blockSize: 16,
    _createHelper: [Function: _createHelper],
    _createHmacHelper: [Function: _createHmacHelper],
    '$super': [Object] },
    Cipher:
    { cfg: [Object],
    createEncryptor: [Function: createEncryptor],
    createDecryptor: [Function: createDecryptor],
    init: [Function: init],
    reset: [Function: reset],
    process: [Function: process],
    finalize: [Function: finalize],
    keySize: 4,
    ivSize: 4,
    _ENC_XFORM_MODE: 1,
    _DEC_XFORM_MODE: 2,
    _createHelper: [Function],
    '$super': [Object] },
    StreamCipher:
    { _doFinalize: [Function: _doFinalize],
    blockSize: 1,
    init: [Function],
    '$super': [Object] },
    BlockCipherMode:
    { createEncryptor: [Function: createEncryptor],
    createDecryptor: [Function: createDecryptor],
    init: [Function: init],
    '$super': [Object] },
    BlockCipher:
    { cfg: [Object],
    reset: [Function: reset],
    _doProcessBlock: [Function: _doProcessBlock],
    _doFinalize: [Function: _doFinalize],
    blockSize: 4,
    init: [Function],
    '$super': [Object] },
    CipherParams:
    { init: [Function: init],
    toString: [Function: toString],
    '$super': [Object] },
    SerializableCipher:
    { cfg: [Object],
    encrypt: [Function: encrypt],
    decrypt: [Function: decrypt],
    _parse: [Function: _parse],
    init: [Function],
    '$super': [Object] },
    PasswordBasedCipher:
    { cfg: [Object],
    encrypt: [Function: encrypt],
    decrypt: [Function: decrypt],
    init: [Function],
    '$super': [Object] } },
    enc:
    { Hex: { stringify: [Function: stringify], parse: [Function: parse] },
    Latin1: { stringify: [Function: stringify], parse: [Function: parse] },
    Utf8: { stringify: [Function: stringify], parse: [Function: parse] },
    Utf16BE: { stringify: [Function: stringify], parse: [Function: parse] },
    Utf16: { stringify: [Function: stringify], parse: [Function: parse] },
    Utf16LE: { stringify: [Function: stringify], parse: [Function: parse] },
    Base64:
    { stringify: [Function: stringify],
    parse: [Function: parse],
    _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' },
    Base64url:
    { stringify: [Function: stringify],
    parse: [Function: parse],
    _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
    safe_map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-' } },
    algo:
    { MD5:
    { _doReset: [Function: _doReset],
    _doProcessBlock: [Function: _doProcessBlock],
    _doFinalize: [Function: _doFinalize],
    clone: [Function: clone],
    init: [Function],
    '$super': [Object] },
    SHA1:
    { _doReset: [Function: _doReset],
    _doProcessBlock: [Function: _doProcessBlock],
    _doFinalize: [Function: _doFinalize],
    clone: [Function: clone],
    init: [Function],
    '$super': [Object] },
    SHA256:
    { _doReset: [Function: _doReset],
    _doProcessBlock: [Function: _doProcessBlock],
    _doFinalize: [Function: _doFinalize],
    clone: [Function: clone],
    init: [Function],
    '$super': [Object] },
    SHA224:
    { _doReset: [Function: _doReset],
    _doFinalize: [Function: _doFinalize],
    init: [Function],
    '$super': [Object] },
    SHA512:
    { _doReset: [Function: _doReset],
    _doProcessBlock: [Function: _doProcessBlock],
    _doFinalize: [Function: _doFinalize],
    clone: [Function: clone],
    blockSize: 32,
    init: [Function],
    '$super': [Object] },
    SHA384:
    { _doReset: [Function: _doReset],
    _doFinalize: [Function: _doFinalize],
    init: [Function],
    '$super': [Object] },
    SHA3:
    { cfg: [Object],
    _doReset: [Function: _doReset],
    _doProcessBlock: [Function: _doProcessBlock],
    _doFinalize: [Function: _doFinalize],
    clone: [Function: clone],
    init: [Function],
    '$super': [Object] },
    RIPEMD160:
    { _doReset: [Function: _doReset],
    _doProcessBlock: [Function: _doProcessBlock],
    _doFinalize: [Function: _doFinalize],
    clone: [Function: clone],
    init: [Function],
    '$super': [Object] },
    HMAC:
    { init: [Function: init],
    reset: [Function: reset],
    update: [Function: update],
    finalize: [Function: finalize],
    '$super': [Object] },
    PBKDF2:
    { cfg: [Object],
    init: [Function: init],
    compute: [Function: compute],
    '$super': [Object] },
    EvpKDF:
    { cfg: [Object],
    init: [Function: init],
    compute: [Function: compute],
    '$super': [Object] },
    AES:
    { _doReset: [Function: _doReset],
    encryptBlock: [Function: encryptBlock],
    decryptBlock: [Function: decryptBlock],
    _doCryptBlock: [Function: _doCryptBlock],
    keySize: 8,
    init: [Function],
    '$super': [Object] },
    DES:
    { _doReset: [Function: _doReset],
    encryptBlock: [Function: encryptBlock],
    decryptBlock: [Function: decryptBlock],
    _doCryptBlock: [Function: _doCryptBlock],
    keySize: 2,
    ivSize: 2,
    blockSize: 2,
    init: [Function],
    '$super': [Object] },
    TripleDES:
    { _doReset: [Function: _doReset],
    encryptBlock: [Function: encryptBlock],
    decryptBlock: [Function: decryptBlock],
    keySize: 6,
    ivSize: 2,
    blockSize: 2,
    init: [Function],
    '$super': [Object] },
    RC4:
    { _doReset: [Function: _doReset],
    _doProcessBlock: [Function: _doProcessBlock],
    keySize: 8,
    ivSize: 0,
    init: [Function],
    '$super': [Object] },
    RC4Drop:
    { cfg: [Object],
    _doReset: [Function: _doReset],
    init: [Function],
    '$super': [Object] },
    Rabbit:
    { _doReset: [Function: _doReset],
    _doProcessBlock: [Function: _doProcessBlock],
    blockSize: 4,
    ivSize: 2,
    init: [Function],
    '$super': [Object] },
    RabbitLegacy:
    { _doReset: [Function: _doReset],
    _doProcessBlock: [Function: _doProcessBlock],
    blockSize: 4,
    ivSize: 2,
    init: [Function],
    '$super': [Object] },
    Blowfish:
    { _doReset: [Function: _doReset],
    encryptBlock: [Function: encryptBlock],
    decryptBlock: [Function: decryptBlock],
    blockSize: 2,
    keySize: 4,
    ivSize: 2,
    init: [Function],
    '$super': [Object] } },
    x64:
    { Word: { init: [Function: init], '$super': [Object] },
    WordArray:
    { init: [Function: init],
    toX32: [Function: toX32],
    clone: [Function: clone],
    '$super': [Object] } },
    MD5: [Function],
    HmacMD5: [Function],
    SHA1: [Function],
    HmacSHA1: [Function],
    SHA256: [Function],
    HmacSHA256: [Function],
    SHA224: [Function],
    HmacSHA224: [Function],
    SHA512: [Function],
    HmacSHA512: [Function],
    SHA384: [Function],
    HmacSHA384: [Function],
    SHA3: [Function],
    HmacSHA3: [Function],
    RIPEMD160: [Function],
    HmacRIPEMD160: [Function],
    PBKDF2: [Function],
    EvpKDF: [Function],
    mode:
    { CBC:
    { init: [Function],
    '$super': [Object],
    Encryptor: [Object],
    Decryptor: [Object] },
    CFB:
    { init: [Function],
    '$super': [Object],
    Encryptor: [Object],
    Decryptor: [Object] },
    CTR:
    { init: [Function],
    '$super': [Object],
    Encryptor: [Object],
    Decryptor: [Object] },
    CTRGladman:
    { init: [Function],
    '$super': [Object],
    Encryptor: [Object],
    Decryptor: [Object] },
    OFB:
    { init: [Function],
    '$super': [Object],
    Encryptor: [Object],
    Decryptor: [Object] },
    ECB:
    { init: [Function],
    '$super': [Object],
    Encryptor: [Object],
    Decryptor: [Object] } },
    pad:
    { Pkcs7: { pad: [Function: pad], unpad: [Function: unpad] },
    AnsiX923: { pad: [Function: pad], unpad: [Function: unpad] },
    Iso10126: { pad: [Function: pad], unpad: [Function: unpad] },
    Iso97971: { pad: [Function: pad], unpad: [Function: unpad] },
    ZeroPadding: { pad: [Function: pad], unpad: [Function: unpad] },
    NoPadding: { pad: [Function: pad], unpad: [Function: unpad] } },
    format:
    { OpenSSL: { stringify: [Function: stringify], parse: [Function: parse] },
    Hex: { stringify: [Function: stringify], parse: [Function: parse] } },
    kdf: { OpenSSL: { execute: [Function: execute] } },
    AES: { encrypt: [Function: encrypt], decrypt: [Function: decrypt] },
    DES: { encrypt: [Function: encrypt], decrypt: [Function: decrypt] },
    TripleDES: { encrypt: [Function: encrypt], decrypt: [Function: decrypt] },
    RC4: { encrypt: [Function: encrypt], decrypt: [Function: decrypt] },
    RC4Drop: { encrypt: [Function: encrypt], decrypt: [Function: decrypt] },
    Rabbit: { encrypt: [Function: encrypt], decrypt: [Function: decrypt] },
    RabbitLegacy: { encrypt: [Function: encrypt], decrypt: [Function: decrypt] },
    Blowfish: { encrypt: [Function: encrypt], decrypt: [Function: decrypt] } }

  8. changed the title [-]Some attributes of globalThis are missing[/-] [+]troubles loading crypto-js[/+] on Feb 12, 2024
  9. changed the title [-]troubles loading crypto-js[/-] [+]Troubles loading crypto-js[/+] on Feb 12, 2024
  10. philippedistributive commented on Mar 5, 2024

    @philippedistributive
    Contributor

    This is now fully fixed in coming release and will print the Crypto object properly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions