Skip to content

Commit 50c98ec

Browse files
committed
Stop using string interpolation on AA SQL
- Don't use SQL string interpolation for Account Abstraction query - It doesn't look like a SQL injection there could be possible, but just in case
1 parent 4de4f2c commit 50c98ec

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

safe_transaction_service/history/models.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
import json
23
from collections.abc import Iterator, Sequence
34
from decimal import Decimal
45
from enum import Enum
@@ -407,10 +408,12 @@ def account_abstraction_txs(self) -> RawQuerySet:
407408
"""
408409
:return: Transactions containing ERC4337 `UserOperation` event
409410
"""
410-
query = '{"topics": ["' + to_0x_hex_str(USER_OPERATION_EVENT_TOPIC) + '"]}'
411+
# Use json.dumps to safely construct the JSON query string
412+
query_json = json.dumps({"topics": [to_0x_hex_str(USER_OPERATION_EVENT_TOPIC)]})
411413

412414
return self.raw(
413-
f"SELECT * FROM history_ethereumtx WHERE '{query}'::jsonb <@ ANY (logs)"
415+
"SELECT * FROM history_ethereumtx WHERE %s::jsonb <@ ANY (logs)",
416+
[query_json],
414417
)
415418

416419

safe_transaction_service/history/tests/test_models.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,20 +350,26 @@ def test_create_from_tx_dict(self):
350350
ethereum_tx.transaction_index, tx_receipt["transactionIndex"]
351351
)
352352

353-
def test_account_abstraction_tx_hashes(self):
353+
def test_account_abstraction_txs(self):
354354
self.assertEqual(len(EthereumTx.objects.account_abstraction_txs()), 0)
355355

356356
# Insert random transaction
357357
EthereumTxFactory()
358358
self.assertEqual(len(EthereumTx.objects.account_abstraction_txs()), 0)
359359

360+
# Insert a non 4337 transaction
361+
_ethereum_tx = EthereumTxFactory(
362+
logs=[clean_receipt_log(log) for log in type_0_tx["receipt"]["logs"]]
363+
)
364+
self.assertEqual(len(EthereumTx.objects.account_abstraction_txs()), 0)
365+
360366
# Insert a 4337 transaction
361-
ethereum_tx = EthereumTxFactory(
367+
aa_ethereum_tx = EthereumTxFactory(
362368
logs=[clean_receipt_log(log) for log in aa_tx_receipt_mock["logs"]]
363369
)
364370
ethereum_txs = EthereumTx.objects.account_abstraction_txs()
365371
self.assertEqual(len(ethereum_txs), 1)
366-
self.assertEqual(ethereum_txs[0], ethereum_tx)
372+
self.assertEqual(ethereum_txs[0], aa_ethereum_tx)
367373

368374
def test_get_deployed_proxies_from_logs(self):
369375
ethereum_tx = EthereumTxFactory(

0 commit comments

Comments
 (0)