Module: Parse::PipelineSecurity

Defined in:
lib/parse/pipeline_security.rb

Overview

Canonical security validator for MongoDB aggregation pipelines and filter hashes that the SDK forwards to the driver or to Parse Server.

Previously the codebase had three different validators with three different rule sets:

  • Parse::Agent::PipelineValidator — strict allowlist for the Agent (read-only paths only)
  • Parse::Query#validate_pipeline! — outer-stage-only denylist
  • Parse::MongoDB.assert_no_denied_operators! — recursive denylist of server-side JS operators

Parse::AtlasSearch.convert_filter_for_mongodb was a complete passthrough that bypassed all three. A user-supplied filter containing $where/$expr/$function/$regex was injected straight into the pipeline $match stage, bypassing every existing constraint guard.

This module consolidates the rules. Every entry point that forwards a caller-supplied pipeline or filter to MongoDB now routes through one of the two public methods here:

  • PipelineSecurity.validate_pipeline! — strict mode (allowlist + size/depth caps). Used by Parse::Agent and by Parse::Query#aggregate for user-facing aggregation entry points.

  • PipelineSecurity.validate_filter! — permissive mode (recursive denylist only). Used by Parse::MongoDB.find/aggregate and Atlas Search filter passthrough where the pipeline is constructed by SDK code but a user-controlled filter hash is interpolated. Refuses $where/$function/$accumulator and the data-mutating stages at any nesting depth.

== Policy: allowlist top-level, denylist recursive

Strict mode enforces ALLOWED_STAGES ONLY at the top-level stage key — nested sub-pipelines (inside $lookup.pipeline, $unionWith.pipeline, $facet.*, $graphLookup) are walked with the operator denylist but NOT with the stage allowlist. This is intentional: Atlas Search and uncommon-but-legitimate read stages like $densify and $fill must be allowed inside sub-pipelines even when the outer pipeline is strict-validated. The denylist is the security boundary; the allowlist is a shape check.

== Caveat for Query#aggregate callers

Parse::Query#aggregate routes through PipelineSecurity.validate_filter!, not PipelineSecurity.validate_pipeline!, so user-supplied pipelines are checked against the denylist only. Permissive mode does NOT block $lookup, $graphLookup, or $unionWith reading from arbitrary collections — these are legitimate read stages but powerful enough to cross Parse ACL/CLP boundaries when the source collection lacks row-level enforcement. Never pass raw attacker-controlled input into Parse::Query#aggregate. Construct the pipeline in SDK code and interpolate only validated values.

== Capability gap: $expr

$expr itself is not in DENIED_OPERATORS. The recursive walker catches $function/$accumulator nested inside $expr, so the immediate JavaScript-execution risk is closed. A future Atlas operator gated under $expr would slip until DENIED_OPERATORS is extended. Defense-in-depth callers concerned about expensive aggregation expressions ($regexMatch ReDoS, large $reduce loops) should validate user input shape before reaching this module.

Defined Under Namespace

Classes: Error

Constant Summary collapse

DENIED_OPERATORS =

Operators that are ALWAYS refused at any nesting depth. These either execute server-side JavaScript ($where, $function, $accumulator) or mutate the database ($out, $merge) or the server itself ($collMod, $createIndex, $dropIndex, $planCacheSetFilter, $planCacheClear). $jsonSchema is refused because its properties name fields inside a value, out of reach of the key-based protectedFields checks, and its pattern runs a regex that skips RegexSecurity and the regex time limit; a { $jsonSchema: { properties: { email: { pattern: "^a" } } } } match reads a protected field one character at a time. None of them are needed for read queries.

%w[
  $where $function $accumulator $jsonSchema
  $out $merge
  $collMod $createIndex $dropIndex
  $planCacheSetFilter $planCacheClear
].freeze
DENIED_FIELD_REFS =

Field-reference paths (string values inside $expr whose first byte is $) that point at server-internal columns and must never be reachable from a user-influenced pipeline. A boolean expression inside $expr over any of these is a 1-bit-per-query side channel that bisects the value of a bcrypt hash, session token, or password-reset token. Names match Parse Server's internal column layout (cf. MongoStorageAdapter).

