-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add check to ensure
initialOwner
is not a ProxyAdmin contract when …
…deploying a transparent proxy (#1083)
- Loading branch information
Showing
13 changed files
with
163 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import test from 'ava'; | ||
import { EthereumProvider } from './provider'; | ||
import { inferProxyAdmin } from './infer-proxy-admin'; | ||
|
||
const addr = '0x123'; | ||
|
||
function makeProviderReturning(result: unknown): EthereumProvider { | ||
return { send: (_method: string, _params: unknown[]) => Promise.resolve(result) } as EthereumProvider; | ||
} | ||
|
||
function makeProviderError(msg: string): EthereumProvider { | ||
return { | ||
send: (_method: string, _params: unknown[]) => { | ||
throw new Error(msg); | ||
}, | ||
} as EthereumProvider; | ||
} | ||
|
||
test('inferProxyAdmin returns true when owner looks like an address', async t => { | ||
// abi encoding of address 0x1000000000000000000000000000000000000123 | ||
const provider = makeProviderReturning('0x0000000000000000000000001000000000000000000000000000000000000123'); | ||
t.true(await inferProxyAdmin(provider, addr)); | ||
}); | ||
|
||
test('inferProxyAdmin returns false when returned value is 32 bytes but clearly not an address', async t => { | ||
// dirty upper bits beyond 20 bytes (the 'abc' in the below) | ||
const provider = makeProviderReturning('0x000000000000000000000abc1000000000000000000000000000000000000123'); | ||
t.false(await inferProxyAdmin(provider, addr)); | ||
}); | ||
|
||
test('inferProxyAdmin returns false when returned value is a string', async t => { | ||
// abi encoding of string 'foo' | ||
const provider = makeProviderReturning( | ||
'0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003666f6f0000000000000000000000000000000000000000000000000000000000', | ||
); | ||
t.false(await inferProxyAdmin(provider, addr)); | ||
}); | ||
|
||
test('inferProxyAdmin throws unrelated error', async t => { | ||
const provider = makeProviderError('unrelated error'); | ||
await t.throwsAsync(() => inferProxyAdmin(provider, addr), { message: 'unrelated error' }); | ||
}); | ||
|
||
test('inferProxyAdmin returns false for invalid selector', async t => { | ||
const provider = makeProviderError( | ||
`Transaction reverted: function selector was not recognized and there's no fallback function`, | ||
); | ||
t.false(await inferProxyAdmin(provider, addr)); | ||
}); |
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,14 @@ | ||
import { callOptionalSignature } from './call-optional-signature'; | ||
import { EthereumProvider } from './provider'; | ||
import { parseAddress } from './utils/address'; | ||
|
||
/** | ||
* Infers whether the address might be a ProxyAdmin contract, by checking if it has an owner() function that returns an address. | ||
* @param provider Ethereum provider | ||
* @param possibleContractAddress The address to check | ||
* @returns true if the address might be a ProxyAdmin contract, false otherwise | ||
*/ | ||
export async function inferProxyAdmin(provider: EthereumProvider, possibleContractAddress: string): Promise<boolean> { | ||
const owner = await callOptionalSignature(provider, possibleContractAddress, 'owner()'); | ||
return owner !== undefined && parseAddress(owner) !== undefined; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; | ||
import {ITransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol"; | ||
import {ProxyAdmin} from "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol"; | ||
|
||
/** | ||
* This contract is for testing only. | ||
* Basic but pointless contract that has its own owner and can call ProxyAdmin functions. | ||
*/ | ||
contract HasOwner is Ownable { | ||
constructor(address initialOwner) Ownable(initialOwner) {} | ||
|
||
function upgradeAndCall( | ||
ProxyAdmin admin, | ||
ITransparentUpgradeableProxy proxy, | ||
address implementation, | ||
bytes memory data | ||
) public payable virtual onlyOwner { | ||
admin.upgradeAndCall{value: msg.value}(proxy, implementation, data); | ||
} | ||
} |
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