-
Notifications
You must be signed in to change notification settings - Fork 266
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ The following options are common to some functions. | |
* `constructorArgs`: (`unknown[]`) Provide arguments for the constructor of the implementation contract. Note that these are different from initializer arguments, and will be used in the deployment of the implementation contract itself. Can be used to initialize immutable variables. | ||
* `initialOwner`: (`string`) the address to set as the initial owner of a transparent proxy's admin or initial owner of a beacon. Defaults to the externally owned account that is deploying the transparent proxy or beacon. Not supported for UUPS proxies. | ||
** *Since:* `@openzeppelin/[email protected]` | ||
* `unsafeSkipProxyAdminCheck`: (`boolean`) Skips checking the `initialOwner` option when deploying a transparent proxy. When deploying a transparent proxy, the `initialOwner` must be the address of an EOA or a contract that can call functions on a ProxyAdmin. It must not be a ProxyAdmin contract itself. Use this if you encounter an error due to this check and are sure that the `initialOwner` is not a ProxyAdmin contract. | ||
** *Since:* `@openzeppelin/[email protected]` | ||
* `timeout`: (`number`) Timeout in milliseconds to wait for the transaction confirmation when deploying an implementation contract. Defaults to `60000`. Use `0` to wait indefinitely. | ||
* `pollingInterval`: (`number`) Polling interval in milliseconds between checks for the transaction confirmation when deploying an implementation contract. Defaults to `5000`. | ||
* `redeployImplementation`: (`"always" | "never" | "onchange"`) Determines whether the implementation contract will be redeployed. Defaults to `"onchange"`. | ||
|
@@ -56,6 +58,7 @@ async function deployProxy( | |
unsafeAllow?: ValidationError[], | ||
constructorArgs?: unknown[], | ||
initialOwner?: string, | ||
unsafeSkipProxyAdminCheck?: boolean, | ||
timeout?: number, | ||
pollingInterval?: number, | ||
redeployImplementation?: 'always' | 'never' | 'onchange', | ||
|
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