Espionage
In United States law, treason, espionage, and spying are separate crimes. Treason and espionage have graduated punishment levels.
The United States in World War I passed the Espionage Act of 1917. Over the years, many spies, such as the Soble spy ring, Robert Lee Johnson, the Rosenberg ring, Aldrich Hazen Ames, Robert Philip Hanssen, Jonathan Pollard, John Anthony Walker, James Hall III, and others have been prosecuted under this law.

History of espionage laws: From ancient times, the penalty for espionage in many countries was execution. This was true right up until the era of World War II; for example, Josef Jakobs was a Nazi spy who parachuted into Great Britain in 1941 and was executed for espionage.
In modern times, many people convicted of espionage have been given penal sentences rather than execution. For example, Aldrich Hazen Ames is an American CIA analyst, turned KGB mole, who was convicted of espionage in 1994; he is serving a life sentence without the possibility of parole in the high-security Allenwood U.S. Penitentiary. Ames was formerly a 31-year CIA counterintelligence officer and analyst who committed espionage against his country by spying for the Soviet Union and Russia. So far as it is known, Ames compromised the second-largest number of CIA agents, second only to Robert Hanssen, who also served a prison sentence until his death in 2023.

Use against non-spies: Espionage laws are also used to prosecute non-spies. In the United States, the Espionage Act of 1917 was used against socialist politician Eugene V. Debs (at that time the Act had much stricter guidelines and amongst other things banned speech against military recruiting). The law was later used to suppress publication of periodicals, for example of Father Coughlin in World War II. In the early 21st century, the act was used to prosecute whistleblowers such as Thomas Andrews Drake, John Kiriakou, and Edward Snowden, as well as officials who communicated with journalists for innocuous reasons, such as Stephen Jin-Woo Kim.
As of 2012, India and Pakistan were holding several hundred prisoners of each other's country for minor violations like trespass or visa overstay, often with accusations of espionage attached. Some of these include cases where Pakistan and India both deny citizenship to these people, leaving them stateless. The BBC reported in 2012 on one such case, that of Mohammed Idrees, who was held under Indian police control for approximately 13 years for overstaying his 15-day visa by 2–3 days after seeing his ill parents in 1999. Much of the 13 years were spent in prison waiting for a hearing, and more time was spent homeless or living with generous families. The Indian People's Union for Civil Liberties and Human Rights Law Network both decried his treatment. The BBC attributed some of the problems to tensions caused by the Kashmir conflict.

Espionage laws in the UK: Espionage is illegal in the UK under the National Security Act 2023, which repealed prior Official Secrets Acts and creates three separate offences for espionage. A person is liable to be imprisoned for life for committing an offence under Section 1 of the Act, or 14 years for an offence under Sections 2 and 3

Government intelligence laws and its distinction from espionage: Government intelligence is very much distinct from espionage, and is not illegal in the UK, providing that the organisations of individuals are registered, often with the ICO, and are acting within the restrictions of the Regulation of Investigatory Powers Act (RIPA). 'Intelligence' is considered legally as "information of all sorts gathered by a government or organisation to guide its decisions. It includes information that may be both public and private, obtained from much different public or secret sources. It could consist entirely of information from either publicly available or secret sources, or be a combination of the two."
However, espionage and intelligence can be linked. According to the MI5 website, "foreign intelligence officers acting in the UK under diplomatic cover may enjoy immunity from prosecution. Such persons can only be tried for spying (or, indeed, any criminal offence) if diplomatic immunity is waived beforehand. Those officers operating without diplomatic cover have no such immunity from prosecution".
There are also laws surrounding government and organisational intelligence and surveillance. Generally, the body involved should be issued with some form of warrant or permission from the government and should be enacting their procedures in the interest of protecting national security or the safety of public citizens. Those carrying out intelligence missions should act within not only RIPA but also the Data Protection Act and Human Rights Act. However, there are spy equipment laws and legal requirements around intelligence methods that vary for each form of intelligence enacted.

