2

私のspec_helperには次のものがあります

c.filter_sensitive_data("<FILTERED>") { keys['s3_key'] }
c.filter_sensitive_data("<REDACTED>") { keys['s3_secret'] }

しかし、仕様を実行すると、カセットに次のエントリが作成されることがわかりました。

Authorization:
- AWS <FILTERED>:this_part_has_not_been_filtered=

ご覧のとおり、フィルタリングされていない部分があります。有用な情報が含まれているかどうかはわかりませんが、念のため貼り付けたくありません。ただし、それには私の鍵や秘密は含まれていないと言えます。綿毛だけですか?気にする必要がありますか?これは、aws-sdk gem を使用しているときに S3 リクエストをフィルタリングするときに通常発生することですか? そうでない場合、すべての認証データをフィルタリングするにはどうすればよいですか?

S3 キーをフィルタリングするための特別な手順はありますか? 私は本当にこれを台無しにしたくありません。

4

1 に答える 1

2

あなたの鍵がそこになければ、安全なようです。確かに、正規表現マッチャーを使用して文字列全体を置き換えることができます%r<#{keys['s3_key']:.*?=>。悪いニュース: regexp はありませんfilter_sensitive_data。良いニュース: より低レベルのメソッドを使用して、それを自分で実装できます。

それが現在の実装ですfilter_sensitive_data

# @param placeholder [String] The placeholder string.
# @param tag [Symbol] Set this to apply this only to cassettes
#  with a matching tag; otherwise it will apply to every cassette.
# @yield block that determines what string to replace
# @yieldparam interaction [(optional) VCR::HTTPInteraction::HookAware] the HTTP interaction
# @yieldreturn the string to replace
def define_cassette_placeholder(placeholder, tag = nil, &block)
  before_record(tag) do |interaction|
    orig_text = call_block(block, interaction)
    log "before_record: replacing #{orig_text.inspect} with #{placeholder.inspect}"
    interaction.filter!(orig_text, placeholder)
  end

  before_playback(tag) do |interaction|
    orig_text = call_block(block, interaction)
    log "before_playback: replacing #{placeholder.inspect} with #{orig_text.inspect}"
    interaction.filter!(placeholder, orig_text)
  end
end
alias filter_sensitive_data define_cassette_placeholder

ソース

これらの方法につながる

  # Replaces a string in any part of the HTTP interaction (headers, request body,
  # response body, etc) with the given replacement text.
  #
  # @param [#to_s] text the text to replace
  # @param [#to_s] replacement_text the text to put in its place
  def filter!(text, replacement_text)
    text, replacement_text = text.to_s, replacement_text.to_s
    return self if [text, replacement_text].any? { |t| t.empty? }
    filter_object!(self, text, replacement_text)
  end

private

  def filter_object!(object, text, replacement_text)
    if object.respond_to?(:gsub)
      object.gsub!(text, replacement_text) if object.include?(text)
    elsif Hash === object
      filter_hash!(object, text, replacement_text)
    elsif object.respond_to?(:each)
      # This handles nested arrays and structs
      object.each { |o| filter_object!(o, text, replacement_text) }
    end

    object
  end

ソース

まあ、このメソッドにモンキー パッチを適用してみます。

あなたの spec_helper のどこかに

class VCR::HTTPInteraction::HookAware
  def filter!(text, replacement_text)
    replacement_text = replacement_text.to_s unless replacement_text.is_a?(Regexp)
    text = text.to_s
    return self if [text, replacement_text].any? { |t| t.empty? }
    filter_object!(self, text, replacement_text)
  end
end

もちろん、エイリアン ライブラリの深い内部構造をいじることをオプトアウトすることもできます。ランダムな英数字データがトークンの近くのカセットに書き込まれていることを知っていても (ただし、後者は含まれません)、偏執的ではありません。

于 2015-08-05T20:46:52.363 に答える