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
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.