Skip to content

Commit

Permalink
JSDNS: don't treat connection error as rcode ServFail being returned
Browse files Browse the repository at this point in the history
  • Loading branch information
lieser committed Jun 3, 2023
1 parent d6bc8c0 commit e57dff8
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 46 deletions.
2 changes: 1 addition & 1 deletion _locales/en_US/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@
"message": "DNS server $1 timed out on a TCP connection."
},
"SERVER_ERROR": {
"message": "Error connecting to DNS server &1."
"message": "Error connecting to DNS server $1."
},
"INCOMPLETE_RESPONSE": {
"message": "Incomplete response from $1."
Expand Down
2 changes: 1 addition & 1 deletion _locales/es_ES/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@
"message": "El servidor DNS $1 agotó el tiempo de espera en una conexión TCP"
},
"SERVER_ERROR": {
"message": "Error al conectarse al servidor DNS &1"
"message": "Error al conectarse al servidor DNS $1"
},
"INCOMPLETE_RESPONSE": {
"message": "Respuesta incompleta de $1"
Expand Down
2 changes: 1 addition & 1 deletion _locales/pt_BR/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@
"message": "O servidor DNS $1 atingiu o tempo limite em uma conexão TCP."
},
"SERVER_ERROR": {
"message": "Erro ao conectar ao servidor DNS &1."
"message": "Erro ao conectar ao servidor DNS $1."
},
"INCOMPLETE_RESPONSE": {
"message": "Resposta incompleta de $1."
Expand Down
2 changes: 1 addition & 1 deletion _locales/uk/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@
"message": "Час очікування DNS-сервера $1 для TCP-з'єднання закінчився."
},
"SERVER_ERROR": {
"message": "Помилка підключення до DNS-сервера &1."
"message": "Помилка підключення до DNS-сервера $1."
},
"INCOMPLETE_RESPONSE": {
"message": "Неповна відповідь від $1."
Expand Down
2 changes: 1 addition & 1 deletion experiments/JSDNS.jsm.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ function DNS_getRDData(str, server, host, recordtype, callback, callbackdata, ho
var rcode = flags & 0xF;
if (rcode !== 0) {
log.debug(debugstr + "Lookup failed with rcode " + rcode);
callback(null, callbackdata, "Lookup failed with rcode " + rcode, rcode);
callback(null, callbackdata, undefined, rcode);
return;
}

Expand Down
4 changes: 3 additions & 1 deletion experiments/jsdns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ declare module browser {
debug: boolean,
) => Promise<void>;

type TxtResult = import("../modules/dns.mjs.js").DnsTxtResult;
type TxtResult = import("../modules/dns.mjs.js").DnsTxtResult | {
error: string,
}
const txt: (name: string) => Promise<TxtResult>;
}
}
28 changes: 7 additions & 21 deletions experiments/jsdns.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,6 @@
// eslint-disable-next-line no-var
var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

/**
* From https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/.
*
* @param {any} obj
* @returns {string}
*/
function toType(obj) {
const typeMatch = Object.prototype.toString.call(obj).match(/\s([a-zA-Z]+)/);
if (!typeMatch || !typeMatch[1]) {
throw new Error(`Failed to get type for ${obj}`);
}
return typeMatch[1];
}

this.jsdns = class extends ExtensionCommon.ExtensionAPI {
/**
* @param {ExtensionCommon.Extension} extension
Expand Down Expand Up @@ -86,18 +72,18 @@ this.jsdns = class extends ExtensionCommon.ExtensionAPI {
if (rcode !== undefined) {
resRcode = rcode;
} else if (queryError !== undefined) {
/** @type {string|string[]} */
let error = "";
if (typeof queryError === "string") {
error = context.extension.localeData.localizeMessage(queryError);
} else if (toType(queryError) === "Array" && queryError[0]) {
error = context.extension.localeData.localizeMessage(queryError[0], queryError[1]);
}
if (!error) {
error = queryError;
} else {
error = context.extension.localeData.localizeMessage(queryError[0] ?? "DKIM_DNSERROR_UNKNOWN", queryError[1]) ||
(queryError[0] ?? "Unknown DNS error");
}
console.warn(`JSDNS failed with: ${error}`);
resRcode = RCODE.ServFail;
defer.resolve({
error,
});
return;
}

const results = dnsResult && dnsResult.map(rdata => {
Expand Down
17 changes: 0 additions & 17 deletions experiments/libunbound.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,14 +374,6 @@ this.libunbound = class extends ExtensionCommon.ExtensionAPI {
* @returns {{libunbound: browser.libunbound}}
*/
getAPI(_context) {
const RCODE = {
NoError: 0, // No Error [RFC1035]
FormErr: 1, // Format Error [RFC1035]
ServFail: 2, // Server Failure [RFC1035]
NXDomain: 3, // Non-Existent Domain [RFC1035]
NotImp: 4, // Non-Existent Domain [RFC1035]
Refused: 5, // Query Refused [RFC1035]
};
const libunboundWorker = this.libunboundWorker;
return {
libunbound: {
Expand All @@ -401,15 +393,6 @@ this.libunbound = class extends ExtensionCommon.ExtensionAPI {
},
async txt(name) {
const res = await libunboundWorker.resolve(name, LibunboundWorker.Constants.RR_TYPE_TXT);
if (res === null) {
// error in libunbound
return {
data: null,
rcode: RCODE.ServFail,
secure: false,
bogus: false,
};
}
const data = res.havedata ? res.data.map(rdata => {
if (typeof rdata !== "string") {
throw Error(`DNS result has unexpected type ${typeof rdata}`);
Expand Down
9 changes: 7 additions & 2 deletions modules/dns.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function configureLibunbound() {
/**
* Check that Thunderbird is online.
*
* @throws {DKIM_SigError} if Thunderbird is offline.
* @throws {DKIM_InternalError} if Thunderbird is offline.
*/
function checkOnlineStatus() {
if (!navigator.onLine) {
Expand All @@ -157,13 +157,18 @@ export default class DNS {
*
* @param {string} name
* @returns {Promise<DnsTxtResult>}
* @throws {DKIM_InternalError} if no DNS response could be retrieved.
*/
static async txt(name) {
switch (prefs["dns.resolver"]) {
case RESOLVER_JSDNS: {
await configureJsdns();
checkOnlineStatus();
return browser.jsdns.txt(name);
const res = await browser.jsdns.txt(name);
if ("error" in res) {
throw new DKIM_InternalError(res.error, "DKIM_DNSERROR_SERVER_ERROR");
}
return res;
}
case RESOLVER_LIBUNBOUND: {
await configureLibunbound();
Expand Down

0 comments on commit e57dff8

Please sign in to comment.