Class: SafeYAML::Whitelist

Inherits:
Object
  • Object
show all
Defined in:
lib/safe_yaml/whitelist.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWhitelist

Returns a new instance of Whitelist.



5
6
7
# File 'lib/safe_yaml/whitelist.rb', line 5

def initialize
  reset!
end

Instance Attribute Details

#allowedObject (readonly)

Returns the value of attribute allowed.



3
4
5
# File 'lib/safe_yaml/whitelist.rb', line 3

def allowed
  @allowed
end

Instance Method Details

#add(*tags, &block) ⇒ Object



60
61
62
63
64
65
# File 'lib/safe_yaml/whitelist.rb', line 60

def add(*tags, &block)
  tags.each do |tag|
    @cached[tag] = {} if block
    @allowed[tag] = block || true
  end
end

#check(tag, value) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/safe_yaml/whitelist.rb', line 9

def check(tag, value)
  @allowed.each do |ok, checker|
    if ok === tag
      check = check_value(ok, checker, value)
      return check if check
    end
  end
  nil
end

#check_value(tag, checker, value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/safe_yaml/whitelist.rb', line 19

def check_value(tag, checker, value)
  if checker == true
    return :cacheable
  end

  if @cached[tag][value]
    return :cacheable
  end

  result = checker.call(value)
  if result == :cacheable
    @cached[tag][value] = true
    return :cacheable
  elsif result
    return :allowed
  else
    return nil
  end
end

#remove(*tags) ⇒ Object



67
68
69
70
71
72
# File 'lib/safe_yaml/whitelist.rb', line 67

def remove(*tags)
  tags.each do |tag|
    @cached.delete(tag)
    @allowed.delete(tag)
  end
end

#reset!Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/safe_yaml/whitelist.rb', line 39

def reset!
  @allowed = {}
  @cached = {}
  if SafeYAML::YAML_ENGINE == "psych"
    # psych doesn't tag the default types, except for binary
    add("!binary",
        "tag:yaml.org,2002:binary")
  else
    add("tag:yaml.org,2002:str",
        "tag:yaml.org,2002:int",
        "tag:yaml.org,2002:float",
        "tag:yaml.org,2002:binary",
        "tag:yaml.org,2002:merge",
        "tag:yaml.org,2002:null",
        %r{^tag:yaml.org,2002:bool#},
        %r{^tag:yaml.org,2002:float#},
        %r{^tag:yaml.org,2002:timestamp#},
        "tag:ruby.yaml.org,2002:object:YAML::Syck::BadAlias")
  end
end