chore: email denylist

This commit is contained in:
Cory Dransfeldt 2024-10-08 10:05:47 -07:00
parent 9b43897f25
commit df5fddefc0
No known key found for this signature in database
2 changed files with 14 additions and 6 deletions

View file

@ -25,13 +25,23 @@ export default {
const email = formData.get('email') const email = formData.get('email')
const message = formData.get('message') const message = formData.get('message')
const hpName = formData.get('hp_name') const hpName = formData.get('hp_name')
if (hpName) return new Response('Spam detected', { status: 400 }) if (hpName) return new Response('Spam detected', { status: 400 })
if (!name || !email || !message) return new Response('Invalid input', { status: 400 }) if (!name || !email || !message) return new Response('Invalid input', { status: 400 })
const emailDomain = email.split('@')[1].toLowerCase()
const supabaseUrl = env.SUPABASE_URL const supabaseUrl = env.SUPABASE_URL
const supabaseKey = env.SUPABASE_KEY const supabaseKey = env.SUPABASE_KEY
const supabase = createClient(supabaseUrl, supabaseKey) const supabase = createClient(supabaseUrl, supabaseKey)
const { data: blockedDomains, error: domainError } = await supabase
.from('blocked_domains')
.select('domain_name')
if (domainError) throw new Error(`Failed to fetch blocked domains: ${domainError.message}`)
const domainList = blockedDomains.map(item => item['domain_name'].toLowerCase())
if (domainList.includes(emailDomain)) return new Response('Email domain is blocked.', { status: 400 })
const { error } = await supabase.from('contacts').insert([ const { error } = await supabase.from('contacts').insert([
{ name, email, message, replied: false } { name, email, message, replied: false }
]) ])
@ -40,15 +50,13 @@ export default {
const forwardEmailApiKey = env.FORWARDEMAIL_API_KEY const forwardEmailApiKey = env.FORWARDEMAIL_API_KEY
const authHeader = 'Basic ' + btoa(`${forwardEmailApiKey}:`) const authHeader = 'Basic ' + btoa(`${forwardEmailApiKey}:`)
const emailData = new URLSearchParams({ const emailData = new URLSearchParams({
from: 'hi@admin.coryd.dev', from: `${name} <hi@admin.coryd.dev>`,
to: 'hi@coryd.dev', to: 'hi@coryd.dev',
subject: 'New contact form submission', subject: `${message}`,
text: `Name: ${name}\nEmail: ${email}\nMessage: ${message}`, text: `Name: ${name}\nEmail: ${email}\nMessage: ${message}`,
replyTo: email replyTo: email
}).toString() }).toString()
const response = await fetch('https://api.forwardemail.net/v1/emails', { const response = await fetch('https://api.forwardemail.net/v1/emails', {
method: 'POST', method: 'POST',
headers: { headers: {

View file

@ -16,7 +16,7 @@ const sanitizeMediaString = (str) => {
const sendEmail = async (subject, text, authHeader, maxRetries = 3) => { const sendEmail = async (subject, text, authHeader, maxRetries = 3) => {
const emailData = new URLSearchParams({ const emailData = new URLSearchParams({
from: 'hi@admin.coryd.dev', from: '<coryd.dev> hi@admin.coryd.dev',
to: 'hi@coryd.dev', to: 'hi@coryd.dev',
subject: subject, subject: subject,
text: text, text: text,