Skip to content

Commit c484fb2

Browse files
committed
fix(examples): linear parseMailbox to avoid polynomial ReDoS
CodeQL flagged the angle-bracket regex in the feedback-worker example as a polynomial regular expression on uncontrolled header input. Replace it with an indexOf/slice parse that is linear in input length while preserving the "Name <addr>" parsing behavior.
1 parent e7996b6 commit c484fb2

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

examples/feedback-worker/src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,14 @@ async function sendSupportEmail(env: Env, subject: string, body: string): Promis
297297
}
298298

299299
function parseMailbox(value: string): { email: string; name?: string } {
300-
const match = value.match(/^([^<]*?)\s*<([^>]+)>$/);
301-
if (!match) return { email: value.trim() };
302-
return { name: match[1].trim().replace(/^"|"$/g, ""), email: match[2].trim() };
300+
// Linear parse (no backtracking regex) for the "Name <addr>" form to avoid
301+
// polynomial ReDoS on uncontrolled header input.
302+
const trimmed = value.trim();
303+
const lt = trimmed.indexOf("<");
304+
if (lt === -1 || !trimmed.endsWith(">")) return { email: trimmed };
305+
const email = trimmed.slice(lt + 1, -1).trim();
306+
const name = trimmed.slice(0, lt).trim().replace(/^"|"$/g, "");
307+
return { name, email };
303308
}
304309

305310
function splitCsv(value: string): string[] {

0 commit comments

Comments
 (0)