コード:
require 'tempfile'
require 'open3'
def valid_pgp_key?(unsafe_user_input)
tempfile = Tempfile.new('pgp_pubkey')
command = ['gpg',
'--no-default-keyring',
'--keyring',
tempfile.path,
'--import',
'-']
stdin, stderrout, thread = Open3.popen2e(command)
stdin.puts(unsafe_user_input)
stdin.close
exit_status = thread.value
tempfile.unlink
exit_status.success? ? true : false
end
unsafe_user_input
完全にサニタイズされていない文字列であり、有効な PGP 公開鍵であることを期待しています (ただし、有効な秘密鍵である可能性があることを受け入れてください)。
質問:
- コマンドを配列呼び出しとして直接渡すことを理解している
execve()
ので、シェルインジェクションについて心配する必要はありません-これは本当ですか? - 悪意のあるユーザーがこのアプローチをキーの検証に悪用することは明らかに可能ですか?もしそうなら、どのような回避策を提案しますか?