Skip to content

Commit 5bd6d44

Browse files
committed
Merge pull request #44 from csantero/invalid-json
add safeguards against invalid raw json strings
2 parents 1f8f94c + 22dc13f commit 5bd6d44

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"comments": [
3+
{
4+
"id": "5",
5+
"body": null,
6+
"customData": { },
7+
"links": {
8+
"post": null
9+
}
10+
}
11+
]
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"comments": [
3+
{
4+
"id": "5",
5+
"body": null,
6+
"customData": {
7+
"unquotedKey": 5
8+
},
9+
"links": {
10+
"post": null
11+
}
12+
}
13+
]
14+
}

JSONAPI.Tests/JSONAPI.Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@
9696
</ItemGroup>
9797
<ItemGroup>
9898
<None Include="app.config" />
99+
<None Include="Data\ReformatsRawJsonStringWithUnquotedKeys.json">
100+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
101+
</None>
102+
<None Include="Data\MalformedRawJsonString.json">
103+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
104+
</None>
99105
<None Include="Data\FormatterErrorSerializationTest.json">
100106
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
101107
</None>

JSONAPI.Tests/Json/JsonApiMediaFormaterTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,44 @@ public void Serializes_attributes_properly()
250250
Assert.AreEqual(expected, output.Trim());
251251
}
252252

253+
[TestMethod]
254+
[DeploymentItem(@"Data\ReformatsRawJsonStringWithUnquotedKeys.json")]
255+
public void Reformats_raw_json_string_with_unquoted_keys()
256+
{
257+
// Arrange
258+
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
259+
MemoryStream stream = new MemoryStream();
260+
261+
// Act
262+
var payload = new [] { new Comment { Id = 5, CustomData = "{ unquotedKey: 5 }"}};
263+
formatter.WriteToStreamAsync(typeof(Comment), payload, stream, null, null);
264+
265+
// Assert
266+
var minifiedExpectedJson = JsonHelpers.MinifyJson(File.ReadAllText("ReformatsRawJsonStringWithUnquotedKeys.json"));
267+
string output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
268+
Trace.WriteLine(output);
269+
output.Should().Be(minifiedExpectedJson);
270+
}
271+
272+
[TestMethod]
273+
[DeploymentItem(@"Data\MalformedRawJsonString.json")]
274+
public void Does_not_serialize_malformed_raw_json_string()
275+
{
276+
// Arrange
277+
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
278+
MemoryStream stream = new MemoryStream();
279+
280+
// Act
281+
var payload = new[] { new Comment { Id = 5, CustomData = "{ x }" } };
282+
formatter.WriteToStreamAsync(typeof(Comment), payload, stream, null, null);
283+
284+
// Assert
285+
var minifiedExpectedJson = JsonHelpers.MinifyJson(File.ReadAllText("MalformedRawJsonString.json"));
286+
string output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
287+
Trace.WriteLine(output);
288+
output.Should().Be(minifiedExpectedJson);
289+
}
290+
253291
[TestMethod]
254292
[DeploymentItem(@"Data\FormatterErrorSerializationTest.json")]
255293
public void Should_serialize_error()

JSONAPI/Json/JsonApiFormatter.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ internal JsonApiFormatter(IModelManager modelManager, IErrorSerializer errorSeri
4242
_modelManager = modelManager;
4343
_errorSerializer = errorSerializer;
4444
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/vnd.api+json"));
45+
ValidateRawJsonStrings = true;
4546
}
4647

48+
public bool ValidateRawJsonStrings { get; set; }
49+
4750
[Obsolete("Use ModelManager.PluralizationService instead")]
4851
public IPluralizationService PluralizationService //FIXME: Deprecated, will be removed shortly
4952
{
@@ -212,8 +215,21 @@ protected void Serialize(object value, Stream writeStream, JsonWriter writer, Js
212215
}
213216
else
214217
{
215-
var minifiedValue = JsonHelpers.MinifyJson((string) propertyValue);
216-
writer.WriteRawValue(minifiedValue);
218+
var json = (string) propertyValue;
219+
if (ValidateRawJsonStrings)
220+
{
221+
try
222+
{
223+
var token = JToken.Parse(json);
224+
json = token.ToString();
225+
}
226+
catch (Exception)
227+
{
228+
json = "{}";
229+
}
230+
}
231+
var valueToSerialize = JsonHelpers.MinifyJson(json);
232+
writer.WriteRawValue(valueToSerialize);
217233
}
218234
}
219235
else

0 commit comments

Comments
 (0)