Download audit report
This document outlines the security review conducted by Nethermind Security for the Bebop JAM (just-in-time aggregation model) contracts, a liquidity aggregator system allowing any token to any token swaps with efficient execution quality. Additionally, an interaction flow has been added to allow Permit2 to work with the existing BebopBlend smart contracts.
The audited code comprises of 1567 lines of code written in the Solidity language, and the audit was performed using (a) manual analysis of the codebase, (b) automated analysis tools, (c) simulation of the smart contract. At the end of the engagement after all fixes were applied, the class hashes for the base account and full account implementations are listed below:
| Audit assessment | |
| Audit Type | Security Review |
| Initial Report | December 5, 2024 |
| Final Report | December 9, 2024 |
| Methods | Manual Review, Automated analysis |
| Repository | bebop-dex/bebop-jam-contracts |
| Commit Hash | 4ab5d579675e28cad503edd58a13e8d3c19833be |
| Final Commit Hash | b7043731d8699d34118a54e44b7ba5f54168e808 |
| Documentation Assessment | High |
| Test Suite Assessment | High |
Findings by severity
| # | Contract | LoC | Comments | Ratio | Blank | Total |
| 1 | src/JamSettlement.sol | 195 | 10 | 5.1% | 14 | 219 |
| 2 | src/JamBalanceManager.sol | 101 | 11 | 10.9% | 12 | 124 |
| 3 | src/libraries/BlendMultiOrder.sol | 60 | 9 | 15.0% | 11 | 80 |
| 4 | src/libraries/BlendAggregateOrder.sol | 125 | 16 | 12.8% | 12 | 153 |
| 5 | src/libraries/JamInteraction.sol | 29 | 2 | 6.9% | 5 | 36 |
| 6 | src/libraries/JamOrder.sol | 52 | 6 | 11.5% | 11 | 69 |
| 7 | src/libraries/JamHooks.sol | 15 | 4 | 26.7% | 5 | 24 |
| 8 | src/libraries/BlendSingleOrder.sol | 36 | 7 | 19.4% | 8 | 51 |
| 9 | src/base/JamTransfer.sol | 66 | 23 | 34.8% | 7 | 96 |
| 10 | src/base/JamPartner.sol | 73 | 26 | 35.6% | 11 | 110 |
| 11 | src/base/JamValidation.sol | 127 | 41 | 32.3% | 16 | 184 |
| 12 | src/base/Errors.sol | 27 | 27 | 100.0% | 27 | 81 |
| 13 | src/interfaces/IBebopBlend.sol | 51 | 28 | 54.9% | 11 | 90 |
| 14 | src/interfaces/IJamBalanceManager.sol | 41 | 32 | 78.0% | 8 | 81 |
| 15 | src/interfaces/IJamSettlement.sol | 44 | 30 | 68.2% | 10 | 84 |
| 16 | src/interfaces/IPermit2.sol | 37 | 38 | 102.7% | 10 | 85 |
| Total | 1079 | 310 | 28.7% | 178 | 1567 |
| # | Finding | Severity | Update |
| 1 | Explicitly specifying partnerInfo with no fees causes transaction to revert | Info | Fixed |
| 2 | Fee distribution between protocol and partner can differ in batchSettle | Info | Acknowledged |
| 3 | Missing EIP-5267 | Info | Fixed |
Bebop JAM is a Just-in-Time aggregation model that allows independent solvers to compete for user orders to offer the best execution outcome. Once user accepts the solver’s offer onchain, the solver is committed to deliver the execution providing accurate or better prices at superior execution cost.
On receiving order request from the user, Bebop JAM orchestrator will query solvers to provide the best solution for the order. User can accepts the solver’s proposal by signing to confirm the trade. The orchestrator returns the accepted order to the winning solver for execution on-chain to complete the transaction.
The solvers can fulfill the order by interacting with external protocols or providing the liquidity directly. Bebop JAM offers an entry point to interact with BebopBlend contract using Permit2+witness functionality using single, multi and aggregate orders.
JamSettlement: The primary contract through which users interact to execute and settle orders. The contract supports four entrypoints, allowing for basic settlements, direct asset swap settlements, batch settlements and retroactive Permit2 support for BebopBlend. This contract inherits and/or utilizes the following:
JamBalanceManager: All asset movement flows through this contract. Permit2 allowances specified by users are given to this address, as well as normal allowances. The exposed functions on this contract are only callable by the JamSettlement contract to ensure that funds cannot be accessed by any other contract.
BebopBlend: An exising Bebop token swap protocol, which has been integrated with JamSettlement to support Permit2 allowances.
This section aims to highlight observations made by the Nethermind team during the audit process that don’t have any security implication and/or don’t neccesarily need to be addressed, but should be noted nonetheless:
4.1.1 Nonces are shared between JamSettlement orders and Permit2
The JamOrder struct contains a field nonce which is used to ensure unique signatures across JamSettlement orders. This nonce that is used for the order is also the same nonce that is passed through to Permit2 for asset transfers. By having this nonce shared between JamSettlement and Permit2 some interesting and undocumented constraints are applied to order nonces.
On the JamSettlement contract there are two separate nonce invalidation storage maps, one for regular orders and one for limit orders. This effectively makes for three separate nonce management systems (regular order, limit order, Permit2) that must be satisfied when an order is processed. The off-chain logic that determines which nonce to use must take into account which nonces are unused in all three of these in order to generate a valid order.
Consider a regular order with a nonce of 15 and it requires a Permit2 asset transfer. This order is executed and the nonce of 15 is consumed both on the regular order nonce storage and the Permit2 nonce storage. Now the user would like to make a new order, this time being a limit order. The user intends to use nonce 15 which is unused on the limit order nonce storage (as it was only used on the regular order nonce storage). This would appear to be valid, but when execution reaches the Permit2 contract the nonce of 15 will have already been used and the transaction will revert.
Another case to consider is the user interacting with other protocols, and a given Permit2 nonce may have been used already. Even if an order is made where the nonce is unused both on the regular order and limit order nonce storage, it may still revert if it has been used on Permit2 by another protocol.
The risk rating methodology used by Nethermind Security follows the principles established by the OWASP Foundation. The severity of each finding is determined by two factors: Likelihood and Impact.
Likelihood measures how likely the finding is to be uncovered and exploited by an attacker. This factor will be one of the following values:
When defining the likelihood of a finding, other factors are also considered. These can include but are not limited to motive, opportunity, exploit accessibility, ease of discovery, and ease of exploit.
Impact is a measure of the damage that may be caused if an attacker exploits the finding. This factor will be one of the following values:
When defining the impact of a finding, other factors are also considered. These can include but are not limited to data and state integrity, loss of availability, financial loss, and reputation damage. After defining the likelihood and impact of an issue, the severity is determined according to the table below.
| Impact / Likelihood | Low | Medium | High |
| High | Medium | High | Critical |
| Medium | Low | Medium | High |
| Low | Info / Best Practices | Low | Medium |
| Undetermined | Undetermined | Undetermined | Undetermined |
To address issues that do not fit a High, Medium or Low severity, Nethermind Security also uses three more finding severities: Informational, Best Practices, and Undetermined.
partnerInfo with no fees causes transaction to revert| File | Fix commit | Type | Status |
JamPartner.sol | c4ad578 | Info | Fixed |
In the function settleInternal, the field partnerInfo in the struct order is used to specify the fees that should be applied to the order. It is a uint256 which contains the address of the partner which will recieve fees, as well as the protocol fee amount and partner fee amount in basis points. During an internal settlement, if the partnerInfo field is empty then no fees are applied, otherwise a separate logic branch for handling fees is entered, as shown below:
function settleInternal(...) external payable nonReentrant {
// ...
if (order.partnerInfo == 0){
uint256[] calldata buyAmounts = validateFilledAmounts(filledAmounts, order.buyAmounts);
balanceManager.transferTokens(order.buyTokens, buyAmounts, msg.sender, order.receiver);
// ...
} else {
balanceManager.transferTokens(order.buyTokens, buyAmounts, msg.sender, order.receiver);
if (protocolFees.length != 0){
balanceManager.transferTokens(order.buyTokens, protocolFees, msg.sender, protocolFeeAddress);
}
if (partnerFees.length != 0){
balanceManager.transferTokens(order.buyTokens, partnerFees, msg.sender, partner);
}
}
// ...
}However, it is possible to provide a partnerInfo field that specifies the partner address, but the fee amounts for the partner and protocol are still zero. Because the partner address is specified, partnerInfo field is nonzero so the fee branch is entered and the function getUpdatedAmountsAndFees is called. This function will create an array newAmounts which will contain the amounts that the user will recieve after fees. There are two if statements which only execute if the relevant fee type is nonzero, and they calculate the new user amount after these fees are subtracted, as shown below:
function getUpdatedAmountsAndFees(...) internal pure returns (...) {
(address partnerAddress, uint16 partnerFee, uint16 protocolFee) = unpackPartnerInfo(partnerInfo);
// ...
newAmounts = new uint256[](tokensLength);
if (protocolFee > 0) {
protocolFees = new uint256[](tokensLength);
for (uint256 i; i < tokensLength; ++i) {
protocolFees[i] = amounts[i] * protocolFee / HUNDRED_PERCENT;
newAmounts[i] = amounts[i] - protocolFees[i];
}
}
if (partnerFee > 0) {
partnerFees = new uint256[](tokensLength);
for (uint256 i; i < tokensLength; ++i) {
partnerFees[i] = amounts[i] * partnerFee / HUNDRED_PERCENT;
newAmounts[i] = newAmounts[i] == 0 ? amounts[i] - partnerFees[i] : newAmounts[i] - partnerFees[i];
}
}
for (uint256 i; i < tokensLength; ++i) {
require(newAmounts[i] >= minAmounts[i], InvalidFilledAmounts(minAmounts[i], newAmounts[i]));
}
// ...When both fees are zero, none of these if statements will execute, and the array newAmounts will never be written to, meaning all values in the array will be zero. At the end of the function a final validation is done to ensure that the newAmounts entries are not less than the minAmounts. In the case of both fees being zero however, each entry in newAmounts is zero, causing the transaction to revert on the require(newAmounts[i] >= minAmounts[i]) comparison.
Recommendation
Consider updating the logic in the function getUpdatedAmountsAndFees to return the correct transferable amounts when both fees are zero.
Resolution
Added verification to ensure that if partnerFee is zero, then partnerAddress is zero as well. Addressed in commit c4ad578.
batchSettle| File | Fix commit | Type | Status |
JamTransfer.sol | — | Info | Acknowledged |
In the function batchSettle after the interactions have executed, the new buy amounts for each order are calculated with fees being taken into account, before being transferred to the order recipient. The calculations are done in calculateNewAmounts, which includes a validation to ensure that that the total fees in the partnerInfo field are consistent between all orders in the batch. A snippet from the function is shown below:
function calculateNewAmounts(...) internal view returns (...) {
JamOrder calldata curOrder = orders[curInd];
// ...
for (uint i; i < curOrder.buyTokens.length; ++i) {
// ...
for (uint j = curInd; j < orders.length; ++j) {
for (uint k; k < orders[j].buyTokens.length; ++k) {
if (orders[j].buyTokens[k] == curOrder.buyTokens[i]) {
// ...
// The total fees specified in `partnerInfo` must be same for all in batch
require(
getTotalFeesBps(curOrder.partnerInfo) == getTotalFeesBps(orders[j].partnerInfo),
DifferentFeesInBatch()
);
}
}
}
// ...
}
// ...
}This check and the associated error message imply that the exact same fees must be applied between all orders in the batch. However, only the total fees for each order is compared. It is possible change the fee distribution between the protocol and partner, as long as the total amounts to the same consistent value across all orders. Also, the partnerAddress can be changed and this will not be detected either. Consider the following example:
OrderA{
protocolFee: 10, partnerFee: 15, partnerAddress: 0xabcd
}
OrderB{
protocolFee: 15, partnerFee: 10, partnerAddress: 0xabcd
}
OrderC{
protocolFee: 5, partnerFee: 25, partnerAddress: 0x1234
}Recommendation
Consider adding a check to ensure that the fee distribution consistent between all orders.
Resolution
Only the total fees are required to be equal, how the fees are distributed is not a concern.
| File | Fix commit | Type | Status |
JamValidation.sol | ef58b97 | Info | Fixed |
The contract JAMSettlement allows solvers to execute orders on behalf of users by implementing the EIP-712 specification, where the user authorizes this by signing some structured data. Rather than importing EIP-712 from a stardard contract library such as Openzeppelin a custom implementation has been written instead. A difference between this custom implementation and the Openzeppelin implementation is the absence of EIP-5267. This EIP allows applications to retrieve and generate appropriate domain separators, helping to integrate EIP-712 signatures securely and scalably, and is commonly included alongside EIP-712.
Recommendation
Consider adding additional custom logic to support EIP-5267 or alternatively importing EIP-712 logic from the Openzeppelin contract library which already includes EIP-5267.
Resolution
Added eip712Domain function to support ERC5267. Addressed in commit ef58b97.
Software documentation refers to the written or visual information that describes the functionality, architecture, design, and implementation of software. It provides a comprehensive overview of the software system and helps users, developers, and stakeholders understand how the software works, how to use it, and how to maintain it. Software documentation can take different forms, such as user manuals, system manuals, technical specifications, requirements documents, design documents, and code comments. Software documentation is critical in software development, enabling effective communication between developers, testers, users, and other stakeholders. It helps to ensure that everyone involved in the development process has a shared understanding of the software system and its functionality. Moreover, software documentation can improve software maintenance by providing a clear and complete understanding of the software system, making it easier for developers to maintain, modify, and update the software over time. Smart contracts can use various types of software documentation. Some of the most common types include:
These types of documentation are essential for smart contract development and maintenance. They help ensure that the contract is properly designed, implemented, and tested, and they provide a reference for developers who need to modify or maintain the contract in the future.
The documentation for the Bebop Jam contracts is contained in the README of the project’s Github. It provides information on the following:
Order struct)The information is presented in a very concise and technical manner, with well-written explanations and references where necessary. It also features a section explaining development dependencies and instructions on how to build and test the code.
Code comments are also of high quality, with explanations for some functions describing the purpose, context, and situations where the function will be called. Other than functions, some comments exist in other areas of the code where extra information is necessary, allowing readers to understand the codebase at a faster pace.
| 6Tests passed | 0Failed |
The complete test output is in the full report.
As auditors, we recommend that any changes or updates made to the audited codebase undergo a re-audit or security review to address potential vulnerabilities or risks introduced by the modifications. By conducting a re-audit or security review of the modified codebase, you can significantly enhance the overall security of your system and reduce the likelihood of exploitation. However, we do not possess the authority or right to impose obligations or restrictions on our clients regarding codebase updates, modifications, or subsequent audits. Accordingly, the decision to seek a re-audit or security review lies solely with you.
This report is based on the scope of materials and documentation provided by you to Nethermind in order that Nethermind could conduct the security review outlined in 1. Executive Summary and 2. Audited Files. The results set out in this report may not be complete nor inclusive of all vulnerabilities. Nethermind has provided the review and this report on an as-is, where-is, and as-available basis. You agree that your access and/or use, including but not limited to any associated services, products, protocols, platforms, content, and materials, will be at your sole risk. Blockchain technology remains under development and is subject to unknown risks and flaws. The review does not extend to the compiler layer, or any other areas beyond the programming language, or other programming aspects that could present security risks. This report does not indicate the endorsement of any particular project or team, nor guarantee its security. No third party should rely on this report in any way, including for the purpose of making any decisions to buy or sell a product, service or any other asset. To the fullest extent permitted by law, Nethermind disclaims any liability in connection with this report, its content, and any related services and products and your use thereof, including, without limitation, the implied warranties of merchantability, fitness for a particular purpose, and non-infringement. Nethermind does not warrant, endorse, guarantee, or assume responsibility for any product or service advertised or offered by a third party through the product, any open source or third-party software, code, libraries, materials, or information linked to, called by, referenced by or accessible through the report, its content, and the related services and products, any hyperlinked websites, any websites or mobile applications appearing on any advertising, and Nethermind will not be a party to or in any way be responsible for monitoring any transaction between you and any third-party providers of products or services. As with the purchase or use of a product or service through any medium or in any environment, you should use your best judgment and exercise caution where appropriate. FOR AVOIDANCE OF DOUBT, THE REPORT, ITS CONTENT, ACCESS, AND/OR USAGE THEREOF, INCLUDING ANY ASSOCIATED SERVICES OR MATERIALS, SHALL NOT BE CONSIDERED OR RELIED UPON AS ANY FORM OF FINANCIAL, INVESTMENT, TAX, LEGAL, REGULATORY, OR OTHER ADVICE.
The Leading Engineers of Blockchain Infrastructure