Skip to content

Commit 2c39ba6

Browse files
fix: resolve security vulnerabilities in contact function
1 parent 3961206 commit 2c39ba6

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

functions/contact.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,17 @@ export const onRequestPost = async (context) => {
4848
return new Response(JSON.stringify({ success: true, skipped: true }), { status: 200, headers: baseHeaders });
4949
}
5050

51-
const sanitize = (v, max) =>
52-
String(v || '').replace(/<[^>]+>/g, '').trim().slice(0, max);
51+
const sanitize = (v, max) => {
52+
if (!v) return '';
53+
return String(v)
54+
.replace(/&/g, '&amp;')
55+
.replace(/</g, '&lt;')
56+
.replace(/>/g, '&gt;')
57+
.replace(/"/g, '&quot;')
58+
.replace(/'/g, '&#039;')
59+
.trim()
60+
.slice(0, max);
61+
};
5362

5463
const name = sanitize(data.name, 100);
5564
const email = sanitize(data.email, 254);
@@ -128,13 +137,19 @@ export const onRequestPost = async (context) => {
128137
const errTxt = await mailResp.text().catch(() => '');
129138
console.error('Resend send failed', mailResp.status, errTxt);
130139
const payload = { success: strictEmail ? false : true, warning: 'Email send failed', degraded: !strictEmail };
131-
if (debugEnabled) payload.debug = { status: mailResp.status, body: errTxt.slice(0, 400) };
132-
return new Response(JSON.stringify(payload), { status: strictEmail ? 502 : 200, headers: baseHeaders });
140+
if (debugEnabled) {
141+
// Safe debug log to console only
142+
console.debug('Resend error body:', errTxt.slice(0, 400));
143+
}
144+
return new Response(JSON.stringify(payload), { status: strictEmail ? 502 : 200, headers: baseHeaders });
133145
}
134146
} catch (e) {
135147
console.error('Resend exception', e);
136148
const payload = { success: strictEmail ? false : true, warning: 'Email send exception', degraded: !strictEmail };
137-
if (debugEnabled) payload.debug = { message: (e && e.message) || String(e) };
149+
if (debugEnabled) {
150+
// Safe debug log to console only
151+
console.debug('Resend exception message:', (e && e.message) || String(e));
152+
}
138153
return new Response(JSON.stringify(payload), { status: strictEmail ? 502 : 200, headers: baseHeaders });
139154
}
140155

@@ -147,7 +162,9 @@ export const onRequestPost = async (context) => {
147162
} catch (e) {
148163
console.error('Unhandled contact function error', e);
149164
const payload = { success: false, error: 'Server error' };
150-
if (debugEnabled) payload.debug = { message: (e && e.message) || String(e) };
165+
if (debugEnabled) {
166+
console.debug('Server error details:', (e && e.message) || String(e));
167+
}
151168
return new Response(JSON.stringify(payload), { status: 500, headers: baseHeaders });
152169
}
153170
};

0 commit comments

Comments
 (0)