Rules allowing MI5 informants to commit crimes are lawful, a tribunal has ruled.
Human rights groups had argued that the government's policy was unlawful and could hide serious abuses.
But the Investigatory Powers Tribunal (IPT), in a 3-2 ruling, said MI5 has "an implied power" to allow crimes under the Security Service Act, although not to grant immunity.
Campaigners said they will appeal the "knife-edge" ruling.
The case came a year after the government confirmed the existence of a previously secret document, dubbed the "Third Directive".
'Most profound issue'
Signed by former Prime Minister David Cameron, it confirmed that MI5 officers could allow their informants and agents to commit crimes in the national interest, without any duty to tell police and prosecutors.
Announcing the decision, IPT president Lord Justice Singh said: "This case raises one of the most profound issues which can face a democratic society governed by the rule of law."
Under the Security Service Act 1989, Lord Singh said MI5 had "implied power ...to engage in the activities which are the subject of the policy under challenge".
However, he added: "It is important to appreciate that this does not mean that it has any power to confer immunity from liability under either the criminal law or the civil law ...on either its own officers or on agents handled by them.
"It does not purport to confer any such immunity and has no power to do so."
This case reveals a fundamental tension between two vital interests: preventing crime and protecting national security.
Government lawyers were insistent that MI5 needs to be able to send agents deep inside terror networks, and, in practical terms, how else could they appear to be credible if they were not seemingly prepared to go along with the plotting?
Secondly, if the rules around such criminality were published, would the targeted suspects not just use them to sniff out the rat?
The IPT, behind closed doors, is thought to have examined records of crimes dating back to October 2000 - around the time the Human Rights Act came into force.
This raises a question: are there records of crimes that occurred before these important safeguards were introduced?
In Northern Ireland there are unresolved allegations of collusion by the security forces in serious crimes during the Troubles.
And while security chiefs will welcome this case as protecting the work of brave agents - it doesn't provide answers to the secrets of the past.
The legal action was taken against the government by four human rights groups.
Privacy International, Reprieve, the Committee on the Administration of Justice, and the Pat Finucane Centre asked the IPT to declare the policy unlawful and grant an injunction "restraining further unlawful conduct".
They argued that the policy "purports to permit (MI5) agents to participate in crime" and effectively "immunises criminal conduct from prosecution".
During the tribunal, lawyers for the Security Service had said: "This is not a 'nice to have power'... it is critical.
"The whole point of the agent involvement is to avoid loss of life and limb."
Responding to the ruling, Maya Foa, director of Reprieve, said the narrow 3-2 majority ruling showed "just how dubious the government's secret policy is".
'Abusive secretive power'
"Our security services play a vital role in keeping this country safe, but history has shown us time and again the need for proper oversight and common sense limits on what agents can do in the public's name."
Ilia Siatitsa, a legal officer at Privacy International, said the group would seek to appeal what it called an "abusive secretive power". She added: "We think the bare majority of the IPT got it seriously wrong."