Skip to content

Commit 176f586

Browse files
committed
Protect dropping from far node inventories
Also changes if/if to switch/case
1 parent fc86402 commit 176f586

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

src/network/serverpackethandler.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -628,23 +628,34 @@ void Server::handleCommand_InventoryAction(NetworkPacket* pkt)
628628

629629
auto check_inv_access = [player, player_has_interact, this] (
630630
const InventoryLocation &loc) -> bool {
631-
if (loc.type == InventoryLocation::CURRENT_PLAYER)
632-
return false; // Only used internally on the client, never sent
633-
if (loc.type == InventoryLocation::PLAYER) {
634-
// Allow access to own inventory in all cases
635-
return loc.name == player->getName();
636-
}
637-
if (loc.type == InventoryLocation::DETACHED) {
638-
if (!getInventoryMgr()->checkDetachedInventoryAccess(loc, player->getName()))
639-
return false;
640-
}
641631

642-
if (!player_has_interact) {
632+
// Players without interact may modify their own inventory
633+
if (!player_has_interact && loc.type != InventoryLocation::PLAYER) {
643634
infostream << "Cannot modify foreign inventory: "
644635
<< "No interact privilege" << std::endl;
645636
return false;
646637
}
647-
return true;
638+
639+
switch (loc.type) {
640+
case InventoryLocation::CURRENT_PLAYER:
641+
// Only used internally on the client, never sent
642+
return false;
643+
case InventoryLocation::PLAYER:
644+
// Allow access to own inventory in all cases
645+
return loc.name == player->getName();
646+
case InventoryLocation::NODEMETA:
647+
{
648+
// Check for out-of-range interaction
649+
v3f node_pos = intToFloat(loc.p, BS);
650+
v3f player_pos = player->getPlayerSAO()->getEyePosition();
651+
f32 d = player_pos.getDistanceFrom(node_pos);
652+
return checkInteractDistance(player, d, "inventory");
653+
}
654+
case InventoryLocation::DETACHED:
655+
return getInventoryMgr()->checkDetachedInventoryAccess(loc, player->getName());
656+
default:
657+
return false;
658+
}
648659
};
649660

650661
/*
@@ -664,18 +675,6 @@ void Server::handleCommand_InventoryAction(NetworkPacket* pkt)
664675
!check_inv_access(ma->to_inv))
665676
return;
666677

667-
InventoryLocation *remote = ma->from_inv.type == InventoryLocation::PLAYER ?
668-
&ma->to_inv : &ma->from_inv;
669-
670-
// Check for out-of-range interaction
671-
if (remote->type == InventoryLocation::NODEMETA) {
672-
v3f node_pos = intToFloat(remote->p, BS);
673-
v3f player_pos = player->getPlayerSAO()->getEyePosition();
674-
f32 d = player_pos.getDistanceFrom(node_pos);
675-
if (!checkInteractDistance(player, d, "inventory"))
676-
return;
677-
}
678-
679678
/*
680679
Disable moving items out of craftpreview
681680
*/

0 commit comments

Comments
 (0)