diff --git a/app/api/parse-email/route.ts b/app/api/parse-email/route.ts index 5b524c6c..2db8bb65 100644 --- a/app/api/parse-email/route.ts +++ b/app/api/parse-email/route.ts @@ -3,11 +3,12 @@ import { parseEmail } from '@/lib/server/email-parser'; export async function POST(request: Request) { try { - const { emailContent } = await request.json(); + const body = await request.json(); + const { emailContent } = body; - if (!emailContent) { + if (!emailContent || typeof emailContent !== 'string') { return NextResponse.json( - { error: 'Email content is required' }, + { error: 'Invalid email content. Expected a string.' }, { status: 400 } ); } @@ -17,7 +18,7 @@ export async function POST(request: Request) { } catch (error) { console.error('Error parsing email:', error); return NextResponse.json( - { error: 'Failed to parse email' }, + { error: 'Failed to parse email', details: error instanceof Error ? error.message : 'Unknown error' }, { status: 500 } ); } diff --git a/lib/mail-parser-wrapper.ts b/lib/mail-parser-wrapper.ts index f39efd6b..63684246 100644 --- a/lib/mail-parser-wrapper.ts +++ b/lib/mail-parser-wrapper.ts @@ -1,6 +1,5 @@ import { simpleParser, ParsedMail, Attachment, HeaderValue, AddressObject } from 'mailparser'; import DOMPurify from 'dompurify'; -import { JSDOM } from 'jsdom'; // Create a window object for DOMPurify const window = new JSDOM('').window; diff --git a/lib/server/email-parser.ts b/lib/server/email-parser.ts index 85b20e54..587644da 100644 --- a/lib/server/email-parser.ts +++ b/lib/server/email-parser.ts @@ -1,13 +1,20 @@ import { simpleParser } from 'mailparser'; -import DOMPurify from 'isomorphic-dompurify'; function cleanHtml(html: string): string { try { - return DOMPurify.sanitize(html, { - ALLOWED_TAGS: ['p', 'br', 'div', 'span', 'a', 'img', 'strong', 'em', 'u', 'ul', 'ol', 'li', 'blockquote', 'pre', 'code', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'], - ALLOWED_ATTR: ['href', 'src', 'alt', 'title', 'class', 'style'], - ALLOWED_URI_REGEXP: /^(?:(?:(?:f|ht)tps?|mailto|tel):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i - }); + // Basic HTML cleaning without DOMPurify + return html + .replace(/)<[^<]*)*<\/script>/gi, '') // Remove script tags + .replace(/)<[^<]*)*<\/style>/gi, '') // Remove style tags + .replace(/]*>/gi, '') // Remove meta tags + .replace(/]*>[\s\S]*?<\/head>/gi, '') // Remove head + .replace(/]*>[\s\S]*?<\/title>/gi, '') // Remove title + .replace(/]*>/gi, '') // Remove body opening tag + .replace(/<\/body>/gi, '') // Remove body closing tag + .replace(/]*>/gi, '') // Remove html opening tag + .replace(/<\/html>/gi, '') // Remove html closing tag + .replace(/\s+/g, ' ') // Clean up whitespace + .trim(); } catch (error) { console.error('Error cleaning HTML:', error); return html;