Module: Landlock::Validation

Defined in:
lib/landlock/validation.rb

Class Method Summary collapse

Class Method Details

.normalize_argv(argv) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
# File 'lib/landlock/validation.rb', line 7

def normalize_argv(argv)
  raise ArgumentError, "argv must be an Array of command arguments" unless argv.is_a?(Array)
  raise ArgumentError, "argv must not be empty" if argv.empty?

  argv
end

.normalize_ports(ports, name) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/landlock/validation.rb', line 14

def normalize_ports(ports, name)
  Array(ports).map do |port|
    integer = Integer(port)
    raise ArgumentError, "#{name} port must be between 0 and 65535" if integer.negative? || integer > 65_535

    integer
  end
end

.validate_existing_path!(path, name, base) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
# File 'lib/landlock/validation.rb', line 33

def validate_existing_path!(path, name, base)
  string = path.to_s
  raise ArgumentError, "#{name} path must not be empty" if string.empty?

  expanded = File.expand_path(string, base)
  raise ArgumentError, "#{name} path does not exist: #{string}" if !File.exist?(expanded)
end

.validate_existing_paths(paths, name, chdir: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/landlock/validation.rb', line 23

def validate_existing_paths(paths, name, chdir: nil)
  base = chdir ? File.expand_path(chdir) : Dir.pwd
  Array(paths)
    .map do |path|
      validate_existing_path!(path, name, base)
      path.to_s
    end
    .uniq
end

.validate_output_limit!(max_output_bytes) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/landlock/validation.rb', line 41

def validate_output_limit!(max_output_bytes)
  return if max_output_bytes.nil?

  Integer(max_output_bytes).tap do |value|
    raise ArgumentError, "max_output_bytes must be non-negative" if value.negative?
  end
end

.validate_timeout!(timeout) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
# File 'lib/landlock/validation.rb', line 49

def validate_timeout!(timeout)
  return if timeout.nil?
  raise ArgumentError, "timeout must be numeric" unless timeout.is_a?(Numeric)

  Float(timeout).tap do |value|
    raise ArgumentError, "timeout must be finite" unless value.finite?
    raise ArgumentError, "timeout must be non-negative" if value.negative?
  end
end