Two MPs have won a High Court battle over laws which they say allow the police and security services to "spy on citizens" without proper safeguards.
The government said it "disagreed absolutely" with the court's ruling and would seek to appeal.
David Davis and Tom Watson argued the Data Retention and Investigatory Powers Act was incompatible with human rights.
It was fast-tracked through Parliament last July, allowing security agencies to gather phone and internet data.
Two High Court judges have now found that the act is "inconsistent with EU law".
The ruling effectively nullifies aspects of the legislation, although that order has been suspended until next March.
Mr Davis, a former Conservative minister and Mr Watson, a civil liberties campaigner who is standing to be Labour's deputy leader, brought the case in conjunction with campaign group Liberty.
The Data Retention and Investigatory Powers Act permits Britain's security agencies and some other public bodies to gather information about who suspects contact by telephone or email.
Analysis by legal affairs correspondent Clive Coleman
It is highly unusual, perhaps unprecedented, for MPs to challenge primary legislation in the courts.
Normally it would be scrutinised in Parliament, but the two MPs say that because the Data Retention and Investigatory Powers Act was rushed through in days, there was no time for proper parliamentary scrutiny and that this judicial review was their only option.
The law permits Britain's security agencies and some other public bodies to gather information about who suspects contact by telephone or email. Some half a million requests are made each year for this data.
The two MPs argued that the legislation was rushed, lacks adequate safeguards and must be rethought.
Their challenge comes as Home Secretary Theresa May drafts even more controversial proposals, dubbed the Snoopers' Charter. This legislation would allow agencies to gather huge amounts of information about how terrorism suspects and criminals use the internet and social media.
This does not include content but does include the fact that calls and emails are made, by whom, to whom and how often.
The MPs argued the act was incompatible with the right to a private and family life, and data protection, under both the Human Rights Act and the European Union Charter of Fundamental Rights.
The High Court, he added, has now ruled that aspects of the bill are unlawful because they are in breach of Article 7 and 8 of the EU Charter of Fundamental Rights and should be "disapplied".
Liberty said the ruling made clear that existing laws governing data retention and access to information required urgent review, as they had been hastily drafted and the safeguards were "extremely lax".
"It catches the communications of everyone in the UK including the emails, calls, texts and web activity of MPs, journalists, lawyers, doctors and other communications that may be confidential or privileged," Liberty said.
"The Act was rushed through Parliament and onto the statute books by ministers claiming 'emergency' legislation was necessary.
"It was privately agreed following discussions between the then three main party leaders. It became law within just three days - denying time for proper parliamentary scrutiny, amendment or even debate."
The government immediately indicated that it would appeal against the ruling.
Security Minister John Hayes said: "We disagree absolutely with this judgment and will seek an appeal. Communications data is not just crucial in the investigation of serious crime.
"It is also a fundamental part of investigating other crimes which still have a severe impact, such as stalking and harassment, as well as locating missing people, including vulnerable people who have threatened to commit suicide.
"The effect of this judgment would be that, in certain cases, communications data that could potentially save lives would only be available to the police and other law enforcement if a communications company had decided to retain it for commercial reasons.
"We believe that is wrong."