fix: validate cached property access
This commit is contained in:
@@ -498,7 +498,24 @@ type cacheMethod struct {
|
||||
|
||||
type propertyCache struct {
|
||||
PropertySpec
|
||||
Method cacheMethod `json:"method"`
|
||||
Method cacheMethod `json:"method"`
|
||||
rwPresent bool
|
||||
}
|
||||
|
||||
func (cache *propertyCache) UnmarshalJSON(data []byte) error {
|
||||
type propertyCacheAlias propertyCache
|
||||
decoded := struct {
|
||||
*propertyCacheAlias
|
||||
RW *string `json:"rw"`
|
||||
}{propertyCacheAlias: (*propertyCacheAlias)(cache)}
|
||||
if err := json.Unmarshal(data, &decoded); err != nil {
|
||||
return err
|
||||
}
|
||||
cache.rwPresent = decoded.RW != nil
|
||||
if decoded.RW != nil {
|
||||
cache.RW = *decoded.RW
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type actionCache struct {
|
||||
@@ -536,6 +553,9 @@ func decodeDeviceInfo(data []byte, model string) (DeviceInfo, error) {
|
||||
|
||||
info := DeviceInfo{Name: cache.Name, Model: cache.Model}
|
||||
for _, cachedProperty := range cache.Properties {
|
||||
if !cachedProperty.rwPresent {
|
||||
return DeviceInfo{}, fmt.Errorf("property %q is missing access metadata", cachedProperty.Name)
|
||||
}
|
||||
property := cachedProperty.PropertySpec
|
||||
if property.SIID == 0 {
|
||||
property.SIID = cachedProperty.Method.SIID
|
||||
@@ -572,7 +592,7 @@ func validateDeviceInfo(info *DeviceInfo, model string) error {
|
||||
properties := make(map[propertyID]PropertySpec, len(info.Properties))
|
||||
for index, property := range info.Properties {
|
||||
if strings.TrimSpace(property.Name) == "" || !validPropertyType(property.Type) ||
|
||||
(property.RW != "r" && property.RW != "w" && property.RW != "rw") || property.SIID <= 0 || property.PIID <= 0 {
|
||||
(property.RW != "" && property.RW != "r" && property.RW != "w" && property.RW != "rw") || property.SIID <= 0 || property.PIID <= 0 {
|
||||
return fmt.Errorf("property %d is invalid", index)
|
||||
}
|
||||
properties[propertyID{siid: property.SIID, piid: property.PIID}] = property
|
||||
|
||||
Reference in New Issue
Block a user