Fix inbound-email data loss, MIME truncation, and header injection

- server/data.js: preserve source/htmlBody/inboundTo/messageId across
  server restarts (sanitizeLoadedContactSubmissions was silently
  dropping them on reload from disk)
- cloudflare/email-worker.js: rewrite MIME parsing to split on the
  actual boundary marker instead of any literal "--", unfold
  multi-line headers, and correctly recombine multi-byte UTF-8 in
  quoted-printable decoding
- server/routes/inbound-email.js: validate Message-ID against RFC 5322
  grammar before storing/using it, and compare the webhook secret with
  timingSafeEqual to match the rest of the codebase's auth checks
- server/routes/contact.js: re-validate messageId at the point it's
  injected into outgoing In-Reply-To/References headers; move the
  allowed reply-from addresses into a shared config constant
- src/AdminPage.tsx: 30s inbox poll now syncs field updates (e.g.
  archived) on already-loaded submissions instead of only appending
  new ones; consolidate the duplicated from-address list
- .claude/launch.json: add a vite dev server preview config used to
  verify these changes

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
nmemmert
2026-07-02 08:21:50 -04:00
parent fbb9a24f32
commit 17c9cbbc8b
7 changed files with 147 additions and 52 deletions
+3
View File
@@ -85,6 +85,9 @@ export const DEFAULT_RESEND_FROM = 'Verse by Verse with Nate <hello@versebyverse
export const DEFAULT_RESEND_TO = 'hello@versebyversewithnate.us'
export const DEFAULT_RESEND_REPLY_TO = 'hello@versebyversewithnate.us'
export const ADMIN_REPLY_FROM = DEFAULT_RESEND_FROM
export const NATE_RESEND_FROM = 'Verse by Verse with Nate <nate@versebyversewithnate.us>'
// Addresses an admin may send a reply from — must stay in sync with the <select> options in src/AdminPage.tsx.
export const ADMIN_REPLY_FROM_OPTIONS = [DEFAULT_RESEND_FROM, NATE_RESEND_FROM]
export const DEFAULT_SEO = {
title: 'Verse by Verse with Nate',
+4
View File
@@ -1048,6 +1048,10 @@ function sanitizeLoadedContactSubmissions(value) {
subscribe: entry.subscribe === true,
archived: entry.archived === true,
emailStatus: normalizeContactEmailStatus(entry.emailStatus, entry.subscribe === true),
source: entry.source === 'inbound-email' || entry.source === 'download' ? entry.source : 'contact-form',
htmlBody: typeof entry.htmlBody === 'string' && entry.htmlBody.trim() ? entry.htmlBody : null,
inboundTo: typeof entry.inboundTo === 'string' ? entry.inboundTo : '',
messageId: typeof entry.messageId === 'string' ? entry.messageId : '',
}))
}
+6 -6
View File
@@ -8,6 +8,7 @@ import {
USE_RESEND_AUTOMATION_WELCOME,
DEFAULT_SEO,
ADMIN_REPLY_FROM,
ADMIN_REPLY_FROM_OPTIONS,
} from '../config.js'
import { state } from '../state.js'
import {
@@ -39,6 +40,9 @@ import {
syncContactToResend,
} from '../email.js'
// RFC 5322 msg-id: "<" printable-ASCII-no-whitespace ">"
const MESSAGE_ID_RE = /^<[\x21-\x7E]+>$/
function upsertContactEmailStatus(submissionId, stream, patch) {
if (!submissionId || typeof submissionId !== 'string') return
if (!stream || typeof stream !== 'string') return
@@ -479,11 +483,7 @@ export function register(app) {
const html = buildAdminReplyTemplate({ recipientName, message })
const replyToAddress = getResendReplyToAddress()
const defaultFrom = getResendFromAddress() || ADMIN_REPLY_FROM
const ALLOWED_FROM = [
'Verse by Verse with Nate <hello@versebyversewithnate.us>',
'Verse by Verse with Nate <nate@versebyversewithnate.us>',
]
const fromAddress = ALLOWED_FROM.includes(requestedFrom) ? requestedFrom : defaultFrom
const fromAddress = ADMIN_REPLY_FROM_OPTIONS.includes(requestedFrom) ? requestedFrom : defaultFrom
const text = `Hi ${recipientName},\n\n${message}\n\nGrace and peace,\nVerse by Verse with Nate\n${replyToAddress}`
const resend = new Resend(process.env.RESEND_API_KEY)
@@ -502,7 +502,7 @@ export function register(app) {
],
headers: {
'X-Contact-Submission-Id': submission.id,
...(submission.messageId ? {
...(typeof submission.messageId === 'string' && MESSAGE_ID_RE.test(submission.messageId) ? {
'In-Reply-To': submission.messageId,
'References': submission.messageId,
} : {}),
+12 -5
View File
@@ -1,8 +1,11 @@
import { randomUUID } from 'node:crypto'
import { randomUUID, timingSafeEqual } from 'node:crypto'
import { state } from '../state.js'
import { queueContactSubmissionsWrite } from '../data.js'
import { MAX_CONTACT_SUBMISSIONS } from '../config.js'
// RFC 5322 msg-id: "<" printable-ASCII-no-whitespace ">"
const MESSAGE_ID_RE = /^<[\x21-\x7E]+>$/
export function register(app) {
app.post('/api/inbound-email', (req, res) => {
const secret = process.env.INBOUND_EMAIL_SECRET
@@ -11,7 +14,9 @@ export function register(app) {
}
const provided = req.get('x-webhook-secret') ?? ''
if (!provided || provided !== secret) {
const a = Buffer.from(provided, 'utf8')
const b = Buffer.from(secret, 'utf8')
if (!provided || a.length !== b.length || !timingSafeEqual(a, b)) {
res.status(401).json({ message: 'Unauthorized.' }); return
}
@@ -26,9 +31,11 @@ export function register(app) {
const fromEmail = fromMatch ? fromMatch[2].trim() : from.trim()
const fromName = fromMatch ? fromMatch[1].trim() : from.trim()
const normalizedMessageId = typeof messageId === 'string' && MESSAGE_ID_RE.test(messageId.trim()) ? messageId.trim() : ''
// Deduplicate by messageId if provided
if (messageId && typeof messageId === 'string' && messageId.trim()) {
const exists = state.contactSubmissions.some(s => s.messageId === messageId.trim())
if (normalizedMessageId) {
const exists = state.contactSubmissions.some(s => s.messageId === normalizedMessageId)
if (exists) {
res.json({ ok: true, duplicate: true }); return
}
@@ -46,7 +53,7 @@ export function register(app) {
archived: false,
source: 'inbound-email',
inboundTo: typeof to === 'string' ? to : '',
messageId: typeof messageId === 'string' ? messageId.trim() : '',
messageId: normalizedMessageId,
emailStatus: {
welcome: { status: 'not-applicable', lastEventAt: null, lastEventType: null, resendEmailId: null, error: null },
adminNotification: { status: 'not-applicable', lastEventAt: null, lastEventType: null, resendEmailId: null, error: null },