The police detention of the partner of a former Guardian journalist at Heathrow Airport in 2013 was lawful, the Court of Appeal has decided.
David Miranda was carrying material from US whistleblower Edward Snowden about security services' surveillance.
He argued the stop had been a disproportionate use of anti-terrorism powers and breached human rights law.
Judges did not agree with his claim, but did say existing laws did not offer enough safeguards for reporters.
They said the use of legislation under the Terrorism Act 2000 at ports and airports, "in respect of journalistic information or material", is incompatible with Article 10 of the European Convention on Human Rights - freedom of expression - and should be examined by Parliament.
Brazilian Mr Miranda is the partner of Glenn Greenwald, who had written a series of stories about spying by the US and UK.
He said he had been "acting in support of Mr Greenwald's activities as a journalist" when stopped while in transit from Germany to Brazil on 18 August 2013.
Mr Miranda was held by Metropolitan Police officers at Heathrow for nine hours.
He was carrying encrypted material "derived from the data obtained by Mr Snowden", a former US National Security Agency contractor who has been granted temporary asylum in Russia.
It included 58,000 highly-classified documents from the UK's Government Communications Headquarters (GCHQ).
Mr Miranda brought a judicial review against the home secretary and the Metropolitan Police, and, in February 2014, the High Court said while the action amounted to "indirect interference" with press freedom, it had to be balanced against national security considerations.
Code of practice
The Court of Appeal said the authorities had been justified in detaining Mr Miranda under Schedule 7 of the Terrorism Act 2000.
"The police exercised the power for a permitted purpose. They were entitled to consider that material in his possession might be released in circumstances falling within the definition of terrorism," it said.
The Home Office maintains rules now in place give adequate protection to journalists, but lawyers for Mr Miranda say they do not go far enough and legislation is required.
A Home Office spokesman said: "We have always been clear that David Miranda's examination by police under Schedule 7 was lawful and proportionate. The Court of Appeal's judgement in this case supports the action taken by police to protect national security.
"In 2015 we changed the code of practice for examining officers to instruct them not to examine journalistic material at all. This goes above and beyond the court's recommendations in this case."
Rosie Brighouse, from human rights group Liberty, said the judgement relating to police stops involving journalist material was a "major victory for the free press".
John Halford, from Bindmans, the law firm which represented Mr Miranda, said the court "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".