SAN FRANCISCO — Demonized by members of Congress and under attack by state attorneys general, the three biggest credit rating companies in the country sent teams of Wall Street lawyers to San Francisco in April to stamp out a threat that could cost hundreds of millions of dollars.

It didn’t go well. The freedom of speech defense — which has historically protected Moody’s, Standard & Poor’s and Fitch Ratings from liability when investments go south — may not be up to the job these days, and a ruling from a San Francisco judge may be partly responsible.