Skip to content

Commit

Permalink
Added more input sanitization and validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed May 28, 2024
1 parent db8c2a4 commit 7f59d7b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ public static unsafe partial class Bindings
if (p_handle is null || LauncherManagerHandlerNative.FromPointer(p_handle) is not { } handler)
return return_value_json.AsError(BUTR.NativeAOT.Shared.Utils.Copy("Handler is null or wrong!", false), false);

var files = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_files, CustomSourceGenerationContext.StringArray);
var moduleInfos = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_module_infos, CustomSourceGenerationContext.ModuleInfoExtendedWithMetadataArray);
var files = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_files, CustomSourceGenerationContext.StringArray)
.Where(x => x is not null).ToArray();;
var moduleInfos = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_module_infos, CustomSourceGenerationContext.ModuleInfoExtendedWithMetadataArray)
.Where(x => x is not null).ToArray();;

var result = handler.InstallModuleContent(files, moduleInfos);
Logger.LogOutput(result);
Expand Down
33 changes: 21 additions & 12 deletions src/Bannerlord.LauncherManager.Native/Bindings.ModuleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public static unsafe partial class Bindings
Logger.LogInput(p_source);
try
{
var source = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_source, CustomSourceGenerationContext.ModuleInfoExtendedArray);
var source = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_source, CustomSourceGenerationContext.ModuleInfoExtendedArray)
.Where(x => x is not null).ToArray();;

var result = ModuleSorter.Sort(source).ToArray();

Expand All @@ -52,7 +53,8 @@ public static unsafe partial class Bindings
Logger.LogInput(p_source, p_options);
try
{
var source = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_source, CustomSourceGenerationContext.ModuleInfoExtendedArray);
var source = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_source, CustomSourceGenerationContext.ModuleInfoExtendedArray)
.Where(x => x is not null).ToArray();;
var options = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_options, CustomSourceGenerationContext.ModuleSorterOptions);

var result = ModuleSorter.Sort(source, options).ToArray();
Expand All @@ -74,7 +76,8 @@ public static unsafe partial class Bindings
Logger.LogInput(p_source, p_module);
try
{
var source = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_source, CustomSourceGenerationContext.ModuleInfoExtendedArray);
var source = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_source, CustomSourceGenerationContext.ModuleInfoExtendedArray)
.Where(x => x is not null).ToArray();;
var module = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_module, CustomSourceGenerationContext.ModuleInfoExtended);

var result = ModuleUtilities.AreDependenciesPresent(source, module);
Expand All @@ -96,7 +99,8 @@ public static unsafe partial class Bindings
Logger.LogInput(p_source, p_module);
try
{
var source = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_source, CustomSourceGenerationContext.ModuleInfoExtendedArray);
var source = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_source, CustomSourceGenerationContext.ModuleInfoExtendedArray)
.Where(x => x is not null).ToArray();;
var module = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_module, CustomSourceGenerationContext.ModuleInfoExtended);

var result = ModuleUtilities.GetDependencies(source, module).ToArray();
Expand All @@ -117,7 +121,8 @@ public static unsafe partial class Bindings
Logger.LogInput(p_source, p_module, p_options);
try
{
var source = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_source, CustomSourceGenerationContext.ModuleInfoExtendedArray);
var source = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_source, CustomSourceGenerationContext.ModuleInfoExtendedArray)
.Where(x => x is not null).ToArray();;
var module = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_module, CustomSourceGenerationContext.ModuleInfoExtended);
var options = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_options, CustomSourceGenerationContext.ModuleSorterOptions);

Expand All @@ -142,7 +147,8 @@ public static unsafe partial class Bindings
Logger.LogInput(p_modules, p_target_module);
try
{
var modules = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_modules, CustomSourceGenerationContext.ModuleInfoExtendedArray);
var modules = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_modules, CustomSourceGenerationContext.ModuleInfoExtendedArray)
.Where(x => x is not null).ToArray();
var targetModule = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_target_module, CustomSourceGenerationContext.ModuleInfoExtended);

