Skip to content

Commit 5038166

Browse files
committed
In configuration settings of the Jint JS engine was added one new property - DisableEval (default false)
1 parent c68af0f commit 5038166

File tree

15 files changed

+274
-4
lines changed

15 files changed

+274
-4
lines changed

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreSettings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public bool DisableBackgroundWork
5252
}
5353

5454
/// <summary>
55-
/// Gets or sets a flag for whether to disable calls of <c>eval</c> function
55+
/// Gets or sets a flag for whether to disable calls of <c>eval</c> function with custom code
56+
/// and <c>Function</c> constructors taking function code as string
5657
/// </summary>
5758
public bool DisableEval
5859
{

src/JavaScriptEngineSwitcher.Jint/JavaScriptEngineSwitcher.Jint.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<PackageTags>$(PackageCommonTags);Jint</PackageTags>
2323
<PackageIconFullPath>../../Icons/JavaScriptEngineSwitcher_Jint_Logo128x128.png</PackageIconFullPath>
2424
<PackageReleaseNotes>1. Jint was updated to version 3.0.0 Beta 2048;
25-
2. In configuration settings of the Jint JS engine was added one new property - `AllowReflection` (default `false`).</PackageReleaseNotes>
25+
2. In configuration settings of the Jint JS engine was added two new properties: `AllowReflection` (default `false`) and `DisableEval` (default `false`).</PackageReleaseNotes>
2626
</PropertyGroup>
2727

2828
<ItemGroup>

src/JavaScriptEngineSwitcher.Jint/JintJsEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public JintJsEngine(JintSettings settings)
125125
.CancellationToken(_cancellationTokenSource.Token)
126126
.DebuggerStatementHandling(debuggerStatementHandlingMode)
127127
.DebugMode(jintSettings.EnableDebugging)
128+
.DisableStringCompilation(jintSettings.DisableEval)
128129
.LimitMemory(jintSettings.MemoryLimit)
129130
.LimitRecursion(jintSettings.MaxRecursionDepth)
130131
.LocalTimeZone(jintSettings.LocalTimeZone ?? TimeZoneInfo.Local)

src/JavaScriptEngineSwitcher.Jint/JintSettings.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ public OriginalDebuggerEventHandler DebuggerStepCallback
5959
set;
6060
}
6161

62+
/// <summary>
63+
/// Gets or sets a flag for whether to disable calls of <c>eval</c> function with custom code
64+
/// and <c>Function</c> constructors taking function code as string
65+
/// </summary>
66+
public bool DisableEval
67+
{
68+
get;
69+
set;
70+
}
71+
6272
/// <summary>
6373
/// Gets or sets a flag for whether to enable debug mode
6474
/// </summary>
@@ -166,6 +176,7 @@ public JintSettings()
166176
DebuggerBreakCallback = null;
167177
DebuggerStatementHandlingMode = JsDebuggerStatementHandlingMode.Ignore;
168178
DebuggerStepCallback = null;
179+
DisableEval = false;
169180
EnableDebugging = false;
170181
LocalTimeZone = TimeZoneInfo.Local;
171182
MaxArraySize = uint.MaxValue;

src/JavaScriptEngineSwitcher.Jint/readme.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
RELEASE NOTES
2020
=============
2121
1. Jint was updated to version 3.0.0 Beta 2048;
22-
2. In configuration settings of the Jint JS engine was added one new property -
23-
`AllowReflection` (default `false`).
22+
2. In configuration settings of the Jint JS engine was added two new properties:
23+
`AllowReflection` (default `false`) and `DisableEval` (default `false`).
2424

