Imagine it’s next December and evidence is mounting that Russian-backed entities were widely active across Facebook and Twitter ahead of the midterm elections.

Users are fleeing the social media companies, and members of Congress are talking about stringent legislation to restrict what the two firms can and can’t do. The stocks of Twitter and Facebook go into free fall.

Today, the two companies are spending large sums to prevent that outcome. But, in a possible sign of Wall Street’s myopia, investors are punishing them for doing so. When Facebook and Twitter this week reported a surge in costs, driven by efforts to secure their networks, investors dumped their stocks. Facebook’s shares plunged 19 percent Thursday, and Twitter’s slid 21 percent Friday.

The sheer amount of the spending may explain some of Wall Street’s nervousness. Facebook and Twitter don’t break out the costs directly related to locking down and cleaning up their networks, but broader measures of expenses ballooned. Company executives said the increases were partly driven by extra outlays related to making their services safe.

In the second quarter, Facebook’s costs totaled $7.4 billion, a 50 percent increase from a year earlier. Revenue growth was 42 percent. In the first half of 2018, Facebook’s capital expenditures, or spending on items like new servers and offices, rose 133 percent from the same period last year. At Twitter, they were up 243 percent.

The money goes to things like a Facebook “deletion center” in Germany that employs hundreds of content moderators.

Such operations are expensive, but investors should remember what happens when serious abuse is revealed. During the Cambridge Analytica imbroglio this year, Facebook’s shares tumbled 18 percent. The hit could be far worse if the company becomes the face of a scandal stemming from the midterms.

Investors might be cautious because they don’t know how big the security bill will be and whether the spending will be effective. But shareholders have been happy to go along with higher costs at Twitter and Facebook when it was for improving their networks for users and advertisers.

Senior executives at Facebook and Twitter seem to understand that if they don’t want to be closely regulated by the government, the companies need to show they can police themselves. The coming months should shed some light on whether they can.

Even if Facebook and Twitter avoid another Cambridge Analytica-style scandal, they still have to work hard to prevent their networks from becoming a source for hate speech and harassment. They may fail as they grapple with allowing some toxic messages and not others.

But Facebook’s and Twitter’s growth, financial success and stock performance almost depend on maintaining networks that feel safe to users. Explaining the investments in security, Jack Dorsey, Twitter’s chief executive, said, “We do believe, ultimately, over time, that this will help our growth story and encourage more people to stay with Twitter and also tell their friends, family and colleagues about all the value they’re getting out of it.”