THE SPEED and enthusiasm with which two federal courts halted President Trump’s latest travel executive order might suggest that the revised policy is as obviously problematic as the last, which was a sloppy rush job that the government poorly defended in court. In fact, the revised policy, while still more likely to harm than help national security, is legally far more defensible. Decades of precedent instruct judges to defer to the executive branch on immigration and national security matters such as this. It should surprise no one if the Supreme Court eventually allows the Trump administration to proceed.

Among other problems, Mr. Trump’s first order essentially broke faith with foreigners who already had commitments from the government, violating their right to due process. In contrast, the revised order would not deny entry to lawful permanent residents or visa holders.

Two federal district court judges nevertheless blocked implementation of the new order this week, finding that it violates the First Amendment’s establishment clause, which demands separation between church and state. Judge Derrick K. Watson relied on previous rulings that government actions “must have a primary secular purpose” as he rejected the national security rationale the Trump administration offered and concluded that Mr. Trump’s transparent aim was “temporarily suspending the entry of Muslims.” Mr. Watson used the president’s previous calls for a Muslim ban, along with a variety of more recent statements from Trump associates, as evidence of Mr. Trump’s discriminatory intent.

But as a thoughtful counterpoint from five judges on the U.S. Court of Appeals for the 9th Circuit pointed out, the Constitution expressly stipulates that Congress can set naturalization policy, and Congress did so in the Immigration and Nationality Act of 1952 — which delegates the president broad authority to “suspend the entry of all aliens or any class of aliens as immigrants or nonimmigrants.” In the past, when the executive branch’s use of that authority has conflicted with other constitutional principles, such as freedom of speech and freedom of association, courts have been deferential to the executive, acknowledging that immigration policy is a complex subject in which a variety of interests can and do motivate decisions and in which the courts should be wary of substituting their judgment for that of the political branches. In general, courts bow to the government as long as it provides “a facially legitimate and bona fide reason” to act.

Even using that deferential standard, critics could argue that the national security justifications the Trump administration has provided for its order are too thin; as a policy matter, the order lacks the support of logic or evidence, despite the Trump administration’s late efforts to locate some. They could also argue that the establishment-clause interests are so strong that the courts must try to ascertain the president’s authentic motivations, as they have in other contexts.

But judges may well flinch at the prospect of conducting a trial to ferret out the president’s intentions and to craft new boundaries for the nation’s immigration policy, restrictions that could be binding on Mr. Trump’s successors, too. The critics have a strong case that the president’s executive order is self-defeating and offensive to American values. But it is far from clear that the courts will deem it illegal, too.