For Vera Cooper, the time had come to buy a gun.

In her mid-70s at the time and widowed several years earlier, she was already feeling vulnerable, living by herself. Then came the tipping point: The plumbing business that Ms. Cooper owns in the Florida Panhandle had to fire a worker, and he stormed out of the office, threatening vengeance.

At a nearby gun store, she settled on a .22 caliber pistol that “felt good in my hand.” Then came the problem. A question on the firearms transaction form she was required to complete asked whether she was an “unlawful user” of marijuana or any other drug categorized by the federal government as a controlled substance.

Ms. Cooper is registered in Florida’s medical marijuana program and relies on the drug to ease her chronic knee pain and sleeplessness. She answered the query accordingly, and was told that as long as she held a marijuana card, buying a gun was not possible.

“I’d feel safer with a gun,” she said. But without the marijuana before bed, she said, she couldn’t sleep anyway. It was a tough choice.

There are relatively few limitations at the federal level on who is eligible to purchase or possess firearms and ammunition. The national background check system looks for issues like a criminal conviction, mental health problems, a dishonorable military discharge, unlawful immigration status or a domestic violence restraining order.

But even as a growing number of states have legalized marijuana, either for recreational or medical use, participating in a state’s medical marijuana system remains a barrier to gun ownership.

The issue is shaping up to be one of the next legal frontiers in the national debate over gun policy, as courts around the country are asked to determine whether the longstanding federal restriction on marijuana users conflicts with Second Amendment gun rights.

Medical marijuana is now legal in 38 states, the District of Columbia and four U.S. territories, and more than 3.5 million people are enrolled in state programs to use it to help with seizures, post-traumatic stress disorder, multiple sclerosis, Crohn’s disease, pain and other conditions. The constellation of medicinal users, however, is certainly much larger since several states, including California, do not require registration, and two dozen states have also legalized recreational marijuana.

But the drug remains technically illegal under federal law, which classifies it as having no currently accepted medical use. One of the places the restriction is being enforced is in firearms shops. Users like Ms. Cooper say the situation is depriving them of their constitutional right to own a gun.

There have not been many prosecutions for lying about marijuana on a gun form or for being caught possessing both, but the penalties can be stiff. Knowingly making a false statement on the document, for instance, is a felony punishable by up to 10 years in prison and fines. The Bureau of Alcohol, Tobacco, Firearms and Explosives has continued to issue warnings that possessing a gun as a marijuana user, either for medical or recreational purposes, is a serious crime.

Hunter Biden, the president’s son, was targeted under the law in September when he was indicted for lying about his crack cocaine addiction when he bought a gun in 2018.

“What we have been seeing are dilemmas in which people who may benefit from medical marijuana, which in many cases has been recommended by a doctor, are forced to choose between effective health care and their gun rights,” said Howard S. Wolfe, a former A.T.F. supervisor who consults on federal firearms policy for a law firm near Philadelphia.

Gun sales have been particularly robust over the past several years, with about 16.4 million sold last year, according to the National Shooting Sports Foundation, a firearms industry trade group, as more people look to guns as a means of self-defense.

Kevin Sabet, president of Smart Approaches to Marijuana, a group that opposes legalization, said it is prudent to prohibit both medical and recreational marijuana users from having firearms.

Stronger strains of marijuana, with higher levels of the psychoactive component THC than in decades past, as well as potent products like concentrates and edibles, can have adverse effects that result in violent behavior, Mr. Sabet said.

“We cannot keep viewing marijuana through the lens of Cheech and Chong, because today’s marijuana can cause extreme paranoia, delusions and psychosis,” he said. “The mixture of weed and guns is a dangerous combination.”

Critics of the prohibition argue that marijuana generally has a mellowing effect while, by contrast, alcohol has a documented role in fueling aggressive behavior. They note that heavy drinkers are not barred from buying firearms under federal law, although a number of states have laws that make it more difficult for people with alcohol problems to possess guns, and gun dealers have the option of not selling to someone who appears impaired.

“We don’t think anyone should have to surrender their constitutional rights to treat themselves with something far safer than alcohol,” said Karen O’Keefe, director of state policies for the Marijuana Policy Project.

In 2016, the U.S. Court of Appeals for the Ninth Circuit held that a Nevada woman was not allowed to purchase a gun since she had a state-issued medical marijuana card — even though she had claimed that she did not actually use the drug. But more recently, challengers to the marijuana-gun prohibition have had some success in the courts, chiefly as a result of a U.S. Supreme Court decision last year that struck down a New York law that had placed strict limits on carrying guns outside the home. The court ruled that gun laws must be “consistent with this nation’s historical tradition of firearm regulation.”

Based on that standard, federal judges in Oklahoma and Texas, as well as the U.S. Court of Appeals for the Fifth Circuit in New Orleans, found that blocking people who use marijuana from possessing firearms is a violation of their Second Amendment rights.

“You are starting to see cracks in the dam,” said William D. Hall, a lawyer representing Ms. Cooper and two other Florida residents in a lawsuit against the Justice Department.

The case in Oklahoma, which has legalized medical marijuana but allows no recreational use, started with Jared Harrison, who was driving to work at a marijuana dispensary when he was pulled over for a traffic violation in 2022. Police officers found a loaded revolver and marijuana in his car. Mr. Harrison, who told them he did not have a medical card, was arrested and ultimately indicted by a federal grand jury for possessing a firearm while being an unlawful user of marijuana.

