forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMTRDeviceDataValidation.mm
233 lines (194 loc) · 8.12 KB
/
MTRDeviceDataValidation.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* Copyright (c) 2024 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "MTRDeviceDataValidation.h"
#import "MTRBaseDevice.h"
#import "MTRLogging_Internal.h"
// MTRDataValueDictionaryIsWellFormed lives in MTRBaseDevice.mm, because it uses
// static functions defined in that file.
#pragma mark - Helpers used by multiple validators
#define MTR_CHECK_CLASS(className) \
^(className * arg) { return MTR_SAFE_CAST(arg, className) != nil; }
// input is not known to be an NSDictionary yet on entry.
//
// expectedShape maps keys to value validator blocks.
static BOOL MTRDictionaryHasExpectedShape(NSDictionary * input, NSDictionary * expectedShape)
{
if (!MTR_SAFE_CAST(input, NSDictionary)) {
return NO;
}
for (id key in expectedShape) {
id value = input[key];
if (!value) {
return NO;
}
auto validator = static_cast<BOOL (^)(id)>(expectedShape[key]);
if (!validator(value)) {
return NO;
}
}
return YES;
}
#pragma mark - Attribute report validation
static const auto sAttributeDataShape = @{
MTRAttributePathKey : MTR_CHECK_CLASS(MTRAttributePath),
MTRDataKey : (^(MTRDeviceDataValueDictionary arg) {
return MTRDataValueDictionaryIsWellFormed(arg);
}),
};
static const auto sAttributeErrorShape = @{
MTRAttributePathKey : MTR_CHECK_CLASS(MTRAttributePath),
MTRErrorKey : MTR_CHECK_CLASS(NSError),
};
BOOL MTRAttributeReportIsWellFormed(NSArray<MTRDeviceResponseValueDictionary> * report)
{
if (!MTR_SAFE_CAST(report, NSArray)) {
MTR_LOG_ERROR("Attribute report is not an array: %@", report);
return NO;
}
for (MTRDeviceResponseValueDictionary item in report) {
// item can be a value report or an error report.
if (!MTRDictionaryHasExpectedShape(item, sAttributeDataShape) && !MTRDictionaryHasExpectedShape(item, sAttributeErrorShape)) {
MTR_LOG_ERROR("Attribute report contains a weird entry: %@", item);
return NO;
}
// Now we know item is in fact a dictionary, and it has at least one of MTRDataKey and MTRErrorKey. Make sure it's
// not claiming both, which could confuse code that examines it.
if (item[MTRDataKey] != nil && item[MTRErrorKey] != nil) {
MTR_LOG_ERROR("Attribute report contains an entry that claims to be both data and error: %@", item);
return NO;
}
}
return YES;
}
#pragma mark - Event report validation
// MTREventIsHistoricalKey is claimed to be present no matter what, as
// long as MTREventPathKey is present.
static const auto sEventDataShape = @{
MTREventPathKey : MTR_CHECK_CLASS(MTREventPath),
MTRDataKey : (^(MTRDeviceDataValueDictionary arg) {
return MTRDataValueDictionaryIsWellFormed(arg);
}),
MTREventIsHistoricalKey : MTR_CHECK_CLASS(NSNumber),
MTREventNumberKey : MTR_CHECK_CLASS(NSNumber),
MTREventPriorityKey : MTR_CHECK_CLASS(NSNumber),
MTREventTimeTypeKey : MTR_CHECK_CLASS(NSNumber),
};
static const auto sEventErrorShape = @{
MTREventPathKey : MTR_CHECK_CLASS(MTREventPath),
MTRErrorKey : MTR_CHECK_CLASS(NSError),
MTREventIsHistoricalKey : MTR_CHECK_CLASS(NSNumber),
};
BOOL MTREventReportIsWellFormed(NSArray<MTRDeviceResponseValueDictionary> * report)
{
if (!MTR_SAFE_CAST(report, NSArray)) {
MTR_LOG_ERROR("Event report is not an array: %@", report);
return NO;
}
for (MTRDeviceResponseValueDictionary item in report) {
// item can be a value report or an error report.
if (!MTRDictionaryHasExpectedShape(item, sEventDataShape) && !MTRDictionaryHasExpectedShape(item, sEventErrorShape)) {
MTR_LOG_ERROR("Event report contains a weird entry: %@", item);
return NO;
}
// Now we know item is in fact a dictionary, and it has at least one of MTRDataKey and MTRErrorKey. Make sure it's
// not claiming both, which could confuse code that examines it.
if (item[MTRDataKey] != nil && item[MTRErrorKey] != nil) {
MTR_LOG_ERROR("Event report contains an entry that claims to be both data and error: %@", item);
return NO;
}
if (item[MTRDataKey]) {
// Check well-formedness of our timestamps. Note that we have
// already validated the type of item[MTREventTimeTypeKey].
uint64_t eventTimeType = [item[MTREventTimeTypeKey] unsignedLongLongValue];
switch (eventTimeType) {
case MTREventTimeTypeSystemUpTime: {
if (!MTR_SAFE_CAST(item[MTREventSystemUpTimeKey], NSNumber)) {
MTR_LOG_ERROR("Event report claims system uptime timing but does not have the time: %@", item);
return NO;
}
break;
}
case MTREventTimeTypeTimestampDate: {
if (!MTR_SAFE_CAST(item[MTREventTimestampDateKey], NSDate)) {
MTR_LOG_ERROR("Event report claims epoch timing but does not have the time: %@", item);
return NO;
}
break;
}
default:
MTR_LOG_ERROR("Unknown time type for event report: %@", item);
return NO;
}
}
}
return YES;
}
#pragma mark - Invoke response validation
BOOL MTRInvokeResponseIsWellFormed(NSArray<MTRDeviceResponseValueDictionary> * response)
{
if (!MTRInvokeResponsesAreWellFormed(response)) {
return NO;
}
// Input is an array with a single value.
if (response.count != 1) {
MTR_LOG_ERROR("Invoke response is not an array with exactly one entry: %@", response);
return NO;
}
return YES;
}
BOOL MTRInvokeResponsesAreWellFormed(NSArray<MTRDeviceResponseValueDictionary> * responses)
{
if (!MTR_SAFE_CAST(responses, NSArray)) {
MTR_LOG_ERROR("Invoke responses are not an array: %@", responses);
return NO;
}
for (MTRDeviceResponseValueDictionary responseValue in responses) {
// Each entry must be a dictionary that has MTRCommandPathKey.
if (!MTR_SAFE_CAST(responseValue, NSDictionary) || !MTR_SAFE_CAST(responseValue[MTRCommandPathKey], MTRCommandPath)) {
MTR_LOG_ERROR("Invoke response has an invalid array entry: %@", responseValue);
return NO;
}
MTRDeviceDataValueDictionary _Nullable data = responseValue[MTRDataKey];
NSError * _Nullable error = responseValue[MTRErrorKey];
if (data != nil && error != nil) {
MTR_LOG_ERROR("Invoke response claims to have both data and error: %@", responseValue);
return NO;
}
if (error != nil) {
if (!MTR_SAFE_CAST(error, NSError)) {
MTR_LOG_ERROR("Invoke response %@ has %@ instead of an NSError", responseValue, error);
return NO;
}
// Valid error response.
continue;
}
if (data == nil) {
// This is valid: indicates a success status response.
continue;
}
if (!MTRDataValueDictionaryIsWellFormed(data)) {
MTR_LOG_ERROR("Invoke response claims to have data that is not a data-value: %@", data);
return NO;
}
// Now we know data is a dictionary (in fact a data-value). The only thing
// we promise about it is that it has type MTRStructureValueType.
if (![MTRStructureValueType isEqual:data[MTRTypeKey]]) {
MTR_LOG_ERROR("Invoke response data is not of structure type: %@", data);
return NO;
}
}
return YES;
}