%w[
  $_hashed_password $_password_history
  $_session_token $_sessionToken
  $sessionToken $session_token
  $_email_verify_token $_perishable_token
  $_failed_login_count $_account_lockout_expires_at
  $_rperm $_wperm
  $_auth_data
].freeze
DENIED_FIELD_REF_PREFIXES =

String prefix for per-provider auth-data field references inside $expr. Parse Server stores per-provider columns as _auth_data_facebook, _auth_data_google, etc. — none of these should be reachable from a user-influenced pipeline. The prefix $_auth_data_ covers all of them without requiring an exhaustive list.

%w[$_auth_data_].freeze
ALLOWED_UNDERSCORE_COLLECTIONS =

MongoDB collection names that an SDK aggregation IS permitted to name in from:/coll:. Any name starting with _ outside this set is refused as an internal Parse Server collection. The four entries here are the only _-prefixed collections that hold Parse SDK data classes; everything else with a leading _ is server-managed state (_SCHEMA discloses class-level permissions; _Hooks discloses Cloud Code webhook URLs + secret keys; _GraphQLConfig discloses GraphQL schema state; _Audit holds operational telemetry; _Idempotency/_PushStatus/ _JobStatus/_JobSchedule/_GlobalConfig/_Audience hold internal Parse Server bookkeeping).

%w[_User _Role _Installation _Session].freeze
INTERNAL_FIELDS_DENYLIST =

Field names that are internal to Parse Server's storage layout and must never appear in returned documents. Most are stripped by Parse::MongoDB.convert_document_to_parse, but a raw-result path (raw: true) bypasses that conversion and would otherwise surface the bcrypt hash, session token, or reset token.

sessionToken / session_token (no leading underscore) are the credential column on _Session rows. Unlike the _User-side _session_token, the Session class declares it as a regular property, so without this entry a master-key agent that has had the class explicitly unhidden would receive raw bearer tokens in every row of a query_class("_Session") response. The denylist is the process-level floor — independent of class-visibility state — so even a deliberate agent_unhidden on _Session (or a compromised superadmin tool) cannot exfiltrate active tokens.

%w[
  _hashed_password _password_history
  _session_token _sessionToken
  sessionToken session_token
  _email_verify_token _perishable_token
  _failed_login_count _account_lockout_expires_at
  _rperm _wperm _tombstone
  _auth_data
].freeze
INTERNAL_FIELDS_PREFIX_DENYLIST =

Prefix covering per-provider auth-data columns (_auth_data_facebook, _auth_data_google, …). Used by strip_internal_fields and by the walk_for_denied! field-name screen.

%w[_auth_data_].freeze
CREDENTIAL_FIELDS_DENYLIST =

The credential / sensitive subset of INTERNAL_FIELDS_DENYLIST. These columns must NEVER appear as a user-influenced $match field name — even on a pipeline that runs with allow_internal_fields: true (which exists to permit SDK-emitted _rperm/_wperm references from readable_by_role / publicly_readable). A $match/$count on a password hash, session/reset token, or auth-data column is a credential- exfiltration oracle (bisect the value char-by-char), and these columns have NO legitimate SDK query use — so the allow_internal_fields escape hatch must not relax them. Derived from INTERNAL_FIELDS_DENYLIST minus the ACL/bookkeeping columns (_rperm/_wperm/_tombstone) the ACL DSL legitimately emits, so the two lists never drift.

(INTERNAL_FIELDS_DENYLIST - %w[_rperm _wperm _tombstone]).freeze
FORENSIC_OPERATORS =

Forensic string-introspection operators. When any of these appears INSIDE $expr with a field-reference input string, the query becomes a per-character oracle even though the operator itself is otherwise legitimate. Refused inside $expr regardless of the input — the validator does not try to introspect operand shapes deeply, and these operators have no legitimate use against Parse-Server-managed columns from an SDK aggregation.

