Skip to content

armgit / posta

Public Imported from GitHub
0

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.

3.4 KB
Raw Blame
// 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 messages

import (
	"testing"

	"github.com/goposta/posta/internal/models"
	"github.com/lib/pq"
)

func TestOriginAllowed(t *testing.T) {
	cases := []struct {
		name    string
		origins []string
		strict  bool
		origin  string
		want    bool
	}{
		{"no allowlist accepts anything", nil, false, "https://evil.test", true},
		{"empty origin allowed by default", []string{"https://example.com"}, false, "", true},
		{"empty origin rejected when strict", []string{"https://example.com"}, true, "", false},
		{"exact match", []string{"https://example.com"}, false, "https://example.com", true},
		{"trailing slash tolerated", []string{"https://example.com"}, false, "https://example.com/", true},
		{"scheme mismatch", []string{"https://example.com"}, false, "http://example.com", false},
		{"port mismatch", []string{"https://example.com"}, false, "https://example.com:8443", false},
		{"host mismatch", []string{"https://example.com"}, false, "https://evil.com", false},
		{"subdomain is not a match", []string{"https://example.com"}, false, "https://www.example.com", false},
		{"wildcard", []string{"*"}, false, "https://anything.test", true},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			form := &models.Form{AllowedOrigins: pq.StringArray(tc.origins), StrictOrigin: tc.strict}
			allowlist, strict := form.OriginPolicy()
			if got := OriginAllowed(allowlist, strict, tc.origin); got != tc.want {
				t.Fatalf("OriginAllowed(%q) = %v, want %v", tc.origin, got, tc.want)
			}
		})
	}
}

func TestRedirectAllowedRejectsOpenRedirects(t *testing.T) {
	form := &models.Form{AllowedOrigins: pq.StringArray{"https://example.com"}}

	cases := []struct {
		target string
		want   bool
	}{
		{"https://example.com/thanks", true},
		{"https://evil.test/phish", false},
		{"javascript:alert(1)", false},
		{"//evil.test", false},
		{"/thanks", false},
		{"", false},
	}
	for _, tc := range cases {
		if got := RedirectAllowed(form.AllowedOrigins, form.RedirectURL, tc.target); got != tc.want {
			t.Fatalf("RedirectAllowed(%q) = %v, want %v", tc.target, got, tc.want)
		}
	}
}

func TestRedirectAllowedWithoutAllowlistRejects(t *testing.T) {
	form := &models.Form{}
	if RedirectAllowed(form.AllowedOrigins, form.RedirectURL, "https://anywhere.test/thanks") {
		t.Fatal("a form with no allowlist must not honour an arbitrary redirect target")
	}
}

func TestRedirectAllowedMatchesConfiguredRedirectHost(t *testing.T) {
	form := &models.Form{RedirectURL: "https://example.com/thanks"}
	if !RedirectAllowed(form.AllowedOrigins, form.RedirectURL, "https://example.com/other") {
		t.Fatal("a target on the configured redirect host should be allowed")
	}
}