---
title: "Take secrets from the environment or a · Agent Wranglers"
url: https://agentwranglers.se/playbook/desktop-apps/command-line-tools/no-secrets-in-flags
id: DSK-24
level: Required
risk: takeover
updated: 2026-10-22
tokens: 988
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Take secrets from the environment or a file instead of a flag

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Command-line tools](https://agentwranglers.se/playbook/desktop-apps/command-line-tools.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-22.

- ID: DSK-24
- Level: Required
- Chapter: [Command-line tools](https://agentwranglers.se/playbook/desktop-apps/command-line-tools.md)
- Rule: Read tokens, passwords and keys from an environment variable, a file, standard input or the system keychain. The tool never accepts a secret as a command-line argument and never prints one.
- Check: The help text lists no flag such as `--token`, `--password` or `--api-key` that takes a value. A test runs every command with `--debug` and a known test token, and the token appears in neither output stream. A saved credentials file has mode 600.
- Risk: An account or system taken over
- How: Arguments show up in shell history, in process lists that other users on the machine can read, and in CI logs. Offer `--token-file` or a `EXAMPLE_TOKEN` variable instead. Store a saved sign-in as [keep tokens in the system keychain](https://agentwranglers.se/playbook/desktop-apps/web-code-inside-desktop-apps/desktop-keychain.md) says, or in a file only the user can read. Mask secrets in debug output, as [keep private data out of logs and addresses](https://agentwranglers.se/playbook/adversarial-testing/data-and-secrets/private-data-out-of-logs.md) says for logs.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### Python example

```python
import os, sys

def load_token(token_file: str | None) -> str:
    if token_file:
        with open(token_file, encoding="utf-8") as handle:
            return handle.read().strip()
    token = os.environ.get("EXAMPLE_TOKEN")
    if not token:
        print("Set EXAMPLE_TOKEN or pass --token-file.", file=sys.stderr)
        sys.exit(EXIT_USAGE)
    return token
```

### Go example

```go
func loadToken(tokenFile string) (string, error) {
	if tokenFile != "" {
		raw, err := os.ReadFile(tokenFile)
		return strings.TrimSpace(string(raw)), err
	}
	if token := os.Getenv("EXAMPLE_TOKEN"); token != "" {
		return token, nil
	}
	return "", errors.New("set EXAMPLE_TOKEN or pass --token-file")
}
```

## Sources

- [CWE-214, Invocation of Process Using Visible Sensitive Information](https://cwe.mitre.org/data/definitions/214.html)
- [OWASP Secrets Management Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html)

## Related rules

- DSK-13 [Keep tokens in the system keychain](https://agentwranglers.se/playbook/desktop-apps/web-code-inside-desktop-apps/desktop-keychain.md) in Desktop apps, extensions and CLIs
- SEC-39 [Keep private data out of logs and addresses](https://agentwranglers.se/playbook/adversarial-testing/data-and-secrets/private-data-out-of-logs.md) in Adversarial testing

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/desktop-apps/command-line-tools/no-secrets-in-flags">Agent Wranglers desktop apps, extensions and CLIs playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Never wait for input when no person is there](https://agentwranglers.se/playbook/desktop-apps/command-line-tools/non-interactive-mode.md)
- Next: [Show a dry run and ask before destructive commands](https://agentwranglers.se/playbook/desktop-apps/command-line-tools/dry-run.md)
- [Command-line tools](https://agentwranglers.se/playbook/desktop-apps/command-line-tools.md)
