Note: A version of this article was first published by The Daily Beast.

A team of terrorists struck the Iranian Parliament and the tomb of Ayatollah Khomeini earlier today. At least 12 people were killed, according to Iran’s state media. Iran’s Revolutionary Guard Corps blamed Saudi Arabia, further escalating tensions in the region. The Islamic State (ISIS) quickly claimed responsibility for the assault via its Amaq News Agency, which released statements and a short video from one of the attacks as it was still ongoing. Amaq says that two suicide bombers detonated their explosive vests at Khomeini’s mausoleum, while inghimasis (well-trained fighters who “immerse” themselves in battle) raided the parliament.

The attack is a significant development, to grossly understate the matter. Tehran had long avoided being hit by the types of “martyrdom” operations that routinely rock the neighboring capitals of Baghdad and Kabul.

This is no accident. The Iranian government’s security services are ruthlessly effective at suppressing all forms of opposition, including both legitimate protesters and those with ill-intentions.

More important, perhaps, is the fact that the Iranian regime cut a secret deal with al Qaeda.

Osama Bin Laden’s organization and its spin-off, the so-called Islamic State, have fought against Iran’s proxies in Iraq and Syria for years. Killing Shiites is a blood sport for ISIS’s Sunni jihadists. And Abu Bakr al Baghdadi’s so-called caliphate reportedly has attempted to strike inside the mullahs’ country before. The group routinely agitates against the Iranians in its videos and propaganda statements. Yet, it wasn’t until now that ISIS successfully attacked the heart of Tehran.

Al Qaeda’s leadership long sought to rein in the anti-Shiite violence in Iraq. Ayman al Zawahiri, Osama bin Laden’s deputy and successor of emir of al Qaeda, even tried to persuade Abu Musab al Zarqawi, the founder of al Qaeda in Iraq, to stop targeting Shiite civilians. Zarqawi hoped to provoke a full scale, sectarian civil war in Iraq. When his men blew up the revered Golden Mosque in Samarra in 2006 they very nearly accomplished that goal. Despite the U.S. “surge” that averted complete disaster in the years that followed, sectarian violence has plagued Iraq ever since.

Zarqawi was killed in June 2006—less than a year after Zawahiri tried to convince him to stop targeting Shiite civilians. Zarqawi’s heirs continued his sectarian strategy inside Iraq. But until 2014, they abided by an order from al Qaeda’s leaders to avoid terrorist operations inside Iranian territory and against Shiites outside of Iraq. The two sides formally split in early 2014. At that point, the organization calling itself the Islamic State (ISIS) gained strength, waging a campaign against Shiites throughout the region—and accusing al Qaeda of being soft on them.

In May 2014, ISIS spokesman Abu Muhammad al Adnani, delivered a stinging rebuke of al Qaeda leaders that exposed their cozy relations with Tehran.

“Let history record that Iran owes al Qaeda invaluably,” Adnani bristled, saying he and his men had “complied with your [al Qaeda’s] request not to target [the Shiites] outside Iraq, in Iran and elsewhere.” Adnani, who was killed in an American airstrike last year, explained that ISIS was “acting upon the orders of al Qaeda to safeguard its interests and supply lines in Iran.”

After al Qaeda’s general command disowned ISIS in February 2014, Baghdadi and Adnani no longer considered themselves bound by al Qaeda leader Ayman al Zawahiri’s edicts. ISIS began launching indiscriminate attacks against Shiite Muslims far outside of Iraq, and it started eyeing operations inside Iran as well.

To this day, however, al Qaeda avoids attacks inside Iran—at least those that can be directly attributed to the organization. (It is possible that al Qaeda supports other regional groups that occasionally target Iranian security forces on their home turf.)

Files recovered during the May 2011 raid on Osama bin Laden’s compound in Abbottabad, Pakistan explain this reticence. One of the documents corroborates Adnani’s testimony regarding al Qaeda’s orders.

In October 2007, bin Laden wrote a letter to the Islamic State of Iraq (ISI), the immediate predecessor to Baghdadi’s ISIS. The missive was likely addressed to Abu Hamza al-Muhajir (a.k.a. Abu Ayyub al Masri), who led the ISI at the time. Abu Bakr al-Baghdadi took charge of this same group in 2010, after Abu Hamza was killed.

Bin Laden disapproved of the ISI’s threats against Iran. “I have a few remarks concerning the matter of your threats to Iran, and I hope that you and your brothers will accommodate it,” bin Laden wrote. The al Qaeda founder continued (emphasis added): “You did not consult with us on that serious issue that affects the general welfare of all of us. We expected you would consult with us for these important matters, for as you are aware, Iran is our main artery for funds, personnel, and communication, as well as the matter of hostages.”

