Secure Credential Sharing with Headless Chrome
In this article
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
- 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) ormkdir %TEMP%\chrome-secure-profile(Windows). - 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. - 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-libsecretor--password-store=kwallet5on Linux, which uses the system keyring. - 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 usepage.type()to fill the fields. Never put credentials directly in the script source code; read them from the file or environment variable. - Use environment variables for credentials in the script. Instead of hardcoding, set environment variables before launching the script:
export CRED_USER='admin'andexport CRED_PASS='secret'. Then in your script, accessprocess.env.CRED_USER(Node.js) oros.environ['CRED_USER'](Python). This keeps credentials out of the source code and command line. - 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. - 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.txtin afinallyblock 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=basiconly 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 600and 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-sandboxif 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).