Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/governor-timelock-compound-duplicate-actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': minor
---

`GovernorTimelockCompound`: Reject proposals containing duplicate actions (same target, value, and calldata) at proposal creation time with a new `GovernorDuplicateProposalAction` error, rather than failing later during queueing.
6 changes: 6 additions & 0 deletions contracts/governance/IGovernor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ interface IGovernor is IERC165, IERC6372 {
*/
error GovernorInvalidSignature(address voter);

/**
* @dev The proposal contains duplicate actions (same target, value, and calldata). This is not supported
* by the Compound timelock, which identifies queued transactions by their hash.
*/
error GovernorDuplicateProposalAction(uint256 index);

/**
* @dev The given `account` is unable to cancel the proposal with given `proposalId`.
*/
Expand Down
30 changes: 30 additions & 0 deletions contracts/governance/extensions/GovernorTimelockCompound.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,36 @@ abstract contract GovernorTimelockCompound is Governor {
return true;
}

/**
* @dev Override of {Governor-_propose} that rejects proposals containing duplicate actions (same target, value,
* and calldata). The Compound timelock identifies queued transactions by their hash, so duplicate actions within
* a single proposal would cause the second `queueTransaction` call to fail. This check prevents such proposals
* from being created in the first place, providing a clearer error message.
*/
function _propose(
address[] memory targets,
uint256[] memory values,
bytes[] memory calldatas,
string memory description,
address proposer
) internal virtual override returns (uint256) {
if (targets.length != values.length || targets.length != calldatas.length || targets.length == 0) {
revert GovernorInvalidProposalLength(targets.length, calldatas.length, values.length);
}
for (uint256 i = 1; i < targets.length; ++i) {
for (uint256 j = 0; j < i; ++j) {
if (
targets[i] == targets[j] &&
values[i] == values[j] &&
keccak256(calldatas[i]) == keccak256(calldatas[j])
) {
revert GovernorDuplicateProposalAction(i);
}
}
}
return super._propose(targets, values, calldatas, description, proposer);
Comment on lines +77 to +88
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Preserve canonical proposal-length validation before duplicate scan.

At Line 74, the nested loop indexes values[i]/calldatas[i] using targets.length. With malformed inputs, this can panic (out-of-bounds) before reaching Governor._propose’s GovernorInvalidProposalLength revert.

Proposed fix
 function _propose(
     address[] memory targets,
     uint256[] memory values,
     bytes[] memory calldatas,
     string memory description,
     address proposer
 ) internal virtual override returns (uint256) {
+    if (targets.length != values.length || targets.length != calldatas.length || targets.length == 0) {
+        revert GovernorInvalidProposalLength(targets.length, calldatas.length, values.length);
+    }
+
     for (uint256 i = 1; i < targets.length; ++i) {
         for (uint256 j = 0; j < i; ++j) {
             if (
                 targets[i] == targets[j] &&
                 values[i] == values[j] &&
                 keccak256(calldatas[i]) == keccak256(calldatas[j])
             ) {
                 revert GovernorDuplicateProposalAction(i);
             }
         }
     }
     return super._propose(targets, values, calldatas, description, proposer);
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@contracts/governance/extensions/GovernorTimelockCompound.sol` around lines 74
- 85, The nested duplicate-scan in GovernorTimelockCompound._propose indexes
values[i] and calldatas[i] assuming all arrays match targets.length, which can
cause out-of-bounds before the canonical GovernorInvalidProposalLength check;
ensure you validate that values.length == targets.length and calldatas.length ==
targets.length (or call the parent length-check) before running the duplicate
loops so the existing GovernorInvalidProposalLength revert fires first; update
_propose to perform the length validation up-front (or delegate to
super._propose length checks) and keep the duplicate detection afterwards, using
the existing GovernorDuplicateProposalAction and GovernorInvalidProposalLength
symbols to guide behavior.

}

/**
* @dev Function to queue a proposal to the timelock.
*/
Expand Down
10 changes: 10 additions & 0 deletions contracts/mocks/governance/GovernorTimelockCompoundMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ abstract contract GovernorTimelockCompoundMock is
return super._cancel(targets, values, calldatas, descriptionHash);
}

function _propose(
address[] memory targets,
uint256[] memory values,
bytes[] memory calldatas,
string memory description,
address proposer
) internal override(Governor, GovernorTimelockCompound) returns (uint256) {
return super._propose(targets, values, calldatas, description, proposer);
}

function _executor() internal view override(Governor, GovernorTimelockCompound) returns (address) {
return super._executor();
}
Expand Down
51 changes: 40 additions & 11 deletions test/governance/extensions/GovernorTimelockCompound.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,47 @@ describe('GovernorTimelockCompound', function () {
target: this.token.target,
data: this.token.interface.encodeFunctionData('approve', [this.receiver.target, ethers.MaxUint256]),
};
const { id } = this.helper.setProposal([action, action], '<proposal description>');
this.helper.setProposal([action, action], '<proposal description>');

await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.connect(this.voter1).vote({ support: VoteType.For });
await this.helper.waitForDeadline();
await expect(this.helper.queue())
.to.be.revertedWithCustomError(this.mock, 'GovernorAlreadyQueuedProposal')
.withArgs(id);
await expect(this.helper.execute())
.to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
.withArgs(id, ProposalState.Succeeded, GovernorHelper.proposalStatesToBitMap([ProposalState.Queued]));
await expect(this.helper.propose())
.to.be.revertedWithCustomError(this.mock, 'GovernorDuplicateProposalAction')
.withArgs(1n);
});

it('if proposal is empty', async function () {
this.helper.setProposal([], '<proposal description>');

await expect(this.helper.propose())
.to.be.revertedWithCustomError(this.mock, 'GovernorInvalidProposalLength')
.withArgs(0n, 0n, 0n);
});

it('if targets/values lengths mismatch', async function () {
this.helper.setProposal(
{
targets: [this.receiver.target],
values: [],
data: [this.receiver.interface.encodeFunctionData('mockFunction')],
},
'<proposal description>',
);
await expect(this.helper.propose())
.to.be.revertedWithCustomError(this.mock, 'GovernorInvalidProposalLength')
.withArgs(1n, 1n, 0n);
});

it('if targets/calldatas lengths mismatch', async function () {
this.helper.setProposal(
{
targets: [this.receiver.target],
values: [0n],
data: [],
},
'<proposal description>',
);
await expect(this.helper.propose())
.to.be.revertedWithCustomError(this.mock, 'GovernorInvalidProposalLength')
.withArgs(1n, 0n, 1n);
});
});

Expand Down