Bin Laden was not against targeting Iran in principle. He simply thought the cost was too high and the benefits al Qaeda received from the relationship were significant.

The “main artery” bin Laden referenced was later targeted in a series of terrorist designations, reward offers and other official statements by the U.S. Treasury and State Departments.

In July 2011, Treasury first identified the al Qaeda leaders, including one known as Yasin al-Suri, who ran the facilitation network inside Iran at the time. Treasury explained that they operated under a formerly “secret deal” between the terror organization and the Iranian government. The Obama administration also referred to the Iranian hub as al Qaeda’s “core facilitation pipeline,” which allowed the jihadist organization to shuttle personnel and funds throughout the Middle East and South Asia. Al Qaeda operatives involved in plots targeting the West have moved through Iran as well.

The leadership of the network has evolved over time for several reasons. In December 2011, for instance, the State Department offered a $10 million reward for Suri, making him one of the most wanted terrorists on the planet. Al Qaeda was forced to reshuffle its deck and place another veteran in charge, but Suri eventually assumed command once again.

In the years that followed, several prominent al Qaeda figures in Iran went on to hold key positions elsewhere, including inside Syria. Indeed, designated terrorists such as Muhsin al-Fadhli and Sanafi al-Nasr, both of whom led the network in Iran for a time, became key players in the so-called “Khorasan Group.”

In 2014, the Obama administration ordered airstrikes targeting the “Khorasan Group,” saying that members of this al Qaeda wing were planning attacks in the West. Both Fadhli and Nasr were killed in American bombings in Syria.

The deal between al Qaeda and the Iranians has survived the wars in Syria and Yemen, despite the fact that they are on opposite sides in those bloody conflicts. It is one of the great curiosities in the jihadist world. There is no question that al Qaeda and Iran’s proxies are at each other’s throats in those countries. Al Qaeda has even kidnapped Iranian diplomats to force exchanges for senior jihadists and family members who were held in some form of custody inside Iran.

Still, even though the two sides frequently clash, their collusion is ongoing. In July 2016, for example, Treasury revealed that Abu Hamza al-Khalidi, al Qaeda’s “Military Commission Chief”—one of the most senior positions in the group—is located in Iran alongside some of his comrades. Another file recovered in bin Laden’s lair refers to Khalidi as part of a “new generation” of leaders groomed to replace their fallen comrades.

As Adnani’s May 2014 message demonstrated, ISIS has been keen to highlight al Qaeda’s unwillingness to strike Iran. The two Sunni jihadist groups remain at odds and ISIS wants to undermine al Qaeda’s legitimacy among Sunni fanatics who may not understand why bin Laden’s heirs would refrain from directly attacking Iran. Other files recovered in Abbottabad show that bin Laden devised plans to undermine Iran’s regional position. But this doesn’t go far enough for some.

According to Treasury, the predecessor to Abu Bakr al Baghdadi’s ISIS had its own relationship with Iranian intelligence during the height of America’s involvement in the Iraq War. But ISIS doesn’t remind its audience of this inconvenient fact. Instead, ISIS criticizes al Qaeda for its willingness to compromise with the Iranians.

In 2016, for example, ISIS’s Naba magazine carried a series of interviews with an al Qaeda defector known as Abu Ubaydah Al Lubnani, who claimed that “Iranian intelligence” was in complete control of al Qaeda’s safe houses inside the country. Interestingly enough, Lubnani himself transited through Iran to the lands of the self-declared caliphate after leaving al Qaeda’s ranks. But the purpose of Lubnani’s interviews was to indict ISIS’s rivals as being soft on the Iranians.

Other propaganda disseminated by ISIS has similarly sought to portray the group as the vanguard of Sunni opposition to Iranian-backed forces throughout the region.

There is no question that after today’s attacks inside Tehran, ISIS will ramp up its messaging along these lines. ISIS can finally claim to have brought the Sunni jihadists’ war to Iran, something al Qaeda has been unwilling to do.

[For a timeline of U.S. government designations and other statements concerning Iran’s agreement with al Qaeda, see FDD’s Long War Journal report: Treasury designates 3 senior al Qaeda members in Iran.]

Thomas Joscelyn is a Senior Fellow at the Foundation for Defense of Democracies and the Senior Editor for FDD's Long War Journal.

Are you a dedicated reader of FDD's Long War Journal? Has our research benefitted you or your team over the years? Support our independent reporting and analysis today by considering a one-time or monthly donation. Thanks for reading! You can make a tax-deductible donation here.