LONDON — A British court has upheld a government policy that allows informants of the British domestic intelligence service to commit crimes when it is deemed to be in the interest of national security, in a case that judges said exposed a tension between the rule of law and public safety.

Under the policy, known as the Third Direction, officers of the Security Service, better known as MI5, can allow their nonstaff informants to engage in criminal activities when operating on cases of terrorism or organized crime, among others.

Much about the policy remains shrouded in secrecy: The MI5 has operated under the guidelines since the early 1990s, and the government only admitted the existence of the directive last year — even so, officials have not acknowledged any case where it has been applied. Four nongovernmental organizations in Britain mounted the legal challenge claiming that the policy was unlawful.

But by a slim 3-to-2 majority, judges of the Investigatory Powers Tribunal, the body that hears complaints about Britain’s intelligence services, dismissed the case on Friday, ruling the policy lawful and arguing that MI5 officers could not function without informants who could commit crimes, often while infiltrating criminal organizations.