Skip to content

Commit 6ba9655

Browse files
TV App: Add validation logic for supported clusters and response commands (#34454)
* Add validation logic * Restyled by google-java-format * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent da6dd90 commit 6ba9655

File tree

2 files changed

+75
-31
lines changed

2 files changed

+75
-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,38 +58,43 @@ 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());
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 {
93+
reader.skipValue();
7094
}
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 {
92-
reader.skipValue();
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
9398
}
9499
}
95100
supportedClusters.add(cluster);

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

+39
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,26 @@ 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+
{
143+
ChipLogError(Zcl, "Failed to parse JSON: %s\n", errors.c_str());
144+
env->DeleteLocalRef(resp);
145+
return chip::Protocols::InteractionModel::Status::Failure;
146+
}
147+
148+
// Validate and access JSON data safely
149+
if (!value.isObject())
150+
{
151+
ChipLogError(Zcl, "Invalid JSON structure: not an object");
152+
env->DeleteLocalRef(resp);
153+
return chip::Protocols::InteractionModel::Status::Failure;
154+
}
155+
136156
Json::Reader reader;
137157
if (!reader.parse(respStr.c_str(), value))
138158
{
@@ -166,7 +186,26 @@ void ContentAppCommandDelegate::FormatResponseData(CommandHandlerInterface::Hand
166186
{
167187
handlerContext.SetCommandHandled();
168188
Json::Reader reader;
189+
190+
Json::CharReaderBuilder readerBuilder;
191+
std::string errors;
192+
169193
Json::Value value;
194+
std::unique_ptr<Json::CharReader> testReader(readerBuilder.newCharReader());
195+
196+
if (!testReader->parse(response, response + std::strlen(response), &value, &errors))
197+
{
198+
ChipLogError(Zcl, "Failed to parse JSON: %s\n", errors.c_str());
199+
return;
200+
}
201+
202+
// Validate and access JSON data safely
203+
if (!value.isObject())
204+
{
205+
ChipLogError(Zcl, "Invalid JSON structure: not an object");
206+
return;
207+
}
208+
170209
if (!reader.parse(response, value))
171210
{
172211
return;

0 commit comments

Comments
 (0)