Class: LittleGhost::Sandbox::EnvironmentPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/little_ghost/sandbox/environment_policy.rb

Overview

Declares which environment values a Sandbox may pass to child processes.

A Policy supplies a scrubbed locale and path baseline when its environment option is omitted. Declaring an EnvironmentPolicy replaces that baseline. Workspace routing variables are added separately. Enabling inheritance here only permits it; an individual process call must also opt in.

See the Workspaces and Sandboxes guide for defaults and host-path considerations.

Constant Summary collapse

DEFAULT_PATH =

:nodoc:

"/usr/local/bin:/usr/bin:/bin"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inherit: false, values: {}) ⇒ EnvironmentPolicy

Builds an environment policy with explicit String-compatible values.

Raises:



49
50
51
52
53
54
55
# File 'lib/little_ghost/sandbox/environment_policy.rb', line 49

def initialize(inherit: false, values: {})
  raise PolicyError, "sandbox environment values must be a Hash" unless values.is_a?(Hash)

  @inherit = !!inherit
  @values = values.to_h { |key, value| [String(key).freeze, String(value).freeze] }.freeze
  freeze
end

Instance Attribute Details

#valuesObject (readonly)

Explicit child environment values, before Workspace routing values are added.



58
59
60
# File 'lib/little_ghost/sandbox/environment_policy.rb', line 58

def values
  @values
end

Class Method Details

.coerce(value) ⇒ Object

Returns value unchanged or builds a policy from a Hash.

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/little_ghost/sandbox/environment_policy.rb', line 34

def self.coerce(value)
  return value if value.is_a?(self)
  raise PolicyError, "sandbox environment must be a Hash" unless value.is_a?(Hash)

  if value.key?(:set) || value.key?("set") || value.key?(:inherit) || value.key?("inherit")
    values = value[:set] || value["set"] || {}
    inherit = value.fetch(:inherit, value.fetch("inherit", false))
  else
    values = value
    inherit = false
  end
  new(inherit:, values:)
end

.default(environment: ENV) ⇒ Object

Builds the safe baseline used when no environment policy is declared.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/little_ghost/sandbox/environment_policy.rb', line 21

def self.default(environment: ENV) # :nodoc:
  lang = environment.fetch("LANG", "C.UTF-8")
  lc_all = environment.fetch("LC_ALL", lang)
  path = environment.fetch("PATH", DEFAULT_PATH)
  path = path.split(File::PATH_SEPARATOR).select do |entry|
    !entry.empty? && Pathname.new(entry).absolute?
  end.uniq.join(File::PATH_SEPARATOR)
  path = DEFAULT_PATH if path.empty?

  new(values: {"LANG" => lang, "LC_ALL" => lc_all, "PATH" => path})
end

Instance Method Details

#inherit?Boolean

Indicates whether a process call may opt into inheriting host values.

Returns:

  • (Boolean)


61
62
# File 'lib/little_ghost/sandbox/environment_policy.rb', line 61

def inherit? = @inherit
# Returns the explicit child environment values.

#to_hObject

Returns the explicit child environment values.



63
# File 'lib/little_ghost/sandbox/environment_policy.rb', line 63

def to_h = values