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

Default chef attributes value may lead to accessing nil. #282

Closed
arlimus opened this Issue Sep 22, 2017 · 3 comments

Comments

Projects
None yet
2 participants
@arlimus
Contributor

arlimus commented Sep 22, 2017

Using "Option 2: Use the chef node object" to pull in chef attributes may lead to code like this:

chef_node['a']['b']['c'] == 'd'

During an inspec check the value of chef_node by default is {} which lead to accessing [] from nil.

Proposed solution: Use a mesh that always returns something accessible by default.

@arlimus arlimus added the Type: Bug label Sep 22, 2017

@arlimus

This comment has been minimized.

Contributor

arlimus commented Sep 22, 2017

This is caused due to the desription in this readme:
https://github.com/chef-cookbooks/audit/tree/4ca2bc44be58fac1080a9907960934e357a33fda#option-2-use-the-chef-node-object

One solution to this problem may be:

default_attributes = {}
default_attributes.default_proc=->(a,_){a}
chef_node = attribute('chef_node', description: 'Chef Node', default: default_attributes)

It's not pretty at all, but it works.

We could also predefine it somewhere and just call it. Or let inspec get a default for {} which maps this behavior:

chef_node = attribute('chef_node', description: 'Chef Node', type: :hash)
@adamleff

This comment has been minimized.

Collaborator

adamleff commented Sep 22, 2017

This doesn't feel like an audit cookbook problem, it feels like an InSpec problem. Since we don't support the passing of attributes during a check (and I don't think we should), I wonder if we should consider stubbing out the attribute DSL method during a check to a simple object with a method_missing with splat args (*args) and just return that object back so anything that tries to call methods on it is a bit safer.

@arlimus

This comment has been minimized.

Contributor

arlimus commented Sep 22, 2017

@adamleff Fully agreed. Hide the implementation, maybe respect types.... moving this to InSpec.
Leaving this issue open since the description will need a change once we are done so that the default value is not {} anymore. (it should just be removed when the problem is solved in inspec)

arlimus added a commit that referenced this issue Sep 22, 2017

arlimus added a commit that referenced this issue Sep 22, 2017

let inspec set the default attribute for chef node attributes
Prevents #282 with InSpec >= 1.38.4

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>

adamleff added a commit that referenced this issue Sep 22, 2017

let inspec set the default attribute for chef node attributes (#283)
Prevents #282 with InSpec >= 1.38.4

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>

miah added a commit to miah/audit that referenced this issue Apr 11, 2018

let inspec set the default attribute for chef node attributes (chef-c…
…ookbooks#283)

Prevents chef-cookbooks#282 with InSpec >= 1.38.4

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment