Created
August 4, 2019 12:21
-
-
Save malaya-zemlya/da35cec76076605316e26dfe9bb1f204 to your computer and use it in GitHub Desktop.
Writing a secret to /tmp securely
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
file=/tmp/test.txt | |
rm -f "$file" | |
touch "$file" # make sure file exists | |
chown -h $(id -u):$(id -g) "$file" # make us the owner of the file | |
chmod -h 600 "$file" # make sure it's not world-accessible | |
# now that the attacker cannot swap the file from underneath us, | |
# check that it's not a symlink | |
if [[ -L "$file" ]]; then | |
>&2 echo "File is a symlink" # | |
rm -f "$file" | |
exit 1 | |
fi | |
# We are good to go | |
printf "Enter a secret: " | |
read -s secret | |
cat >"$file" <<< "$secret" # in case secret contains "-n" or some such | |
echo | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A much easier way is to keep secrets in a directory that is not world-writeable, but hey, where's fun in that