-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider: reject toxic transaction requests
Fixes 0xsequence/issue-tracker#629.
- Loading branch information
Showing
3 changed files
with
185 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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { commons } from '@0xsequence/core' | ||
import { ethers } from 'ethers' | ||
|
||
export function validateTransactionRequest(wallet: string, transaction: commons.transaction.Transactionish) { | ||
const transactions = commons.transaction.fromTransactionish(wallet, transaction) | ||
const unwound = commons.transaction.unwind(wallet, transactions) | ||
unwound.forEach(transaction => validateTransaction(wallet, transaction)) | ||
} | ||
|
||
function validateTransaction(wallet: string, transaction: commons.transaction.Transaction) { | ||
if (transaction.to.toLowerCase() === wallet.toLowerCase()) { | ||
if (transaction.data) { | ||
const data = ethers.utils.arrayify(transaction.data) | ||
if (data.length >= 4) { | ||
const selector = ethers.utils.hexlify(data.slice(0, 4)) | ||
switch (selector) { | ||
case '0x7a9a1628': // execute((bool,bool,uint256,address,uint256,bytes)[],uint256,bytes) | ||
case '0x61c2926c': // selfExecute((bool,bool,uint256,address,uint256,bytes)[]) | ||
break | ||
default: | ||
throw new Error('self calls other than execute and selfExecute are forbidden') | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (transaction.delegateCall) { | ||
throw new Error('delegate calls are forbidden') | ||
} | ||
} |
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,146 @@ | ||
import { commons } from '@0xsequence/core' | ||
import { expect } from 'chai' | ||
import { validateTransactionRequest } from '../src/transactions' | ||
|
||
const self = '0x5e1f5e1f5e1f5e1f5e1f5e1f5e1f5e1f5e1f5e1f' | ||
const to = '0x0123456789012345678901234567890123456789' | ||
|
||
describe('validating transaction requests', () => { | ||
it('should throw an error when a transaction does a self call', () => { | ||
const transaction = { | ||
to, | ||
data: commons.transaction.encodeBundleExecData({ | ||
entrypoint: to, | ||
transactions: [ | ||
{ | ||
to: self, | ||
data: '0x12345678' | ||
} | ||
] | ||
}) | ||
} | ||
|
||
expect(() => validateTransactionRequest(self, transaction)).to.throw() | ||
}) | ||
|
||
it('should throw an error when a transaction does a deep self call', () => { | ||
const transaction = { | ||
to, | ||
data: commons.transaction.encodeBundleExecData({ | ||
entrypoint: to, | ||
transactions: [ | ||
{ | ||
to: self, | ||
data: commons.transaction.encodeBundleExecData({ | ||
entrypoint: self, | ||
transactions: [ | ||
{ | ||
to: self, | ||
data: '0x12345678' | ||
} | ||
] | ||
}) | ||
} | ||
] | ||
}) | ||
} | ||
|
||
expect(() => validateTransactionRequest(self, transaction)).to.throw() | ||
}) | ||
|
||
it('should throw an error when a transaction does a delegate call', () => { | ||
const transaction = { | ||
to, | ||
data: commons.transaction.encodeBundleExecData({ | ||
entrypoint: to, | ||
transactions: [ | ||
{ | ||
to, | ||
delegateCall: true | ||
} | ||
] | ||
}) | ||
} | ||
|
||
expect(() => validateTransactionRequest(self, transaction)).to.throw() | ||
}) | ||
|
||
it('should throw an error when a transaction does a deep delegate call', () => { | ||
const transaction = { | ||
to, | ||
data: commons.transaction.encodeBundleExecData({ | ||
entrypoint: to, | ||
transactions: [ | ||
{ | ||
to: self, | ||
data: commons.transaction.encodeBundleExecData({ | ||
entrypoint: self, | ||
transactions: [ | ||
{ | ||
to: self, | ||
delegateCall: true | ||
} | ||
] | ||
}) | ||
} | ||
] | ||
}) | ||
} | ||
|
||
expect(() => validateTransactionRequest(self, transaction)).to.throw() | ||
}) | ||
|
||
it('should succeed when a transaction does only a deep execute call', () => { | ||
const transaction = { | ||
to, | ||
data: commons.transaction.encodeBundleExecData({ | ||
entrypoint: to, | ||
transactions: [ | ||
{ | ||
to: self, | ||
data: commons.transaction.encodeBundleExecData({ | ||
entrypoint: self, | ||
transactions: [ | ||
{ | ||
to: self, | ||
value: '1000000000000000000' | ||
} | ||
] | ||
}) | ||
} | ||
] | ||
}) | ||
} | ||
|
||
expect(() => validateTransactionRequest(self, transaction)).to.not.throw() | ||
}) | ||
|
||
it('should not throw an error in general', () => { | ||
const transaction = { | ||
to, | ||
data: commons.transaction.encodeBundleExecData({ | ||
entrypoint: to, | ||
transactions: [ | ||
{ | ||
to: self, // self without data is ok | ||
value: '1000000000000000000' | ||
}, | ||
{ | ||
to: self, // execute is ok | ||
data: commons.transaction.encodeBundleExecData({ | ||
entrypoint: self, | ||
transactions: [ | ||
{ | ||
to, | ||
data: '0x12345678' | ||
} | ||
] | ||
}) | ||
} | ||
] | ||
}) | ||
} | ||
|
||
expect(() => validateTransactionRequest(self, transaction)).to.not.throw() | ||
}) | ||
}) |