Module: Cybertrain::ParameterFilter

Defined in:
cybertrain/http/parameter_filter.rb

Overview

The parameter names whose values never reach a log line or the development error page: Rails’ filter_parameters. A name is filtered when it contains one of ParameterFilter.names, ASCII letters compared case-insensitively, so password also filters user[password] and password_confirmation, and token filters authenticity_token.

The list starts as DEFAULT_NAMES; Application#boot installs Config#filter_parameters (CYBERTRAIN_FILTER_PARAMETERS) here, since the HTTP layer cannot read the Config itself (the Params.max_page shape). RequestLogger filters the query string of its Started line with ParameterFilter.filter_query and its path with ParameterFilter.filter_path (signed tokens); Dev::ErrorPage the request line and its parameter dump; HostAuthorization the path of its Blocked host line.

Every scan here works on bytes: a query string or a parameter name may hold any bytes, invalid UTF-8 included, and is read through a to_s local (spikes/NOTES.md rules 52 and 62).

Examples:

config/app.rb

config.filter_parameters = config.filter_parameters + ["otp", "ssn"]

Constant Summary collapse

FILTERED =

What a filtered value is replaced with.

"[FILTERED]"
DEFAULT_NAMES =

The names filtered by default (and Config#filter_parameters’ default).

["password", "password_confirmation", "token", "secret", "secret_key_base",
"session_key", "api_key", "authorization"]

Class Method Summary collapse

Class Method Details

.filter?(name) ⇒ Boolean

Whether a parameter’s value is filtered: its name contains one of names, ASCII case-insensitively.

Examples:

Cybertrain::ParameterFilter.filter?("user[Password]")  # => true

Parameters:

  • name (String) —

    the parameter name as the application sees it (decoded)

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
80
# File 'cybertrain/http/parameter_filter.rb', line 70

def self.filter?(name)
  s = name.to_s # NOTES rule 62
  bytes = Array.new(0) { 0 }
  i = 0
  n = s.bytesize
  while i < n
    bytes << ParameterFilter.lower_byte(s.getbyte(i))
    i += 1
  end
  ParameterFilter.bytes_match?(bytes)
end

.filter_path(path) ⇒ String

A path with each segment shaped like a Token (1 to 18 digits, --, 1 to 18 digits, --, 64 hex digits; a .format after it is kept) replaced by [FILTERED]: /passwords/7--1900--<64 hex>/edit becomes /passwords/[FILTERED]/edit. Log lines written before routing (no parameter names yet: RequestLogger’s Started, HostAuthorization’s Blocked host) use it, so a password reset or confirmation link’s token never reaches the log, whatever names holds. Every other byte is kept as sent.

Parameters:

  • path (String)

Returns:

  • (String)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'cybertrain/http/parameter_filter.rb', line 129

def self.filter_path(path)
  s = path.to_s # NOTES rule 62
  n = s.bytesize
  out = +""
  i = 0
  while i < n
    if s.getbyte(i) == 47
      out << "/"
      i += 1
      next
    end
    j = i
    j += 1 while j < n && s.getbyte(j) != 47
    stop = ParameterFilter.token_end(s, i, j)
    if stop >= 0 && (stop == j || s.getbyte(stop) == 46)
      out << FILTERED
      out << s.byteslice(stop, j - stop).to_s
    else
      out << s.byteslice(i, j - i).to_s
    end
    i = j
  end
  out
end

.filter_query(text) ⇒ String

A query string (or urlencoded form body) with the value of each filtered pair replaced by [FILTERED]: user%5Bpassword%5D=x&page=2 becomes user%5Bpassword%5D=[FILTERED]&page=2. Pairs are split on &; each name is percent-decoded (+ as a space) for the comparison only, and everything else is copied byte for byte, undecoded, so the result is still the text as sent apart from the filtered values. A pair without = has no value and is kept.

Parameters:

  • text (String)

Returns:

  • (String)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'cybertrain/http/parameter_filter.rb', line 92

def self.filter_query(text)
  s = text.to_s # NOTES rule 62
  n = s.bytesize
  return s if n == 0 || @filter_names.empty?

  # Started as +"" and filled with << only (NOTES rules 60 and 66).
  out = +""
  i = 0
  while i <= n
    j = i
    j += 1 while j < n && s.getbyte(j) != 38
    e = i
    e += 1 while e < j && s.getbyte(e) != 61
    if e < j && ParameterFilter.encoded_name_match?(s, i, e)
      out << s.byteslice(i, e - i + 1).to_s
      out << FILTERED
    else
      out << s.byteslice(i, j - i).to_s
    end
    out << "&" if j < n
    i = j + 1
  end
  out
end

.names ⇒ Array<String>

Returns the names filtered now, lowercase.

Returns:

  • (Array<String>) —

    the names filtered now, lowercase



40
41
42
# File 'cybertrain/http/parameter_filter.rb', line 40

def self.names
  @filter_names
end

.names=(list) ⇒ nil

Installs the names to filter (Application#boot passes Config#filter_parameters). Each is lowercased; empty ones are dropped, so [] turns filtering off.

Parameters:

  • list (Array<String>)

Returns:

  • (nil)


50
51
52
53
54
55
56
57
58
59
60
# File 'cybertrain/http/parameter_filter.rb', line 50

def self.names=(list)
  names = Array.new(0) { "" }
  i = 0
  while i < list.size
    name = list[i].to_s.downcase
    names << name unless name.empty?
    i += 1
  end
  @filter_names = names
  nil
end