Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inherited properties from Object.prototype break checkType in ParamType #4896

Open
zZoMROT opened this issue Dec 11, 2024 · 0 comments
Open
Assignees
Labels
investigate Under investigation and may be a bug. v6 Issues regarding v6

Comments

@zZoMROT
Copy link

zZoMROT commented Dec 11, 2024

Ethers Version

6.13.4

Search Terms

utils, properties, defineProperties, ParamType, checkType, keys

Describe the Problem

Description:

If a developer extends Object.prototype in their project by adding a custom function, this property gets copied into the ParamType object during its creation. This results in breaking the checkType method, as it attempts to process the inherited property as part of the expected data.

Screenshot 2024-12-10 23 56 56

Proposed solution:

Add a filter using Object.prototype.hasOwnProperty to ensure only the object's own properties are processed when creating ParamType or invoking checkType. This will prevent inherited properties from being mistakenly copied.

Additional information:

In version 5, the similar issue also causes an error during RPC requests:

Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.7.0)

This occurs because the chainId cannot be retrieved due to an incorrect header object that includes a custom prototype property. Fixing the header iteration logic as described above will also resolve this issue. It can be fixed by

  • replacing number 1:
    for (var key of [...Object.getOwnPropertyNames(connection.headers), ...Object.getOwnPropertySymbols(connection.headers)]) {
    
    instead of the code at this line.
  • replacing number 2:
    Object.getOwnPropertyNames(object).forEach(key => { 
        result[key] = (object)[key]; 
    });
    Object.getOwnPropertySymbols(object).forEach(symbol => { 
        result[symbol] = (object)[symbol]; 
    });
    
    instead of the code at this line

Code Snippet

Object.prototype.customMethod = function () { return "custom"; };

const yourInfuraKey = process.env.INFURA_API_KEY || 'add your key here';
const provider = new ethers.JsonRpcProvider(`https://mainnet.infura.io/v3/${yourInfuraKey}`);

const abi = [{
   "inputs": [],
   "name": "decimals",
   "outputs": [
      {
         "internalType": "uint256",
         "name": "value",
         "type": "uint256"
      }
   ],
   "stateMutability": "view",
   "type": "function"
}];

const constractAddress = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
const usdcToken = new ethers.Contract(constractAddress, abi, provider);
await usdcToken.decimals();


### Contract ABI

_No response_

### Errors

```shell
TypeError: type.split is not a function

Environment

No response

Environment (Other)

No response

@zZoMROT zZoMROT added investigate Under investigation and may be a bug. v6 Issues regarding v6 labels Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigate Under investigation and may be a bug. v6 Issues regarding v6
Projects
None yet
Development

No branches or pull requests

2 participants