Skip to content

Commit 3d8f2e7

Browse files
committedJul 23, 2024
Add validation logic
1 parent f6022e4 commit 3d8f2e7

File tree

2 files changed

+71
-31
lines changed

2 files changed

+71
-31
lines changed
 

‎examples/tv-app/android/App/platform-app/src/main/java/com/matter/tv/server/utils/ResourceUtils.java

+36-31
Original file line numberDiff line numberDiff line change
@@ -58,39 +58,44 @@ public Set<SupportedCluster> getSupportedClusters(final Resources resources, fin
5858
SupportedCluster cluster = new SupportedCluster();
5959
while (reader.hasNext()) {
6060
String name = reader.nextName();
61-
if (name.equals(KEY_CLUSTER_ID)) {
62-
cluster.clusterIdentifier = reader.nextInt();
63-
} else if (name.equals(KEY_FEATURE_FLAGS)) {
64-
cluster.features = reader.nextInt();
65-
} else if (name.equals(KEY_OPTIONAL_COMMANDS)) {
66-
List<Integer> commands = new ArrayList<>();
67-
reader.beginArray();
68-
while (reader.hasNext()) {
69-
commands.add(reader.nextInt());
70-
}
71-
reader.endArray();
72-
int[] commandIds = new int[commands.size()];
73-
int i = 0;
74-
for (Integer command : commands) {
75-
commandIds[i++] = command;
76-
}
77-
cluster.optionalCommandIdentifiers = commandIds;
78-
} else if (name.equals(KEY_OPTIONAL_ATTRIBUTES)) {
79-
List<Integer> attributes = new ArrayList<>();
80-
reader.beginArray();
81-
while (reader.hasNext()) {
82-
attributes.add(reader.nextInt());
83-
}
84-
reader.endArray();
85-
int[] attributeIds = new int[attributes.size()];
86-
int i = 0;
87-
for (Integer command : attributes) {
88-
attributeIds[i++] = command;
89-
}
90-
cluster.optionalAttributesIdentifiers = attributeIds;
91-
} else {
61+
try {
62+
if (name.equals(KEY_CLUSTER_ID)) {
63+
cluster.clusterIdentifier = reader.nextInt();
64+
} else if (name.equals(KEY_FEATURE_FLAGS)) {
65+
cluster.features = reader.nextInt();
66+
} else if (name.equals(KEY_OPTIONAL_COMMANDS)) {
67+
List<Integer> commands = new ArrayList<>();
68+
reader.beginArray();
69+
while (reader.hasNext()) {
70+
commands.add(reader.nextInt());
71+
}
72+
reader.endArray();
73+
int[] commandIds = new int[commands.size()];
74+
int i = 0;
75+
for (Integer command : commands) {
76+
commandIds[i++] = command;
77+
}
78+
cluster.optionalCommandIdentifiers = commandIds;
79+
} else if (name.equals(KEY_OPTIONAL_ATTRIBUTES)) {
80+
List<Integer> attributes = new ArrayList<>();
81+
reader.beginArray();
82+
while (reader.hasNext()) {
83+
attributes.add(reader.nextInt());
84+
}
85+
reader.endArray();
86+
int[] attributeIds = new int[attributes.size()];
87+
int i = 0;
88+
for (Integer command : attributes) {
89+
attributeIds[i++] = command;
90+
}
91+
cluster.optionalAttributesIdentifiers = attributeIds;
92+
} else {
9293
reader.skipValue();
9394
}
95+
} catch (NumberFormatException | IllegalStateException e) {
96+
Log.e(TAG, "Invalid number format in JSON for key: " + name, e);
97+
reader.skipValue(); // Skip the invalid entry
98+
}
9499
}
95100
supportedClusters.add(cluster);
96101
reader.endObject();

‎examples/tv-app/android/java/ContentAppCommandDelegate.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ Status ContentAppCommandDelegate::InvokeCommand(EndpointId epId, ClusterId clust
133133
JniUtfString respStr(env, resp);
134134
ChipLogProgress(Zcl, "ContentAppCommandDelegate::InvokeCommand got response %s", respStr.c_str());
135135

136+
Json::CharReaderBuilder readerBuilder;
137+
std::string errors;
138+
139+
std::unique_ptr<Json::CharReader> testReader(readerBuilder.newCharReader());
140+
141+
if (!testReader->parse(respStr.c_str(), respStr.c_str() + std::strlen(respStr.c_str()), &value, &errors)) {
142+
ChipLogError(Zcl, "Failed to parse JSON: %s\n", errors.c_str());
143+
env->DeleteLocalRef(resp);
144+
return chip::Protocols::InteractionModel::Status::Failure;
145+
}
146+
147+
// Validate and access JSON data safely
148+
if (!value.isObject()) {
149+
ChipLogError(Zcl, "Invalid JSON structure: not an object");
150+
env->DeleteLocalRef(resp);
151+
return chip::Protocols::InteractionModel::Status::Failure;
152+
}
153+
136154
Json::Reader reader;
137155
if (!reader.parse(respStr.c_str(), value))
138156
{
@@ -166,7 +184,24 @@ void ContentAppCommandDelegate::FormatResponseData(CommandHandlerInterface::Hand
166184
{
167185
handlerContext.SetCommandHandled();
168186
Json::Reader reader;
187+
188+
Json::CharReaderBuilder readerBuilder;
189+
std::string errors;
190+
169191
Json::Value value;
192+
std::unique_ptr<Json::CharReader> testReader(readerBuilder.newCharReader());
193+
194+
if (!testReader->parse(response, response + std::strlen(response), &value, &errors)) {
195+
ChipLogError(Zcl, "Failed to parse JSON: %s\n", errors.c_str());
196+
return;
197+
}
198+
199+
// Validate and access JSON data safely
200+
if (!value.isObject()) {
201+
ChipLogError(Zcl, "Invalid JSON structure: not an object");
202+
return;
203+
}
204+
170205
if (!reader.parse(response, value))
171206
{
172207
return;

0 commit comments

Comments
 (0)