Chef Foodcritic 规则不捕获属性字符串

Chef Foodcritic rule not catching attribute strings

我写了一个 foodcritic 规则来捕获任何试图写入 /etc 目录下的 directories/files 黑名单的尝试。

当列入黑名单的路径作为配方中的字符串传递给资源声明时,规则会触发,但是当它们作为属性传递时,规则不会触发:

@resources = [
'file',
'template',
'remote_file',
'remote_directory',
'directory'
]

@blacklist = [
'/etc/ssh/',
'/etc/init',
...
]

rule 'RULE001', 'do not manipulate /etc other than init/,init.d/ & default/' do
  tags %w(security)
  recipe do |ast|
    violations = []
    @resources.each do |resource_type|
      violations << find_resources(ast, type: resource_type).select do |resource|
        res_str = (resource_attribute(resource, 'path' || resource_name(resource)).to_s
        @blacklist.any? { |cmd| res_str.include? cmd }
      end
    end
    violations.flatten
  end
end

使用下面的测试,文字字符串被捕获,但是当作为属性传递时,它们被传递。谁能看到我遗漏了什么?

attributes/default.rb:

default['testbook']['etc-test'] = '/etc/ssh/test.conf'
default['testbook']['etc-dir-test'] = 'etc/ssh/somedir/'

recipes/default.rb:

#template '/etc/ssh/test.conf' do <-- caught
template node['testbook']['etc-test'] do #<-- not caught
  source 'test.conf'
  owner 'nobody'
  group 'nobody'
  mode '0644'
  action :create
end

#directory '/etc/ssh/somedir' do <-- caught
directory node['testbook']['etc-dir-test'] do <-- not caught
  action :create
end

是的,这不是您可以通过静态分析完全处理的事情。 Foodcritic 和类似的工具只能处理代码中的静态内容,任何可能在运行时发生变化的内容都不会被知道。