Government data laws which critics say allow the police and security services to "spy on citizens" without proper safeguards are legally flawed and must be changed, the High Court has ruled.
Two judges in London declared the 2014 Data Retention and Investigatory Powers Act (Dripa), which permits the interception and retention of information from phone and internet communications, "inconsistent with EU law".
The ruling has alarmed the Government.
Security Minister John Hayes said: "We disagree absolutely with this judgment and will seek an appeal."
The judges granted Home Secretary Theresa May permission to appeal because of the public importance of their decision.
Conservative former shadow home secretary David Davis and Labour backbencher Tom Watson brought the legal challenge with campaigning members of the public Peter Brice and Geoffrey Lewis,.
The MPs applied for judicial review, saying they were concerned to protect the confidentiality of their contacts with constituents and other members of the public - including whistleblowers - who might approach them with sensitive information.
Lord Justice Bean and Mr Justice Collins declared that Section 1 of Dripa was flawed because it did not lay down clear and precise rules "strictly restricting" data retention to the purpose of preventing and detecting "precisely defined serious offences, or of conducting criminal prosecutions relating to such offences".
The judges also condemned data being retained without the safeguard of a prior review by a court or other independent administrative body.
They gave the Government and Parliament a deadline of March 2016 "to put matters right".
They warned that, if Parliament fails to act, they will disapply provisions contained in Section 1 which allow the retention of data without prior review, unless the data is held for "the prevention and detection of serious offences, or the conduct of criminal prosecutions".
Mr Davis, Conservative MP for Haltemprice and Howden, said: "The court has recognised what was clear to many last year - that the Government's hasty and ill-thought through legislation is fatally flawed.
He said the Government "will now have to rewrite the law to require judicial or independent approval before accessing innocent people's data".
Mr Watson, Labour MP for West Bromwich East, said: "The Government was warned that rushing through important security legislation would end up with botched law.
"Now the High Court has said they must come back to Parliament and do it properly."
But Mr Hayes said on behalf of the Government that the ruling could adversely affect fighting crimes such as stalking and harassment which might not come into the "serious crime" category.
Mr Hayes said: "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."
The MPs' successful challenge was backed by human rights pressure group Liberty.
Legal director James Welch said: "Liberty has long called for fundamental reform of our surveillance laws to ensure the public's rights are properly respected by our Government - the chorus of voices demanding change is now growing."
At a recent two-day hearing, Dinah Rose QC, for the MPs, argued that the legislation was rushed through in July last year "with enormous speed and hardly any scrutiny".
Ms Rose said both MPs appreciated that the authorities needed powers to fight crime and terrorism.
"Their concern is that this legislation doesn't contain the necessary minimum safeguards to protect against the risk of arbitrary, disproportionate or abusive retention and use of personal data, and for that reason it breaches the fundamental right to privacy."
Richard Drabble QC appeared on behalf of Mr Brice and Mr Lewis and argued that there were "fundamental legal reasons why the regime must fall".
A Government legal team, led by James Eadie QC, argued that provisions contained in the new data retention laws, combined with those already found in the Regulation of Investigatory Powers Act 2000 (Ripa), provided adequate safeguards and made the scheme compatible with EU law.
The judges disagreed.
Rachel Logan, Amnesty UK's legal programme director, said: "There's no doubt that the wheels have come off the Government's unlawful and unaccountable surveillance regime.
"Only last week Amnesty was informed that we too have been spied on, meaning we can't guarantee our contacts can communicate with us confidentially. Members of the public will want to know what on earth is going on."