Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect usage of value instead of values in InSpec AST helper method #7016

Open
mupxmup opened this issue Apr 10, 2024 · 0 comments
Open

Comments

@mupxmup
Copy link

mupxmup commented Apr 10, 2024

Describe the problem

In the collect_input method of the Profile::AstHelper::InputCollectorBase class within InSpec, there's an inconsistency in how array values are accessed. On line 51 of the profile_ast_helpers.rb file, the code attempts to access values from an array node using the value method instead of values, which may lead to unexpected behavior or errors. While it expects a single value (string, boolean, etc.), providing an array for value leads to a NoMethodError.

For example, the following code snippet throws an error:

invalid_targets = input(
'invalid_targets',
value: [
'127.0.0.1',
'::1',
],
)

/opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/inspec-core-5.22.40/lib/inspec/utils/profile_ast_helpers.rb:51:in block in collect_input': undefined method value' for s(:array, (NoMethodError)
s(:str, "127.0.0.1"),
s(:str, "::1")):RuboCop::AST::ArrayNode
from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/inspec-core-5.22.40/lib/inspec/utils/profile_ast_helpers.rb:39:in each' from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/inspec-core-5.22.40/lib/inspec/utils/profile_ast_helpers.rb:39:in collect_input'
from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/inspec-core-5.22.40/lib/inspec/utils/profile_ast_helpers.rb:352:in on_lvasgn' from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/traversal.rb:137:in block in on_dstr'
from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/traversal.rb:137:in each' from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/traversal.rb:137:in on_dstr'
from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/ast-2.4.2/lib/ast/processor/mixin.rb:259:in process' from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/inspec-core-5.22.40/lib/inspec/profile.rb:580:in block (2 levels) in info_from_parse'
from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/mixin/descendence.rb:98:in each_node' from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/inspec-core-5.22.40/lib/inspec/profile.rb:578:in block in info_from_parse'
from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/inspec-core-5.22.40/lib/inspec/profile.rb:568:in each' from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/inspec-core-5.22.40/lib/inspec/profile.rb:568:in info_from_parse'
from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/inspec-core-5.22.40/lib/inspec/profile.rb:822:in check' from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/inspec-core-5.22.40/lib/inspec/cli.rb:180:in check'
from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/thor-1.2.2/lib/thor/command.rb:27:in run' from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/thor-1.2.2/lib/thor/invocation.rb:127:in invoke_command'
from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/thor-1.2.2/lib/thor.rb:392:in dispatch' from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/thor-1.2.2/lib/thor/base.rb:485:in start'
from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/inspec-core-5.22.40/lib/inspec/base_cli.rb:35:in start' from /opt/inspec/embedded/lib/ruby/gems/3.1.0/gems/inspec-bin-5.22.40/bin/inspec:12:in <top (required)>'
from /opt/inspec/bin/inspec:284:in load' from /opt/inspec/bin/inspec:284:in

'

Possible Solution

Replacing child_node.value.value with child_node.value.values on line 51 of profile_ast_helpers.rb. This ensures that all values within the array node are properly collected. Another solution is to check the type of child_node.value before accessing its value.

if child_node.value.is_a?(Array)
opts.merge!(child_node.key.value => child_node.value.values)
else
opts.merge!(child_node.key.value => child_node.value)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant