This week, U.S. Customs and Border Protection suffered an extraordinary defeat in New Hampshire. A state court found that CBP broke the law by setting up “checkpoints” far from the border to ensnare marijuana users and that local law enforcement violated the state constitution by colluding with CBP officers to search vehicles for drugs. As a result, the court suppressed all evidence of drug possession gathered by police at the checkpoints. Its decision is a stinging rebuke of federal immigration agents’ invasive tactics—which have escalated dramatically under President Donald Trump—as well as an unqualified victory for those states that have refused to cooperate with Trump’s immigration dragnets.

The trouble in New Hampshire began in August and September. CBP, which has been emboldened by the Trump administration to resume controversial practices that declined under President Barack Obama, decided to set up a checkpoint in Woodstock. Although the town is 90 miles from Canada, CBP is authorized to conduct searches up to 100 miles from the nearest international border. The ostensible purpose of the checkpoint was to enforce immigration law, and officers demanded proof of citizenship from drivers who passed through. But they also brought drug-sniffing dogs, allegedly to help them “detect concealed humans.”

As the American Civil Liberties Union of New Hampshire later exposed in court, the real purpose of these drug-sniffing dogs was, naturally, to find drugs. But the U.S. attorney for the state does not bring federal charges against those carrying small amounts of cannabis. So CBP asked the Woodstock Police Department to participate in its checkpoint. The two agencies decided to work together. CBP would stop drivers and have a dog sniff their cars. If the dog “signaled,” a CBP officer would search the car. If the officer found drugs, he would hand them off to WPD, which would press charges for drug possession.

This cooperation was a gift to the Woodstock police for two reasons. First, New Hampshire’s marijuana decriminalization law would take effect within just weeks; by working with CBP, local law enforcement could maximize its marijuana prosecutions before it lost the power to arrest cannabis users. Second, and more importantly, the New Hampshire Constitution prohibits canine searches of vehicles without a reasonable suspicion of criminal activity—unlike the federal Fourth Amendment, which permits them. Thus, by teaming up with CBP, the Woodstock police hoped to reap the rewards of searches it could not legally conduct on its own.

CBP found cannabis on several dozen people, and state prosecutors brought charges against most of them. Sixteen of those defendants—represented by the New Hampshire ACLU, along with co-counsel Buzz Scherr and Mark Sisti—decided to fight back. When New Hampshire police find contraband during an unlawful search, state courts suppress the evidence to safeguard the defendant’s rights. Although the drugs in these cases had been found by federal agents, state prosecutors brought state charges in a state court. And, under state law, the evidence was seized illegally. As a result, Sisti and the ACLU asked Judge Thomas Rappa of the Plymouth District Court to suppress all evidence gathered during the checkpoint.

Now, four months after a contentious hearing at which several CBP agents stretched the limits of the truth under oath, Rappa has agreed. In his order, the judge explained that, because “the defendants in this matter are facing prosecution in the state court for violation of state laws,” the “constitutional protections of the New Hampshire Constitution should apply.” He then held that “the evidence would be inadmissible if seized by law enforcement officials because there was no articulable reasonable suspicion that any of these defendants was involved in criminal activity prior to the initial dog search.” The “admissibility of the evidence,” he wrote, “does not change based on the fact that it was seized by federal officers then handed over the state.”

In an astonishing passage, Rappa found that CBP had attempted to deceive the court.

Rappa added that not all of the evidence had been seized by CBP: In reality, Woodstock police helped federal agents conduct the searches. CBP agents attempted to obscure this fact at trial, strenuously denying their collusion with the WPD. But their conflicting stories fell apart under Sisti’s questioning, and Rappa made clear in his decision that he did not believe them. “There were times when WPD actually seized contraband from the defendant’s vehicle,” he wrote. Local law enforcement even helped with “traffic control” and “supervision of detainees.” In sum, Rappa found that “the state and federal authorities were absolutely working in collaboration with each other.”

The judge could have stopped there; after all, this finding was sufficient to suppress all evidence collected by WPD, thwarting the state’s prosecutions. But he noted that this collusion raised the broader issue of whether CBP’s actions were legal under federal law. The U.S. Supreme Court has found that immigration checkpoints are constitutional under the Fourth Amendment as long as their true purpose is to detect immigration violations. It has also ruled that checkpoints designed to detect drug activity are unconstitutional. The question for Rappa, then, is whether CBP lied when it alleged that its check was meant to enforce immigration law rather than to ferret out drug users.

In an astonishing passage, Rappa found that CBP had attempted to deceive the court and that the agency’s real intention was to search cars for drugs with no reasonable suspicion. “While the stated purpose of the checkpoints in this matter was screening for immigration violations,” the judge wrote, “the primary purpose of the action was detection and seizure of drugs.” CBP agents were “aware of that prior to setting up the checkpoints which is precisely why they felt the need to reach out to the state and local agencies for assistance.” Rappa pointed to emails exchanged between CBP and WPD that make it “patently clear that the primary purpose” of WPD’s presence was to seize their drugs for state prosecution. “As such,” he concluded, “the checkpoints were unconstitutional under both state and federal law.”

New Hampshire may appeal Rappa’s decision, but it’s difficult to imagine the state winning in a higher court. The facts cut sharply against both CBP and WPD, neither of which expected to have to defend their conduct under oath. Unfortunately, with the Trump administration’s encouragement, CBP officers routinely abuse their power and rarely face consequences—even for outright lawlessness. Rappa’s ruling serves as a crucial reminder that the agency can’t always use the pretext of illegal immigration to curtail civil liberties. Just ask the 16 defendants who proved this week that even under Trump, federal immigration agents may still be hauled into a court of law and held accountable for inflicting injustice.