%w[
  $regexMatch $regexFind $regexFindAll
  $substr $substrBytes $substrCP
  $indexOfBytes $indexOfCP
  $strLenBytes $strLenCP
  $strcasecmp
].freeze
ALLOWED_STAGES =

Top-level pipeline stages permitted by the strict validator. The set covers Parse-Stack's own aggregation use, plus Atlas Search entry points ($search, $searchMeta, $listSearchIndexes) so that Parse::AtlasSearch calls do not break. $vectorSearch is included for Parse::VectorSearch — like $search, it is a read-only Atlas index stage and must be the FIRST stage of the pipeline (Atlas refuses it otherwise). $rankFusion (Atlas 8.0+) is the native server-side reciprocal-rank-fusion stage used by Parse::VectorSearch::Hybrid — also a read-only stage-0 operator.

%w[
  $match $group $sort $project $limit $skip $unwind $lookup
  $count $addFields $set $unset $bucket $bucketAuto $facet
  $sample $sortByCount $replaceRoot $replaceWith $redact
  $graphLookup $unionWith
  $search $searchMeta $listSearchIndexes $vectorSearch $rankFusion
].freeze
STAGE0_ONLY_ATLAS_STAGES =

Atlas operators that are valid only as the FIRST stage of a pipeline (Atlas refuses them anywhere else). They are present in ALLOWED_STAGES so the SDK's own modules — Parse::AtlasSearch and Parse::VectorSearch — can emit them; both of those modules bypass validate_pipeline! and build their pipelines internally. Caller-supplied pipelines (e.g. through Parse::Agent::Tools.aggregate) must NOT include these stages: the Agent's tenant-scope $match prepend would push them off stage 0, and the proper agent surface for full-text and vector search is the dedicated atlas_search / semantic_search tools, not raw aggregate.

%w[
  $search $searchMeta $vectorSearch $listSearchIndexes $rankFusion
].freeze
MAX_REGEX_PATTERN_LENGTH =

Cap on the length of a caller-supplied $regex (or the regex: field inside $regexMatch / $regexFind / $regexFindAll) pattern string. ReDoS protection: doesn't catch every pathological pattern (small patterns like (a+)+$ can still backtrack catastrophically), but caps the worst class of caller-shipped patterns and stops the "1MB regex" denial-of-service shape that an attacker could send through vector_filter: / filter: / where:. Legitimate Parse-Server queries are well under this.

512
MAX_PIPELINE_STAGES =

Cap on number of top-level stages in a strict-validated pipeline.

20
MAX_DEPTH =

Cap on nested object/array depth during recursive walks. Stops a caller from forcing the validator into a near-infinite traversal. Legitimate Parse-generated pipelines with $facet containing $lookup with let and correlated sub-pipelines ($match.$expr. $and.[…]) can reach depth 12+ on a normal read, so we keep comfortable headroom above the real ceiling.

20
INTERNAL_REDACT_MAX_DEPTH =

Depth bound for redact_internal_fields_deep!. $lookup/$graphLookup/ $unionWith embed foreign documents at shallow alias depth, so this is generous; the bound exists only to fail safe on cyclic/pathological docs.

32
DEFAULT_REGEX_MAX_TIME_MS =

Server-side time limit, in milliseconds, applied to a mongo-direct read whose filter or pipeline carries a regex that is not escaped literal text, when the caller gave no max_time_ms. The pattern checker refuses the shapes known to backtrack catastrophically; this bounds the rest. Set default_regex_max_time_ms to nil to disable.

5_000

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_regex_max_time_ms ⇒ Integer?

Time limit (ms) for mongo-direct reads that carry a non-literal regex and no explicit max_time_ms. Defaults to DEFAULT_REGEX_MAX_TIME_MS; nil disables it.

Returns:

  • (Integer, nil)


1392
# File 'lib/parse/pipeline_security.rb', line 1392

attr_writer :default_regex_max_time_ms

Class Method Details

.assert_collection_allowed!(name) ⇒ Object

