Skip to content

fix: prevent data source filter panics on unknown or non-string fields - #344

Open
nagaboinaramgopal wants to merge 2 commits into
apache:mainfrom
nagaboinaramgopal:fix/datasource-filter-panic
Open

fix: prevent data source filter panics on unknown or non-string fields#344
nagaboinaramgopal wants to merge 2 commits into
apache:mainfrom
nagaboinaramgopal:fix/datasource-filter-panic

Conversation

@nagaboinaramgopal

@nagaboinaramgopal nagaboinaramgopal commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Description

Several data sources match their filters against the looked-up JSON field, and there were two problems with the JSON-backed implementations.

They asserted the value as a string, for example volumeJSON[name].(string). A filter on a field that does not exist gets nil, and one on a numeric field gets a float64, so the assertion panics the provider with interface conversion: interface {} is nil, not string.

Stringifying with fmt.Sprintf("%v", ...) alone is still wrong for a missing field, since fmt.Sprintf("%v", nil) is the literal <nil>, which a permissive regex such as .* would match, so a filter on a nonexistent field could match.

Fixed both across the affected JSON-backed data source filters: look the field up explicitly, return no match when it is absent or nil, and otherwise stringify with fmt.Sprintf("%v", ...) so numeric and bool fields match safely. The reflection based (pod, cluster) and custom (role, project) filter implementations work differently and are not touched here.

Testing

Added a unit test on the volume data source covering a filter on an unknown field, a permissive regex on a missing field, a numeric field matched against its string form, and a positive match. It needs no live CloudStack:

go test ./cloudstack/ -run TestApplyVolumeFiltersDoesNotPanicOnUnknownOrNonStringField

The unknown and numeric cases panic against the original code, and the missing-field case matches <nil>; all pass with the fix.

The data source filter helpers indexed the resource JSON map and asserted the
result to string: <json>[updatedName].(string). This panics when a filter name
matches no field (the lookup returns nil) or when the field is not a string
(numbers unmarshal to float64, booleans to bool). Format the value with
fmt.Sprintf("%v", ...) instead, matching applyIpAddressFilters. Covers the
instance, network_offering, physical_network, service_offering, ssh_keypair,
template, user, volume, vpc, vpn_connection and zone data sources. Adds a unit
test.

Signed-off-by: Ramgopal Nagaboina <ramgopal.nagaboina.dev@gmail.com>
fmt.Sprintf("%v", nil) yields the literal "<nil>", which a permissive regex such as ".*" would match, so a filter on a field that does not exist could match. Look the field up explicitly and return no match when it is absent or nil, in every data source filter (including ipaddress and vpc_offering, which already used fmt.Sprintf). Extend the unit test to cover a permissive regex on a missing field and a positive match.
@nagaboinaramgopal
nagaboinaramgopal force-pushed the fix/datasource-filter-panic branch from 2a7d799 to d5f1178 Compare September 7, 2026 20:12
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

Successfully merging this pull request may close these issues.

1 participant