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.

fix: give forms full share, widget and endpoint URLs

Jonas Kaninda273b94b95239
diff --git a/cmd/posta/server.go b/cmd/posta/server.go
index f843c3a..10df840 100644
--- a/cmd/posta/server.go
+++ b/cmd/posta/server.go
@@ -551,7 +551,7 @@ func startEmbeddedWorker(db *gorm.DB,
inboundHandler := worker.NewInboundProcessHandler(
repositories.NewInboundEmailRepository(db),
newWebhookDispatcher(db, cfg),
- cfg.ApiBaseURL,
+ cfg.PublicAPIURL(),
[]byte(cfg.JWTSecret),
)
inboundHandler.OnForwarded(metrics.IncrementInboundForwarded)
diff --git a/cmd/posta/worker.go b/cmd/posta/worker.go
index 330b76e..fc694c5 100644
--- a/cmd/posta/worker.go
+++ b/cmd/posta/worker.go
@@ -181,7 +181,7 @@ func runWorker() error {
inboundHandler := worker.NewInboundProcessHandler(
repositories.NewInboundEmailRepository(db),
newWebhookDispatcher(db, cfg),
- cfg.ApiBaseURL,
+ cfg.PublicAPIURL(),
[]byte(cfg.JWTSecret),
)
inboundHandler.OnForwarded(metrics.IncrementInboundForwarded)
diff --git a/internal/config/config.go b/internal/config/config.go
index d2d2b05..a06f387 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -543,6 +543,17 @@ func (c *Config) InitWorker() error {
return c.validateWorker()
}
+// PublicAPIURL is the base of the URLs Posta gives out for its public API:
+// form endpoints, share pages, the widget script, inbound attachment links.
+// POSTA_API_URL when the API has its own host, otherwise POSTA_WEB_URL, which
+// serves the API too. Empty only when neither is set.
+func (c *Config) PublicAPIURL() string {
+ if c.ApiBaseURL != "" {
+ return strings.TrimSuffix(c.ApiBaseURL, "/")
+ }
+ return strings.TrimSuffix(c.AppWebURL, "/")
+}
+
// networkPolicy reads a netguard policy from the environment, stopping on an
// allowed network that does not parse rather than running with a policy
// other than the one configured.
diff --git a/internal/config/config_test.go b/internal/config/config_test.go
index 10c53b0..2c40518 100644
--- a/internal/config/config_test.go
+++ b/internal/config/config_test.go
@@ -157,3 +157,21 @@ func TestAllowedCORSOrigins(t *testing.T) {
})
}
}
+
+// Form links and inbound attachment links use POSTA_API_URL when the API has
+// its own host, and POSTA_WEB_URL otherwise, as the configuration docs say.
+func TestPublicAPIURL(t *testing.T) {
+ cases := []struct {
+ api, web, want string
+ }{
+ {"https://api.example.com/", "https://posta.example.com", "https://api.example.com"},
+ {"", "https://posta.example.com/", "https://posta.example.com"},
+ {"", "", ""},
+ }
+ for _, tc := range cases {
+ c := &Config{ApiBaseURL: tc.api, AppWebURL: tc.web}
+ if got := c.PublicAPIURL(); got != tc.want {
+ t.Errorf("api=%q web=%q: got %q, want %q", tc.api, tc.web, got, tc.want)
+ }
+ }
+}
diff --git a/internal/routes/routes.go b/internal/routes/routes.go
index 111b78d..6fbf5c6 100644
--- a/internal/routes/routes.go
+++ b/internal/routes/routes.go
@@ -499,7 +499,7 @@ func InitRoutes(app *okapi.Okapi, db *gorm.DB, redisClient *redis.Client, cfg *c
metrics.IncrementMessageReceived(string(status))
})
- r.h.form = handlers.NewFormHandler(formRepo, messageRepo, domainRepo, auditLogger, cfg.ApiBaseURL)
+ r.h.form = handlers.NewFormHandler(formRepo, messageRepo, domainRepo, auditLogger, cfg.PublicAPIURL())
r.h.form.SetKeyCache(messageSvc)
r.h.form.SetEdition(edition)
r.h.message = handlers.NewMessageHandler(messageRepo, formRepo, messageFilterRepo, messageSvc, auditLogger)
diff --git a/web/src/views/forms/FormDetail.vue b/web/src/views/forms/FormDetail.vue
index de166ea..f80c2b4 100644
--- a/web/src/views/forms/FormDetail.vue
+++ b/web/src/views/forms/FormDetail.vue
@@ -82,12 +82,27 @@ function syncResponseFields(f: Form) {
}
const notifyText = ref("");
+function withOrigin(s: FormSnippet): FormSnippet {
+ if (!s.endpoint.startsWith("/")) return s;
+ const origin = window.location.origin;
+ const abs = (u: string) => (u.startsWith("/") ? origin + u : u);
+ const endpoint = abs(s.endpoint);
+ return {
+ ...s,
+ endpoint,
+ share_url: abs(s.share_url),
+ html: s.html.split(s.endpoint).join(endpoint),
+ fetch: s.fetch.split(s.endpoint).join(endpoint),
+ widget: s.widget.replace('src="/', `src="${origin}/`),
+ };
+}
+
async function load() {
loading.value = true;
try {
const [formRes, snippetRes] = await Promise.all([formsApi.get(id), formsApi.snippet(id)]);
form.value = formRes.data.data;
- snippet.value = snippetRes.data.data;
+ snippet.value = withOrigin(snippetRes.data.data);
originsText.value = (formRes.data.data.allowed_origins || []).join(", ");
syncResponseFields(formRes.data.data);
notifyText.value = (formRes.data.data.notify_emails || []).join(", ");
@@ -172,7 +187,7 @@ async function rotateKey() {
const res = await formsApi.rotateKey(id);
form.value = res.data.data;
const snippetRes = await formsApi.snippet(id);
- snippet.value = snippetRes.data.data;
+ snippet.value = withOrigin(snippetRes.data.data);
notify.success("Public key rotated. Update your embed code.");
} catch (e: any) {
notify.error(apiMessage(e, "Failed to rotate key"));