Skip to content

Commit

Permalink
fix: error handling for domainToASCII (#1608)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSun90 authored Aug 7, 2024
1 parent afd3b54 commit af21197
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ export async function lookupAllAddresses(host: string, lookup: LookupFunction, s

signal.addEventListener('abort', onAbort);

lookup(url.domainToASCII(host), { all: true }, (err, addresses) => {
const domainInASCII = url.domainToASCII(host);
lookup(domainInASCII === '' ? host : domainInASCII, { all: true }, (err, addresses) => {
signal.removeEventListener('abort', onAbort);

err ? reject(err) : resolve(addresses);
Expand Down
5 changes: 2 additions & 3 deletions src/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ export async function sendMessage(host: string, port: number, lookup: LookupFunc
reject(new AbortError());
};

signal.addEventListener('abort', onAbort);

lookup(url.domainToASCII(host), { all: true }, (err, addresses) => {
const domainInASCII = url.domainToASCII(host);
lookup(domainInASCII === '' ? host : domainInASCII, { all: true }, (err, addresses) => {
signal.removeEventListener('abort', onAbort);

err ? reject(err) : resolve(addresses);
Expand Down
19 changes: 17 additions & 2 deletions test/unit/connector-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Mitm = require('mitm');
const sinon = require('sinon');
const url = require('node:url');
const dns = require('dns');
const assert = require('chai').assert;

const {
Expand All @@ -25,7 +25,7 @@ describe('lookupAllAddresses', function() {
}

assert.isOk(lookup.called, 'Failed to call `lookup` function for hostname');
assert.isOk(lookup.calledWithMatch(url.domainToASCII(server)), 'Unexpected hostname passed to `lookup`');
assert.isOk(lookup.calledWithMatch('xn--tiq21tzznxb.ad'), 'Unexpected hostname passed to `lookup`');
});

it('test ASCII Server name', async function() {
Expand All @@ -45,6 +45,21 @@ describe('lookupAllAddresses', function() {
assert.isOk(lookup.called, 'Failed to call `lookup` function for hostname');
assert.isOk(lookup.calledWithMatch(server), 'Unexpected hostname passed to `lookup`');
});

it('test invalid ASCII Server name', async function() {
const server = 'http:wrong';
const controller = new AbortController();

let actualError;
try {
await lookupAllAddresses(server, dns.lookup, controller.signal);
} catch (err) {
actualError = err;
}

assert.instanceOf(actualError, Error);
assert.strictEqual(actualError.message, 'getaddrinfo ENOTFOUND http:wrong');
});
});

describe('connectInSequence', function() {
Expand Down

0 comments on commit af21197

Please sign in to comment.