He’s a criminal, but he “did the right thing” when it mattered – alerting cops to what he feared was a terror plot the day before the Fourth of July.

At about 5 p.m. yesterday, an unidentified thief with a police record broke into a red van that had been parked at 53rd Street and Second Avenue in Brooklyn’s Sunset Park for about a month, a source told The Post.

He was stunned when he looked inside – it was filled with gas cans and Styrofoam cups containing a mysterious white substance with protruding wires and switches.

The street is lined with brownstones, and there’s a ballet studio and a small Muslim school. So he drove the van 15 blocks to 37th Street and parked it at a desolate waterfront location behind the Costco store and next to some little-used piers.

Then he got out and called a cop he knows from his run-ins with the law.

“He did the right thing,” a high-ranking officer said. “And he possibly saved a lot of people’s lives.”

Another source said cops are unlikely to file charges for the break-in.

The van had Delaware plates that had been issued to another vehicle. It’s not clear when its cargo was put inside.

Police Commissioner Ray Kelly joined cops from the Bomb Squad and the anti-terror task force at the scene. Cops shut down three blocks.