Germany's highest court has ruled that laws allowing the country's BND foreign intelligence service to spy on foreigners' telecommunications outside Germany breach fundamental rights.
The Constitutional Court case was brought by foreign journalists, who say the ruling is a win for press freedom.
The BND will no longer be able to monitor the emails or other data of foreigners abroad, without good reason.
Currently some of that data is passed on to other countries' spy agencies.
The key question considered by the court was whether the German state was bound by the protections of the constitution outside the country.
The BND (Federal Intelligence Service) is already barred from snooping on German citizens' internet data abroad but the new ruling means that German spies will only be able to monitor foreign nationals abroad if there is evidence of a threat.
Among those bringing the case was the international organisation Reporters Without Borders, which advocates for press and information freedom globally.
Speaking before the verdict, the head of the group's German office, Christian Mihr, said journalists had to be able to guarantee the safety of their sources.
"We protect journalists so that they can go about their work, and doing work means that the sources, the informants that they have, can turn to them with confidence," he said. "With the lawsuit against the BND law, we want to strengthen the protection of sources and informants in the digital realm."
Responding to the ruling, BND chief Bruno Kahl said: "What is new is that fundamental rights apply internationally, which the court has ruled on for the first time."
He said the BND would help the government and parliament to amend the law, adding: "The protection of fundamental rights has to be considered, as well as fulfilment of the BND's mandate to keep Germany secure."
The court gave the German government until the end of 2021 to amend the law that regulates the BND in response to its ruling.
In February 2017 the German news magazine Der Spiegel reported that at least 50 telephone numbers used by international journalists had been monitored by the BND. Reporters working for the BBC, news agency Reuters and the New York Times newspaper were allegedly among those spied on. The BND made no comment on the report.
Beginning in 2013, the US whistleblower Edward Snowden exposed massive global surveillance by the US National Security Agency (NSA) which, he said, was assisted by the BND and UK spy agency GCHQ.