Bug #39564
openjson/array parameter values corrupted when changing parent hostgroup
Description
- Bug
When changing the parent hostgroup of a hostgroup that has a json or
array type parameter, the parameter value gets corrupted with escaped
quotes on every save.
- Steps to reproduce
1. Create a hostgroup HG-Parent
2. Create a child hostgroup HG-Child under HG-Parent
3. Add a json type parameter to HG-Child with value ["server","admin-access"]
4. Edit HG-Child and change its parent to a different hostgroup
5. Click Submit
- Expected result
Parameter value remains: ["server","admin-access"]
- Actual result
Parameter value becomes: [\"server\",\"admin-access\"]
- Root Cause
format_value_before_type_cast() in key_type.rb unconditionally calls
JSON.dump(val) even when val is already a String. During the
process_hostgroup AJAX re-render triggered by a parent change, the Ruby
Array gets serialized to a JSON String for the form field. When the
form is submitted, that String goes through JSON.dump a second time,
double-encoding it and corrupting the value permanently.
- Fix
Added 'unless val.is_a?(String)' guard to JSON.dump and YAML.dump
calls in format_value_before_type_cast in
app/models/concerns/key_type.rb
- Affected file
app/models/concerns/key_type.rb
Updated by The Foreman Bot 10 days ago
- Status changed from New to Ready For Testing
- Pull request https://github.com/theforeman/foreman/pull/11119 added