Stop me if you’ve heard this one before: An Israeli soldier, an Egyptian soldier, and a Hamas militant walk up to a wall, and start shooting … in the same direction. Of course you haven’t heard that one before; the joke would be its utter impossibility. And yet, the Washington Post’s Sudarsan Raghavan and William Booth report, the three have bonded together to fight an even greater threat than each other — ISIS:

The Islamic State’s affiliate in Egypt is staging increasingly sophisticated and daring attacks, officials and analysts say, prompting Israel, Egypt and the Palestinian militant group Hamas to form an unlikely alliance against the terrorist group.

Hamas deployed several hundred fighters last week to Gaza’s border with Egypt’s lawless northern Sinai as part of a deal with Egypt to keep militants of the Islamic State — also known as ISIS or ISIL — from entering the coastal enclave.

That came days after Israeli Prime Minister Benjamin Netanyahu praised his country’s decision to build a new barrier along the Israel-Egypt border, warning that “we would have been overflowed by thousands of ISIS fighters from Sinai.”

The growing concerns have given birth to the greatest cooperation between the militaries of Egypt and Israel since their 1979 peace deal, according to officials from both countries.

If this sounds far-fetched, well … it is, but bear with me for a moment. Each of the three has a stake in keeping ISIS out of the region, at least theoretically. For Israel and Egypt the threat is obvious, but it also applies to Hamas. Ten months ago, ISIS threatened to supplant Hamas in Gaza, accusing Hamas for not being extreme enough in its application of shari’a:

Islamic State insurgents threatened on Tuesday to turn the Gaza Strip into another of their Middle East fiefdoms, accusing Hamas, the organization that rules the Palestinian territory, of being insufficiently stringent about religious enforcement.

The video statement, issued from an Islamic State stronghold in Syria, was a rare public challenge to Hamas, which has been cracking down on jihadis in Gaza who oppose its truces with Israel and reconciliation with the U.S.-backed rival Palestinian faction Fatah.

“We will uproot the state of the Jews (Israel) and you and Fatah, and all of the secularists are nothing and you will be over-run by our creeping multitudes,” said a masked Islamic State member in the message addressed to the “tyrants of Hamas”.

“The rule of sharia (Islamic law) will be implemented in Gaza, in spite of you. We swear that what is happening in the Levant today, and in particular the Yarmouk camp, will happen in Gaza,” he said, referring to Islamic State advances in Syria, including in a Damascus district founded by Palestinian refugees.

Perhaps Hamas took that threat seriously. Or perhaps the reports of a grand alliance between Hamas, Egypt, and Israel are just rumors and speculation. Nine months ago, the Times of Israel warned that Hamas had joined an alliance, all right, but with ISIS, and against Israel and Egypt:

So what are Hamas drones doing deep inside Egyptian territory? Simple — they are spying on Egypt, gathering intelligence about Egyptian troop movements along the border with Gaza.

Hamas’s objective is not necessarily a terror attack against Egypt, but rather tracking troop movements, the location of army positions and deployments in order to allow members of its military wing, Izz ad-Din al-Qassam, to keep smuggling routes open between Sinai and Gaza. These routes are vital to Hamas on one side of the border, and Islamic State on the other.

This is one of the most carefully guarded secrets of the Hamas military wing in recent months — a close cooperation with the members of the Islamic State-affiliated Wilayat al-Sina, “Province of Sinai,” which was previously known as Ansar Bait al-Maqdis.

Egyptian army officials were among the first to grasp the depth of the relationship. It is for this reason that Egypt has been expending so much energy on destroying tunnels under the border and putting a stop to the smuggling from Sinai to Gaza and vice versa.

Both ISIS and Hamas are known for their mercurial temperaments, so it’s possible that all of these reports are accurate — for their particular moments in time. Plus, Hamas has a reason to cooperate with Egypt, at least in the short run:

Israeli and Egyptian officials have long suspected that Hamas, which rules the Gaza Strip, allows the Sinai militants to enter the enclave through smuggling tunnels and use the strip as a safe haven. Israelis say Hamas brings arms across the Sinai into Gaza and is helped by Bedouin smugglers, some tied to the Islamic State. Hamas denies the allegations, saying it has no sympathies to the Islamic State, which branded Hamas as infidels in a video two years ago.

But the concerns were enough for the Egyptians to apply pressure on Hamas to control its border and prevent any movement of fighters or couriers between Gaza and Sinai. Gaza depends on Egypt in part for its economic survival, and Hamas is keen to have its border with Egypt reopened.

Hamas claims it has positioned 300 fighters in support of “our Egyptian brothers,” but don’t think for a moment that they’re cooperating or allying with Israel. Even the Egyptians had better take a trust-but-verify position with their “ally.” Hamas has much more in common with ISIS than it does with either of its two putative coalition partners, and it will revert to type as soon as the conditions are ripe for the transition.