Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix L-08
  • Loading branch information
ernestognw committed Aug 2, 2023
commit a94adcd82c8c58e02af6778e08d22a6221a89546
27 changes: 27 additions & 0 deletions contracts/mocks/UpgradeableBeaconMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {IBeacon} from "../proxy/beacon/IBeacon.sol";

contract UpgradeableBeaconMock is IBeacon {
address public implementation;

constructor(address impl) {
implementation = impl;
}
}

interface IProxyExposed {
// solhint-disable-next-line func-name-mixedcase
function $getBeacon() external view returns (address);
}

contract UpgradeableBeaconReentrantMock is IBeacon {
error BeaconProxyBeaconSlotAddress(address beacon);

function implementation() external view override returns (address) {
// Revert with the beacon seen in the proxy at the moment of calling to check if it's
// set before the call.
revert BeaconProxyBeaconSlotAddress(IProxyExposed(msg.sender).$getBeacon());
}
}
12 changes: 0 additions & 12 deletions contracts/mocks/UpgreadeableBeaconMock.sol

This file was deleted.

4 changes: 2 additions & 2 deletions contracts/proxy/ERC1967/ERC1967Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ library ERC1967Utils {
revert ERC1967InvalidBeacon(newBeacon);
}

StorageSlot.getAddressSlot(BEACON_SLOT).value = newBeacon;

address beaconImplementation = IBeacon(newBeacon).implementation();
if (beaconImplementation.code.length == 0) {
revert ERC1967InvalidImplementation(beaconImplementation);
}

StorageSlot.getAddressSlot(BEACON_SLOT).value = newBeacon;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions test/proxy/ERC1967/ERC1967Utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ERC1967Utils = artifacts.require('$ERC1967Utils');
const V1 = artifacts.require('DummyImplementation');
const V2 = artifacts.require('CallReceiverMock');
const UpgradeableBeaconMock = artifacts.require('UpgradeableBeaconMock');
const UpgradeableBeaconReentrantMock = artifacts.require('UpgradeableBeaconReentrantMock');

contract('ERC1967Utils', function (accounts) {
const [, admin, anotherAccount] = accounts;
Expand Down Expand Up @@ -155,6 +156,17 @@ contract('ERC1967Utils', function (accounts) {
await expectEvent.inTransaction(receipt.tx, await V2.at(this.utils.address), 'MockFunctionCalled');
});
});

describe('reentrant beacon implementation() call', function () {
it('sees the new beacon implementation', async function () {
const newBeacon = await UpgradeableBeaconReentrantMock.new();
await expectRevertCustomError(
this.utils.$upgradeBeaconToAndCall(newBeacon.address, '0x'),
'BeaconProxyBeaconSlotAddress',
[newBeacon.address],
);
});
});
});
});
});