Skip to content

Reject untrusted Lua bytecode while preserving script caching - #2138

Open
Tiago Nápoli (tiagonapoli) wants to merge 24 commits into
microsoft:mainfrom
tiagonapoli:tiagonapoli/fix-lua-bytecode
Open

Tiago Nápoli (tiagonapoli) wants to merge 24 commits into
microsoft:mainfrom
tiagonapoli:tiagonapoli/fix-lua-bytecode

Conversation

@tiagonapoli

Copy link
Copy Markdown
Collaborator

Summary

  • reject customer-supplied Lua binary chunks for EVAL and SCRIPT LOAD
  • compile customer input using Lua text-only mode before generating cacheable bytecode
  • preserve Garnet's application bytecode cache and binary-only session loading performance
  • tag script data as source text or Garnet-generated bytecode so only internally generated chunks reach the binary loader
  • remove the fallback that returned original customer bytes when compilation failed
  • use exact-length text loading for internal loadstring compatibility handling

Security boundary

Customer input is always treated as source text and compiled with mode t. Only successful internal compilation produces a GarnetGeneratedBinary chunk, which is cached and subsequently loaded with mode b. The public LuaScriptHandle constructor creates source-text handles, so arbitrary host-provided bytes cannot opt into binary loading.

Testing

  • full Lua scripting suite on .NET 8: 638 passed, 30 skipped, 0 failed
  • full Lua scripting suite on .NET 10: 638 passed, 30 skipped, 0 failed
  • full Debug solution build for net8.0 and net10.0: 0 warnings, 0 errors
  • regression coverage for binary-safe EVAL, SCRIPT LOAD, cache exclusion, blocked side effects, compilation failure output, embedded NUL input, host-inserted source, and cross-session caching

Supersedes #2111 while retaining bytecode caching.

Tiago Napoli added 9 commits September 15, 2026 14:34
Keep application bytecode caching while requiring customer input to compile in text-only mode and tagging internally generated chunks for binary-only loading.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Generate the test bytecode directly through Garnet's internal compiler instead of round-tripping through a Lua script.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Keep regression coverage focused on supported network-facing scripting workflows.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Remove the unrelated ScriptHashKey allocation refactor from the Lua bytecode fix.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
@tiagonapoli
Tiago Nápoli (tiagonapoli) marked this pull request as ready for review September 16, 2026 17:21
Copilot AI balanced review requested due to automatic review settings September 16, 2026 17:21
Replace the implicit source-data constructor with a named FromSource factory.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The claimed host-inserted source-cache behavior lacks automated regression coverage.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Hardens Lua scripting by treating customer input as text while retaining Garnet-generated bytecode caching.

Changes:

  • Adds explicit text and trusted-binary chunk types and loading modes.
  • Compiles source safely before caching internal bytecode.
  • Adds bytecode rejection and exact-length parsing tests.
File summaries
File Description
LuaScriptTests.cs Adds scripting security regressions.
LuaScriptRunnerTests.cs Tests compilation and chunk tagging.
SessionScriptCache.cs Separates source compilation from cached loading.
NativeMethods.cs Adds text-only and binary-only native loaders.
LuaStateWrapper.cs Exposes mode-specific loading wrappers.
LuaScriptHandle.cs Tags cached script data by origin.
LuaRunner.Loader.cs Safely compiles source into bytecode.
LuaRunner.Functions.cs Propagates chunk loading mode.
LuaRunner.cs Distinguishes public source from internal bytecode.
LuaCommands.cs Routes commands through secure loading paths.
LuaScriptCacheOperations.cs Updates cache benchmarks for the new APIs.
Review details
  • Files reviewed: 11/11 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread libs/server/Lua/SessionScriptCache.cs Outdated
Comment on lines +210 to +213
if (luaScriptHandle.Chunk.Kind == LuaScriptChunkKind.GarnetGeneratedBinary)
return TryLoadCompiled(session, luaScriptHandle.Chunk, digest, ref luaScriptHandle, out runner, out digestOnHeap);

return TryCompileAndLoad(session, luaScriptHandle.ScriptData.Span, digest, ref luaScriptHandle, out runner, out digestOnHeap);
Tiago Napoli added 13 commits September 16, 2026 10:37
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ec2a6abf-4bb4-48e1-a752-32672cb07303
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants