Congress gave final approval Thursday to a package of strict sanctions punishing Russia for its cyberintrusions in last year’s elections, delivering a challenge to President Trump, who had sought flexibility to negotiate his own deal with Russian President Vladimir Putin.

The White House has been coy on whether Mr. Trump will sign the sanctions bill, which also includes new penalties against Iran and North Korea. Newly minted communications director Anthony Scaramucci said Mr. Trump may veto the bill and write tougher language himself.

But Congress likely would override his veto. The package cleared the House on Tuesday on a 419-3 vote and won passage in the Senate on Thursday on a 98-2 vote.

“The last eight months, what price has Russia paid for attacking American democracy? Very little. This legislation would begin to change that,” said Sen. John McCain, Arizona Republican. “The United States of America needs to send a strong message to Vladimir Putin and any other aggressor that we will not tolerate attacks on our democracy.”

The Russia sanctions codify a series of penalties President Obama imposed during his final months in office, letting the Treasury Department block property and other dealings of Russian entities and individuals the U.S. has accused of unsettling activity.

The latest bill lists some of those targets, such as the head of Russia’s military intelligence. It would also expand sanctions to the energy, railway and mining sectors of Russia’s economy.

Mr. Putin, traveling in Finland on Thursday, said he was growing tired of “loutish behavior toward our country.”

“We are behaving very composedly and patiently, but we will have to respond at a certain point,” Mr. Putin said, according to the TASS news agency.

The Kommersant newspaper reported that if Mr. Trump does sign the sanctions, Russia will expel dozens of American diplomats.

Mr. Obama used executive authority to issue the sanctions as retaliation for a series of what he called international transgressions, such as interfering in Ukraine and meddling in the U.S. election.

Capitol Hill feared Mr. Trump would use his own powers to lift the penalties without winning any real concessions from Moscow in exchange. The bill is a striking instance of congressional assertion on foreign policy — an area traditionally left to the president.

Still, lawmakers have taken pains in the legislation to respect the president’s authority. In order to block Mr. Trump from lifting sanctions, Congress would have to pass a resolution of disapproval, which the president would likely veto, requiring a two-thirds vote to override.

That was the same method adopted for the Iran nuclear deal. In that instance, most Democrats backed the president and helped preserve his free hand in carrying out the agreement.

For the Russian sanctions, the final sticking point was over whether Democrats would have power to force a vote in the House. In the end, the legislation allows for the top House Democrat to call for a vote on a resolution of disapproval in the event that the president tries to waive sanctions.

Lawmakers were prodded to pass the bill in part because of Mr. Trump’s inconsistent performance with regard to Russia.

The president has promised a tough approach at times and seemed inclined to ease penalties at other times.

The White House has been just as unclear about its approach to the sanctions bill. Over the weekend, Mr. Scaramucci hinted that Mr. Trump could veto the legislation, but new press secretary Sarah Huckabee Sanders said the White House found the current version acceptable.

On Thursday, Mr. Scaramucci remained noncommittal.

“He may sign the sanctions exactly the way they are or he may veto the sanctions and negotiate an even tougher deal against the Russians,” the top spokesman told CNN.

The votes this week marked a rare bipartisan accomplishment. Democrats have been resisting or obstructing most business, hoping to dent Mr. Trump’s agenda, but the sanctions bill marked a chance to attack the president by passing legislation.

Democrats said they were pleased that the bill, despite its sanctions against Iran, didn’t scuttle Mr. Obama’s nuclear deal with the Islamic republic. Instead, they said, it works the edges of that agreement to punish the regime in Tehran for supporting terrorists.

“This is about sending a message to Iran that when you violate the international order, there are consequences,” said Sen. Robert Menendez, New Jersey Democrat.

Senators also said to expect more action against North Korea as the U.S. seeks to strangle business ties between the isolated country and China, which accounts for most of North Korea’s economy.

The two senators who voted against the sanctions were Rand Paul, Kentucky Republican, and Bernard Sanders, Vermont independent.