Module: Ruby::Rego::Builtins::Glob

Extended by:
RegistryHelpers
Defined in:
lib/ruby/rego/builtins/glob.rb,
lib/ruby/rego/builtins/glob/compiler.rb

Overview

Built-in glob helpers.

Defined Under Namespace

Classes: GlobCompiler

Constant Summary collapse

GLOB_FUNCTIONS =
{
  "glob.match" => { arity: 3, handler: :match },
  "glob.quote_meta" => { arity: 1, handler: :quote_meta }
}.freeze
QUOTE_META_PATTERN =

Metacharacters escaped by glob.quote_meta, matching OPA: the wildcards, class, and brace characters plus the escape character itself. (OPA does not escape the , that raw gobwas QuoteMeta does — verified against opa eval.)

/[*?\[\]{}\\]/
DEFAULT_DELIMITERS =
["."].freeze
MAX_DELIMITERS =

Upper bound on the delimiter count. The non-delimiter character class is built once from the delimiters, so an enormous delimiter array would do O(n) work before the per-token compile-size guard applies; a count far above any real use is rejected as undefined.

1 << 16
GLOB_TIMEOUT_SECONDS =

Per-match timeout (shared knob with the regex builtins) guarding against catastrophic backtracking on an untrusted compiled pattern.

ENV.fetch("RUBY_REGO_REGEX_TIMEOUT", "1.0").to_f

Class Method Summary collapse

Methods included from RegistryHelpers

register_configured_functions

Class Method Details

.match(pattern, delimiters, value) ⇒ Ruby::Rego::BooleanValue

Parameters:

Returns:



74
75
76
77
78
79
80
81
# File 'lib/ruby/rego/builtins/glob.rb', line 74

def self.match(pattern, delimiters, value)
  pattern_text = string_arg(pattern, "glob.match")
  text = string_arg(value, "glob.match")
  regexp = compile(pattern_text, separators(delimiters))
  BooleanValue.new(regexp.match?(text))
rescue Regexp::TimeoutError
  raise_glob_error("pattern timed out", pattern_text.to_s)
end

.quote_meta(pattern) ⇒ Ruby::Rego::StringValue

Parameters:

Returns:



85
86
87
88
# File 'lib/ruby/rego/builtins/glob.rb', line 85

def self.quote_meta(pattern)
  text = string_arg(pattern, "glob.quote_meta")
  StringValue.new(text.gsub(QUOTE_META_PATTERN) { |char| "\\#{char}" })
end

.register!Ruby::Rego::Builtins::BuiltinRegistry



62
63
64
65
66
# File 'lib/ruby/rego/builtins/glob.rb', line 62

def self.register!
  registry = BuiltinRegistry.instance
  register_configured_functions(registry, GLOB_FUNCTIONS)
  registry
end