"And it came to pass, when Joshua was by Jericho" (Joshua 5:13). But is it not written that "Jericho was shut up tightly" (Joshua 6:1)? Rather, this teaches that he was at the outskirts of the city, which counts as part of the city. And we have learned in the Mishnah: One who places his eruv in the outskirts of a city has done nothing at all. "And he said: Are you for us or for our adversaries?" (Joshua 5:13). But how could Joshua do such a thing? Did not Rabbi Yehoshua ben Levi say: It is forbidden for a person to give a greeting of peace at night, lest the one he greets be a demon? It is different there, for the figure said to him, "I am captain of the host of the LORD." But perhaps he was lying? It is an accepted tradition that demons do not utter the name of Heaven in vain.
The Angel Captain Joshua Met Before the Walls of Jericho
Yalkut Shimoni on Nach 15:14
ויהי בהיות יהושע ביריחו (יהושע ה, יג). והכתיב ויריחו סוגרת ומסוגרת אלא מלמד לעבורה של עיר שהוא כעיר, ותנן הנותן עירובו בעבורה של עיר לא עשה ולא כלום. ויאמר הלנו אתה אם לצרינו וגו'. והיכי עביד הכי והא אמר רבי יהושע בן לוי אסור לאדם ליתן שלום בלילה שמא שר הוא, שאני התם דקאמר ליה שר צבא ה', ודילמא משקרי, גמירי דלא מפקי שם שמים לבטלה: