Class: Vangrail::Admission

Inherits:
Object
  • Object
show all
Defined in:
lib/vangrail/origin.rb

Overview

Whether a named capability may be exercised, given the cell that asked and the cell that supplied the arguments.

Two questions, because they are not the same. Who requested the tool is the planner's question: only privileged origin may ask. What the arguments were derived from is the policy's question: a deployment that wants retrieved data to feed cite says so. A deployment that does not say so cannot be surprised by a wiki page that asked for a shell.

gate = Admission.new(allow: { cite: %i[data], search: [] })
gate.permit?(:cite, request: Cell.user(q), arguments: Cell.data(page))
# => true
gate.permit?(:search, request: Cell.user(q))
# => true
gate.permit?(:shell, request: Cell.user(q))
# => false
gate.permit?(:shell, request: Cell.data(page))
# => false

An empty gate grants nothing. A key in allow is the grant; the value is which untrusted origins may supply arguments. A request cell that carries its own capability set must include the name.

Instance Method Summary collapse

Constructor Details

#initialize(allow: {}) ⇒ Admission

Returns a new instance of Admission.



391
392
393
394
395
# File 'lib/vangrail/origin.rb', line 391

def initialize(allow: {})
  @allow = allow.transform_keys(&:to_sym)
                .transform_values { |kinds| Array(kinds).map(&:to_sym) }
                .freeze
end

Instance Method Details

#permit?(capability, request:, arguments: nil) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/vangrail/origin.rb', line 397

def permit?(capability, request:, arguments: nil)
  raise ArgumentError, 'request must be a Cell' unless request.is_a?(Cell)
  raise ArgumentError, 'arguments must be a Cell' if !arguments.nil? && !arguments.is_a?(Cell)

  cap = capability.to_sym
  return false unless request.privileged?
  return false unless granted?(cap, request)
  return true if arguments.nil? || arguments.privileged?

  allowed = @allow[cap] || []
  arguments.origins.all? { |origin| origin.privileged? || allowed.include?(origin.kind) }
end