Skip to content

Commit

Permalink
Prevent transfers to yourself (#4471)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Oct 2, 2023
1 parent 344cb99 commit efd9c97
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions backend/canisters/user/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed

- Notifications for custom messages should use the sub-type ([#4465](https://github.com/open-chat-labs/open-chat/pull/4465))
- Prevent transfers to yourself ([#4471](https://github.com/open-chat-labs/open-chat/pull/4471))

## [[2.0.867](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.867-user)] - 2023-09-27

Expand Down
3 changes: 3 additions & 0 deletions backend/canisters/user/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type SendMessageResponse = variant {
InvalidRequest : text;
TransferFailed : text;
TransferCannotBeZero;
TransferCannotBeToSelf;
UserSuspended;
InternalError : text;
};
Expand Down Expand Up @@ -447,6 +448,7 @@ type SendMessageWithTransferToChannelResponse = variant {
InvalidRequest : text;
TransferFailed : text;
TransferCannotBeZero;
TransferCannotBeToSelf;
UserSuspended;
CommunityFrozen;
RulesNotAccepted;
Expand Down Expand Up @@ -482,6 +484,7 @@ type SendMessageWithTransferToGroupResponse = variant {
InvalidRequest : text;
TransferFailed : text;
TransferCannotBeZero;
TransferCannotBeToSelf;
UserSuspended;
ChatFrozen;
RulesNotAccepted;
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user/api/src/updates/send_message_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum Response {
InvalidRequest(String),
TransferFailed(String),
TransferCannotBeZero,
TransferCannotBeToSelf,
UserSuspended,
InternalError(String),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub enum Response {
InvalidRequest(String),
TransferFailed(String),
TransferCannotBeZero,
TransferCannotBeToSelf,
UserSuspended,
CommunityFrozen,
RulesNotAccepted,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum Response {
InvalidRequest(String),
TransferFailed(String),
TransferCannotBeZero,
TransferCannotBeToSelf,
UserSuspended,
ChatFrozen,
RulesNotAccepted,
Expand Down
6 changes: 5 additions & 1 deletion backend/canisters/user/impl/src/updates/send_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ fn validate_request(args: &Args, state: &RuntimeState) -> ValidateRequestResult
}
})
} else if args.recipient == my_user_id {
ValidateRequestResult::Valid(my_user_id, UserType::_Self)
if matches!(args.content, MessageContentInitial::Crypto(_)) {
ValidateRequestResult::Invalid(TransferCannotBeToSelf)
} else {
ValidateRequestResult::Valid(my_user_id, UserType::_Self)
}
} else if let Some(chat) = state.data.direct_chats.get(&args.recipient.into()) {
let user_type = if chat.is_bot { UserType::Bot } else { UserType::User };
ValidateRequestResult::Valid(my_user_id, user_type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ async fn send_message_with_transfer_to_channel(
PrepareResult::RecipientBlocked => return RecipientBlocked,
PrepareResult::InvalidRequest(t) => return InvalidRequest(t),
PrepareResult::TransferCannotBeZero => return TransferCannotBeZero,
PrepareResult::TransferCannotBeToSelf => return TransferCannotBeToSelf,
};

// Make the crypto transfer
Expand Down Expand Up @@ -111,6 +112,7 @@ async fn send_message_with_transfer_to_group(
PrepareResult::RecipientBlocked => return RecipientBlocked,
PrepareResult::InvalidRequest(t) => return InvalidRequest(t),
PrepareResult::TransferCannotBeZero => return TransferCannotBeZero,
PrepareResult::TransferCannotBeToSelf => return TransferCannotBeToSelf,
};

// Make the crypto transfer
Expand Down Expand Up @@ -169,6 +171,7 @@ enum PrepareResult {
RecipientBlocked,
InvalidRequest(String),
TransferCannotBeZero,
TransferCannotBeToSelf,
}

fn prepare(content: &MessageContentInitial, state: &RuntimeState) -> PrepareResult {
Expand All @@ -184,6 +187,10 @@ fn prepare(content: &MessageContentInitial, state: &RuntimeState) -> PrepareResu

let pending_transaction = match &content {
MessageContentInitial::Crypto(c) => {
let my_user_id = state.env.canister_id().into();
if c.recipient == my_user_id {
return TransferCannotBeToSelf;
}
if state.data.blocked_users.contains(&c.recipient) {
return RecipientBlocked;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
// default the receiver to the other user in a direct chat
if (chat.kind === "direct_chat") {
receiver = $userStore[chat.them.userId];
} else if (defaultReceiver !== undefined) {
} else if (defaultReceiver !== undefined && defaultReceiver !== user.userId) {
receiver = $userStore[defaultReceiver];
}
});
Expand Down

0 comments on commit efd9c97

Please sign in to comment.