Summary
When soulmateA leaves a message, the messages can be overwritten by a new message from either Soulmate A or SoulmateB. There is no assurance that the message was view or who the message was from. *Also the formatting is odd with the niceWords at the end. IE:
====================================
DEFAULT MESSAGE APPEARS AS: , darling <-- no message set
soulmate1 sets message "Message 1a"
soulmate1 views MSG: "Message 1a, my dear"
**soulmate2 does not check message**
soulmate2 sets a message "Message 1b"
soulmate1 Checks MSG: "Message 1b, honey"
soulmate2 Checks MSG: "Message 1b, honey"
====================================
Vulnerability Details
Messages will missed or overwritten and possibly cause a divorce.
Impact
Confusion in messaging or missed messages or sad times.
Tools Used
HardHat
Recommendations
add a new uint256 sharedSpaceId
and a new mapping mapping(address owner => uint256 id) public sharedSpaceToId
to keep the address to ID. Then in writeMessageInSharedSpace()
leave message in sharedSpace[sharedSpaceId]
instead. Also update the readMessageInSharedSpace()
to load sharedSpaceToId[msg.sender]
. This will ensure that the messages that are being read are from the other soulmate and will not overwrite the others message - only your own.
mapping(address owner => uint256 id) public sharedSpaceToId;
uint256 public sharedSpaceId;;
[previous code]
if (soulmate != address(0)) {
revert Soulmate__alreadyHaveASoulmate(soulmate);
}
address soulmate1 = idToOwners[nextID][0];
address soulmate2 = idToOwners[nextID][1];
if (soulmate1 == address(0)) {
idToOwners[nextID][0] = msg.sender;
ownerToId[msg.sender] = nextID;
sharedSpaceToId[msg.sender] = sharedSpaceId;
sharedSpaceId++;
emit SoulmateIsWaiting(msg.sender);
} else if (soulmate2 == address(0)) {
idToOwners[nextID][1] = msg.sender;
ownerToId[msg.sender] =nextID;
sharedSpaceToId[msg.sender] = sharedSpaceId;
sharedSpaceId++;
soulmateOf[msg.sender] = soulmate1;
soulmateOf[soulmate1] = msg.sender;
idToCreationTimestamp[nextID] = block.timestamp;
emit SoulmateAreReunited(soulmate1, soulmate2, nextID);
_mint(msg.sender, nextID++);
}
function writeMessageInSharedSpace(string calldata message) external {
uint256 id = ownerToId[msg.sender];
address soulmate = soulmateOf[msg.sender];
uint256 _sharedSpaceId = sharedSpaceToId[soulmate];
sharedSpace[_sharedSpaceId] = message;
emit MessageWrittenInSharedSpace(id, message);
}
function readMessageInSharedSpace() external view returns (string memory) {
return string.concat(sharedSpace[sharedSpaceToId[msg.sender]], ", ", niceWords[block.timestamp % niceWords.length]);
}