[curl] Using cURL with a username and password?

The safest way to pass credentials to curl is to be prompted to insert them. This is what happens when passing the username as suggested earlier (-u USERNAME).

But what if you can't pass the username that way? For instance the username might need to be part of the url and only the password be part of a json payload.

tl;dr: This is how to use curl safely in this case:

read -p "Username: " U; read -sp "Password: " P; curl --request POST -d "{\"password\":\"${P}\"}" https://example.com/login/${U}; unset P U

read will prompt for both username and password from the command line, and store the submitted values in two variables that can be references in subsequent commands and finally unset.

I'm gonna elaborate on why the other solutions are not ideal.

Why are environment variables unsafe

  1. Access and exposure mode of the content of an environment variable, can not be tracked (ps -eww ) since the environment is implicitly available to a process
  2. Often apps grab the whole environment and log it for debugging or monitoring purposes (sometimes on log files plaintext on disk, especially after an app crashes)
  3. Environment variables are passed down to child processes (therefore breaking the principle of least privilege)
  4. Maintaining them is an issue: new engineers don't know they are there, and are not aware of requirements around them - e.g., not to pass them to sub-processes - since they're not enforced or documented.

Why is it unsafe to type it into a command on the command line directly Because your secret then ends up being visible by any other user running ps -aux since that lists commands submitted for each currently running process. Also because your secrte then ends up in the bash history (once the shell terminates).

Why is it unsafe to include it in a local file Strict POSIX access restriction on the file can mitigate the risk in this scenario. However, it is still a file on your file system, unencrypted at rest.