Skip to content

Latest commit

 

History

History
58 lines (42 loc) · 2.2 KB

7-unwrap-panic-usage.md

File metadata and controls

58 lines (42 loc) · 2.2 KB

Unwrap Panic Usage

Description

The unwrap-panic function attempts to 'unpack' its argument. If it fails, it throws a runtime error, which aborts the execution of the current transaction.

Exploit Scenario

(define-public (fulfil-listing-ft (listing-id uint) (nft-asset-contract <nft-trait>) (payment-asset-contract <ft-trait>))
	(let 
        (listing (unwrap-panic (map-get? listings listing-id) err-unknown-listing))
		(taker tx-sender)
		) (
		(try! (assert-can-fulfil (contract-of nft-asset-contract) (some (contract-of payment-asset-contract)) listing))
		(try! (as-contract (transfer-nft nft-asset-contract (get token-id listing) tx-sender taker)))
		(try! (transfer-ft payment-asset-contract (get price listing) taker (get maker listing)))
		(map-delete listings listing-id)
		(ok listing-id)
	)
)

The vulnerable code example can be found here.

Remediation

Use unwrap! instead of unwrap-panic to handle the error.

(define-public (fulfil-listing-ft (listing-id uint) (nft-asset-contract <nft-trait>) (payment-asset-contract <ft-trait>))
	(let 
        (listing (unwrap! (map-get? listings listing-id) err-unknown-listing))
		(taker tx-sender)
		) (
		(try! (assert-can-fulfil (contract-of nft-asset-contract) (some (contract-of payment-asset-contract)) listing))
		(try! (as-contract (transfer-nft nft-asset-contract (get token-id listing) tx-sender taker)))
		(try! (transfer-ft payment-asset-contract (get price listing) taker (get maker listing)))
		(map-delete listings listing-id)
		(ok listing-id)
	)
)

The remediated code example can be found here.

References