Skip to content

Commit

Permalink
fix: Authenticate capability missing on Send packet method. #182 (#643)
Browse files Browse the repository at this point in the history
* fix: fix send authenticate

* chore: pass build

* fix: add test for capability check

* chore: build pass

---------

Co-authored-by: sabinchitrakar <immortal.infidel@gmail.com>
  • Loading branch information
ibrizsabin and sabinchitrakar authored Aug 24, 2023
1 parent c4eac8e commit ace5988
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 5 deletions.
2 changes: 1 addition & 1 deletion contracts/cosmwasm-vm/cw-ibc-core/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl<'a> CwIbcCoreContext<'a> {
let packet: RawPacket = Message::decode(packet_bytes.as_slice())
.map_err(|error| ContractError::IbcDecodeError { error })?;

self.send_packet(deps, &env, packet)
self.send_packet(deps, &env, info, packet)
}
CoreExecuteMsg::ReceivePacket { msg } => {
cw_println!(deps, "[IBCCore] Receive Packet Called");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl<'a> CwIbcCoreContext<'a> {
&self,
deps: DepsMut,
env: &Env,
info: MessageInfo,
packet: RawPacket,
) -> Result<Response, ContractError> {
let src_port = to_ibc_port_id(&packet.source_port)?;
Expand All @@ -37,6 +38,12 @@ impl<'a> CwIbcCoreContext<'a> {
let dst_port = to_ibc_port_id(&packet.destination_port)?;
let dst_channel = to_ibc_channel_id(&packet.destination_channel)?;

let authenticated =
self.authenticate_capability(deps.as_ref().storage, info, src_port.as_bytes().to_vec());
if !authenticated {
return Err(ContractError::Unauthorized {});
}

let chan_end_on_a = self.get_channel_end(deps.storage, &src_port, &src_channel)?;
cw_println!(deps, "fetched channel_end");
if chan_end_on_a.state_matches(&State::Closed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ fn test_for_channel_close_confirm() {
#[test]
fn test_for_packet_send() {
let mut deps = deps();
let info = create_mock_info("alice", "umlg", 20000000);
let info = create_mock_info("moduleaddress", "umlg", 20000000);
let mut contract = CwIbcCoreContext::default();
let env = get_mock_env();
let timestamp_future = Timestamp::default();
Expand Down Expand Up @@ -870,6 +870,13 @@ fn test_for_packet_send() {
consenus_state.get_keccak_hash().to_vec(),
)
.unwrap();
contract
.bind_port(
&mut deps.storage,
&IbcPortId::from_str(&packet.source_port).unwrap(),
Addr::unchecked("moduleaddress").to_string(),
)
.unwrap();

let res = contract.execute(
deps.as_mut(),
Expand Down
151 changes: 148 additions & 3 deletions contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,16 @@ fn test_packet_send() {
)
.unwrap();

let res = contract.send_packet(deps.as_mut(), &mock_env(), packet);
let info = create_mock_info("moduleaddress", "test", 100);
contract
.bind_port(
&mut deps.storage,
&IbcPortId::from_str(&packet.source_port).unwrap(),
Addr::unchecked("moduleaddress").to_string(),
)
.unwrap();

let res = contract.send_packet(deps.as_mut(), &mock_env(), info, packet);
println!("{:?}", res);
assert!(res.is_ok());
let res = res.unwrap();
Expand All @@ -132,8 +141,16 @@ fn test_packet_send_fail_channel_not_found() {

packet.sequence = 1;
packet.data = vec![0];
let info = create_mock_info("moduleaddress", "test", 100);
contract
.bind_port(
&mut deps.storage,
&IbcPortId::from_str(&packet.source_port).unwrap(),
Addr::unchecked("moduleaddress").to_string(),
)
.unwrap();
contract
.send_packet(deps.as_mut(), &mock_env(), packet)
.send_packet(deps.as_mut(), &mock_env(), info, packet)
.unwrap();
}

Expand Down Expand Up @@ -226,8 +243,136 @@ fn test_packet_send_fail_misiing_sequense() {
consenus_state.get_keccak_hash().to_vec(),
)
.unwrap();
let info = create_mock_info("moduleaddress", "test", 100);
contract
.bind_port(
&mut deps.storage,
&IbcPortId::from_str(&packet.source_port).unwrap(),
Addr::unchecked("moduleaddress").to_string(),
)
.unwrap();

contract
.send_packet(deps.as_mut(), &mock_env(), info, packet)
.unwrap();
}

#[should_panic(
expected = "IbcDecodeError { error: DecodeError { description: \"CapabilityNotFound\", stack: [] } }"
)]
#[test]
fn test_packet_send_fails_for_invalid_port() {
let contract = CwIbcCoreContext::default();
let mut deps = deps();
let env = get_mock_env();
let timestamp_future = Timestamp::default();
let timeout_height_future = 10;

let mut packet = get_dummy_raw_packet(timeout_height_future, timestamp_future.nanoseconds());
packet.sequence = 1;
packet.data = vec![0];
let src_port = to_ibc_port_id(&packet.source_port).unwrap();
let src_channel = to_ibc_channel_id(&packet.source_channel).unwrap();

let _dst_port = to_ibc_port_id(&packet.destination_port).unwrap();
let _dst_channel = to_ibc_channel_id(&packet.destination_channel).unwrap();

let chan_end_on_a = ChannelEnd::new(
State::TryOpen,
Order::default(),
Counterparty::new(
to_ibc_port_id(&packet.destination_port).unwrap(),
Some(to_ibc_channel_id(&packet.destination_channel).unwrap()),
),
vec![IbcConnectionId::default()],
Version::new("ics20-1".to_string()),
);

let conn_prefix = common::ibc::core::ics23_commitment::commitment::CommitmentPrefix::try_from(
"hello".to_string().as_bytes().to_vec(),
);

let conn_end_on_a = ConnectionEnd::new(
ConnectionState::Open,
ClientId::default(),
ConnectionCounterparty::new(
ClientId::default(),
Some(ConnectionId::default()),
conn_prefix.unwrap(),
),
get_compatible_versions(),
ZERO_DURATION,
);

contract
.store_channel_end(
&mut deps.storage,
&src_port.clone(),
&src_channel.clone(),
&chan_end_on_a,
)
.unwrap();
let conn_id_on_a = &chan_end_on_a.connection_hops()[0];
contract
.store_connection(&mut deps.storage, &conn_id_on_a.clone(), &conn_end_on_a)
.unwrap();
contract
.store_next_sequence_send(
&mut deps.storage,
&src_port,
&src_channel,
&Sequence::from(1),
)
.unwrap();

let client_state = ClientState {
latest_height: 10,
..get_dummy_client_state()
};

let client = client_state.to_any().encode_to_vec();
contract
.store_client_state(
&mut deps.storage,
&env,
&IbcClientId::default(),
client,
client_state.get_keccak_hash().to_vec(),
)
.unwrap();
let consenus_state: ConsensusState = common::icon::icon::lightclient::v1::ConsensusState {
message_root: vec![1, 2, 3, 4],
next_proof_context_hash: vec![1, 2, 3, 4],
}
.try_into()
.unwrap();
let height = RawHeight {
revision_number: 0,
revision_height: 10,
}
.try_into()
.unwrap();
let consenus_state_any = consenus_state.to_any().encode_to_vec();
contract
.store_consensus_state(
&mut deps.storage,
&IbcClientId::default(),
height,
consenus_state_any,
consenus_state.get_keccak_hash().to_vec(),
)
.unwrap();

let info = create_mock_info("moduleaddress", "test", 100);
contract
.bind_port(
&mut deps.storage,
&IbcPortId::from_str(&packet.source_port).unwrap(),
Addr::unchecked("moduleaddress").to_string(),
)
.unwrap();
packet.source_port = "invalidPort".to_string();
contract
.send_packet(deps.as_mut(), &mock_env(), packet)
.send_packet(deps.as_mut(), &mock_env(), info, packet)
.unwrap();
}

0 comments on commit ace5988

Please sign in to comment.