When Islamic State called on members of Iran's Sunni Muslim minority in March to wage a religious war on their Shi'ite rulers, few people took the threat seriously. And yet within three months, militants have breached security at the very heart of the nation, killing at least 17 people.
This week's attacks at parliament and the mausoleum of the Islamic Republic's founder Ayatollah Ruhollah Khomeini have exposed shortcomings among the Revolutionary Guards Corps (IRGC) which was supposed to be protecting these potent symbols of Iran's revolution.
They have also undermined Tehran's belief that by backing offensives against Islamic State across the Middle East, it can keep the militant Sunni group away from Iran.
Undaunted, officials say Iran will step up the strategy, which includes sending fighters to battle Islamic State in Syria and Iraq alongside allied Shi'ite militia groups.
And with Supreme Leader Ayatollah Ali Khamenei and the IRGC blaming Saudi Arabia for the attacks, tensions are only likely to deepen between the two arch rivals competing for influence in an already chaotic region. Riyadh denies the charges, describing Tehran instead as "the number one state sponsor of terrorism".
Wednesday's killings in Tehran by Iranian members of Islamic State drew a shocked response similar to that in Western countries when they too have been attacked by locally-born jihadists. Now Iranians are worrying about how many others are out there, planning similar assaults.
One senior Iranian official told Reuters that Islamic State had established a network of support in the country, and suggested that members' motivation was as much political and economic as to do with Sunni radicals' belief that Shi'ites are infidels.
"Sunni extremism is spreading in Iran like many other countries. And not all of these young people who join extremist groups are necessarily religious people," said the official, who asked not to be named. "But the establishment is well aware of the problem and is trying to tackle it."
Most Iranian Sunnis, who form up to 10 percent of the population, reject Islamic State's ideology. But some young Sunnis seem to regard policies of Shi'ite-led Iran as oppressive at home or aggressive abroad, such as in Iraq and Syria, pushing more of them into the arms of jihadist groups.
Iran has been trying to stem the spread of radicalism into Sunni majority regions, which are usually less economically developed. Authorities said 1,500 young Iranians were prevented from joining Islamic State in 2016.
Iran's Sistan and Baluchestan province, which borders Pakistan and Afghanistan, is home to the Balouch minority and has long been a hotbed of Sunni insurgents.
Two Sunni groups, Jaish al-Adl and Jundallah, have been fighting the IRGC for over a decade. This has mostly been in remote areas but some say it was almost inevitable that violence would eventually spread to Tehran, as it did this week.
"It's not the attacks that are surprising. It's that Iran has been able to avoid one for so long. The attacks were a wake-up call for Iran's security apparatus," said senior Iran analyst Ali Vaez of the International Crisis Group. "But so too will they probably serve as one for jihadists, who will be encouraged to exploit Iran's vulnerabilities."
Since its creation shortly after the 1979 Islamic revolution, the IRGC has functioned as Iran's most powerful internal and external security force, with a sophisticated intelligence and surveillance network.
The IRGC has vowed revenge on Islamic State - known by its opponents under the Arab acronym of Daesh - but a top security official said this won't be easy.
"The attacks showed the vulnerability of our security system, at the borders and within Iran," the official said, asking not to be named. However, he added: "Many planned attacks by Daesh have been foiled by our security forces in the past years. Many terrorist cells were dismantled. Our Guards have been vigilant."
Syrian rebels and Iraqi forces are closing in on Islamic State in those countries, and the official said the group appeared to have tried to strike back in Tehran.
"The attacks are the result of Daesh being weakened in the region. They blame Iran for that ... But Iran will not abandon its fight against terrorism," he added.
Open discussion of security vulnerabilities is taboo in Iran. However, Behnam Ben Taleblu, a senior Iran analyst at the Washington-based Foundation for Defense of Democracies, criticized the idea that Syria and Iraq could form an effective first line of defense for Iran.
"Iranian officials have long justified their country's active military presence in Iraq and Syria as a way to keep the homeland safe," he said. "Wednesday's attacks expose the folly of that strategy."
A senior official, who also asked not to be named, said the attacks would push Iran toward "a harsher regional policy".
Sanam Vakil, associate fellow with Chatham House's Middle East and North Africa Programme, dismissed any expectation that Tehran might try to ease spiraling tensions with Riyadh. "If we are expecting to see any change in Iran's regional policy or a retreat in any way - that is not going to happen," she said.
Newly re-elected President Hassan Rouhani has said the attacks will make Iran more united. Analysts, however, believe they will exacerbate domestic tensions between Rouhani, a pragmatist, and his rivals among hardline clergy and the IRGC.
They have repeatedly criticized Rouhani's attempts to improve relations with the outside world.
Rouhani has generally lost out to the hardliners, who through the IRGC's Al Quds force - expeditionary units which are fighting in Iraq and Syria as well as organizing Shi'ite allies - continue to call the shots. In the view of the hardliners' critics, they are helping to drive alienated Sunnis toward militant groups.