BEIRUT — As Syria’s war enters what could be its last and most dangerous stretch, the Syrian government and its allies will have to contend for the first time with the presence of foreign troops in the quest to bring the rest of the country back under President Bashar al-Assad’s control.

The government’s recent defeat of rebels in the southwest of Syria has put Assad unassailably in control of a majority of the country, his hold on power now facing no discernible military or diplomatic threat.

But at least a third of Syria remains outside government control, and those areas are occupied by Turkish and American troops. Turkey has deployed soldiers in the northwest, in rebel-held parts of the province of Aleppo and in Idlib, which Assad has identified as the next target of an offensive. About 2,000 U.S. Special Operations forces hold sway in the northeast, in support of their Kurdish allies fighting the Islamic State.

Iran has meanwhile entrenched its forces and allied militias alongside loyalist Syrian troops across government-held territory, stirring deep concern in Israel.

Even as the war enters its final stages, the risk that it could ignite a wider conflict has not passed, analysts say.

It will fall to Russia to steer Syria through the pitfalls ahead, as the only outside power to enjoy good relations with all the countries that have a stake in the Syrian war, including Israel and Iran. After intervening in the conflict in 2015 to save the Assad regime, Moscow has largely succeeded in balancing the competing interests of the various players, tamping down fears that the conflict could ignite a regional conflagration.

But Russia’s capacity to manage these competing concerns is limited and will be tested by the coming battles, said Riad Kahwaji, who heads the Dubai-based Inegma defense consultancy.

“Russia is not as much in control as it likes to appear,” he said, citing the apparent failure of recent Russian diplomacy aimed at addressing Israel’s concerns about Iran’s presence.

“Iran has invested so much in Syria, it’s not going to leave now, if ever,” he said. “So with Iran refusing to get its forces out of Syria and the Israel insistence that it leave, eventually there is going to be a clash.”

Russia’s priorities for now are to stabilize the areas already recaptured by Syrian forces, bring back at least some of the 6 million refugees who fled the country, rebuild the Syrian army, kick-start reconstruction and secure international recognition for Russian efforts through a peace settlement, said Kamal Alam of the London-based Royal United Services Institute, who was recently in Damascus.

“The Russians are aware that the areas that have been retaken are ticking time bombs, and they want to make sure there are no large-scale revenge attacks,” he said. “The war isn’t quite over, but now is the time to stabilize the country, and in reality the hard work has been done.”

The immediate priority for the Syrian government, however, is to regain the remaining territories outside its control, starting with the province of Idlib, according to recent statements by Assad and his allies.

Syrian troops who have been freed up after pacifying the south have been redeploying to the north in anticipation of an offensive in Idlib.

At the same time, Turkey has been reinforcing its observation posts in the province, which were established under an agreement with the Russians. Turkish President Recep Tayyip Erdogan has called Idlib a “red line” and vowed to resist any attempt to reclaim the province from Turkey’s sphere of influence, raising the risk of a showdown between his country, which is a NATO ally, and Syria, Russia and Iran.

After Syrian government forces advanced into southwest Syria, a few hundred rebel fighters and their families began leaving Daraa on July 15. (Reuters)

A fight for Idlib could be more devastating than any of the previous battles. As Syrian forces recaptured other pockets of territory, the battles were settled by evacuating rebels to Idlib, which now contains as many as 70,000 fighters. That is the biggest concentration of opposition fighters yet assembled. A significant number of them are extremists belonging to al-Qaeda affiliates in Syria.

Idlib also harbors an estimated 3 million civilians, many of whom took refuge there after fleeing fighting elsewhere in the country. An assault on Idlib could trigger Syria’s largest humanitarian crisis yet and prompt a new exodus of refugees to Turkey and perhaps to Europe, analysts say.

Russian officials have made it clear they would prefer a negotiated solution for Idlib, along the lines of the one that ended the recent battle for southwestern Syria — a relatively bloodless outcome compared with bloodbaths elsewhere. In the southwest, most of the rebel groups agreed to reconcile with the Syrian government in return for a degree of local control, and some joined loyalist troops to fight for a last pocket controlled by the Islamic State.

Russia’s special envoy for Syria, Alexander Lavrentiev, told the Russian news agency Tass that Russia hopes to use southwestern Syria as a model for Idlib. In the hope of avoiding a bloody battle, Russia is ready to provide “any help” to moderate rebels who agree to fight the extremist groups, he said.

The Syrian opposition is willing to entertain the prospect of such an arrangement but wants to know what it might gain in return, said Bassam Barabandi, a former Syrian diplomat who defected in Washington and now works with the opposition. At a minimum, he said, the rebels in Idlib want guarantees that they will have some form of autonomy and protection, perhaps under Turkish and Russian auspices.

“This is the question,” he said. “What is the price, and what do we get?”

Russia has said in the past that it wants negotiated settlements, only to be preempted by Syrian government offensives, Barabandi said. A potentially devastating battle for Idlib is highly likely, he said, but there is also a chance that the diplomacy will work and that the worst of the violence in Syria will be over. “I think by the end of this year, we will see the end of major conflict,” he said.

At least for now, the U.S.-held area of northeastern Syria is considered the least problematic. President Trump has said he wants the U.S. Special Operations forces there to stay only as long as it takes to ensure the complete defeat of the Islamic State, which is still holding out in a last pocket of shrinking territory near the Iraqi border.

The Syrian Kurds, who have been allied with U.S. forces, have embarked on talks with Damascus aimed at salvaging some form of local autonomy from their conquests.