Skip to content

Commit 4b23dca

Browse files
ahuo1GodGun123
andauthored
fix: validate __type field before accessing in fromJson (#1009)
Co-authored-by: ahuo <[email protected]>
1 parent 6731c51 commit 4b23dca

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/json_export.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,20 @@ JsonExporter::ExpectedEntry JsonExporter::fromJson(const nlohmann::json& source)
112112
}
113113
}
114114

115-
if(!source.contains("__type") && !source.is_array())
115+
if(source.is_array())
116116
{
117-
return nonstd::make_unexpected("Missing field '__type'");
117+
if(source.empty())
118+
return nonstd::make_unexpected("Missing field '__type'");
119+
const auto& first = source[0];
120+
if(!first.is_object() || !first.contains("__type"))
121+
return nonstd::make_unexpected("Missing field '__type'");
122+
if(!first["__type"].is_string())
123+
return nonstd::make_unexpected("Invalid '__type' (must be string)");
124+
}
125+
else
126+
{
127+
if(!source.is_object() || !source.contains("__type") || !source["__type"].is_string())
128+
return nonstd::make_unexpected("Missing field '__type'");
118129
}
119130

120131
auto& from_converters =

0 commit comments

Comments
 (0)