Customs officers are legally allowed to search travelers' personal electronics without a warrant — whether they're visitors or American citizens.

A Harvard student said he was recently denied entry to the US after officers questioned him about his religion and then searched his phone and laptop and found that his friends had written anti-American social-media posts.

Rights groups have sued the US government over the practice, arguing that officers should be required to obtain a warrant before such an invasive search.

Travelers can refuse access to their devices, but customs officers are not obligated to allow someone into the country.

For now, lawyers recommend that travelers carry burner phones, encrypt their devices, or simply not bring electronics at all.

Visit Business Insider's homepage for more stories.

When you're entering the United States, federal agents have broad authority to search citizens and visitors alike — including their personal electronics.

That's what reportedly happened to Ismail Ajjawi, a Palestinian resident of Lebanon, when he tried to enter the US to start his first semester at Harvard.

Ajjawi told The Harvard Crimson that customs officers at Boston's Logan International Airport demanded he unlock his phone and laptop and then spent five hours searching the devices.

He said the officers asked him about his religion and about political, anti-American posts his friends had made on social media. A Customs and Border Protection representative told Insider that Ajjawi was "deemed inadmissible to the United States based on information discovered during the CBP inspection."

Read more: Immigration officers refused to let a Harvard student into the US after reportedly questioning him about his religion and his friends' anti-American social-media posts

Rights groups have sued the US government over the practice

A US Customs and Border agent processing passengers in Los Angeles. AP Photo/Nick Ut

Ajjawi is far from the only one. As CBP outlines in a tear sheet it provides to people at the border, federal agents can seize and search your phone without a warrant and even make a copy of it to have forensic experts analyze its contents off-site.

In 2017, the Electronic Frontier Foundation and the American Civil Liberties Union filed a lawsuit against the US Department of Homeland Security — on behalf of 11 people who had their phones and laptops searched at the border — meant to require the government get warrants before searching electronic devices.

How can they do that?

The Supreme Court decided in 1976 and 2004 that people have fewer claims to their Fourth Amendment privacy rights granted by the Constitution when entering the country, because the government has to protect its borders.

While the court has ruled that the police can't search people's phones inside the country without a warrant because they contain troves of personal information, it hasn't yet decided on a case about phone searches at the border.

"Searches of people at the border is an area where there's a wide gap between what we think people's rights are and what their facts are on the ground," Nathan Freed Wessler, a staff attorney with the ACLU, told Insider in February 2017. "Various courts haven't had an opportunity to weigh in on these issues yet, so CBP is operating with a lot of claimed authority and a lot of latitude."

Here's what you need to know about the practice and what you can do to protect the personal information on your electronic devices at the border:

Can you refuse to give them your phone?

A CBP officer checking the passport of a nonresident visitor to the US at McCarran International Airport in Las Vegas in December 2011. AP Photo/Julie Jacobson

Yes, you can refuse access to your phone, but border agents can then make your life difficult.

Wessler said agents could detain you — courts are divided on how long is too long — take your phone and try to unlock it on site, and even take your phone and send it to experts to unlock it.

They may also make copies of your devices to peruse later. The Department of Homeland Security says it will destroy that copy if the data is not "necessary for law-enforcement purposes."

If you are a citizen or a legal permanent resident, they have to let you back into the country eventually. If you are not a citizen, border agents can refuse your entry to the US.

"People need to decide whether they're willing to endure those inconveniences when they're deciding whether to give their password," Wessler said. "For noncitizens, visa holders, and others, people often need to consider whether there's a risk they'll be denied entry and turned away at the border for refusing to comply. We've received scattered results of that happening."

Can you say you have confidential files on your phone?

Lawyers, medical professionals, and journalists can say they have privileged, confidential files, Wessler said, but there is no guarantee agents will recognize it as a deterrent.

Customs and Border Protection has recognized that lawyers, in particular, have attorney-client privilege and that agents have to get approval from an agency attorney before proceeding with the search — but they can still search the phone.

