Repository: microsoft/garnet
File: libs/server/Module/ModuleUtils.cs (C#)

Code before:
                }
            }

            // Get all assemblies from binary files
            if (!FileUtils.TryLoadAssemblies(binaryFiles, out loadedAssemblies, out _))
            {
                errorMessage = CmdStrings.RESP_ERR_GENERIC_LOADING_ASSEMBLIES;
                return false;
            }

            // If necessary, check that all assemblies are digitally signed
            if (!allowUnsignedAssemblies)
            {
                foreach (var loadedAssembly in loadedAssemblies)
                {
                    var publicKey = loadedAssembly.GetName().GetPublicKey();
                    if (publicKey == null || publicKey.Length == 0)
                    {
                        errorMessage = CmdStrings.RESP_ERR_GENERIC_ASSEMBLY_NOT_SIGNED;
                        return false;
                    }
                }
            }

            return true;
        }
    }
}

Review comment (bug):
I wonder if there's any concerns about not unloading the assembly upon failure of signature verification? 🤔 

The loading of assembly can trigger some code to be executed too? Not sure what the threat model actually is here so this may not be problem, if the provided paths are considered to be trusted.

To be more specific, it isn't strictly specified when a module initializer[^1] is run and it could be run arbitarily[^2] before accessing any fields/methods in a module.

[^1]: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/module-initializers
[^2]: https://github.com/dotnet/runtime/blob/main/docs/design/specs/Ecma-335-Augments.md#module-initializer

Code after:
                    errorMessage = CmdStrings.RESP_ERR_GENERIC_BINARY_FILES_NOT_IN_ALLOWED_PATHS;
                    return false;
                }
            }

            // If necessary, check that all assemblies are digitally signed
            if (!allowUnsignedAssemblies)
            {
                foreach (var filePath in files)
                {
                    using var fs = File.OpenRead(filePath);
                    using var peReader = new PEReader(fs);

                    var metadataReader = peReader.GetMetadataReader();
                    var assemblyPublicKeyHandle = metadataReader.GetAssemblyDefinition().PublicKey;

                    if (assemblyPublicKeyHandle.IsNil)
                    {
                        errorMessage = CmdStrings.RESP_ERR_GENERIC_ASSEMBLY_NOT_SIGNED;
                        return false;
                    }

                    var publicKeyBytes = metadataReader.GetBlobBytes(assemblyPublicKeyHandle);
                    if (publicKeyBytes == null || publicKeyBytes.Length == 0)
                    {
                        errorMessage = CmdStrings.RESP_ERR_GENERIC_ASSEMBLY_NOT_SIGNED;
                        return false;
                    }
                }
            }

            // Get all assemblies from binary files
            if (!FileUtils.TryLoadAssemblies(binaryFiles, out loadedAssemblies, out _))
            {
                errorMessage = CmdStrings.RESP_ERR_GENERIC_LOADING_ASSEMBLIES;
                return false;
            }

            return true;
        }
    }
}