A federal appeals court on Wednesday ruled that the Department of Justice (DOJ) could withhold funding from cities and states that refuse to cooperate with the Trump administration's crackdown on undocumented immigrants.

A three-judge panel on the 2nd Circuit Court of Appeals unanimously overturned a district court judge's ruling that the department lacked the authority to impose immigration-related conditions on certain funding.

The panel’s opinion , written by Judge Reena Raggi, found that Congress had delegated authority to the attorney general to set conditions on the federal grant program it had created, called the Edward Byrne Memorial Justice Assistance Grant Program.

“Repeatedly and throughout its pronouncement of Byrne Program statutory requirements, Congress makes clear that a grant applicant demonstrates qualification by satisfying statutory requirements in such form and according to such rules as the Attorney General establishes,” wrote Raggi, who was appointed to the court by George W. Bush. “This confers considerable authority on the Attorney General.”

The states challenging the policy are New York, Connecticut, New Jersey, Rhode Island, Washington, Massachusetts and Virginia.

Sessions announced in 2017 that the DOJ would start withholding funding from the Byrne grant program, which provides criminal justice funding, from so-called sanctuary cities that helped shield undocumented immigrants from federal authorities.

"So-called 'sanctuary' policies make all of us less safe because they intentionally undermine our laws and protect illegal aliens who have committed crimes,” Sessions said at the time. “These policies also encourage illegal immigration and even human trafficking by perpetuating the lie that in certain cities, illegal aliens can live outside the law."

"From now on, the Department will only provide Byrne JAG grants to cities and states that comply with federal law, allow federal immigration access to detention facilities, and provide 48 hours notice before they release an illegal alien wanted by federal authorities," he added. "This is consistent with long-established cooperative principles among law enforcement agencies."

The states' lawsuit argued that the attorney general's coercion was unconstitutional.

"DOJ has thus forced the States into an untenable position: accept unlawful and unconstitutional conditions that diminish our sovereign ability to set our own law enforcement priorities and protect our communities, or forfeit Byrne JAG funding, thus undermining the vital programs that such funding supports," they wrote.

— This report was updated at 12:05 p.m.