Should you ask for a lawyer?

If you decline to unlock your phone and agents give you a hard time, Wessler said, you should feel "empowered" to ask for a lawyer, though you would have to pay for the person's services yourself. The government is not required to provide you with one.

If you expect to encounter issues at the border, Wessler said, it would help to carry a signed letter from your attorney saying he or she would represent you.

"US citizens and green-card holders have the right to request an attorney," he said. "It's not clear at all whether the government has been respecting those requests to the extent that it should."

How common is it?

The number of devices agents are looking through is increasing, according to CBP data and the ACLU and Electronic Frontier Foundation's lawsuit:

8,500 in 2015

19,000 in 2016

30,000 in 2017

33,000 in 2018

The 19,033 travelers who had their devices searched in 2016 were among 391 million travelers to the US that year.

From October 2008 to June 2010, by contrast, more than 6,500 people had their electronic devices searched at the border, nearly half of whom were US citizens, according to government data provided to the ACLU through a Freedom of Information Act request.

Wessler said that while seizing phones at the border "is not a new problem," the ACLU had seen an uptick in people saying their devices had been searched. Department of Homeland Security data shows that the number of devices searched increased 124% from the 2015 to 2016 fiscal years, a trend that looks poised to continue under the Trump administration.

The customs border crossing at Calexico, California. Josh Denmark/DHS

What that data doesn't reveal is the breakdown of people's race, religion, nationality, why agents decided to search the devices, or whether people's information gleaned from the searches is being stored in government databases.

Diane Maye, a plaintiff in the ACLU lawsuit who is a former Air Force captain, was detained in a small interrogation room at the Miami airport after returning from Norway.

Border agents asked her to unlock her laptop and phone, searched her computer, and then took her phone to another room for two hours, presumably to search it as well.

"As I sat in the interrogation room, I felt humiliated and violated," Maye said in 2017. "I worried that border agents would read my email messages and texts, look at my banking information, and look through my photos. I feared they would download all of my personal information and contact lists and share it with other government agencies.

"This was my life, and a border agent held it in the palm of his hand."

How can you protect your data?

First, Wessler says, travel only with the data you need. That may mean using burner phones or laptops for traveling. After all, he said, "authorities can't search what you don't have."

Second, use encryption services. The Electronic Frontier Foundation and Wired have exhaustive guides to keeping federal authorities — or hackers, for that matter — from accessing your data. Always choose long, strong, unique passwords for each device and account.

Third, turn your devices off before going through customs. This is when the encryption services are at their strongest.

These recommendations aren't options for everyone, but adopting one or more of them could help you keep your data under wraps.

Will this change in the future?

A CBP officer checking a passenger's documents and luggage. CBP

CBP maintains that searches are done to "protect the American people."

"Electronic device searches are integral in some cases to determining an individual's intentions upon entering the United States," John Wagner, the deputy executive assistant commissioner for CBP's Office of Field Operations, said in an agency press release in April 2017. "These searches, which affect fewer than one-hundredth of 1% of international travelers, have contributed to national security investigations, arrests for child pornography and evidence of human trafficking."

The 9th US Circuit Court of Appeals — covering Alaska, Arizona, California, Hawaii, Idaho, Montana, Nevada, Oregon, and Washington — has ruled that border agents must demonstrate a "reasonable suspicion" of criminal wrongdoing before doing a full forensic search of electronic devices, in which they download and analyze the full contents. They can do a cursory search, however, in which they thumb through the phone, without that suspicion.

The Supreme Court decided in Riley v. California in 2014 that searching cellphones without a warrant inside the US violated the Fourth Amendment's right to privacy.

The ACLU and Electronic Frontier Foundation's lawsuit is still playing out in court, but it could clarify whether agents are allowed to conduct such searches at ports of entry. The attorneys on the case said they hoped to force the government to get warrants before searching electronics at the border and to delete any information it's storing from devices searched this way.