Refuses any collection name reserved for Parse Server's internal state. Accepts the four SDK-data system classes (_User, _Role, _Installation, _Session) and any non-_-prefixed name. Used by LookupRewriter and by the Agent's pipeline walker to enforce a hard floor independent of any per-Agent MetadataRegistry.hidden? policy.

Parameters:

  • name (String, Symbol, nil) —

    the collection name from from:/coll:. nil is treated as "no collection named" -- the caller passes through.

Raises:



330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/parse/pipeline_security.rb', line 330

def assert_collection_allowed!(name)
  return if name.nil?
  str = name.to_s
  return if str.empty?
  return unless str.start_with?("_")
  return if ALLOWED_UNDERSCORE_COLLECTIONS.include?(str)
  raise Error.new(
    "SECURITY: Collection '#{str}' is reserved for Parse Server's internal " \
    "state and is not reachable from an SDK aggregation pipeline.",
    operator: str,
    reason: :denied_internal_collection,
  )
end

.identity_preserving?(pipeline) ⇒ Boolean

Whether no stage of pipeline can change a row's identity (_id / objectId).

The _User self exemption (a user sees their own email) is decided from the identity of the row that comes OUT of the pipeline. A stage such as { "$set" => { "_id" => "<caller id>" } } would otherwise let another user's row claim the exemption. Only stages that cannot write an identity key count as preserving: filters, sorts, paging, $unset, $project that only includes or excludes _id, and $addFields / $set / $lookup / $graphLookup / $unwind / $geoNear that write no identity key. Every other stage ($group, $replaceRoot, $facet, $unionWith, ...) and any unknown shape returns false, so the exemption is withheld and the field stripped.

Parameters:

Returns:

  • (Boolean)


531
532
533
534
535
# File 'lib/parse/pipeline_security.rb', line 531

def identity_preserving?(pipeline)
  return true if pipeline.nil?
  return false unless pipeline.is_a?(Array)
  pipeline.all? { |stage| identity_preserving_stage?(stage) }
end

.redact_internal_fields_deep!(node, depth: INTERNAL_REDACT_MAX_DEPTH) ⇒ Object

Recursively delete INTERNAL_FIELDS_DENYLIST / INTERNAL_FIELDS_PREFIX_DENYLIST keys from node AND every embedded sub-document/array element, in place.

This is the process-level floor that stops Parse-Server-internal credential columns (_hashed_password, _session_token, _auth_data_*, _rperm/_wperm, ...) from reaching a scoped caller through ANY result shape — most importantly a foreign-class document pulled in via $lookup/$graphLookup/$unionWith under an arbitrary alias. Neither the per-class protectedFields strip (keyed on the OUTER class) nor the ACL sub-document walk (which only DROPS ACL-failing sub-docs, never strips field names) covers that alias. Unlike strip_internal_fields (one level, non-mutating), this walks the whole tree and mutates in place so it can run as the last step over a result set.

Structural columns (_id, _p_*, _created_at, _updated_at, _acl) are intentionally NOT in the denylist, so object/ACL reconstruction is unaffected.

Parameters:

  • node (Object) —

    a result row (Hash), array, or scalar.

Returns:

  • (Object) —

    the same node, mutated.



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/parse/pipeline_security.rb', line 383

def redact_internal_fields_deep!(node, depth: INTERNAL_REDACT_MAX_DEPTH)
  case node
  when Hash
    # Always clean the current level (even at the depth floor) so an
    # embedded document sitting exactly at the bound is still scrubbed.
    node.delete_if do |key, _value|
      ks = key.to_s
      INTERNAL_FIELDS_DENYLIST.include?(ks) ||
        INTERNAL_FIELDS_PREFIX_DENYLIST.any? { |prefix| ks.start_with?(prefix) }
    end
    node.each_value { |v| redact_internal_fields_deep!(v, depth: depth - 1) } if depth > 0
  when Array
    node.each { |el| redact_internal_fields_deep!(el, depth: depth - 1) } if depth > 0
  end
  node
end

