SAN FRANCISCO — Ali Alzabarah was an engineer who rose through the ranks at Twitter to a job that gave him access to personal information and account data of the social media service’s millions of users.

Ahmad Abouammo was a media partnerships manager at the company who could see the email addresses and phone numbers of Twitter accounts.

On Wednesday, the Justice Department accused the two men of using their positions and their access to Twitter’s internal systems to aid Saudi Arabia by obtaining information on American citizens and Saudi dissidents who opposed the policies of the kingdom and its leaders.

The two men, Mr. Alzabarah and Mr. Abouammo, were charged with acting as agents of a foreign power inside the United States, in the first complaint of its kind involving Saudis in the country. The case raised questions about the security of American technology companies already under scrutiny for spreading disinformation and influencing public opinion, showing that these firms can be penetrated from the inside as well.