2525
=============
2626
DOCUMENTATION
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using Xunit;
2+
3+
using JavaScriptEngineSwitcher.ChakraCore;
4+
using JavaScriptEngineSwitcher.Core;
5+
6+
namespace JavaScriptEngineSwitcher.Tests.ChakraCore
7+
{
8+
public class EvalTests : EvalTestsBase
9+
{
10+
protected override string EngineName
11+
{
12+
get { return "ChakraCoreJsEngine"; }
13+
}
14+
15+
16+
private IJsEngine CreateJsEngine(bool disableEval)
17+
{
18+
var jsEngine = new ChakraCoreJsEngine(new ChakraCoreSettings
19+
{
20+
DisableEval = disableEval
21+
});
22+
23+
return jsEngine;
24+
}
25+
26+
27+
public override void UsageOfEvalFunction()
28+
{
29+
// Arrange
30+
int TestDisableEvalSetting(bool disableEval)
31+
{
32+
using (var jsEngine = CreateJsEngine(disableEval: disableEval))
33+
{
34+
return jsEngine.Evaluate<int>("eval('2*2');");
35+
}
36+
}
37+
38+
// Act and Assert
39+
Assert.Equal(4, TestDisableEvalSetting(false));
40+
41+
JsRuntimeException exception = Assert.Throws<JsRuntimeException>(() => TestDisableEvalSetting(true));
42+
Assert.Equal("Runtime error", exception.Category);
43+
Assert.Equal("Eval of strings is disabled in this runtime.", exception.Description);
44+
}
45+
46+
public override void UsageOfFunctionConstructor()
47+
{
48+
// Arrange
49+
int TestDisableEvalSetting(bool disableEval)
50+
{
51+
using (var jsEngine = CreateJsEngine(disableEval: disableEval))
52+
{
53+
return jsEngine.Evaluate<int>("new Function('return 2*2;')();");
54+
}
55+
}
56+
57+
// Act and Assert
58+
Assert.Equal(4, TestDisableEvalSetting(false));
59+
60+
JsRuntimeException exception = Assert.Throws<JsRuntimeException>(() => TestDisableEvalSetting(true));
61+
Assert.Equal("Runtime error", exception.Category);
62+
Assert.Equal("Eval of strings is disabled in this runtime.", exception.Description);
63+
}
64+
}
65+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Xunit;
2+
3+
using JavaScriptEngineSwitcher.Core;
4+
5+
namespace JavaScriptEngineSwitcher.Tests
6+
{
7+
public abstract class EvalTestsBase : TestsBase
8+
{
9+
[Fact]
10+
public virtual void UsageOfEvalFunction()
11+
{
12+
// Arrange
13+
const string input = "eval('2*2');";
14+
const int targetOutput = 4;
15+
16+
// Act
17+
int output;
18+
19+
using (var jsEngine = CreateJsEngine())
20+
{
21+
output = jsEngine.Evaluate<int>(input);
22+
}
23+
24+
// Assert
25+
Assert.Equal(targetOutput, output);
26+
}
27+
28+
[Fact]
29+
public virtual void UsageOfFunctionConstructor()
30+
{
31+
// Arrange
32+
const string input = "new Function('return 2*2;')();";
33+
const int targetOutput = 4;
34+
35+
// Act
36+
int output;
37+
38+
using (var jsEngine = CreateJsEngine())
39+
{
40+
output = jsEngine.Evaluate<int>(input);
41+
}
42+
43+
// Assert
44+
Assert.Equal(targetOutput, output);
45+
}
46+
}
47+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#if !NET452
2+
using Xunit;
3+
4+
using JavaScriptEngineSwitcher.Core;
5+
using JavaScriptEngineSwitcher.Jint;
6+
7+
namespace JavaScriptEngineSwitcher.Tests.Jint
8+
{
9+
public class EvalTests : EvalTestsBase
10+
{
11+
protected override string EngineName
12+
{
13+
get { return "JintJsEngine"; }
14+
}
15+
16+
17+
private IJsEngine CreateJsEngine(bool disableEval)
18+
{
19+
var jsEngine = new JintJsEngine(new JintSettings
20+
{
21+
DisableEval = disableEval
22+
});
23+
24+
return jsEngine;
25+
}
26+
27+
28+
public override void UsageOfEvalFunction()
29+
{
30+
// Arrange
31+
int TestDisableEvalSetting(bool disableEval)
32+
{
33+
using (var jsEngine = CreateJsEngine(disableEval: disableEval))
34+
{
35+
return jsEngine.Evaluate<int>("eval('2*2');");
36+
}
37+
}
38+
39+
// Act and Assert
40+
Assert.Equal(4, TestDisableEvalSetting(false));
41+
42+
JsRuntimeException exception = Assert.Throws<JsRuntimeException>(() => TestDisableEvalSetting(true));
43+
Assert.Equal("Runtime error", exception.Category);
44+
Assert.Equal("String compilation has been disabled in engine options", exception.Description);
45+
}
46+
47+
public override void UsageOfFunctionConstructor()
48+
{
49+
// Arrange
50+
int TestDisableEvalSetting(bool disableEval)
51+
{
52+
using (var jsEngine = CreateJsEngine(disableEval: disableEval))
53+
{
54+
return jsEngine.Evaluate<int>("new Function('return 2*2;')();");
55+
}
56+
}
57+
58+
// Act and Assert
59+
Assert.Equal(4, TestDisableEvalSetting(false));
60+
61+
JsRuntimeException exception = Assert.Throws<JsRuntimeException>(() => TestDisableEvalSetting(true));
62+
Assert.Equal("Runtime error", exception.Category);
63+
Assert.Equal("String compilation has been disabled in engine options", exception.Description);
64+
}
65+
}
66+
}
67+
#endif
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace JavaScriptEngineSwitcher.Tests.Jurassic
2+
{
3+
public class EvalTests : EvalTestsBase
4+
{
5+
protected override string EngineName
6+
{
7+
get { return "JurassicJsEngine"; }
8+
}
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace JavaScriptEngineSwitcher.Tests.Msie
2+
{
3+
public class EvalTests : EvalTestsBase
4+
{
5+
protected override string EngineName
6+
{
7+
get { return "MsieJsEngine"; }
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)