Judge Patrick R. Wyrick, of the U.S. District Court for the Western District in Oklahoma, dismissed the case in February, writing that the federal law “takes a sledgehammer” to the right of armed self-defense. He contended that the use of marijuana was not “a violent, forceful or threatening act” nor did it carry any “of the characteristics that the nation’s history and tradition of firearms regulation supports.” Prosecutors said that they have appealed the judge’s ruling.

In Florida, Ms. Cooper’s legal challenge last year was led by Nikki Fried, the chair of the state Democratic Party, who was then Florida’s agriculture commissioner. It was dismissed by a federal judge, but a federal appeals court heard oral arguments on an appeal last month. Ms. Fried, who did not run for re-election, is no longer a party to the lawsuit.

Those opposed to the federal ban say it encourages people to purchase weapons or marijuana in the illicit markets, or to lie on the federal form about their marijuana use. “It certainly sets perverse incentives,” said Matthew Larosiere, a gun rights lawyer who has represented medical marijuana patients.

In Virginia, Deja Taylor, the mother of a 6-year-old boy who shot and seriously wounded his first-grade teacher in a classroom, was sentenced on Nov. 15 to 21 months in prison on federal charges of using marijuana while owning the firearm her child used, and for making false statements about her drug use when she bought the weapon.

Barring marijuana users and others from possessing guns dates back to the Gun Control Act of 1968, passed during a period of high-profile political assassinations in the country. But the number of people identified with a drug conflict is quite small. A 2022 report by the U.S. Sentencing Commission found that while the overwhelming majority of firearms offenders were in categories prohibited from owning guns, about 5 percent of that restricted lot were illegal drug users or were addicted to controlled substances at the time of their offenses.

Some states have tried to deal with the prohibition by affirming the gun rights of marijuana users, although such measures cannot prevent federal prosecutions, and it remains a crime under various state laws to carry a firearm while under the influence.

In Delaware, which legalized medical marijuana in 2011, a statute enacted this year making recreational use lawful removed possession of the drug from the list of circumstances that prohibit someone from having a gun under state law. Minnesota incorporated similar language into its law. Mississippi and Oklahoma have statutes providing that one cannot be denied the right to “own, purchase or possess” a firearm solely because that person is a medical marijuana patient or caregiver.

While those states cannot control the federal background check requirement, state medical marijuana programs generally do not ask card applicants whether they own a gun. That creates an opportunity for people to buy a gun first and then obtain a marijuana card. Don Spencer, president of the Oklahoma Second Amendment Association, says he provides this advice: “You better acquire all the firearms you want before you apply for that marijuana card.”

In a largely cash industry that is vulnerable to robberies, the issue of carrying guns is seen as potentially critical. Designated medical marijuana caregivers, who help registered patients acquire and use marijuana, are not subject to the same firearm restriction as their patients, as long as the caregivers are not users themselves, according to a 2019 F.B.I. memo on enforcement of the law.

Catherine Lewis, who owns two dispensaries and a grow operation in central Maine, is a licensed caregiver. But she also uses medical marijuana, as she said many caregivers do. She said she would prefer to have the option to carry a firearm for protection, since she delivers to patients in remote, rural areas with little to no police presence. Instead, she carries a stun gun and wasp spray.

“I’ve had to get firm with my delivery policies because of safety concerns,” said Ms. Lewis, who is the board chair of the Medical Marijuana Caregivers of Maine Trade Association. “I need to meet the people first in my store or have their doctors vouch for them.”

Ms. Lewis noted that last month she received inquiries from some members who wanted guidance on whether they could protect themselves with firearms during the manhunt for Robert Card, who killed 18 people in a mass shooting in Lewiston on Oct. 25. “I told them to do whatever they needed to do for themselves and their families,” she recalled.

The federal government’s stance on medical marijuana and guns has also been a complicating factor for veterans, many of whom hold armed security jobs and rely on the drug to treat symptoms of PTSD, traumatic brain injuries and other health complications.

“So, they are just lying when they go to buy a gun or on their applications for their jobs,” said Sean Kiernan, chief executive officer of the Weed for Warriors Project, a nonprofit group that helps veterans get access to free or discounted medical marijuana.

Mr. Kiernan noted that some veterans are buying firearms or marijuana — or both — in the illegal marketplace so there is no official record. And some, he said, are making their own so-called ghost guns at home. “It’s creating a mockery of the system,” he said.

With veterans in mind, Representative Brian Mast, a Republican from Florida who is a veteran himself, reintroduced the Gun Rights and Marijuana Act in April. The bill, which the congressman’s office said is pending in the House Judiciary Committee, would allow marijuana consumers to purchase guns in states where medical or recreational use is allowed.

Phil Gruver, 41, a truck company technician in Pennsylvania, got his medical marijuana card in 2017 when he already owned a handgun and a rifle for protection and outings at a local range. He said he had decided to disregard the federal law.

“I love guns and I’m not turning mine in because I believe that I still have a constitutional right to have them,” he said.

Mr. Gruver uses marijuana to help him with PTSD and depression that were brought on by traumatic episodes earlier in his life. The marijuana has enabled him to significantly cut back on his prescribed mental health medications — whose use does not bar gun ownership.

“The only things I’m killing by using marijuana medicinally are my symptoms and pizza and ice cream while I’m sitting on my couch,” Mr. Gruver said.

Research was contributed by Jack Begg, Susan C. Beachy, Kitty Bennett, Alain Delaquérière and Kirsten Noyes.