Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport features 5 2.x error handling 2023-11-19 #413

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions chrome/content/dkim.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ DKIM_Verifier.Display = (function() {
function handleException(e) {
try {
// log error
if (e instanceof DKIM_Verifier.DKIM_InternalError) {
if (e instanceof DKIM_Verifier.DKIM_TempError) {
log.error(e);
} else {
log.fatal(e);
Expand Down Expand Up @@ -207,6 +207,7 @@ DKIM_Verifier.Display = (function() {
*
* @param {IAuthVerifier.IAuthResult} result
* @return {void}
* @throws {Error}
*/
// eslint-disable-next-line complexity
function displayResult(result) {
Expand Down Expand Up @@ -248,8 +249,7 @@ DKIM_Verifier.Display = (function() {
highlightHeader("nosig");
break;
default:
throw new DKIM_Verifier.DKIM_InternalError("unknown res_num: " +
result.dkim[0].res_num);
throw new Error(`unknown res_num: ${result.dkim[0].res_num}`);
}

// policyAddUserExceptionButton
Expand Down
2 changes: 1 addition & 1 deletion chrome/locale/de/dkim.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ NOT_EMAIL = Keine E-Mail
# DKIM_INTERNALERROR
DKIM_INTERNALERROR = DKIM Verifier: Interner Fehler
DKIM_INTERNALERROR_NAME = Interner Fehler
DKIM_INTERNALERROR_DEFAULT = Fehler";
DKIM_INTERNALERROR_DEFAULT = Fehler
DKIM_INTERNALERROR_INCORRECT_EMAIL_FORMAT = E-Mail ist nicht richtig formatiert
DKIM_INTERNALERROR_INCORRECT_FROM = Absenderadresse ist falsch formatiert
# DKIM_INTERNALERROR - DNS
Expand Down
22 changes: 12 additions & 10 deletions modules/ARHParser.jsm.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

// options for ESLint
/* global Components, Services */
/* global Logging, DKIM_InternalError */
/* global Logging, DKIM_Error */
/* exported EXPORTED_SYMBOLS, ARHParser */

"use strict";
Expand Down Expand Up @@ -132,6 +132,7 @@ let ARHParser = {
*
* @param {String} authresHeader Authentication-Results header
* @return {ARHHeader} Parsed Authentication-Results header
* @throws {DKIM_Error}
*/
parse: function _ARHParser_parse(authresHeader) {
// remove header name
Expand All @@ -148,7 +149,7 @@ let ARHParser = {
reg_match = match(authresHeaderRef, `${value_cp}(?:${CFWS_p}([0-9]+)${CFWS_op})?`);
const authserv_id = reg_match[1] || reg_match[2];
if (!authserv_id) {
throw new Error("Error matching the ARH authserv-id.");
throw new DKIM_Error("Error matching the ARH authserv-id.");
}
res.authserv_id = authserv_id;
if (reg_match[3]) {
Expand Down Expand Up @@ -182,6 +183,7 @@ let ARHParser = {
*
* @param {RefString} str
* @return {ARHResinfo|null} Parsed resinfo
* @throws {DKIM_Error|Error}
*/
function parseResinfo(str) {
log.trace("parse str: " + str.toSource());
Expand Down Expand Up @@ -210,10 +212,10 @@ function parseResinfo(str) {
throw exception;
}
if (!reg_match[1]) {
throw new Error("Error matching the ARH method.");
throw new DKIM_Error("Error matching the ARH method.");
}
if (!reg_match[3]) {
throw new Error("Error matching the ARH result.");
throw new DKIM_Error("Error matching the ARH result.");
}
res.method = reg_match[1];
if (reg_match[2]) {
Expand Down Expand Up @@ -247,10 +249,10 @@ function parseResinfo(str) {
res.propertys.policy = {};
while ((reg_match = match_o(str, propspec_p)) !== null) {
if (!reg_match[1]) {
throw new Error("Error matching the ARH property name.");
throw new DKIM_Error("Error matching the ARH property name.");
}
if (!reg_match[2]) {
throw new Error("Error matching the ARH property sub-name.");
throw new DKIM_Error("Error matching the ARH property sub-name.");
}
let property = res.propertys[reg_match[1]];
if (!property) {
Expand All @@ -272,7 +274,7 @@ function parseResinfo(str) {
* @param {string} method
* @param {string} resultKeyword
* @returns {void}
* @throws {DKIM_InternalError} if result keyword is invalid for the method.
* @throws {DKIM_Error} if result keyword is invalid for the method.
*/
function checkResultKeyword(method, resultKeyword) {
let allowedKeywords;
Expand Down Expand Up @@ -307,7 +309,7 @@ function checkResultKeyword(method, resultKeyword) {
// And don't restrict the keyword.

if (allowedKeywords && !allowedKeywords.includes(resultKeyword)) {
throw new DKIM_InternalError(`Result keyword "${resultKeyword}" is not allowed for method "${method}"`);
throw new DKIM_Error(`Result keyword "${resultKeyword}" is not allowed for method "${method}"`);
}
}

Expand Down Expand Up @@ -343,13 +345,13 @@ class RefString {
* @param {RefString} str
* @param {String} pattern
* @return {String[]} An Array, containing the matches
* @throws if match no match found
* @throws {DKIM_Error} if match no match found
*/
function match(str, pattern) {
const reg_match = match_o(str, pattern);
if (reg_match === null) {
log.trace("str to match against:" + str.toSource());
throw new Error("Parsing error");
throw new DKIM_Error("Parsing error");
}
return reg_match;
}
Expand Down
35 changes: 25 additions & 10 deletions modules/AuthVerifier.jsm.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// options for ESLint
/* global Components, Services, MailServices */
/* global Logging, ARHParser */
/* global PREF, dkimStrings, domainIsInDomain, getDomainFromAddr, tryGetFormattedString, DKIM_InternalError */
/* global PREF, dkimStrings, domainIsInDomain, getDomainFromAddr, tryGetFormattedString */
/* exported EXPORTED_SYMBOLS, AuthVerifier */

"use strict";
Expand All @@ -41,7 +41,6 @@ Cu.import("resource:///modules/iteratorUtils.jsm");

Cu.import("resource://dkim_verifier/logging.jsm.js");
Cu.import("resource://dkim_verifier/helper.jsm.js");
Cu.import("resource://dkim_verifier/MsgReader.jsm.js");
Cu.import("resource://dkim_verifier/ARHParser.jsm.js");
// @ts-ignore
let DKIM = {};
Expand Down Expand Up @@ -133,7 +132,22 @@ var AuthVerifier = {
}

// create msg object
let msg = await DKIM.Verifier.createMsg(msgURI);
let msg = null;
try {
msg = await DKIM.Verifier.createMsg(msgURI);
}
catch (error) {
log.error("Parsing of message failed", error);
return Promise.resolve({
version: "2.1",
dkim: [{
version: "2.0",
result: "PERMFAIL",
res_num: this.DKIM_RES.PERMFAIL,
result_str: dkimStrings.getString("DKIM_INTERNALERROR_INCORRECT_EMAIL_FORMAT"),
}],
});
}

// ignore must be signed for outgoing messages
if (msg.DKIMSignPolicy.shouldBeSigned && isOutgoing(msgHdr)) {
Expand Down Expand Up @@ -225,6 +239,7 @@ var AuthVerifier = {
* @param {nsIMsgDBHdr} msgHdr
* @param {Object} msg
* @return {SavedAuthResult|Null}
* @throws Error
*/
// eslint-disable-next-line complexity
function getARHResult(msgHdr, msg) {
Expand Down Expand Up @@ -337,7 +352,7 @@ function getARHResult(msgHdr, msg) {
case 2: // ignore
break;
default:
throw new DKIM_InternalError("invalid error.algorithm.sign.rsa-sha1.treatAs");
throw new Error("invalid error.algorithm.sign.rsa-sha1.treatAs");
}
}
}
Expand Down Expand Up @@ -391,6 +406,7 @@ function saveAuthResult(msgHdr, savedAuthResult) {
*
* @param {nsIMsgDBHdr} msgHdr
* @return {SavedAuthResult|Null} savedAuthResult
* @throws {Error}
*/
function loadAuthResult(msgHdr) {
if (prefs.getBoolPref("saveResult")) {
Expand Down Expand Up @@ -443,8 +459,7 @@ function loadAuthResult(msgHdr) {
return savedAuthResult;
}

throw new DKIM_InternalError("AuthResult result has wrong Version (" +
savedAuthResult.version + ")");
throw new Error(`AuthResult result has wrong Version (${savedAuthResult.version})`);
}
}

Expand All @@ -456,6 +471,7 @@ function loadAuthResult(msgHdr) {
*
* @param {ARHResinfo} arhDKIM
* @return {dkimSigResultV2}
* @throws {Error}
*/
function arhDKIM_to_dkimSigResultV2(arhDKIM) {
/** @type {dkimSigResultV2} */
Expand Down Expand Up @@ -490,8 +506,7 @@ function arhDKIM_to_dkimSigResultV2(arhDKIM) {
}
break;
default:
throw new DKIM_InternalError("invalid dkim result in arh: " +
arhDKIM.result);
throw new Error(`invalid dkim result in arh: ${arhDKIM.result}`);
}

let sdid = arhDKIM.propertys.header.d;
Expand Down Expand Up @@ -547,7 +562,7 @@ function dkimResultV1_to_dkimSigResultV2(dkimResultV1) {
*
* @param {dkimSigResultV2} dkimSigResult
* @return {AuthResultDKIM}
* @throws DKIM_InternalError
* @throws {Error}
*/
function dkimSigResultV2_to_AuthResultDKIM(dkimSigResult) { // eslint-disable-line complexity
/** @type {IAuthVerifier.AuthResultDKIM} */
Expand Down Expand Up @@ -678,7 +693,7 @@ function dkimSigResultV2_to_AuthResultDKIM(dkimSigResult) { // eslint-disable-li
authResultDKIM.result_str = dkimStrings.getString("NOSIG");
break;
default:
throw new DKIM_InternalError("unknown result: " + dkimSigResult.result);
throw new Error(`unknown result: ${dkimSigResult.result}`);
}

return authResultDKIM;
Expand Down
8 changes: 5 additions & 3 deletions modules/DNSWrapper.jsm.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

// options for ESLint
/* global Components, Services, XPCOMUtils */
/* global Logging, PREF, JSDNS, libunbound, DKIM_InternalError */
/* global Logging, PREF, JSDNS, libunbound, DKIM_TempError */
/* exported EXPORTED_SYMBOLS, DNS */

"use strict";
Expand Down Expand Up @@ -86,12 +86,13 @@ var DNS = {
* @param {String} [rrtype="TXT"]
*
* @return {Promise<DNSResult>}
* @throws {DKIM_TempError|Error}
*/
resolve: async function DNS_resolve(name, rrtype="A") {

// @ts-expect-error
if (Services.netUtils.offline) {
throw new DKIM_InternalError(null, "DKIM_DNSERROR_OFFLINE");
throw new DKIM_TempError("DKIM_DNSERROR_OFFLINE");
}

switch (prefs.getIntPref("resolver")) {
Expand Down Expand Up @@ -143,6 +144,7 @@ var DNS = {
* @param {string} name
* @param {string} rrtype
* @return {Promise<DNSResult>}
* @throws {DKIM_TempError}
*/
function asyncJSDNS_QueryDNS(name, rrtype) {
function dnsCallback(dnsResult, defer, queryError, rcode) {
Expand All @@ -159,7 +161,7 @@ function asyncJSDNS_QueryDNS(name, rrtype) {
result.rcode = RCODE.NoError;
}
if (result.rcode !== RCODE.NoError && queryError) {
throw new DKIM_InternalError(queryError, "DKIM_DNSERROR_SERVER_ERROR");
throw new DKIM_TempError("DKIM_DNSERROR_SERVER_ERROR");
}
result.secure = false;
result.bogus = false;
Expand Down
11 changes: 5 additions & 6 deletions modules/MsgReader.jsm.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// options for ESLint
/* global Components */
/* global Logging */
/* global Deferred, DKIM_InternalError */
/* global Deferred, DKIM_Error */
/* exported EXPORTED_SYMBOLS, MsgReader */

"use strict";
Expand Down Expand Up @@ -50,7 +50,7 @@ var MsgReader = {
*
* @param {String} msgURI
* @return {Promise<{headerPlain: string, bodyPlain: string}>}
* @throws DKIM_InternalError
* @throws {DKIM_Error}
*/
read: function _MsgReader_read(msgURI) {
/** @type {IDeferred<{headerPlain: string, bodyPlain: string}>} */
Expand Down Expand Up @@ -98,8 +98,7 @@ var MsgReader = {
if (posEndHeader === -1) {
// in this case, the message has no body, but headers must end with a newline
if (!str.endsWith("\r\n")) {
throw new DKIM_InternalError("Message is not in correct e-mail format",
"DKIM_INTERNALERROR_INCORRECT_EMAIL_FORMAT");
throw new DKIM_Error("Message is not in correct e-mail format");
}
res.headerPlain = str;
res.bodyPlain = "";
Expand Down Expand Up @@ -138,6 +137,7 @@ var MsgReader = {
* @return {Map}
* key: {String} <header name>
* value: {Array[String]}
* @throws {DKIM_Error}
*/
parseHeader: function _MsgReader_parseHeader(headerPlain) {
var headerFields = new Map();
Expand All @@ -157,8 +157,7 @@ var MsgReader = {
}
headerFields.get(hName).push(headerArray[i]+"\r\n");
} else {
throw new DKIM_InternalError("Could not split header into name and value",
"DKIM_INTERNALERROR_INCORRECT_EMAIL_FORMAT");
throw new DKIM_Error("Could not split header into name and value");
}
}

Expand Down
4 changes: 2 additions & 2 deletions modules/SQLiteTreeView.jsm.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class SQLiteTreeView {

// test that db exists
if (!this.file.exists()) {
throw new Error("SQLite File "+path+" must exist");
throw new Error(`SQLite File ${path} must exist`);
}

this.tableName = tableName.replace(/\W/g, "");
Expand Down Expand Up @@ -346,7 +346,7 @@ class SQLiteTreeView {

// test that table exists
if (!this.conn.tableExists(this.tableName)) {
throw new Error("Table "+this.tableName+" must exist");
throw new Error(`Table ${this.tableName} must exist`);
}

if ( treeBox.columns.count !== this.columns.length) {
Expand Down
Loading