Skip to content

Commit

Permalink
Merge pull request #164 from Bananapus/fix/metadata-rounding
Browse files Browse the repository at this point in the history
metadata rounding fix- audit finding 19
  • Loading branch information
mejango authored Jun 30, 2024
2 parents e003ca2 + 51a9900 commit 668b4ad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/libraries/JBMetadataResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ library JBMetadataResolver {
// For each id, add it to the lookup table with the next free offset, then increment the offset by the data
// length (rounded up)
for (uint256 _i; _i < _ids.length; ++_i) {
if (_datas[_i].length < 32) revert DATA_NOT_PADDED();
if (_datas[_i].length < 32 || _datas[_i].length % JBMetadataResolver.WORD_SIZE != 0) {
revert DATA_NOT_PADDED();
}

metadata = abi.encodePacked(metadata, _ids[_i], bytes1(uint8(_offset)));
_offset += _datas[_i].length / JBMetadataResolver.WORD_SIZE;
Expand Down
24 changes: 24 additions & 0 deletions test/TestMetadataParserLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,28 @@ contract JBDelegateMetadataLib_Test_Local is Test {
vm.expectRevert(abi.encodeWithSignature("DATA_NOT_PADDED()"));
parser.createMetadata(_ids, _datas);
}

/**
* @notice Test creating and parsing bytes only metadata.
*/
function test_create_incorrect_data_length() external {
bytes4[] memory _ids = new bytes4[](10);
bytes[] memory _datas = new bytes[](10);

for (uint256 _i; _i < _ids.length; _i++) {
_ids[_i] = bytes4(uint32(_i + 1 * 1000));

_datas[_i] = abi.encodePacked(
bytes1(uint8(_i + 1)), uint32(69), bytes2(uint16(_i + 69)), bytes32(uint256(type(uint256).max))
);
}

// Ensure data length is not multiple of word size and is gt word size.
assertGt(_datas[0].length, 32);
assertGt(_datas[0].length % 32, 0);

// New revert to help integrators.
vm.expectRevert(abi.encodeWithSignature("DATA_NOT_PADDED()"));
bytes memory _metadata = parser.createMetadata(_ids, _datas);
}
}

0 comments on commit 668b4ad

Please sign in to comment.