Potential fix for code scanning alert no. 30: Code injection #3335
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Potential fix for https://github.com/chef/supermarket/security/code-scanning/30
To fix the problem, remove the use of
.constantize
on user-provided input. Instead, explicitly map allowed user input values to their corresponding classes using a Ruby hash. For example, use a lookup table:ALLOWED_RESOURCE_TYPES = {'Cookbook' => Cookbook, 'Tool' => Tool}
and doresource_class = ALLOWED_RESOURCE_TYPES[params[:resourceable_type]]
. Then, only proceed if the mapping is successful.In this file, there are two places using
.constantize
:create
, on lines 35-38, it is already protected by a whitelist array, but should use the mapping approach.destroy_group
, on lines 76-79, similar direct use of.constantize
should be replaced with a safe mapping.Define the mapping at the top of the class, and use it both in
create
anddestroy_group
. Do not rely on dynamic lookup.Suggested fixes powered by Copilot Autofix. Review carefully before merging.