Judges have ruled that a controversial clause in the Terrorism Act contravenes human rights if it is used to detain journalists, while examining claims police unlawfully detained a man linked to the Edward Snowden leaks.

The Court of Appeal was examining David Miranda's argument that he should never have been held at Heathrow for nine hours and been forced to give up files exposing extensive internet and phone surveillance by the US and its allies.

Mr Miranda, the Brazilian partner of former Guardian writer Glenn Greenwald, was detained by the Metropolitan Police at the airport in August 2013 under the Terrorism Act as revelations about the NSA and GCHQ continued.

Video: Interview with David Miranda

He had been travelling from Germany to Brazil via London, carrying records including encrypted material derived from data obtained by Mr Snowden during his work for the CIA.

Police seized his laptop, phones, DVDs, USB sticks and even video games consoles after holding him for the maximum of nine hours for questioning.

Mr Miranda had no right to a lawyer, to remain silent or resist the seizures under Schedule 7 of the Terrorist Act, which give police the power to stop anyone at a UK port or border to investigate whether they are involved in the “commission, preparation or instigation of acts of terrorism”.

But judges ruled that stopping Mr Miranda was lawful and that police “exercised the power for a permitted purpose”.

Lord Dyson, Master of the Rolls, told the court: “They were entitled to consider that material in his possession might be released in circumstances falling within the definition of terrorism.”

Mr Miranda had his laptop, phones, DVDs and USB sticks seized by police (AFP/Getty Images)

Lord Dyson, who is the most senior civil judge in England and Wales, Lord Justice Richards and Lord Justice Floyd dismissed a further ground of appeal, rejecting Mr Miranda's argument that the use of the stop power was “an unjustified and disproportionate interference with his right to freedom of expression".

They found that ”compelling national security interests“ outweighed Mr Miranda's rights under Article 10 of the European Convention on Human Rights but ruled in his favour on press freedom.

Judges concluded that, when “used in respect of journalistic information or material”, Schedule 7 violates freedom of expression because it is not “prescribed by law”.

“The power is not subject to sufficient legal safeguards to avoid the risk that it will be exercised arbitrarily,” Lord Dyson said. “The court therefore grants a certificate of incompatibility (with article 10).

The Snowden leaks revealed secret surveillance programmes (AFP/Getty Images)

"It will be a matter for Parliament to decide how to provide such a safeguard.

"The most obvious safeguard would be some form of judicial or other independent and impartial scrutiny conducted in such a way as to protect the confidentiality in the material."

They also questioned the broad definition of “terrorism” under the act, saying the word should not be applied to anyone who “unwittingly or accidentally” commits an action that endangers lives.

The case was heard at the Court of Appeal (Getty Images)

Mr Miranda said he was “thrilled” with the court ruling. “My purpose was to show UK's terrorism law violates press freedoms,” he wrote on Twitter. “And journalism isn't ‘terrorism.’ We won!”

One of his lawyers, Kate Goold, said terrorism must be kept within its “ordinary natural meaning to ensure that legitimate public interest journalism is not stifled through the use of draconian powers because of the fear of remote consequences”.

“The notion of a journalist becoming an ”accidental terrorist“ has been whole-heartedly rejected,” she added.

Mr Miranda had been helping his partner Glenn Greenwald, the journalist who broke allegations about the National Security Agency by whistleblower Edward Snowden (AFP)

Mr Greenwald hailed the ruling as a “huge win”, saying it showed the Terrorism Act “violates fundamental rights” in its lack of protection for journalists.

Mr Snowden, who is wanted under the US Espionage Act and has been granted temporary asylum in Russia, said the UK’s “effort to criminalise an act of journalism as terrorism” had been rejected.

The judgement was called a “major victory for the free press” by Liberty.

Rosie Brighouse, the campaign group’s legal director, said: “Schedule 7 has been a blot on our legal landscape for years - breathtakingly broad and intrusive, ripe for discrimination, routinely misused. Its repeal is long overdue.

"It is also a timely reminder of how crucial the Human Rights Act is for protecting journalists' rights. Once again it has come to the rescue of press freedom in the face of arbitrary abuse of power by the state."