Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e1a3ff5
punch an allow list in for Lua functions
kevin-montrose Mar 4, 2025
6850cca
implement lua function allow listing
kevin-montrose Mar 4, 2025
63907a8
test to make sure we're exporting exactly what Redis does by default,…
kevin-montrose Mar 4, 2025
594104b
implement cjson.encode and decode
kevin-montrose Mar 5, 2025
26f3a67
implement bit.xxx operations in Lua scripts; the rules around these a…
kevin-montrose Mar 5, 2025
ff6e6d8
fix typo in AllowedFunctions
kevin-montrose Mar 5, 2025
0bff569
implement message pack support (cmsgpack.pack and cmsgpack.unpack)
kevin-montrose Mar 6, 2025
b623e67
implement struct.pack/unpack/size; these are provided by Lua and are …
kevin-montrose Mar 7, 2025
2616937
allow redis functions to be allowed/denied rather than unconditionall…
kevin-montrose Mar 7, 2025
1db1e76
fix formatting
kevin-montrose Mar 7, 2025
fc6b596
Move intermediate binary blobs onto ScratchBufferManager
kevin-montrose Mar 7, 2025
a329de3
Merge branch 'main' into luaRuntimeLibraries
kevin-montrose Mar 7, 2025
2b47c4b
formatting
kevin-montrose Mar 10, 2025
712fc1c
Merge branch 'main' into luaRuntimeLibraries
kevin-montrose Mar 10, 2025
7f89a92
strange this doesn't error locally; removing preview use of params Re…
kevin-montrose Mar 10, 2025
5cfe179
test for #1079, fails in main and passes here
kevin-montrose Mar 10, 2025
383942b
tests (and fixes) for math.frexp and math.ldexp
kevin-montrose Mar 10, 2025
078c100
tests for table.maxn and loadstring
kevin-montrose Mar 10, 2025
f02ae24
prevent LuaAllowedFunctions from importing functions not on the defau…
kevin-montrose Mar 10, 2025
5f39d75
Merge branch 'main' into luaRuntimeLibraries
kevin-montrose Mar 10, 2025
7a56e0e
enforce maximum depth for cjson functions
kevin-montrose Mar 10, 2025
e21eba2
enforce maximum encoding depth for cmsgpack.pack
kevin-montrose Mar 10, 2025
11b8623
hey, a reliable-ish repro for the linux longjmp issue; disable these …
kevin-montrose Mar 10, 2025
c030abe
condition more exception tests on 'we're on Windows' until the refact…
kevin-montrose Mar 10, 2025
0bbfa52
Merge branch 'main' into luaRuntimeLibraries
kevin-montrose Mar 11, 2025
f4e4e32
Merge branch 'main' into luaRuntimeLibraries
kevin-montrose Mar 11, 2025
170e7a4
Merge branch 'main' into luaRuntimeLibraries
kevin-montrose Mar 12, 2025
6c7665e
knock out a missing TODO; remove List<byte> and use scratch buffer in…
kevin-montrose Mar 12, 2025
f01d5cf
Merge branch 'main' into luaRuntimeLibraries
kevin-montrose Mar 12, 2025
63a4074
Update test/Garnet.test/LuaScriptRunnerTests.cs
kevin-montrose Mar 12, 2025
90ad7d9
Merge branch 'main' into luaRuntimeLibraries
kevin-montrose Mar 13, 2025
217b98e
add more conditions to skip exceptions in .NET 9 or non-Windows build…
kevin-montrose Mar 13, 2025
d72162d
Merge branch 'main' into luaRuntimeLibraries
kevin-montrose Mar 13, 2025
39d0987
Merge branch 'main' into luaRuntimeLibraries
kevin-montrose Mar 13, 2025
81fa07b
Merge branch 'main' into luaRuntimeLibraries
kevin-montrose Mar 14, 2025
b892db8
Merge branch 'main' into luaRuntimeLibraries
kevin-montrose Mar 14, 2025
0303f5d
Merge branch 'main' into luaRuntimeLibraries
kevin-montrose Mar 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/BDN.benchmark/Lua/LuaParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public LuaParams(LuaMemoryManagementMode mode, bool memoryLimit, TimeSpan? timeo
/// Get the equivalent <see cref="LuaOptions"/>.
/// </summary>
public LuaOptions CreateOptions()
=> new(Mode, MemoryLimit ? "2m" : "", Timeout ?? System.Threading.Timeout.InfiniteTimeSpan, LuaLoggingMode.Enable);
=> new(Mode, MemoryLimit ? "2m" : "", Timeout ?? System.Threading.Timeout.InfiniteTimeSpan, LuaLoggingMode.Enable, []);

/// <summary>
/// String representation
Expand Down
2 changes: 1 addition & 1 deletion benchmark/BDN.benchmark/Operations/OperationsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public virtual void GlobalSetup()
QuietMode = true,
EnableLua = true,
DisablePubSub = true,
LuaOptions = new(LuaMemoryManagementMode.Native, "", Timeout.InfiniteTimeSpan, LuaLoggingMode.Enable),
LuaOptions = new(LuaMemoryManagementMode.Native, "", Timeout.InfiniteTimeSpan, LuaLoggingMode.Enable, []),
};

