US District Judge Ellen Lipton Hollander said Musk's Department of Government Efficiency (DOGE) was intruding into "the personal affairs of millions of Americans" as part of its hunt for fraud and waste under President Donald Trump.
"To be sure, rooting out possible fraud, waste, and mismanagement in the SSA is in the public interest. But, that does not mean that the government can flout the law to do so," Hollander said.
The case has shed light for the first time on the amount of personal information DOGE staffers have been given access to in the databases, which hold vast amounts of sensitive data on most Americans.
The SSA administers benefits for tens of millions of older Americans and people with disabilities, and is just one of at least 20 agencies DOGE has accessed since January.
Hollander said at the heart of the case was a decision by new leadership at the SSA to give 10 DOGE staffers unfettered access to the records of millions of Americans.Â
She said lawyers for SSA had acknowledged that agency leaders had given DOGE access to a "massive amount" of records.
"The DOGE Team is essentially engaged in a fishing expedition at SSA, in search of a fraud epidemic, based on little more than suspicion. It has launched a search for the proverbial needle in the haystack, without any concrete knowledge that the needle is actually in the haystack," Hollander said.
One of the systems DOGE accessed is called Numident, or Numerical Identification, known inside the agency as the "crown jewels," three former and current SSA staffers told Reuters. Numident contains personal information of everyone who has applied for or been given a social security number.
Thursday's ruling is one of the most significant legal setbacks for DOGE to date. It comes two days after a federal judge ruled Musk's efforts to shut down the US Agency for International Development were likely illegal because he is not a Senate-confirmed cabinet official.
DOGE did not immediately respond to a request for comment.
A White House spokesman criticised the decision in a statement and said Trump will "continue to seek all legal remedies available to ensure the will of the American people goes into effect."
"This is yet another activist judge abusing the judicial system to try and sabotage the president's attempts to rid the government of waste, fraud, and abuse," spokesman Harrison Fields said.