.refuse_protected_field_references!(pipeline, collection_name, resolution)

This method returns an undefined value.

Wave-3 TRACK-CLP-4: refuse caller-supplied pipelines that reference a protected field via $<field> on the RHS of a $project / $addFields / $set / $group / $bucket / $replaceWith / $lookup.let clause.

The protectedFields enforcement layer (CLPScope.redact_protected_fields!) strips the field by NAME from the result rows. But a pipeline can launder a protected field through a rename:

{ "$addFields" => { "ssn_copy" => "$ssn" } } { "$project" => { "renamed" => "$ssn", "objectId" => 1 } } { "$group" => { "_id" => "$ssn", "n" => { "$sum" => 1 } } }

The post-fetch strip walks the rows and deletes ssn keys, but the value is now stored under ssn_copy / renamed / _id, so the strip walks past it. This scanner runs BEFORE the pipeline reaches Mongo: any $<field> string whose unprefixed name is in the class's protected-fields set raises CLPScope::Denied so the caller knows the join was refused, rather than silently leaking the renamed value.

Variable references ($$ROOT, $$CURRENT, $$user_var) are NOT field references — they're aggregation variables. The walker checks the leading $ is single, not double, before treating the string as a field path.

Master mode + nil resolution short-circuit at the entry: the walker is a no-op when the caller can read everything anyway.

Parameters:

  • pipeline (Array<Hash>) —

    the caller-supplied pipeline, before SDK-side ACL stages are prepended.

  • collection_name (String) —

    the queried collection / class.

  • resolution (Parse::ACLScope::Resolution, nil) —

    the resolved scope; nil-or-master short-circuits.

Raises:



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/parse/pipeline_security.rb', line 437

def refuse_protected_field_references!(pipeline, collection_name, resolution)
  return if resolution.nil? || (resolution.respond_to?(:master?) && resolution.master?)
  return if pipeline.nil? || pipeline.empty?
  perms = resolution.respond_to?(:permission_strings) ? resolution.permission_strings : nil
  return if perms.nil?

  # Lazy-require to avoid forcing CLPScope load order when the
  # caller hasn't otherwise needed it.
  require_relative "clp_scope" unless defined?(Parse::CLPScope)

  require_relative "atlas_search/protected_paths" unless defined?(Parse::AtlasSearch::ProtectedPaths)

  # Read the schema the CLP gate reads: the resolution's client, not the
  # default one, so a multi-app setup scans the right application's
  # protectedFields. `strict:` fails closed when the schema is
  # unresolvable; an empty set would let a `$match` on a protected
  # field through as a value oracle.
  client = schema_client_for(resolution)
  protected_set = Parse::CLPScope.protected_fields_for(collection_name, perms, client: client, strict: true)
  # A joined class whose schema is unresolvable is refused after the
  # walk, so a denial that names a protected field of the queried class
  # is reported first.
  unresolved = nil
  sets = Hash.new do |memo, klass|
    memo[klass] = begin
        Parse::CLPScope.protected_fields_for(klass, perms, client: client, strict: true)
      rescue Parse::CLPScope::Denied => e
        unresolved ||= e
        Set.new
      end
  end
  sets[collection_name.to_s] = protected_set || Set.new

  pipeline.each_with_index do |stage, idx|
    refuse_internal_search_paths!(stage, collection_name.to_s, resolution)
    walk_stage_refs!(stage, collection_name.to_s, sets, "pipeline[#{idx}]")
    refuse_protected_stage_keys!(stage, collection_name.to_s, sets, "pipeline[#{idx}]")
  end
  raise unresolved if unresolved
  nil
end

.regex_time_budget(node) ⇒ Integer?

The time limit to apply to a read of node (a filter or pipeline): default_regex_max_time_ms when it carries a regex that is not escaped literal text, otherwise nil.

Parameters:

Returns:

  • (Integer, nil)


1403
1404
1405
1406
1407
# File 'lib/parse/pipeline_security.rb', line 1403

