Skip to content

Commit

Permalink
Validate synapseTxId (#3317)
Browse files Browse the repository at this point in the history
  • Loading branch information
abtestingalpha authored Oct 22, 2024
1 parent 0651118 commit e4c67ec
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ export const bridgeTxStatusController = async (req, res) => {
try {
const { destChainId, bridgeModule, synapseTxId } = req.query

const txIdWith0x = !synapseTxId.startsWith('0x')
? `0x${synapseTxId}`
: synapseTxId

const status = await Synapse.getBridgeTxStatus(
Number(destChainId),
bridgeModule,
synapseTxId
txIdWith0x
)

if (status) {
Expand Down
6 changes: 5 additions & 1 deletion packages/rest-api/src/routes/bridgeTxStatusRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { showFirstValidationError } from '../middleware/showFirstValidationError
import { bridgeTxStatusController } from '../controllers/bridgeTxStatusController'
import { CHAINS_ARRAY } from '../constants/chains'
import { VALID_BRIDGE_MODULES } from '../constants'
import { validateKappa } from '../validations/validateKappa'

const router = express.Router()

Expand Down Expand Up @@ -134,7 +135,10 @@ router.get(
check('synapseTxId')
.exists()
.withMessage('synapseTxId is required')
.isString(),
.isString()
.withMessage('synapseTxId must be a string')
.custom((value) => validateKappa(value))
.withMessage('synapseTxId must be valid hex string'),
],
showFirstValidationError,
bridgeTxStatusController
Expand Down
14 changes: 14 additions & 0 deletions packages/rest-api/src/tests/bridgeTxStatusRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ describe('Get Bridge TX Status Route', () => {
expect(response.body.error).toHaveProperty('field', 'synapseTxId')
}, 10000)

it('should return 400 for invalid synapseTxId', async () => {
const response = await request(app).get('/bridgeTxStatus').query({
destChainId: '1',
bridgeModule: 'SynapseRFQ',
synapseTxId: "'0x1234' OR '1'='1'",
})

expect(response.status).toBe(400)
expect(response.body.error).toHaveProperty(
'message',
'synapseTxId must be valid hex string'
)
}, 10000)

it('should return 400 for missing destChainId', async () => {
const response = await request(app).get('/bridgeTxStatus').query({
bridgeModule: 'bridge',
Expand Down
11 changes: 11 additions & 0 deletions packages/rest-api/src/validations/validateKappa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const validateKappa = (synapseTxId: string) => {
let hexRegex

if (synapseTxId.startsWith('0x')) {
hexRegex = /^0x[0-9a-fA-F]{64}$/
} else {
hexRegex = /^[0-9a-fA-F]{64}$/
}

return hexRegex.test(synapseTxId)
}

0 comments on commit e4c67ec

Please sign in to comment.