In a ruling that may disrupt the UK government's push to introduce new surveillance legislation, the European Court of Justice (ECJ) has declared that under EU law the "general and indiscriminate" collection of data on a mass scale is unlawful.

The case, initially brought forward by Brexit Secretary David Davis and Labour's Tom Watson, revolved around privacy issues with the Data Retention and Investigatory Powers Act 2014 (Dripa), the predecessor to the Investigatory Powers Bill, which recently gained Royal Assent.

The legal challenge argued the UK proposals about mass retention of data, which forces telecommunications firms to store citizen data for 12 months, was illegal.

Now, after a years-long wrangle, the ECJ ruling has agreed, stressing communicaions data should also only be used to combat serious crime.

The UK Home Office has said it was "disappointed" with the ruling.

The regime was deemed illegal because it does not require that the data be kept within the EU, does not provide for notification after the event to people whose data has been accessed and it lets police and public bodies authorise their own access.

In the legal lingo of the court ruling, the judges said: "EU law precludes national legislation that prescribes general and indiscriminate retention of data.

"The fact that the data is retained without the users of electronic communications services being informed of the fact is likely to cause the persons concerned to feel that their private lives are the subject of constant surveillance. Consequently, only the objective of fighting serious crime is capable of justifying such interference.

"Such national legislation therefore exceeds the limits of what is strictly necessary and cannot be considered to be justified within a democratic society, as required by the directive, read in the light of the charter."

In response to the court's decision, Martha Spurrier, Liberty's director, said the judgement "upholds the rights of ordinary British people not to have their personal lives spied on without good reason or an independent warrant."

"It is the first serious post-referendum test for our Government's commitment to protecting human rights and the rule of law," she added. "The UK may have voted to leave the EU – but we didn't vote to abandon our rights and freedoms. The government must now make urgent changes to the Investigatory Powers Act to comply with this."

The Investigatory Powers Bill, also known as the Snoopers' Charter, gives police and intelligences agencies including GCHQ and MI5 spying, surveillance and hacking powers. These include the bulk collection and interception of citizen phone calls, messages and internet records.

After the ECJ decision, Tom Watson MP said: "This ruling shows it's counter-productive to rush new laws through Parliament without a proper scrutiny.

"At a time when we face a real and ever-present terrorist threat, the security forces may require access to personal information none of us would normally hand over. That's why it's absolutely vital that proper safeguards are put in place to ensure this power is not abused, as it has been in the recent past.

"Most of us can accept that our privacy may occasionally be compromised in the interests of keeping us safe, but no one would consent to giving the police or the government the power to arbitrarily seize our phone records or emails to use as they see fit.

GCHQ spied on CEO of Europe's biggest internet hosting company OVH, reveal Snowden files
GCHQ spies will get enhances spying powers under the new law GCHQ

"It's for judges, not ministers, to oversee these powers. I'm pleased the court has upheld the earlier decision of the UK courts."

Meanwhile, a Home Office spokesperson said: "We are disappointed with the judgement from the European court of justice and will be considering its potential implications.

"It will now be for the court of appeal to determine the case. The government will be putting forward robust arguments to the court of appeal about the strength of our existing regime for communications data retention and access.

"Given the importance of communications data to preventing and detecting crime, we will ensure plans are in place so that the police and other public authorities can continue to acquire such data in a way that is consistent with EU law and our obligation to protect the public."