The government passed a new press shield law on Wednesday evening, which will protect journalists—and their anonymous sources — from search warrants and police surveillance.

The Journalistic Source Protection Act, also known as Bill S-231, was first introduced by Conservative Senator Claude Carignan in November 2016 after news broke that Quebec and Montreal police had been spying on journalists to find the sources of embarrassing leaks.

The legislation passed unanimously on Wednesday evening with all three of the main political parties backing the motion.

Bill S-231 amends the Canada Evidence Act. The new bill allows journalists the right to refuse to disclose information or documents that could identify a source who has requested anonymity. Under the new law, security agencies will only be granted search warrants or production orders for a journalist’s source material if a judge decides there’s no other ‘reasonable’ way for them to get it or if the importance of the investigation outweighs the public interest of protecting a journalistic source.

“Now we have some protections for confidential sources, and it’s a very well-drafted piece of legislation.”

If a judge does approve the search warrant or a demand for a journalist’s information, law enforcement will have to meet the same criteria in order to be able to examine, reproduce or make copies of the seized material. The law also reverses the burden of proof from the journalist, who won’t be expected to defend their sources, to law enforcement agencies demanding access to the material.

Another key amendment being brought in by S-231 is to the Criminal Code—the law will ensure only a superior court judge is able to issue a search warrant against a journalist. This means justices of the peace won’t have that power, like they did during the recent controversy over police spying on journalists in Quebec.

Lawyers say this change is important because some justices of the peace come directly from the civil service and have a history of working closely with police. Critics say this could weaken their impartiality when evaluating the rights of journalists to protect sources versus the needs of law enforcement to gather information.

“It’s a great step for democratic society,” Mark Bantey, a media law specialist in Montreal told VICE News. “It’s a little late in coming, but who’s going to complain? Now we have some protections for confidential sources, and it’s a very well-drafted piece of legislation.”

In 2016, it was revealed that for months, Montreal police had been tracking journalist Patrick Lagacé’s phone calls and texts and following his movements through his phone’s GPS, in an effort to find the source of an internal leak. Police had obtained 24 warrants targeting Lagacé to find his confidential sources in the police department.

Quebec’s provincial police force also obtained orders allowing it access to phone records from six journalists. All of these requests were granted by justices of the peace, who will not longer be able to authorize investigations against journalists under the new legislation.

“By adopting the bill Parliament will be taking a strong stand for media rights”

Under S-231, Globe and Mail reporter Daniel LeBlanc, whose reporting on the Liberal Party’s sponsorship scandal was based on key information provided by an anonymous source known only as ‘ma chouette,’ would’ve had additional legal protections. The fight to protect the whistleblower went all the way to the Supreme Court in 2010, and the Globe ultimately won.

“By adopting the bill Parliament will be taking a strong stand for media rights, and bringing Canada closer into compliance with international standards for the protection of sources,” said Duncan Pike, advocacy coordinator for Canadian Journalists for Free Expression.

Before passing the bill, Canada had been one of the world’s only industrialized countries without “ legislation for the protection of journalists’ sources,” Pike said. Last year, Canada dropped four spots to 22nd in Reporters Without Borders’ World Press Freedom Ranking because of the “series of scandals” involving police spying on reporters.

The new law won’t protect sources who aren’t anonymous. Thus, it will not apply in the case of VICE News reporter Ben Makuch, who is looking to appeal to the Supreme Court an order to hand over screenshots of his messages with an alleged ISIS fighter to the RCMP. The order compelling Makuch to give up his communications has been upheld by two previous court decisions.

“We’re thrilled that Canadian journalists will now finally have some much needed protections if S-231 passes as is expected,” said president of VICE Canada Ryan Archibald. “This is bittersweet though, as VICE Canada still has a fight on its hands — this bill is a good first step but it does not go far enough to protect our own journalist, and Cyberwar host, Ben Makuch.”