fix: validate QR and action metadata edges

This commit is contained in:
2026-07-21 11:16:52 +08:00
parent 965e035052
commit 2cbac73f2a
4 changed files with 171 additions and 13 deletions
+14 -3
View File
@@ -161,7 +161,7 @@ func fetchDeviceInfo(ctx context.Context, httpClient *http.Client, model string)
if err != nil {
return DeviceInfo{}, fmt.Errorf("%w: %w", &GetDeviceInfoError{DeviceModel: model}, err)
}
if err := validateDeviceInfo(info, model); err != nil {
if err := validateDeviceInfo(&info, model); err != nil {
return DeviceInfo{}, fmt.Errorf("%w: %w", &GetDeviceInfoError{DeviceModel: model}, err)
}
return info, nil
@@ -546,21 +546,27 @@ func decodeDeviceInfo(data []byte, model string) (DeviceInfo, error) {
}
info.Actions = append(info.Actions, action)
}
if err := validateDeviceInfo(info, model); err != nil {
if err := validateDeviceInfo(&info, model); err != nil {
return DeviceInfo{}, err
}
return info, nil
}
func validateDeviceInfo(info DeviceInfo, model string) error {
func validateDeviceInfo(info *DeviceInfo, model string) error {
if info.Model == "" || info.Model != model {
return fmt.Errorf("model %q does not match requested model %q", info.Model, model)
}
type propertyID struct {
siid int
piid int
}
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 {
return fmt.Errorf("property %d is invalid", index)
}
properties[propertyID{siid: property.SIID, piid: property.PIID}] = property
}
for index, action := range info.Actions {
if strings.TrimSpace(action.Name) == "" || action.SIID <= 0 || action.AIID <= 0 {
@@ -570,6 +576,11 @@ func validateDeviceInfo(info DeviceInfo, model string) error {
if !validPropertyType(input.Type) || input.SIID <= 0 || input.PIID <= 0 || input.SIID != action.SIID {
return fmt.Errorf("action %d input %d is invalid", index, inputIndex)
}
property, exists := properties[propertyID{siid: input.SIID, piid: input.PIID}]
if !exists {
return fmt.Errorf("action %d input %d does not reference a property", index, inputIndex)
}
info.Actions[index].Inputs[inputIndex] = property
}
}
return nil