1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2026-10-29 13:42:30 +00:00

feat(luahost): Lua-controlled settings and decoded header access (#630)

* feat(luahost): capture config settings set by Lua scripts

Add inbucket.config, a Lua-settable settings namespace, with its first
field decode_headers registering additional message headers for Inbucket
to decode.  Values are captured once at startup from the first pooled
LState and exposed to the assembly via Host.ScriptConfig().

Settings that only have value to the Lua script belong here rather than
in env config.  No consumers of decode_headers yet; that follows.

* feat(message): expose decoded headers to extensions

StoreManager gains ExtraDecodeHeaders, passed through to
enmime.DecodeHeaders, and the decoded header set (defaults plus any
registered extras) is attached to the InboundMessage event as
Headers (net/textproto.MIMEHeader).

Lua scripts access it via the read-only inbound_message.header field,
added with the new inbound_header binding.  Lookups are exact-then-
case-insensitive, since enmime canonicalizes keys with email-specific
rules that differ from net/textproto.

FullAssembly wires ScriptConfig().DecodeHeaders into StoreManager.

Part of #627.

* docs(extension): document shared and untrusted header semantics

Reviewer note: BeforeMessageStored listeners receive the same decoded
header map, so a Go listener's mutations are visible to later listeners.
Also record that sender-supplied header values (Authentication-Results,
X-Spam-Status) are untrusted unless the deployment controls provenance.

Part of #627.

---------

Co-authored-by: hillyerd-bot <hillyerd-bot@users.noreply.github.com>
This commit is contained in:
James Hillyerd
2026-10-23 15:38:37 -07:00
committed by GitHub
parent 59ecd449b6
commit bd4cc4b842
15 changed files with 498 additions and 5 deletions
@@ -2,6 +2,7 @@ package luahost
import (
"net/mail"
"net/textproto"
"testing"
"github.com/inbucket/inbucket/v3/pkg/extension/event"
@@ -20,6 +21,10 @@ func TestInboundMessageGetters(t *testing.T) {
},
Subject: "subj1",
Size: 42,
Headers: textproto.MIMEHeader{
"Subject": {"subj1"},
"X-Spam-Status": {"Yes"},
},
}
script := `
assert(msg, "msg should not be nil")
@@ -28,6 +33,10 @@ func TestInboundMessageGetters(t *testing.T) {
assert_eq(msg.subject, "subj1")
assert_eq(msg.size, 42, "msg.size")
assert_eq(msg.header.subject, "subj1")
assert_eq(msg.header["x-spam-status"], "Yes")
assert(msg.header["x-not-registered"] == nil, "unregistered header should be nil")
assert_eq(msg.from.name, "name1", "from.name")
assert_eq(msg.from.address, "addr1", "from.address")
@@ -41,10 +50,26 @@ func TestInboundMessageGetters(t *testing.T) {
ls, _ := test.NewLuaState()
registerInboundMessageType(ls)
registerMailAddressType(ls)
registerHeaderType(ls)
ls.SetGlobal("msg", wrapInboundMessage(ls, want))
require.NoError(t, ls.DoString(script))
}
func TestInboundMessageHeaderReadOnly(t *testing.T) {
script := `
msg.header = {}
`
got := &event.InboundMessage{}
ls, _ := test.NewLuaState()
registerInboundMessageType(ls)
registerHeaderType(ls)
ls.SetGlobal("msg", wrapInboundMessage(ls, got))
err := ls.DoString(script)
require.Error(t, err)
assert.Contains(t, err.Error(), "header is read-only")
}
func TestInboundMessageSetters(t *testing.T) {
want := &event.InboundMessage{
Mailboxes: []string{"mb1", "mb2"},