Deadline to amend UK surveillance laws
High Court judges have given the UK government six months to revise parts of its Investigatory Powers Act.
Rules governing the British surveillance system must be changed quickly because they are "incompatible" with European laws, said judges.
The government has been given a deadline of 1 November this year to make the changes.
The court decision came out of legal action by human rights group Liberty.
It started its legal challenge to the Act saying clauses that allow personal data to be gathered and scrutinised violated citizens' basic rights to privacy.
The court did not agree that the Investigatory Powers Act called for a "general and indiscriminate retention" of data on individuals, as Liberty claimed.
However, the judges did call on the government to speed up the process of updating laws to ensure they were compatible with European Union legislation.
In late 2017, government ministers accepted that its Act did not align with European law which only allows data to be gathered and accessed for the purposes of tackling "serious crime". In addition, decisions to hold data must be subject to review.
By contrast, the UK law would see the data gathered and held for more mundane purposes and without significant oversight. The powers requiring data to be retained came into force in December 2016.
One proposed change to tackle the problems was to create an Office for Communications Data Authorisations that would oversee requests to data from police and other organisations.
The government said it planned to revise the law by April 2019 but Friday's ruling means it now has only six months to complete the task.
In a statement, security minister Ben Wallace said: "Liberty has for years created misplaced fear about this legislation, and we are pleased that the court recognises the importance of communications data in fighting crime and keeping families and communities safe."
He welcomed the "pragmatic" ruling that balanced security concerns with the right to privacy.
Martha Spurrier, director of Liberty, said the powers to grab data in the Act put sensitive information at "huge risk".
Javier Ruiz, policy director at the Open Rights Group which campaigns on digital issues, said: "We are disappointed the court decided to narrowly focus on access to records but did not challenge the general and indiscriminate retention of communications data."
Cash to support the legal challenge was raised via a crowdfunding website. Liberty has started another fundraising campaign to help bankroll the next stage of its legal challenge. This will seek to challenge "bulk interception" of browsing and calling habits.