Class: Synthra::Parser::AST::ConditionNode

Inherits:
Node
  • Object
show all
Defined in:
lib/synthra/parser/ast.rb

Overview

Condition node for conditional fields

Represents the condition part of a conditional field (if ...). The condition references another field that must be truthy.

Examples:

DSL

admin_note: text if is_admin

AST

ConditionNode.new(field_name: "is_admin")

Constant Summary collapse

DANGEROUS_METHODS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Dangerous method names that should never be allowed in conditions

%w[class instance_eval eval send __send__ method_missing].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_name:, line: nil) ⇒ ConditionNode

Create a new ConditionNode

Parameters:

  • field_name (String)

    name of field to check for truthiness

  • line (Integer, nil) (defaults to: nil)

    source line number



435
436
437
438
# File 'lib/synthra/parser/ast.rb', line 435

def initialize(field_name:, line: nil)
  super(line: line)
  @field_name = field_name
end

Instance Attribute Details

#field_nameObject (readonly)

Returns the value of attribute field_name.



427
428
429
# File 'lib/synthra/parser/ast.rb', line 427

def field_name
  @field_name
end

Instance Method Details

#evaluate(context) ⇒ Boolean

Evaluate the condition against a context

Checks if the referenced field has a truthy value in the given context. Security: Only uses hash access, never method calls, and validates field names against a whitelist of dangerous methods.

Examples:

condition = ConditionNode.new(field_name: "is_admin")
condition.evaluate({ "is_admin" => true })   # => true
condition.evaluate({ "is_admin" => false })  # => false
condition.evaluate({ "is_admin" => nil })    # => false

Parameters:

Returns:

  • (Boolean)

    true if condition is met



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/synthra/parser/ast.rb', line 456

def evaluate(context)

  # Security: Only use hash access, never public_send
  # Reject dangerous field names that could be exploited
  field_name_str = @field_name.to_s
  return false if field_name_str.start_with?("__")

  # Whitelist validation: reject dangerous method names
  return false if self.class::DANGEROUS_METHODS.include?(field_name_str)
  
  return false unless context.respond_to?(:[])

  value = context[@field_name] || context[@field_name.to_sym]
  !!value
end