1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2026-10-29 05:32:31 +00:00
Files
go-inbucket/pkg/extension/luahost/bind_config_test.go
T
James Hillyerd bd4cc4b842 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>
2026-10-23 15:38:37 -07:00

82 lines
2.0 KiB
Go

package luahost
import (
"testing"
"github.com/inbucket/inbucket/v3/pkg/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInbucketConfigDecodeHeaders(t *testing.T) {
script := `
assert(inbucket, "inbucket should not be nil")
assert(inbucket.config, "inbucket.config should not be nil")
-- Verify decode_headers starts off nil.
assert(inbucket.config.decode_headers == nil,
"decode_headers should be nil")
-- Set and read back.
inbucket.config.decode_headers = {
"X-Spam-Status",
"Authentication-Results",
}
assert_eq(inbucket.config.decode_headers,
{ "X-Spam-Status", "Authentication-Results" })
-- Unknown fields read as nil.
assert(inbucket.config.bogus == nil, "unknown config field should be nil")
`
ls, _ := test.NewLuaState()
registerInbucketTypes(ls)
require.NoError(t, ls.DoString(script))
ib, err := getInbucket(ls)
require.NoError(t, err)
assert.Equal(t,
[]string{"X-Spam-Status", "Authentication-Results"},
ib.Config.DecodeHeaders)
}
func TestInbucketConfigDecodeHeadersEmpty(t *testing.T) {
script := `
inbucket.config.decode_headers = {}
assert(inbucket.config.decode_headers == nil,
"empty decode_headers should read as nil")
`
ls, _ := test.NewLuaState()
registerInbucketTypes(ls)
require.NoError(t, ls.DoString(script))
ib, err := getInbucket(ls)
require.NoError(t, err)
assert.Empty(t, ib.Config.DecodeHeaders)
}
func TestInbucketConfigInvalidIndex(t *testing.T) {
script := `
inbucket.config.bogus = true
`
ls, _ := test.NewLuaState()
registerInbucketTypes(ls)
err := ls.DoString(script)
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid inbucket.config index")
}
func TestInbucketConfigDecodeHeadersInvalidEntry(t *testing.T) {
script := `
inbucket.config.decode_headers = { "X-Spam-Status", 42 }
`
ls, _ := test.NewLuaState()
registerInbucketTypes(ls)
err := ls.DoString(script)
require.Error(t, err)
assert.Contains(t, err.Error(), `decode_headers[2] must be a string`)
}