A journalist's partner held for nine hours at Heathrow Airport under anti-terror laws has lost a Court of Appeal claim that police acted unlawfully - but won a ruling hailed as a victory for press freedom.
Leading judges in London ruled the use of a "stop power" in the case of David Miranda - partner of former Guardian writer Glenn Greenwald, who exposed secret information on US surveillance leaked by whistleblower Edward Snowden - was justified.
Brazilian Mr Miranda was detained by the Metropolitan Police at the airport in August 2013 under Schedule 7 of the Terrorism Act 2000 while he was travelling from Germany to Brazil.
He was questioned and items in his possession were taken from him, including encrypted material derived from data obtained by Mr Snowden,
Mr Miranda, who was carrying the material to assist Mr Greenwald in his journalistic work, claimed the acts of the police were unlawful, but the appeal court has ruled that they "exercised the power for a permitted purpose".
Master of the Rolls Lord Dyson, announcing the decision of the three-judge court, said of the police: "They were entitled to consider that material in his possession might be released in circumstances falling within the definition of terrorism."
Lord Dyson, Lord Justice Richards and Lord Justice Floyd dismissed a further ground of appeal. Lord Dyson said: "The court rejects Mr Miranda's argument that the use of the stop power against him was an unjustified and disproportionate interference with his right to freedom of expression despite the fact that this was a case involving an interference with press freedom."
The judge said "compelling national security interests" outweighed Mr Miranda's rights under Article 10 of the European Convention on Human Rights - the right to freedom of expression.
Although the judges dismissed Mr Miranda's appeal on the issue of the legality of the police actions, they ruled in his favour on another ground by holding that the stop power, "if used in respect of journalistic information or material", is incompatible with Article 10 because it is not "prescribed by law".
That aspect of the court's ruling was hailed as a victory for press freedom.
In court, Lord Dyson announced: "The power is not subject to sufficient legal safeguards to avoid the risk that it will be exercised arbitrarily. The court therefore grants a certificate of incompatibility.
"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."
Rosie Brighouse, legal officer for Liberty, said after the ruling: "This judgment is a major victory for the free press. 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."
Lord Dyson accepted there were "constraints" on the exercise of the stop power, "but in my judgment they do not afford effective protection of journalists' Article 10 rights".
He added: "The central concern is that disclosure of journalistic material (whether or not it involves the identification of a journalist's source) undermines the confidentiality that is inherent in such material and which is necessary to avoid the chilling effect of disclosure and to protect Article 10 rights.
"If journalists and their sources can have no expectation of confidentiality, they may decide against providing information on sensitive matters of public interest.
"That is why the confidentiality of such information is so important. It is, therefore, of little or no relevance that the Schedule 7 powers may only be exercised in a confined geographical area or that a person may not be detained for longer than nine hours.
"I accept that the fact that the powers must be exercised rationally, proportionately and in good faith provides a degree of protection. But the only safeguard against the powers not being so exercised is the possibility of judicial review proceedings."
Lord Dyson went on: "In my view, the possibility of such proceedings provides little protection against the damage that is done if journalistic material is disclosed and used in circumstances where this should not happen."
He concluded that the possibility of judicial review proceedings to challenge "the rationality, proportionality and good faith of a decision to interfere with freedom of expression in cases involving journalistic material cases does not afford an adequate safeguard".
Kate Goold, of law firm Bindmans, which represented Mr Miranda, said in a statement: "We welcome this court's principled and decisive ruling that Schedule 7 needs to come in line with other legislation to ensure that the seizure of journalistic material is protected by judicial safeguards."
John Halford, also part of the Bindmans legal team, said the court "has decided that taking effective action against terrorism involves using instruments that are fit for purpose, rather than those that are so blunt that they inevitably damage the interests of democratic societies based on free speech and the journalists that are their champions".
Rachel Logan, Amnesty UK's legal programme director, said: "This is human rights law doing exactly what it's meant to, weighing the arguments and standing up for freedom of the press.
"At base this case is about whether journalists are free to go about their work in Britain without interference dressed up as security.
"Human rights guard everything we value in society - freedom of the press and the right to challenge the state when it goes too far. We would do well to remember how precious those safeguards are."