Skip to content

Instantly share code, notes, and snippets.

@malaya-zemlya
Created August 4, 2019 12:21
Show Gist options
  • Save malaya-zemlya/da35cec76076605316e26dfe9bb1f204 to your computer and use it in GitHub Desktop.
Save malaya-zemlya/da35cec76076605316e26dfe9bb1f204 to your computer and use it in GitHub Desktop.
Writing a secret to /tmp securely
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
@malaya-zemlya
Copy link
Author

A much easier way is to keep secrets in a directory that is not world-writeable, but hey, where's fun in that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment