Exposing foreign governments’ meddling in U.S. politics is a worthy goal. Infringing on First Amendment freedoms is no way to go about it.

A federal appeals court, confirming a lower-court ruling, has just struck down as unconstitutional key provisions of a Maryland law called the Online Electioneering Transparency and Accountability Act, passed by Annapolis lawmakers last year following alarms over the Kremlin’s use of social media to influence U.S. public political discussion.

The law sought to regulate all but the smallest platforms that accept political ads, including newspapers as well as social media sites like Google and Facebook. In particular, it required platforms to retain a great deal of information about political ads they carried and make it available on request to state election regulators, including when each ad had run, what its planned or actual target audience was, and how many impressions of it had been served. Meanwhile, in the name of public transparency, the law required each platform to publish within 48 hours on its site searchable records of who had purchased political ads and how much they had paid.

Some of the terms of the law were vague, such as a definition of “campaign materials” to include material that relates to a “prospective candidate” or a position for or against either an actual or a prospective ballot measure. Lots of issues become the subject of prospective ballot measures, whether or not they ever make it through the process to reach the voters.

It’s true that under the series of Supreme Court cases kicked off by Buckley v. Valeo, the U.S. Supreme Court has upheld some restrictions on campaign speech. But it’s always been speech by those who are themselves seeking to influence election outcomes — i.e. candidates, donors and entities like PACs and parties. What it has not allowed in the name of campaign regulation is ordering media outlets, or third parties,to say one thing or refrain from saying another. (Broadcasters historically have come under different rules because they use airwaves, which are seen as a resource doled out by government.)

These principles have been around for a while, which is why Maryland legislators had to ignore repeated warnings that the bill would probably be found unconstitutional. Gov. Larry Hogan refused to sign it citing that reason; it became law anyway.

Making matters worse, the provisions were likely to hobble some ways voters learn about local candidates and controversies. Google immediately stopped hosting political ads in Maryland, a step particularly unhelpful to newcomer candidates, for whom advertising may be one of the few effective ways to boost name recognition. Other platforms, including some Maryland newspapers, also faced a tough position as the effective date of the law drew near. Rather than publish disclosures that might expose to competitors’ eyes confidential information about their ad rates and viewer reach, they might prefer just to immunize themselves by turning down political and issue ads in the future as a category.

Newspapers sued, and in January a federal district court struck down the relevant provisions of the law as a violation of the First Amendment. That ruling was upheld Friday by a unanimous panel of the U.S. Court of Appeals for the Fourth Circuit.

Judge J. Harvie Wilkinson’s opinion for the court called the law “a compendium of traditional First Amendment infirmities.”

“Requiring the press itself to disclose the identity or characteristics of political speakers is a problematic step,” the court wrote.

Nor did it help that the provisions of the law were a remarkably bad fit for the problem it sought to address. In the first place, the court noted, Russian interference had largely taken the form not of paid advertising, but of the use of dummy accounts posing as American individuals or groups. When the trollmasters had gone further, as by organizing pretend events, they had mostly not done so by backing particular candidates or ballot measures, the types of advertising caught by the act. Instead, their favored tactic was to widen divisions by promoting both sides of much-inflamed issues like police conduct, race and gun control — material that in nearly every instance the Maryland rules would fail to catch.

In reciting the oath of office, elected officials vow to uphold the Constitution. That should mean carefully considering the constitutionality of laws before they vote. Let’s hope next time the legislature takes this lesson to heart.