def regex_time_budget(node)
  budget = default_regex_max_time_ms
  return nil if budget.nil?
  nonliteral_regex?(node, 0) ? budget : nil
end

.strip_internal_fields(doc) ⇒ Object

Strip INTERNAL_FIELDS_DENYLIST keys from a Hash document (one level deep -- raw search documents are flat). Returns a new Hash; the input is not mutated. Non-Hash inputs return unchanged so callers can pipe arbitrary cursor entries through this.



348
349
350
351
352
353
354
355
356
# File 'lib/parse/pipeline_security.rb', line 348

def strip_internal_fields(doc)
  return doc unless doc.is_a?(Hash)
  doc.each_with_object({}) do |(key, value), out|
    k = key.to_s
    next if INTERNAL_FIELDS_DENYLIST.include?(k)
    next if INTERNAL_FIELDS_PREFIX_DENYLIST.any? { |prefix| k.start_with?(prefix) }
    out[key] = value
  end
end

.valid_filter?(node) ⇒ Boolean

Returns true if the node passes permissive validation.

Returns:

  • (Boolean) —

    true if the node passes permissive validation.



311
312
313
314
315
316
# File 'lib/parse/pipeline_security.rb', line 311

def valid_filter?(node)
  validate_filter!(node)
  true
rescue Error
  false
end

.valid_pipeline?(pipeline) ⇒ Boolean

Returns true if the pipeline passes strict validation.

Returns:

  • (Boolean) —

    true if the pipeline passes strict validation.



303
304
305
306
307
308
# File 'lib/parse/pipeline_security.rb', line 303

def valid_pipeline?(pipeline)
  validate_pipeline!(pipeline)
  true
rescue Error
  false
end

.validate_filter!(node, allow_internal_fields: false) ⇒ true

Permissive validation: walks the given Hash or Array (or anything else, which is a no-op) and refuses any nested key that appears in DENIED_OPERATORS. Does NOT check the top-level stage allowlist or the stage count cap. Used by direct-MongoDB sinks where callers have explicit intent and want flexibility in stage selection, but server-side JS and data-mutating operators must still be refused.

Parameters:

  • node (Hash, Array, Object) —

    the structure to walk.

  • allow_internal_fields (Boolean) (defaults to: false) —

    when true, skip the INTERNAL_FIELDS_DENYLIST check (e.g. for SDK-generated ACL filters that legitimately reference _rperm/_wperm via Query#readable_by_role and friends). The DENIED_OPERATORS walk and forensic-operator gating still apply. Default false for callers that forward raw, user-influenced pipelines (e.g. Agent MCP tools).

Returns:

  • (true)

Raises:

  • (Error) —

    if a denied operator is found at any depth.



297
298
299
300
# File 'lib/parse/pipeline_security.rb', line 297

def validate_filter!(node, allow_internal_fields: false)
  walk_for_denied!(node, depth: 0, allow_internal_fields: allow_internal_fields)
  true
end

.validate_pipeline!(pipeline) ⇒ true

Strict validation: pipeline must be a non-empty Array of Hashes, each Hash's top-level key must be in ALLOWED_STAGES, and no entry in DENIED_OPERATORS may appear at any nesting depth.

Parameters:

  • pipeline (Array<Hash>) —

    the aggregation pipeline.

Returns:

  • (true)

Raises:

  • (Error) —

    if validation fails.



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/parse/pipeline_security.rb', line 259

def validate_pipeline!(pipeline)
  unless pipeline.is_a?(Array)
    raise Error.new("Pipeline must be an Array, got #{pipeline.class}", reason: :invalid_type)
  end
  if pipeline.empty?
    raise Error.new("Pipeline cannot be empty", reason: :empty_pipeline)
  end
  if pipeline.size > MAX_PIPELINE_STAGES
    raise Error.new(
      "Pipeline exceeds maximum of #{MAX_PIPELINE_STAGES} stages (got #{pipeline.size})",
      reason: :too_many_stages,
    )
  end

  pipeline.each_with_index do |stage, idx|
    validate_stage!(stage, idx)
  end
  true
end