Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.
Go email-relayemail-routinghttp-to-smtpmail-deliverymailchimp-alternativemailgun-alternativemailsendermailservermailtemplatemailtestpostmark-alternativesend-emailsendgrid-alternativesendmailtransactional-emailtransactional-outbox
// Copyright (c) 2026 Jonas Kaninda.
//
// This file is part of Posta.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
package handlers
import (
"bytes"
"net/url"
"strings"
"testing"
"github.com/goposta/posta/internal/models"
)
func TestPrefillAttrs(t *testing.T) {
form := &models.Form{Fields: models.FieldDefs{
{Key: "plan", Label: "Plan", Type: models.FieldSelect, Options: []models.FieldOption{{Value: "pro"}}},
{Key: "topics", Label: "Topics", Type: models.FieldMultiSelect, Options: []models.FieldOption{{Value: "a, b"}, {Value: "c"}}},
{Key: "email", Label: "Email", Type: models.FieldEmail, PII: true},
{Key: "old", Label: "Old", Type: models.FieldText, Archived: true},
{Key: "note", Label: "Note", Type: models.FieldText},
}}
query := url.Values{
"plan": {"pro"},
"topics": {"a, b", "c"},
"email": {"[email protected]"},
"old": {"x"},
"unknown": {"x"},
"note": {strings.Repeat("x", maxPrefillLength+1)},
}
attrs := prefillAttrs(form, query)
got := make([]string, 0, len(attrs))
for _, a := range attrs {
got = append(got, string(a))
}
want := []string{`data-value-plan="pro"`, `data-value-topics="["a, b","c"]"`}
if strings.Join(got, " ") != strings.Join(want, " ") {
t.Errorf("prefill attrs:\n got %v\nwant %v (PII, archived, unknown and overlong values must be skipped)", got, want)
}
schemaless := prefillAttrs(&models.Form{}, url.Values{"name": {"Ada"}, "other": {"x"}})
if len(schemaless) != 1 || string(schemaless[0]) != `data-value-name="Ada"` {
t.Errorf("schemaless prefill = %v, want only the built-in name field", schemaless)
}
}
func TestHostedPageEscapesFormText(t *testing.T) {
var buf bytes.Buffer
err := hostedPageTemplate.Execute(&buf, hostedPageData{
Title: `</title><script>alert(1)</script>`,
Description: `" onmouseover="alert(1)`,
Key: "abc",
Theme: "light",
HeaderImage: "javascript:alert(1)",
Attrs: prefillAttrs(&models.Form{}, url.Values{"name": {`"><script>alert(1)</script>`}}),
AbuseMail: "mailto:[email protected]?subject=x",
})
if err != nil {
t.Fatal(err)
}
page := buf.String()
for _, bad := range []string{"<script>alert(1)", `" onmouseover="`, `src="javascript:`} {
if strings.Contains(page, bad) {
t.Errorf("page contains unescaped %q", bad)
}
}
for _, want := range []string{
`<script src="/widget/v1/posta-form.js" async></script>`,
"Never submit passwords through Posta forms.",
`href="mailto:[email protected]?subject=x"`,
} {
if !strings.Contains(page, want) {
t.Errorf("page is missing %q", want)
}
}
}
func TestAbuseMailtoFallsBackToAdmin(t *testing.T) {
h := &FormIngestHandler{abuseFallback: "[email protected]"}
if got := h.abuseContact(""); got != "[email protected]" {
t.Errorf("fallback contact = %q", got)
}
if got := h.abuseContact("[email protected]"); got != "[email protected]" {
t.Errorf("configured contact = %q", got)
}
if got := abuseMailto("[email protected]", "KEY1"); got != "mailto:[email protected]?subject=Abuse%20report%3A%20form%20KEY1" {
t.Errorf("mailto = %q", got)
}
}