Skip to content

Commit

Permalink
fix(bodystructure): Handle invalid BODYSTRUCTURE from Bluemind mail s…
Browse files Browse the repository at this point in the history
…erver
  • Loading branch information
andris9 committed Jan 8, 2025
1 parent a11e05d commit b63f861
Show file tree
Hide file tree
Showing 2 changed files with 1,022 additions and 17 deletions.
45 changes: 28 additions & 17 deletions lib/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const iconv = require('iconv-lite');

const FLAG_COLORS = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'grey'];

module.exports = {
const tools = {
encodePath(connection, path) {
path = (path || '').toString();
if (!connection.enabled.has('UTF8=ACCEPT') && /[&\x00-\x08\x0b-\x0c\x0e-\x1f\u0080-\uffff]/.test(path)) {
Expand Down Expand Up @@ -57,7 +57,7 @@ module.exports = {
if (!a || !b) {
return false;
}
return module.exports.normalizePath(connection, a) === module.exports.normalizePath(connection, b);
return tools.normalizePath(connection, a) === tools.normalizePath(connection, b);
},

updateCapabilities(list) {
Expand Down Expand Up @@ -336,11 +336,11 @@ module.exports = {
break;

case 'envelope':
map.envelope = module.exports.parseEnvelope(attribute);
map.envelope = tools.parseEnvelope(attribute);
break;

case 'bodystructure':
map.bodyStructure = module.exports.parseBodystructure(attribute);
map.bodyStructure = tools.parseBodystructure(attribute);
break;

case 'internaldate': {
Expand Down Expand Up @@ -398,7 +398,7 @@ module.exports = {
}

if (map.flags) {
let flagColor = module.exports.getFlagColor(map.flags);
let flagColor = tools.getFlagColor(map.flags);
if (flagColor) {
map.flagColor = flagColor;
}
Expand Down Expand Up @@ -438,7 +438,7 @@ module.exports = {
address = '';
}
return {
name: module.exports.processName(libmime.decodeWords(getStrValue(addr[0]))),
name: tools.processName(libmime.decodeWords(getStrValue(addr[0]))),
address
};
})
Expand Down Expand Up @@ -609,7 +609,7 @@ module.exports = {
// body parameter parenthesized list
if (i < node.length - 1) {
if (node[i]) {
curNode.parameters = this.getStructuredParams(node[i]);
curNode.parameters = tools.getStructuredParams(node[i]);
}
i++;
}
Expand All @@ -619,7 +619,7 @@ module.exports = {

// body parameter parenthesized list
if (node[i]) {
curNode.parameters = this.getStructuredParams(node[i]);
curNode.parameters = tools.getStructuredParams(node[i]);
}
i++;

Expand Down Expand Up @@ -652,7 +652,7 @@ module.exports = {

// envelope
if (node[i]) {
curNode.envelope = module.exports.parseEnvelope([].concat(node[i] || []));
curNode.envelope = tools.parseEnvelope([].concat(node[i] || []));
}
i++;

Expand All @@ -671,14 +671,23 @@ module.exports = {
curNode.lineCount = Number((node[i] || {}).value || 0) || 0;
}
i++;
} else if (/^text\//.test(curNode.type)) {
// text/* adds additional line count values
}

// line count
if (node[i]) {
curNode.lineCount = Number((node[i] || {}).value || 0) || 0;
if (/^text\//.test(curNode.type)) {
// text/* adds additional line count value

// NB! some less known servers do not include the line count value
// length should be 12+
if (node.length === 11 && Array.isArray(node[i + 1]) && !Array.isArray(node[i + 2])) {
// invalid structure, disposition params are shifted
} else {
// correct structure, line count number is provided
if (node[i]) {
// line count
curNode.lineCount = Number((node[i] || {}).value || 0) || 0;
}
i++;
}
i++;
}

// extension data (not available for BODY requests)
Expand All @@ -700,7 +709,7 @@ module.exports = {
if (Array.isArray(node[i]) && node[i].length) {
curNode.disposition = ((node[i][0] || {}).value || '').toString().toLowerCase();
if (Array.isArray(node[i][1])) {
curNode.dispositionParameters = this.getStructuredParams(node[i][1]);
curNode.dispositionParameters = tools.getStructuredParams(node[i][1]);
}
}
i++;
Expand Down Expand Up @@ -762,7 +771,7 @@ module.exports = {
return;
}

let dateStr = module.exports.formatDate(value).replace(/^0/, ''); //starts with date-day-fixed with leading 0 replaced by SP
let dateStr = tools.formatDate(value).replace(/^0/, ''); //starts with date-day-fixed with leading 0 replaced by SP
let timeStr = value.toISOString().substr(11, 8);

return `${dateStr} ${timeStr} +0000`;
Expand Down Expand Up @@ -856,3 +865,5 @@ module.exports = {
return result.join(',');
}
};

module.exports = tools;
Loading

0 comments on commit b63f861

Please sign in to comment.