Skip to content

Commit 082d324

Browse files
committed
In configuration settings of the Jint JS engine was added one new property - MaxJsonParseDepth (default 64)
1 parent 8057868 commit 082d324

File tree

5 files changed

+71
-4
lines changed

5 files changed

+71
-4
lines changed

src/JavaScriptEngineSwitcher.Jint/JavaScriptEngineSwitcher.Jint.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
<Description>JavaScriptEngineSwitcher.Jint contains adapter `JintJsEngine` (wrapper for the Jint JavaScript Engine (http://github.com/sebastienros/jint) version 3.0.0 Beta 2049).</Description>
2222
<PackageTags>$(PackageCommonTags);Jint</PackageTags>
2323
<PackageIconFullPath>../../Icons/JavaScriptEngineSwitcher_Jint_Logo128x128.png</PackageIconFullPath>
24-
<PackageReleaseNotes>1. Jint was updated to version 3.0.0 Beta 2049;
25-
2. Added support of .NET 6.</PackageReleaseNotes>
24+
<PackageReleaseNotes>In configuration settings of the Jint JS engine was added one new property - `MaxJsonParseDepth` (default `64`).</PackageReleaseNotes>
2625
</PropertyGroup>
2726

2827
<ItemGroup>

src/JavaScriptEngineSwitcher.Jint/JintJsEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public JintJsEngine(JintSettings settings)
131131
.LimitRecursion(jintSettings.MaxRecursionDepth)
132132
.LocalTimeZone(jintSettings.LocalTimeZone ?? TimeZoneInfo.Local)
133133
.MaxArraySize(jintSettings.MaxArraySize)
134+
.MaxJsonParseDepth(jintSettings.MaxJsonParseDepth)
134135
.MaxStatements(jintSettings.MaxStatements)
135136
.Strict(jintSettings.StrictMode)
136137
.TimeoutInterval(jintSettings.TimeoutInterval)

src/JavaScriptEngineSwitcher.Jint/JintSettings.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ public uint MaxArraySize
9797
set;
9898
}
9999

100+
/// <summary>
101+
/// Gets or sets a maximum depth allowed when parsing JSON data using the <c>JSON.parse</c> static method
102+
/// </summary>
103+
public int MaxJsonParseDepth
104+
{
105+
get;
106+
set;
107+
}
108+
100109
/// <summary>
101110
/// Gets or sets a maximum allowed depth of recursion:
102111
/// <c>-1</c> - recursion without limits;
@@ -181,6 +190,7 @@ public JintSettings()
181190
EnableDebugging = false;
182191
LocalTimeZone = TimeZoneInfo.Local;
183192
MaxArraySize = uint.MaxValue;
193+
MaxJsonParseDepth = 64;
184194
MaxRecursionDepth = -1;
185195
MaxStatements = 0;
186196
MemoryLimit = 0;

src/JavaScriptEngineSwitcher.Jint/readme.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
=============
1919
RELEASE NOTES
2020
=============
21-
1. Jint was updated to version 3.0.0 Beta 2049;
22-
2. Added support of .NET 6.
21+
In configuration settings of the Jint JS engine was added one new property -
22+
`MaxJsonParseDepth` (default `64`).
2323

2424
=============
2525
DOCUMENTATION

test/JavaScriptEngineSwitcher.Tests/Jint/CommonTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,63 @@ public void MappingRuntimeErrorDuringArraySizeExceeded()
250250
Assert.Equal("The array size 1000000000 is larger than maximum allowed (1000000)", exception.Description);
251251
}
252252

253+
[Fact]
254+
public void MappingRuntimeErrorDuringMaxJsonParseDepthReached()
255+
{
256+
// Arrange
257+
const string input = @"var data = '{\n' +
258+
' ""menu"": {\n' +
259+
' ""id"": ""file"",\n' +
260+
' ""value"": ""File"",\n' +
261+
' ""popup"": {\n' +
262+
' ""menuItem"": [\n' +
263+
' { ""value"": ""New"", ""onclick"": ""CreateNewDoc()"" },\n' +
264+
' { ""value"": ""Open"", ""onclick"": ""OpenDoc()"" },\n' +
265+
' { ""value"": ""Close"", ""onclick"": ""CloseDoc()"" }\n' +
266+
' ]\n' +
267+
' }\n' +
268+
' }\n' +
269+
'}'
270+
;
271+
272+
JSON.parse(data);";
273+
274+
JsRuntimeException exception = null;
275+
276+
// Act
277+
using (var jsEngine = new JintJsEngine(
278+
new JintSettings
279+
{
280+
MaxJsonParseDepth = 4
281+
}
282+
))
283+
{
284+
try
285+
{
286+
jsEngine.Execute(input, "menu.js");
287+
}
288+
catch (JsRuntimeException e)
289+
{
290+
exception = e;
291+
}
292+
}
293+
294+
// Assert
295+
Assert.NotNull(exception);
296+
Assert.Equal("Runtime error", exception.Category);
297+
Assert.Equal("Max. depth level of JSON reached at position 82", exception.Description);
298+
Assert.Equal("SyntaxError", exception.Type);
299+
Assert.Equal("menu.js", exception.DocumentName);
300+
Assert.Equal(16, exception.LineNumber);
301+
Assert.Equal(1, exception.ColumnNumber);
302+
Assert.Empty(exception.SourceFragment);
303+
Assert.Equal(
304+
" at Global code (parse menu.js:16:12)" + Environment.NewLine +
305+
" at Global code (menu.js:16:1)",
306+
exception.CallStack
307+
);
308+
}
309+
253310
[Fact]
254311
public void MappingRuntimeErrorDuringRecursionDepthOverflow()
255312
{

0 commit comments

Comments
 (0)