Sha256: b0aae813c2ced9ddac9e9670f3c5e3b637b290d98ea1bb5b741f7da722745fd8

Contents?: true

Size: 1.25 KB

Versions: 10

Compression:

Stored size: 1.25 KB

Contents

require "forwardable"

module Imap; end

module Imap::Backup
  module Text; end

  # Wraps standard output and hides passwords from debug output
  # Any text matching Net::IMAP debug output of passwords is sanitized
  class Text::Sanitizer
    extend Forwardable

    delegate puts: :output
    delegate write: :output

    # @param output [IO] the stream to write output to
    def initialize(output)
      @output = output
      @current = ""
    end

    # Outputs everything up to the last newline character,
    # storing whatever follows the newline.
    # @param args [Array<String>] lines of text
    # @return [void]
    def print(*args)
      @current << args.join
      loop do
        line, newline, rest = @current.partition("\n")
        break if newline != "\n"

        clean = sanitize(line)
        output.puts clean
        @current = rest
      end
    end

    # Outputs any text still not printed
    # @return [void]
    def flush
      return if @current == ""

      clean = sanitize(@current)
      output.puts clean
    end

    private

    attr_reader :output

    def sanitize(text)
      # Hide password in Net::IMAP debug output
      text.gsub(
        /\A(C: RUBY\d+ LOGIN \S+) \S+/,
        "\\1 [PASSWORD REDACTED]"
      )
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
imap-backup-15.0.3.rc1 lib/imap/backup/text/sanitizer.rb
imap-backup-15.0.2 lib/imap/backup/text/sanitizer.rb
imap-backup-15.0.1 lib/imap/backup/text/sanitizer.rb
imap-backup-15.0.0 lib/imap/backup/text/sanitizer.rb
imap-backup-14.6.1 lib/imap/backup/text/sanitizer.rb
imap-backup-14.6.0 lib/imap/backup/text/sanitizer.rb
imap-backup-14.5.2 lib/imap/backup/text/sanitizer.rb
imap-backup-14.5.1 lib/imap/backup/text/sanitizer.rb
imap-backup-14.5.0 lib/imap/backup/text/sanitizer.rb
imap-backup-14.4.5 lib/imap/backup/text/sanitizer.rb