var isSelected = Marshal.GetDelegateForFunctionPointer<N_IsSelected>(new IntPtr(p_is_selected));
Expand Down Expand Up @@ -177,7 +183,8 @@ public static unsafe partial class Bindings
Logger.LogInput(p_modules, p_target_module);
try
{
var modules = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_modules, CustomSourceGenerationContext.ModuleInfoExtendedArray);
var modules = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_modules, CustomSourceGenerationContext.ModuleInfoExtendedArray)
.Where(x => x is not null).ToArray();;
var targetModule = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_target_module, CustomSourceGenerationContext.ModuleInfoExtended);

var result = ModuleUtilities.ValidateLoadOrder(modules, targetModule).ToArray();
Expand Down Expand Up @@ -207,7 +214,8 @@ public static unsafe partial class Bindings
Logger.LogInput(p_module, p_target_module);
try
{
var modules = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_module, CustomSourceGenerationContext.ModuleInfoExtendedArray);
var modules = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_module, CustomSourceGenerationContext.ModuleInfoExtendedArray)
.Where(x => x is not null).ToArray();;
var targetModule = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_target_module, CustomSourceGenerationContext.ModuleInfoExtended);

var getSelected = Marshal.GetDelegateForFunctionPointer<N_GetSelected>(new IntPtr(p_get_selected));
Expand Down Expand Up @@ -287,7 +295,8 @@ public static unsafe partial class Bindings
Logger.LogInput(p_module, p_target_module);
try
{
var modules = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_module, CustomSourceGenerationContext.ModuleInfoExtendedArray);
var modules = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_module, CustomSourceGenerationContext.ModuleInfoExtendedArray)
.Where(x => x is not null).ToArray();;
var targetModule = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_target_module, CustomSourceGenerationContext.ModuleInfoExtended);

var getSelected = Marshal.GetDelegateForFunctionPointer<N_GetSelected>(new IntPtr(p_get_selected));
Expand Down Expand Up @@ -388,7 +397,7 @@ public static unsafe partial class Bindings
var result = new ModuleInfoExtendedWithPath(module, new string(param_string.ToSpan(p_path)));

Logger.LogOutput(result);
return return_value_json.AsValue(result, CustomSourceGenerationContext.ModuleInfoExtended, false);
return return_value_json.AsValue(result, CustomSourceGenerationContext.ModuleInfoExtendedWithPath, false);
}
catch (Exception e)
{
Expand All @@ -406,10 +415,10 @@ public static unsafe partial class Bindings
doc.LoadXml(new string(param_string.ToSpan(p_xml_content)));

var module = ModuleInfoExtended.FromXml(doc);
var result = new ModuleInfoExtendedWithMetadata(module, Enum.TryParse<ModuleProviderType>(new string(param_string.ToSpan(p_path)), out var e) ? e : ModuleProviderType.Default, new string(param_string.ToSpan(p_path)));
var result = new ModuleInfoExtendedWithMetadata(module, Enum.TryParse<ModuleProviderType>(new string(param_string.ToSpan(p_module_provider)), out var e) ? e : ModuleProviderType.Default, new string(param_string.ToSpan(p_path)));

Logger.LogOutput(result);
return return_value_json.AsValue(result, CustomSourceGenerationContext.ModuleInfoExtended, false);
return return_value_json.AsValue(result, CustomSourceGenerationContext.ModuleInfoExtendedWithMetadata, false);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bannerlord.ModuleManager" Version="5.0.221" />
<PackageReference Include="Bannerlord.ModuleManager" Version="5.0.222" />
<PackageReference Include="PolySharp" Version="1.13.2" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageReference Condition="$(TargetFramework) == 'netstandard2.0'" Include="System.Memory" Version="4.5.0" />
</ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/Bannerlord.LauncherManager/Utils/ModuleIssueRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ private static string Version(ApplicationVersionRange version) => version == App

ModuleIssueType.DependencyNotLoadedAfterThis => new BUTRTextObject("{=2ALJB7z2}'{SOURCEID}' should be loaded after '{TARGETID}'")
.SetTextVariable("ID", issue.SourceId),

// TODO:
// Add new overloads

_ => throw new ArgumentOutOfRangeException()
};
Expand Down

0 comments on commit 7f59d7b

Please sign in to comment.