Skip to content

Commit 82578b1

Browse files
committed
more remotecall security
1 parent 6bdba83 commit 82578b1

File tree

13 files changed

+4692
-93
lines changed

13 files changed

+4692
-93
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,6 @@ _TeamCity.OCTGN
8080
/installer/*.exe
8181
/.HistoryData
8282
/packages
83-
/octgnFX/Octgn.Desktop/Packages/octgn.packages.jodsengine/engine
83+
/octgnFX/Octgn.Desktop/Packages/octgn.packages.joodsengine/engine
84+
*TestResults.xml
85+
*TestResult.xml

octgnFX/Octgn.JodsEngine/Octgn.JodsEngine_qm2tucxo_wpftmp.csproj

Lines changed: 808 additions & 0 deletions
Large diffs are not rendered by default.

octgnFX/Octgn.JodsEngine/Scripting/Engine.cs

Lines changed: 735 additions & 90 deletions
Large diffs are not rendered by default.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
3+
namespace Octgn.Scripting
4+
{
5+
/// <summary>
6+
/// Interface for the scripting engine to enable testing
7+
/// </summary>
8+
public interface IScriptingEngine : IDisposable
9+
{
10+
/// <summary>
11+
/// Executes a function securely with validation
12+
/// </summary>
13+
void ExecuteFunctionSecureNoFormat(string function, string args);
14+
15+
/// <summary>
16+
/// Executes a function without security validation
17+
/// </summary>
18+
void ExecuteFunctionNoFormat(string function, string args);
19+
}
20+
21+
/// <summary>
22+
/// Interface for function existence validation - allows mocking in tests
23+
/// </summary>
24+
public interface IFunctionValidator
25+
{
26+
/// <summary>
27+
/// Checks if a function exists and is callable in the current scope
28+
/// </summary>
29+
bool IsFunctionAvailable(string functionName, out object functionObject);
30+
}
31+
32+
/// <summary>
33+
/// Interface for code execution - allows mocking in tests
34+
/// </summary>
35+
public interface ICodeExecutor
36+
{
37+
/// <summary>
38+
/// Executes the function with the given arguments
39+
/// </summary>
40+
void ExecuteFunction(string function, string args);
41+
}
42+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using Microsoft.Scripting.Hosting;
3+
4+
namespace Octgn.JodsEngine.Scripting
5+
{
6+
/// <summary>
7+
/// Default implementation of IFunctionValidator that checks actual scope
8+
/// </summary>
9+
public class DefaultFunctionValidator : IFunctionValidator
10+
{
11+
private readonly ScriptScope _scope;
12+
13+
public DefaultFunctionValidator(ScriptScope scope)
14+
{
15+
_scope = scope ?? throw new ArgumentNullException(nameof(scope));
16+
}
17+
18+
public bool IsFunctionAvailable(string functionName)
19+
{
20+
if (string.IsNullOrEmpty(functionName))
21+
return false;
22+
23+
return _scope.TryGetVariable(functionName, out var functionObject) && functionObject != null;
24+
}
25+
}
26+
27+
/// <summary>
28+
/// Default implementation of ICodeExecutor that executes actual code
29+
/// </summary>
30+
public class DefaultCodeExecutor : ICodeExecutor
31+
{
32+
private readonly Action<string, string> _executeFunction;
33+
34+
public DefaultCodeExecutor(Action<string, string> executeFunction)
35+
{
36+
_executeFunction = executeFunction ?? throw new ArgumentNullException(nameof(executeFunction));
37+
}
38+
39+
public void ExecuteFunction(string functionName, string arguments)
40+
{
41+
_executeFunction(functionName, arguments);
42+
}
43+
}
44+
45+
/// <summary>
46+
/// Mock implementation of IFunctionValidator for testing
47+
/// </summary>
48+
public class MockFunctionValidator : IFunctionValidator
49+
{
50+
private readonly Func<string, bool> _isAvailableFunc;
51+
52+
public MockFunctionValidator(Func<string, bool> isAvailableFunc = null)
53+
{
54+
_isAvailableFunc = isAvailableFunc ?? (_ => true); // Default to allowing all functions
55+
}
56+
57+
public bool IsFunctionAvailable(string functionName)
58+
{
59+
return _isAvailableFunc(functionName);
60+
}
61+
}
62+
63+
/// <summary>
64+
/// Mock implementation of ICodeExecutor for testing
65+
/// </summary>
66+
public class MockCodeExecutor : ICodeExecutor
67+
{
68+
private readonly Action<string, string> _executeAction;
69+
70+
public MockCodeExecutor(Action<string, string> executeAction = null)
71+
{
72+
_executeAction = executeAction ?? ((f, a) => { }); // Default to no-op
73+
}
74+
75+
public void ExecuteFunction(string functionName, string arguments)
76+
{
77+
_executeAction(functionName, arguments);
78+
}
79+
}
80+
}

octgnFX/Octgn.Library/Octgn.Library.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<Compile Include="Networking\SocketBase.cs" />
108108
<Compile Include="Networking\SocketMessageProcessorBase.cs" />
109109
<Compile Include="Networking\SocketReceiveBundle.cs" />
110+
<Compile Include="Scripting\ScriptingInterfaces.cs" />
110111
<Compile Include="SavedPasswordManager.cs" />
111112
<Compile Include="Paths.cs" />
112113
<Compile Include="Properties\AssemblyInfo.cs" />
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
3+
namespace Octgn.Library.Scripting
4+
{
5+
/// <summary>
6+
/// Interface for the scripting engine to support dependency injection
7+
/// </summary>
8+
public interface IScriptingEngine : IDisposable
9+
{
10+
void ExecuteFunctionSecureNoFormat(string function, string args);
11+
}
12+
13+
/// <summary>
14+
/// Interface for validating function availability
15+
/// </summary>
16+
public interface IFunctionValidator
17+
{
18+
bool IsFunctionAvailable(string functionName);
19+
}
20+
21+
/// <summary>
22+
/// Interface for executing code
23+
/// </summary>
24+
public interface ICodeExecutor
25+
{
26+
void ExecuteFunction(string functionName, string arguments);
27+
}
28+
29+
/// <summary>
30+
/// Default implementation of IFunctionValidator that checks actual scope
31+
/// </summary>
32+
public class DefaultFunctionValidator : IFunctionValidator
33+
{
34+
private readonly Func<string, bool> _checkFunction;
35+
36+
public DefaultFunctionValidator(Func<string, bool> checkFunction)
37+
{
38+
_checkFunction = checkFunction ?? throw new ArgumentNullException(nameof(checkFunction));
39+
}
40+
41+
public bool IsFunctionAvailable(string functionName)
42+
{
43+
if (string.IsNullOrEmpty(functionName))
44+
return false;
45+
46+
return _checkFunction(functionName);
47+
}
48+
}
49+
50+
/// <summary>
51+
/// Default implementation of ICodeExecutor that executes actual code
52+
/// </summary>
53+
public class DefaultCodeExecutor : ICodeExecutor
54+
{
55+
private readonly Action<string, string> _executeFunction;
56+
57+
public DefaultCodeExecutor(Action<string, string> executeFunction)
58+
{
59+
_executeFunction = executeFunction ?? throw new ArgumentNullException(nameof(executeFunction));
60+
}
61+
62+
public void ExecuteFunction(string functionName, string arguments)
63+
{
64+
_executeFunction(functionName, arguments);
65+
}
66+
}
67+
68+
/// <summary>
69+
/// Mock implementation of IFunctionValidator for testing
70+
/// </summary>
71+
public class MockFunctionValidator : IFunctionValidator
72+
{
73+
private readonly Func<string, bool> _isAvailableFunc;
74+
75+
public MockFunctionValidator(Func<string, bool> isAvailableFunc = null)
76+
{
77+
_isAvailableFunc = isAvailableFunc ?? (_ => true); // Default to allowing all functions
78+
}
79+
80+
public bool IsFunctionAvailable(string functionName)
81+
{
82+
return _isAvailableFunc(functionName);
83+
}
84+
}
85+
86+
/// <summary>
87+
/// Mock implementation of ICodeExecutor for testing
88+
/// </summary>
89+
public class MockCodeExecutor : ICodeExecutor
90+
{
91+
private readonly Action<string, string> _executeAction;
92+
93+
public MockCodeExecutor(Action<string, string> executeAction = null)
94+
{
95+
_executeAction = executeAction ?? ((f, a) => { }); // Default to no-op
96+
}
97+
98+
public void ExecuteFunction(string functionName, string arguments)
99+
{
100+
_executeAction(functionName, arguments);
101+
}
102+
}
103+
}

octgnFX/Octgn.Test/App.config

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@
262262
<assemblyIdentity name="Castle.Core" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
263263
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
264264
</dependentAssembly>
265+
<dependentAssembly>
266+
<assemblyIdentity name="Microsoft.Scripting" publicKeyToken="7f709c5b713576e1" culture="neutral" />
267+
<bindingRedirect oldVersion="0.0.0.0-1.3.3.0" newVersion="1.3.3.0" />
268+
</dependentAssembly>
269+
<dependentAssembly>
270+
<assemblyIdentity name="Microsoft.Dynamic" publicKeyToken="7f709c5b713576e1" culture="neutral" />
271+
<bindingRedirect oldVersion="0.0.0.0-1.3.3.0" newVersion="1.3.3.0" />
272+
</dependentAssembly>
265273
</assemblyBinding>
266274
</runtime>
267275
</configuration>

octgnFX/Octgn.Test/Octgn.Test.csproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@
115115
<Reference Include="nunit.framework, Version=3.13.3.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
116116
<HintPath>..\..\packages\NUnit.3.13.3\lib\net45\nunit.framework.dll</HintPath>
117117
</Reference>
118+
<Reference Include="NUnit3.TestAdapter, Version=5.0.0.0, Culture=neutral, PublicKeyToken=4cb40d35494691ac, processorArchitecture=MSIL">
119+
<HintPath>..\..\packages\NUnit3TestAdapter.5.0.0\lib\net462\NUnit3.TestAdapter.dll</HintPath>
120+
<Private>False</Private>
121+
</Reference>
118122
<Reference Include="System" />
119123
<Reference Include="System.Configuration" />
120124
<Reference Include="System.Net" />
@@ -130,11 +134,12 @@
130134
<Compile Include="Library\Networking\GameBroadcastingTests.cs" />
131135
<Compile Include="OctgnApp\Play\State\GameSaveTests.cs" />
132136
<Compile Include="OctgnApp\Play\State\StateSaveTests.cs" />
137+
<Compile Include="OctgnApp\Scripting\RemoteCallValidUseCasesTests.cs" />
138+
<Compile Include="OctgnApp\Scripting\SecurityTests.cs" />
133139
<Compile Include="OctgnApp\Scripting\Versioning.cs" />
134140
<Compile Include="PlayGround.cs" />
135141
<Compile Include="Server\GameStatusResetTests.cs" />
136142
<Compile Include="Server\ServerPortReuseTests.cs" />
137-
138143
<Compile Include="Utils\HttpEcho.cs" />
139144
<Compile Include="VersionTest.cs" />
140145
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -168,6 +173,10 @@
168173
<Project>{6dd203af-5003-4af3-a982-67f10b704d4a}</Project>
169174
<n>Octgn.Server</n>
170175
</ProjectReference>
176+
<ProjectReference Include="..\Octgn.JodsEngine\Octgn.JodsEngine.csproj">
177+
<Project>{74009662-AA58-4A94-97B6-1AD019ECA302}</Project>
178+
<n>Octgn.JodsEngine</n>
179+
</ProjectReference>
171180
</ItemGroup>
172181
<ItemGroup>
173182
<None Include="App.config" />

0 commit comments

Comments
 (0)