WASHINGTON — The Supreme Court ruled on Wednesday in favor of a fired air marshal, saying he was covered by a federal law protecting whistle-blowers.

Chief Justice John G. Roberts Jr., writing for the majority in the 7-to-2 decision, said that the ruling might create security problems by entrusting the confidentiality of sensitive security information to “the idiosyncratic judgment” of each of the Transportation Security Administration’s employees, but that Congress could address the issue by amending the law. The president could also prohibit disclosure of the information by executive order.

“It is not our role to do so for them,” Chief Justice Roberts wrote.

In 2003, the air marshal, Robert J. MacLean, received a secret briefing from the T.S.A. about a terrorist threat affecting long-distance flights involving weapons smuggled onto aircraft in cameras and children’s toys. Two days later, he received a text message from the security agency saying that it was canceling assignments that required an overnight stay in order to save money.

Mr. MacLean complained to his superiors, saying the move would imperil public safety because air marshals would be absent from some important flights. They responded that the cuts were required by a budget shortfall.