The Pasuk describes the victims of the plague of the firstborn as follows:
It came to pass at midnight, and the Lord smote every firstborn in the land of Egypt, from the firstborn of Pharaoh who sits on his throne to the firstborn of the captive who is in the dungeon, and every firstborn animal.
כט. וַיְהִי | בַּחֲצִי הַלַּיְלָה וַיהֹוָה הִכָּה כָל בְּכוֹר בְּאֶרֶץ מִצְרַיִם מִבְּכֹר פַּרְעֹה הַיּשֵׁב עַל כִּסְאוֹ עַד בְּכוֹר הַשְּׁבִי אֲשֶׁר בְּבֵית הַבּוֹר וְכֹל בְּכוֹר בְּהֵמָה:
Rashi commenting on this verse states that foreigners were included in this plague:
"smote every firstborn": Even [a firstborn] of another nation who was in Egypt.
הכה כל בכור: אף של אומה אחרת והוא במצרים
What is the reason for God killing the Foreigner who was visiting Egypt? How is that משפט (justice)?