mirror of
https://github.com/jhillyerd/inbucket.git
synced 2026-10-29 13:42:30 +00:00
bd4cc4b842
* 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>
76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
package luahost
|
|
|
|
import (
|
|
"net/textproto"
|
|
"strings"
|
|
|
|
lua "github.com/yuin/gopher-lua"
|
|
)
|
|
|
|
const headerName = "inbound_header"
|
|
|
|
func registerHeaderType(ls *lua.LState) {
|
|
// header type.
|
|
mt := ls.NewTypeMetatable(headerName)
|
|
|
|
// Methods.
|
|
ls.SetField(mt, "__index", ls.NewFunction(headerIndex))
|
|
ls.SetField(mt, "__newindex", ls.NewFunction(headerNewIndex))
|
|
}
|
|
|
|
func wrapHeader(ls *lua.LState, val textproto.MIMEHeader) *lua.LUserData {
|
|
ud := ls.NewUserData()
|
|
ud.Value = val
|
|
ls.SetMetatable(ud, ls.GetTypeMetatable(headerName))
|
|
|
|
return ud
|
|
}
|
|
|
|
// Checks there is a MIMEHeader at stack position `pos`, else throws Lua error.
|
|
func checkHeader(ls *lua.LState, pos int) textproto.MIMEHeader {
|
|
ud := ls.CheckUserData(pos)
|
|
if v, ok := ud.Value.(textproto.MIMEHeader); ok {
|
|
return v
|
|
}
|
|
ls.ArgError(pos, headerName+" expected")
|
|
return nil
|
|
}
|
|
|
|
// Gets a header value by name. Header names are case-insensitive; the first
|
|
// value is returned when a header was set more than once, and nil when the
|
|
// header is absent or unset. This emulates a Lua table, allowing
|
|
// `msg.header["x-spam-status"]`.
|
|
func headerIndex(ls *lua.LState) int {
|
|
h := checkHeader(ls, 1)
|
|
name := ls.CheckString(2)
|
|
|
|
// Exact match first, then case-insensitive. Decoded keys are canonicalized
|
|
// by enmime using email-specific rules that differ from net/textproto, so
|
|
// comparing against the stored keys is more reliable than canonicalizing
|
|
// the requested name. The header set is small, a scan is cheap.
|
|
if values, ok := h[name]; ok && len(values) > 0 {
|
|
ls.Push(lua.LString(values[0]))
|
|
return 1
|
|
}
|
|
for key, values := range h {
|
|
if len(values) > 0 && strings.EqualFold(key, name) {
|
|
ls.Push(lua.LString(values[0]))
|
|
return 1
|
|
}
|
|
}
|
|
|
|
// Header absent, or registered but unset on this message.
|
|
ls.Push(lua.LNil)
|
|
|
|
return 1
|
|
}
|
|
|
|
// Rejects writes; headers are read-only.
|
|
func headerNewIndex(ls *lua.LState) int {
|
|
checkHeader(ls, 1)
|
|
name := ls.CheckString(2)
|
|
ls.RaiseError("header %q is read-only", name)
|
|
|
|
return 0
|
|
}
|