Module: Parse::AtlasSearch::ProtectedPaths

Defined in:
lib/parse/atlas_search/protected_paths.rb

Overview

Shared "does this path touch a protected field" logic for every Atlas Search and vector search entry point.

Stripping a protected field from the RESULT rows does not stop it from deciding which documents MATCH, how they RANK, or which rows a filter keeps. A scoped caller that can name a protected field in a $search path, a highlight, an autocomplete field, a sort, or a $match filter key can test guesses against its value. These helpers find every such reference so the entry points can refuse the call with CLPScope::Denied.

Path comparison rules:

  • A String or Symbol path is reduced to its first dotted segment, with a _p_ storage prefix removed (_p_owner counts as owner, secret.sub counts as secret). The storage columns _created_at / _updated_at / _id map to createdAt / updatedAt / objectId.
  • An Array path touches a protected field when any element does.
  • A { "value" => ..., "multi" => ... } path object is judged by its value.
  • A { "wildcard" => ... } path object, or any other non-String path, is treated as touching every field.

Every check is a no-op for a master resolution, a nil resolution, or an empty protected set.

Constant Summary collapse

STORAGE_ALIASES =

Storage-form column names that differ from their Parse field name.

{
  "_created_at" => "createdAt",
  "_updated_at" => "updatedAt",
  "_id" => "objectId",
}.freeze
LOGICAL_OPERATORS =

Query operators whose value is a list of sub-filters.

%w[$and $or $nor].freeze
ALL_FIELDS =

Sentinel yielded for a reference that reaches every field (a wildcard path, a queryString query).

:__all_fields__
CREDENTIAL_COLLECTIONS =

Collections whose documents carry Parse Server's credential columns (_hashed_password, _perishable_token, _session_token, ...).

%w[_User _Session].freeze
ACL_COLUMNS =

Sample names a wildcard glob is tested against: every internal column, plus a representative _auth_data_<provider> column. ACL columns present in every Parse collection.

%w[_rperm _wperm _tombstone].freeze
CREDENTIAL_GLOB_SAMPLES =

Stored credential column names a wildcard glob is tested against on _User / _Session (or an unknown collection). Only stored, underscore-prefixed names are listed: sessionToken is a REST field name, not a stored column, so s* does not count as reaching it.

(
  Parse::PipelineSecurity::INTERNAL_FIELDS_DENYLIST.select { |n| n.start_with?("_") } - ACL_COLUMNS +
  %w[anonymous apple facebook github google instagram linkedin mfa twitter].map { |p| "_auth_data_#{p}" } +
  %w[_perishable_token_expires_at _email_verify_token_expires_at _password_changed_at]
).uniq.freeze

Class Method Summary collapse

Class Method Details

.assert_filter_allowed!(filter, protected_fields, resolution, collection_name: nil, method_name: "Parse::AtlasSearch.search") ⇒ Object

Refuse a $match filter whose predicate KEYS (top level, and inside $and / $or / $nor / $not) or $expr field references touch a protected field. A filter decides which rows survive, so filtering on a protected field is an oracle on its value even when the field is stripped from the output.



334
335
336
337
338
339
340
341
342
# File 'lib/parse/atlas_search/protected_paths.rb', line 334

def assert_filter_allowed!(filter, protected_fields, resolution, collection_name: nil,
                                                                  method_name: "Parse::AtlasSearch.search")
  return unless enforce?(resolution, protected_fields)
  each_filter_reference(filter) do |ref|
    next unless touches?(ref, protected_fields)
    raise_denied!(collection_name, method_name, "filter field", ref)
  end
  nil
end

.assert_no_internal_paths!(stage: nil, paths: nil, collection_name: nil, resolution: nil, method_name: "Parse::AtlasSearch.search", what: "path") ⇒ Object

Refuse a $search / $searchMeta body, or a list of paths, that references a Parse Server internal column. Unlike the protectedFields checks this runs for every caller, master included: a wildcard, regex, or autocomplete operator over _perishable_token or _hashed_password returns which documents match a prefix, so row counts reveal the value. On _User and _Session a non-master caller is also refused a path that reaches every field, since on a dynamically mapped index that includes those columns.

Parameters:

  • stage (Hash, nil) (defaults to: nil) —

    a $search / $searchMeta stage to walk.

  • paths (Object, nil) (defaults to: nil) —

    a path, or an Array of paths.

Raises:



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/parse/atlas_search/protected_paths.rb', line 249

def assert_no_internal_paths!(stage: nil, paths: nil, collection_name: nil, resolution: nil,
                              method_name: "Parse::AtlasSearch.search", what: "path")
  refs = []
  unless stage.nil?
    each_search_reference(stage) { |ref| refs << ref }
    each_query_string_field(stage) do |field|
      next unless internal_path?(field, collection_name: collection_name) ||
                  internal_glob?(field, collection_name: collection_name)
      raise Parse::CLPScope::Denied.new(
        collection_name, :find,
        "#{method_name} refused: a queryString query names the Parse Server " \
        "internal field #{field.inspect}; matching on it would reveal its value.",
      )
    end
  end
  refs.concat(paths.is_a?(Array) ? paths : [paths]) unless paths.nil?
  all_fields_refused = CREDENTIAL_COLLECTIONS.include?(collection_name.to_s) &&
                       !(resolution.respond_to?(:master?) && resolution.master?)
  refs.each do |ref|
    if ref != ALL_FIELDS && internal_path?(ref, collection_name: collection_name)
      raise Parse::CLPScope::Denied.new(
        collection_name, :find,
        "#{method_name} refused: #{what} #{ref.inspect} names a Parse Server " \
        "internal field; matching, ranking, or filtering on it would reveal its value.",
      )
    end
    next unless all_fields_refused && wildcard_path?(ref)
    raise Parse::CLPScope::Denied.new(
      collection_name, :find,
      "#{method_name} refused: a #{what} that reaches every field of " \
      "#{collection_name} would include its credential columns; name the " \
      "fields to search explicitly.",
    )
  end
  nil
