Skip to content

Commit 067aebe

Browse files
andris9claude
andcommitted
fix: parse Ethereal response props without polynomial regex backtracking
getTestMessageUrl() extracted the trailing bracket group of the server response with a regex that CodeQL flagged for polynomial backtracking on adversarial responses (js/polynomial-redos). Replace it with linear string scanning; a 200k-case fuzz comparison confirmed the old and new parsers extract identical values. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 0cee4fe commit 067aebe

2 files changed

Lines changed: 61 additions & 5 deletions

File tree

‎lib/nodemailer.js‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,20 @@ module.exports.getTestMessageUrl = function (info) {
147147
}
148148

149149
const infoProps = new Map();
150-
info.response.replace(/\[([^\]]+)\]$/, (m, props) => {
151-
props.replace(/\b([A-Z0-9]+)=([^\s]+)/g, (m, key, value) => {
152-
infoProps.set(key, value);
153-
});
154-
});
150+
151+
// Extract the trailing "[...]" part of the response (no "]" allowed inside)
152+
// with linear string scanning; the equivalent regex /\[([^\]]+)\]$/ was
153+
// flagged for polynomial backtracking on adversarial server responses
154+
const response = info.response.toString();
155+
if (response.length > 2 && response.charAt(response.length - 1) === ']') {
156+
const open = response.indexOf('[', response.lastIndexOf(']', response.length - 2) + 1);
157+
if (open >= 0 && open < response.length - 2) {
158+
const props = response.substring(open + 1, response.length - 1);
159+
props.replace(/\b([A-Z0-9]+)=([^\s]+)/g, (m, key, value) => {
160+
infoProps.set(key, value);
161+
});
162+
}
163+
}
155164

156165
if (infoProps.has('STATUS') && infoProps.has('MSGID')) {
157166
return (testAccount.web || ETHEREAL_WEB) + '/message/' + infoProps.get('MSGID');
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const { describe, it } = require('node:test');
4+
const assert = require('node:assert/strict');
5+
const nodemailer = require('../../lib/nodemailer');
6+
7+
// Regression tests for the trailing "[...]" parser in getTestMessageUrl; the
8+
// previous regex implementation was flagged for polynomial backtracking
9+
describe('getTestMessageUrl', () => {
10+
it('should return the message url for an Ethereal response', () => {
11+
const url = nodemailer.getTestMessageUrl({
12+
response: '250 Accepted [STATUS=new MSGID=abc.123-def]'
13+
});
14+
assert.ok(url, 'expected an url');
15+
assert.ok(url.endsWith('/message/abc.123-def'), 'unexpected url: ' + url);
16+
});
17+
18+
it('should use the last bracket group of the response', () => {
19+
const url = nodemailer.getTestMessageUrl({
20+
response: '250 OK [foo] [STATUS=new MSGID=abc]'
21+
});
22+
assert.ok(url, 'expected an url');
23+
assert.ok(url.endsWith('/message/abc'), 'unexpected url: ' + url);
24+
});
25+
26+
it('should return false when there is no trailing props block', () => {
27+
assert.strictEqual(nodemailer.getTestMessageUrl(false), false);
28+
assert.strictEqual(nodemailer.getTestMessageUrl({}), false);
29+
assert.strictEqual(nodemailer.getTestMessageUrl({ response: '250 Accepted' }), false);
30+
assert.strictEqual(nodemailer.getTestMessageUrl({ response: '250 Accepted [STATUS=new MSGID=x] trailing' }), false);
31+
assert.strictEqual(nodemailer.getTestMessageUrl({ response: '250 Accepted [STATUS=new MSGID=x]y]' }), false);
32+
assert.strictEqual(nodemailer.getTestMessageUrl({ response: '[]' }), false);
33+
});
34+
35+
it('should return false when STATUS or MSGID is missing', () => {
36+
assert.strictEqual(nodemailer.getTestMessageUrl({ response: '250 Accepted [MSGID=abc]' }), false);
37+
assert.strictEqual(nodemailer.getTestMessageUrl({ response: '250 Accepted [STATUS=new]' }), false);
38+
});
39+
40+
it('should handle adversarial responses in linear time', () => {
41+
const response = '['.repeat(50000);
42+
const start = Date.now();
43+
assert.strictEqual(nodemailer.getTestMessageUrl({ response }), false);
44+
const duration = Date.now() - start;
45+
assert.ok(duration < 100, 'parsing took too long: ' + duration + 'ms');
46+
});
47+
});

0 commit comments

Comments
 (0)