User Agents

Web browsers and everyday web tech, explained

Secure Credential Sharing with Headless Chrome

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to securely pass usernames, passwords, tokens, or API keys to a headless Chrome instance—typically used for automated testing, scraping, or screenshot services—without exposing them in command-line arguments, logs, or process listings. You'll use Chrome's built-in --password-store and file-based flags, or a temporary profile with encrypted storage, so credentials never appear in plain text in your shell history or CI logs.

What You'll Need

  • Google Chrome or Chromium installed (version 80+ recommended).
  • Basic command-line familiarity (Bash, PowerShell, or a scripting language).
  • Access to the target credentials (e.g., a test account or API token).
  • Optional: a secrets manager (like Vault, AWS Secrets Manager, or a local encrypted file) to store credentials.

Step-by-Step

  1. Create a temporary Chrome profile directory. This isolates all cookies, cache, and stored credentials from your main browser. For example: mkdir /tmp/chrome-secure-profile (Linux/macOS) or mkdir %TEMP%\chrome-secure-profile (Windows).
  2. Write credentials to a temporary file with restricted permissions. Instead of passing credentials as command-line arguments, store them in a file that only the current user can read. For example, echo 'username:password' > /tmp/creds.txt && chmod 600 /tmp/creds.txt. This file will be used to populate the profile's login data.
  3. Launch headless Chrome with the profile and a password-store flag. Use --headless=new --user-data-dir=/tmp/chrome-secure-profile --password-store=basic. This tells Chrome to store passwords in the profile's local database (encrypted with a hardcoded key, not ideal for high-security, but better than command-line). If you need a more secure encryption, use --password-store=gnome-libsecret or --password-store=kwallet5 on Linux, which uses the system keyring.
  4. Inject credentials via a script or automation tool. Use Chrome DevTools Protocol (CDP) or a library like Puppeteer, Playwright, or Selenium to navigate to the login page and fill the form. For example, in Puppeteer: const browser = await puppeteer.launch({headless: 'new', args: ['--user-data-dir=/tmp/chrome-secure-profile']}); Then read the credentials file from your script and use page.type() to fill the fields. Never put credentials directly in the script source code; read them from the file or environment variable.
  5. Use environment variables for credentials in the script. Instead of hardcoding, set environment variables before launching the script: export CRED_USER='admin' and export CRED_PASS='secret'. Then in your script, access process.env.CRED_USER (Node.js) or os.environ['CRED_USER'] (Python). This keeps credentials out of the source code and command line.
  6. If using a secrets manager, retrieve credentials at runtime. For example, with AWS Secrets Manager: aws secretsmanager get-secret-value --secret-id my-login --query SecretString --output text > /tmp/creds.json. Then parse the JSON in your script. This avoids storing credentials in files at all.
  7. Clean up after execution. After the headless Chrome session ends, delete the temporary profile directory and any credential files. Use rm -rf /tmp/chrome-secure-profile /tmp/creds.txt in a finally block in your script or after the command completes.

Common Mistakes

  • Passing credentials in the command line. For example, chrome --headless --username=admin --password=secret—these appear in the process list and shell history.
  • Using a shared or default Chrome profile. This can leak cookies and stored credentials to other sessions and may lock the profile if Chrome is already running.
  • Storing credentials in the script source code. If the script is committed to a repository, credentials are exposed.
  • Forgetting to restrict file permissions. If the credential file is readable by other users, it's a security hole.
  • Not cleaning up temporary files. Leaving credential files on disk can lead to unauthorized access later.

Tips

  • Use a dedicated test account. Avoid using production credentials for automated tests.
  • Rotate credentials regularly. If you must store them, change them often.
  • Leverage a secrets manager. It's the most secure way—credentials never touch the filesystem.
  • Use --disable-features=PasswordImport? Actually, avoid disabling password import; instead, use the --password-store=basic only for low-risk tests. For production, use the system keyring.
  • Log only non-sensitive data. Ensure your script doesn't print credentials to stdout.

Troubleshooting

  • Chrome fails to start with the profile. Ensure the profile directory exists and is writable. Also, check that no other Chrome process is using the same --user-data-dir.
  • Passwords are not saved. If you're using --password-store=basic, Chrome may not prompt to save passwords in headless mode. Instead, you can pre-populate the profile's Login Data SQLite database using a script, or use CDP to set credentials directly.
  • Credentials are visible in process list. If you see them, you likely passed them as command-line arguments. Switch to environment variables or a file.
  • Permission denied when reading credential file. Check the file permissions—use chmod 600 and ensure the script runs as the same user.
  • Secrets manager returns an error. Verify your authentication (e.g., AWS IAM role) and that the secret name is correct.
  • Headless Chrome exits immediately. Add --no-sandbox if running as root in a container, but be aware of the security implications.

Frequently Asked Questions

Can I pass credentials via URL parameters?

Yes, but it's insecure—URLs are logged in browser history, proxy logs, and server logs. Avoid it.

Is it safe to use --password-store=basic?

It's better than command-line, but the encryption key is hardcoded in Chrome, so it's not strong. Use it only for low-risk test accounts.

How do I use environment variables with Puppeteer?

Set them in your shell before launching the script, then access them in Node.js via process.env.VARIABLE_NAME.

Can I use a keyring in headless mode?

Yes, on Linux you can use --password-store=gnome-libsecret if a keyring daemon is running. On Windows, Chrome uses the Credential Manager automatically.

What about two-factor authentication?

Headless Chrome can't handle real 2FA easily. You can use a test account with 2FA disabled, or use a pre-authenticated session cookie.

How do I clean up the profile after a crash?

Add a try/finally block in your script to delete the profile directory, or use a temporary directory that the OS cleans on reboot (like /tmp).