Germany's top court on Wednesday allowed a central security database aimed at keeping track of violent extremists to stand but said lawmakers must bolster its civil rights protections.
The ruling upheld the so-called "Anti-Terror Database" designed to pool information collected by police and intelligence services to prevent attacks. But it said it must be tweaked by the end of 2014 to include more data privacy provisions.
The scarlet-robed judges of the Federal Constitutional Court in the southwestern city of Karlsruhe said that attacks by militants represented an assault on the "common good as a whole" and had to be "fought with the tools of the state under the rule of law".
But it said "transparency" measures must be woven in and that officials charged with protecting data privacy must be given a clear watchdog role in the operation of the database.
A retired judge had filed the challenge to the 2007 law targeting Islamic extremists, in a complaint which also applied to an update passed last year to cover the far-right scene.
The plaintiff said he feared overreach by the authorities "like in the Nazi era by the Gestapo".
The German government welcomed the ruling.
"I think we can be happy that the constitutionality of this law has been upheld," Interior Minister Hans-Peter Friedrich told reporters, pledging to implement the changes the court ordered.
The Anti-Terror Database contains information on more than 16,000 people including names, dates of birth, addresses, bank details, religion as well as registered weapons and "skills relevant to terrorism".
Those listed include militant suspects but also their contacts.
The interior ministry said more than 80 percent of the people covered live abroad but are believed to belong to radical Islamic groups with ties to Germany.
The law has been controversial because it blurs the strict dividing line between law enforcement and intelligence established after World War II to stamp out the abuses of the Nazi period.
The ruling also covers data on known neo-Nazis which officials began collecting after a far-right trio blamed for the deaths of 10 people between 2000 and 2007 came to light.