War: In war, espionage is considered permissible as many nations recognize the inevitability of opposing sides seeking intelligence each about the dispositions of the other. To make the mission easier and successful, combatants wear disguises to conceal their true identity from the enemy while penetrating enemy lines for intelligence gathering. However, if they are caught behind enemy lines in disguises, they are not entitled to prisoner-of-war status and subject to prosecution and punishment—including execution.
The Hague Convention of 1907 addresses the status of wartime spies, specifically within "Laws and Customs of War on Land" (Hague IV); October 18, 1907: CHAPTER II Spies". Article 29 states that a person is considered a spy who, acts clandestinely or on false pretences, infiltrates enemy lines with the intention of acquiring intelligence about the enemy and communicate it to the belligerent during times of war. Soldiers who penetrate enemy lines in proper uniforms for the purpose of acquiring intelligence are not considered spies but are lawful combatants entitled to be treated as prisoners of war upon capture by the enemy. Article 30 states that a spy captured behind enemy lines may only be punished following a trial. However, Article 31 provides that if a spy successfully rejoined his own military and is then captured by the enemy as a lawful combatant, he cannot be punished for his previous acts of espionage and must be treated as a prisoner of war. This provision does not apply to citizens who committed treason against their own country or co-belligerents of that country and may be captured and prosecuted at any place or any time regardless whether he rejoined the military to which he belongs or not or during or after the war.
The ones that are excluded from being treated as spies while behind enemy lines are escaping prisoners of war and downed airmen as international law distinguishes between a disguised spy and a disguised escaper. It is permissible for these groups to wear enemy uniforms or civilian clothes in order to facilitate their escape back to friendly lines so long as they do not attack enemy forces, collect military intelligence, or engage in similar military operations while so disguised. Soldiers who are wearing enemy uniforms or civilian clothes simply for the sake of warmth along with other purposes rather than engaging in espionage or similar military operations while so attired are also excluded from being treated as unlawful combatants.
Saboteurs are treated as spies as they too wear disguises behind enemy lines for the purpose of waging destruction on an enemy's vital targets in addition to intelligence gathering. For example, during World War II, eight German agents entered the U.S. in June 1942 as part of Operation Pastorius, a sabotage mission against U.S. economic targets. Two weeks later, all were arrested in civilian clothes by the FBI thanks to two German agents betraying the mission to the U.S. Under the Hague Convention of 1907, these Germans were classified as spies and tried by a military tribunal in Washington D.C. On August 3, 1942, all eight were found guilty and sentenced to death. Five days later, six were executed by electric chair at the District of Columbia jail. Two who had given evidence against the others had their sentences reduced by President Franklin D. Roosevelt to prison terms. In 1948, they were released by President Harry S. Truman and deported to the American Zone of occupied Germany.
The U.S. codification of enemy spies is Article 106 of the Uniform Code of Military Justice. This provides a mandatory death sentence if a person captured in the act is proven to be "lurking as a spy or acting as a spy in or about any place, vessel, or aircraft, within the control or jurisdiction of any of the armed forces, or in or about any shipyard, any manufacturing or industrial plant, or any other place or institution engaged in work in aid of the prosecution of the war by the United States, or elsewhere".

Spy fiction: Spies have long been favorite topics for novelists and filmmakers. An early example of espionage literature is Kim by the English novelist Rudyard Kipling, with a description of the training of an intelligence agent in the Great Game between the UK and Russia in 19th century Central Asia. An even earlier work was James Fenimore Cooper's classic novel, The Spy, written in 1821, about an American spy in New York during the Revolutionary War.
During the many 20th-century spy scandals, much information became publicly known about national spy agencies and dozens of real-life secret agents. These sensational stories piqued public interest in a profession largely off-limits to human interest news reporting, a natural consequence of the secrecy inherent in their work. To fill in the blanks, the popular conception of the secret agent has been formed largely by 20th and 21st-century fiction and film. Attractive and sociable real-life agents such as Valerie Plame find little employment in serious fiction, however.