end

.assert_paths_allowed!(paths, protected_fields, resolution, collection_name: nil, method_name: "Parse::AtlasSearch.search", what: "path") ⇒ Object

Refuse when any of paths touches a protected field.

Parameters:

  • paths (Object) —

    a single path or an Array of paths.

  • what (String) (defaults to: "path") —

    used in the error message ("search path", "highlight path", ...).

Raises:



95
96
97
98
99
100
101
102
103
# File 'lib/parse/atlas_search/protected_paths.rb', line 95

def assert_paths_allowed!(paths, protected_fields, resolution, collection_name: nil,
                                                               method_name: "Parse::AtlasSearch.search",
                                                               what: "path")
  return unless enforce?(resolution, protected_fields)
  list = paths.is_a?(Array) ? paths : [paths]
  hit = list.find { |p| touches?(p, protected_fields) }
  return if hit.nil? && !list.empty?
  raise_denied!(collection_name, method_name, what, hit.nil? ? ALL_FIELDS : hit)
end

.assert_search_stage_allowed!(stage, protected_fields, resolution, collection_name: nil, method_name: "Parse::AtlasSearch.search_with_stage") ⇒ Object

Walk a $search / $searchMeta body (or a whole stage) and refuse any path, sort key, or query-string reference that touches a protected field. Operators nest freely (compound must/should/filter/mustNot, embeddedDocument, facet operators, score functions), so every path / defaultPath value at any depth is checked.



113
114
115
116
117
118
119
120
121
# File 'lib/parse/atlas_search/protected_paths.rb', line 113

def assert_search_stage_allowed!(stage, protected_fields, resolution, collection_name: nil,
                                                                       method_name: "Parse::AtlasSearch.search_with_stage")
  return unless enforce?(resolution, protected_fields)
  each_search_reference(stage) do |ref|
    next unless ref == ALL_FIELDS || touches?(ref, protected_fields)
    raise_denied!(collection_name, method_name, "$search path", ref)
  end
  nil
end

.enforce?(resolution, protected_fields) ⇒ Boolean

Returns true when the scope is subject to protectedFields.

Returns:

  • (Boolean) —

    true when the scope is subject to protectedFields.



51
52
53
54
55
# File 'lib/parse/atlas_search/protected_paths.rb', line 51

def enforce?(resolution, protected_fields)
  return false if resolution.nil?
  return false if resolution.respond_to?(:master?) && resolution.master?
  !(protected_fields.nil? || protected_fields.empty?)
end

.internal_path?(path, collection_name: nil) ⇒ Boolean

Whether path names one of Parse Server's internal columns (PipelineSecurity::INTERNAL_FIELDS_DENYLIST or an _auth_data_<provider> column). A wildcard path object counts when its glob (other than the bare *, handled as reaching every field) matches one of them, so *token and ?hashed* are caught.

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/parse/atlas_search/protected_paths.rb', line 149

def internal_path?(path, collection_name: nil)
  case path
  when String, Symbol
    head = path.to_s.split(".").first.to_s
    Parse::PipelineSecurity::INTERNAL_FIELDS_DENYLIST.include?(head) ||
      Parse::PipelineSecurity::INTERNAL_FIELDS_PREFIX_DENYLIST.any? { |p| head.start_with?(p) }
  when Array
    path.any? { |p| internal_path?(p, collection_name: collection_name) }
  when Hash
    value = path.key?("value") ? path["value"] : path[:value]
    glob = path.key?("wildcard") ? path["wildcard"] : path[:wildcard]
    (!value.nil? && internal_path?(value)) || internal_glob?(glob, collection_name: collection_name)
  else
    false
  end
end

.query_string_fields(text) ⇒ Object



198
199
200
# File 'lib/parse/atlas_search/protected_paths.rb', line 198

def query_string_fields(text)
  text.to_s.gsub(/\\(.)/m, "\\1").scan(QUERY_STRING_FIELD_RE).map(&:first)
end

.root_field(path) ⇒ String?

The Parse field name a single String path addresses.

Returns:



59
60
61
62
63
64
65
# File 'lib/parse/atlas_search/protected_paths.rb', line 59

def root_field(path)
  head = path.to_s.split(".").first.to_s
  return nil if head.empty?
  head = STORAGE_ALIASES.fetch(head, head)
  head = head.delete_prefix("_p_") if head.start_with?("_p_") && head.length > 3
  head
end

.touches?(path, protected_fields) ⇒ Boolean

Whether path touches a protected field.

Parameters:

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/parse/atlas_search/protected_paths.rb', line 72

def touches?(path, protected_fields)
  return false if protected_fields.nil? || protected_fields.empty?
  case path
  when String, Symbol
    protected_fields.include?(root_field(path))
  when Array
    path.any? { |p| touches?(p, protected_fields) }
  when Hash
    value = path.key?("value") ? path["value"] : path[:value]
    has_wildcard = path.key?("wildcard") || path.key?(:wildcard)
    return true if has_wildcard || value.nil?
    touches?(value, protected_fields)
  else
    true
  end
end