if (Params.useAof)
Expand Down
23 changes: 22 additions & 1 deletion libs/host/Configuration/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,27 @@ internal sealed class Options
[Option("lua-logging-mode", Required = false, HelpText = "Behavior of redis.log(...) when called from Lua scripts. Defaults to Enable.")]
public LuaLoggingMode LuaLoggingMode { get; set; }

// Parsing is a tad tricky here as JSON wants to set to empty at certain points
//
// A bespoke union-on-set gets the desired semantics.
private readonly HashSet<string> luaAllowedFunctions = [];

[OptionValidation]
[Option("lua-allowed-functions", Separator = ',', Required = false, HelpText = "If set, restricts the functions available in Lua scripts to given list.")]
public IEnumerable<string> LuaAllowedFunctions
{
get => luaAllowedFunctions;
set
{
if (value == null)
{
return;
}

luaAllowedFunctions.UnionWith(value);
}
}

[FilePathValidation(false, true, false)]
[Option("unixsocket", Required = false, HelpText = "Unix socket address path to bind server to")]
public string UnixSocketPath { get; set; }
Expand Down Expand Up @@ -811,7 +832,7 @@ public GarnetServerOptions GetServerOptions(ILogger logger = null)
LoadModuleCS = LoadModuleCS,
FailOnRecoveryError = FailOnRecoveryError.GetValueOrDefault(),
SkipRDBRestoreChecksumValidation = SkipRDBRestoreChecksumValidation.GetValueOrDefault(),
LuaOptions = EnableLua.GetValueOrDefault() ? new LuaOptions(LuaMemoryManagementMode, LuaScriptMemoryLimit, LuaScriptTimeoutMs == 0 ? Timeout.InfiniteTimeSpan : TimeSpan.FromMilliseconds(LuaScriptTimeoutMs), LuaLoggingMode, logger) : null,
LuaOptions = EnableLua.GetValueOrDefault() ? new LuaOptions(LuaMemoryManagementMode, LuaScriptMemoryLimit, LuaScriptTimeoutMs == 0 ? Timeout.InfiniteTimeSpan : TimeSpan.FromMilliseconds(LuaScriptTimeoutMs), LuaLoggingMode, LuaAllowedFunctions, logger) : null,
UnixSocketPath = UnixSocketPath,
UnixSocketPermission = unixSocketPermissions
};
Expand Down
3 changes: 3 additions & 0 deletions libs/host/defaults.conf
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,9 @@
/* Allow redis.log(...) to write to the Garnet logs */
"LuaLoggingMode": "Enable",

/* Allow all built in and redis.* functions by default */
"LuaAllowedFunctions": null,

/* Unix socket address path to bind the server to */
"UnixSocketPath": null,

Expand Down
21 changes: 21 additions & 0 deletions libs/server/ArgSlice/ScratchBufferManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ public ArgSlice CreateArgSlice(string str)
return retVal;
}

public ReadOnlySpan<byte> UTF8EncodeString(string str)
{
// We'll always need AT LEAST this many bytes
ExpandScratchBufferIfNeeded(str.Length);

var space = FullBuffer()[scratchBufferOffset..];

// Attempt to fit in the existing buffer first
if (!Encoding.UTF8.TryGetBytes(str, space, out var written))
{
// If that fails, figure out exactly how much space we need
var neededBytes = Encoding.UTF8.GetByteCount(str);
ExpandScratchBufferIfNeeded(neededBytes);

space = FullBuffer()[scratchBufferOffset..];
written = Encoding.UTF8.GetBytes(str, space);
}

return space[..written];
}

/// <summary>
/// Create an ArgSlice that includes a header of specified size, followed by RESP Bulk-String formatted versions of the specified ArgSlice values (arg1 and arg2)
/// </summary>
Expand Down
5 changes: 4 additions & 1 deletion libs/server/Lua/LuaOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging;

Expand All @@ -18,6 +19,7 @@ public sealed class LuaOptions
public string MemoryLimit = "";
public TimeSpan Timeout = System.Threading.Timeout.InfiniteTimeSpan;
public LuaLoggingMode LogMode = LuaLoggingMode.Silent;
public HashSet<string> AllowedFunctions = [];

/// <summary>
/// Construct options with default options.
Expand All @@ -30,12 +32,13 @@ public LuaOptions(ILogger logger = null)
/// <summary>
/// Construct options with specific settings.
/// </summary>
public LuaOptions(LuaMemoryManagementMode memoryMode, string memoryLimit, TimeSpan timeout, LuaLoggingMode logMode, ILogger logger = null) : this(logger)
public LuaOptions(LuaMemoryManagementMode memoryMode, string memoryLimit, TimeSpan timeout, LuaLoggingMode logMode, IEnumerable<string> allowedFunctions, ILogger logger = null) : this(logger)
{
MemoryManagementMode = memoryMode;
MemoryLimit = memoryLimit;
Timeout = timeout;
LogMode = logMode;
AllowedFunctions = new HashSet<string>(allowedFunctions, StringComparer.OrdinalIgnoreCase);
}

/// <summary>
Expand Down
Loading