diff --git a/__tests__/test-dnssd-readfqdn-fix.js b/__tests__/test-dnssd-readfqdn-fix.js new file mode 100644 index 0000000..ccadef2 --- /dev/null +++ b/__tests__/test-dnssd-readfqdn-fix.js @@ -0,0 +1,17 @@ +require('../lib/transport/ip/dnssd-readfqdn-fix'); +const BufferWrapper = require('dnssd/lib/BufferWrapper'); + +describe('dnssd readFQDN fix', () => { + it('does not loop on a self-referencing compression pointer', () => { + expect(new BufferWrapper(Buffer.from([0xc0, 0x00])).readFQDN()).toBe('.'); + }); + + it('still follows valid compression pointers', () => { + // "a." at offset 0, then label "b" + pointer to offset 0 -> "b.a." + const buf = Buffer.from([0x01, 0x61, 0x00, 0x01, 0x62, 0xc0, 0x00]); + const w = new BufferWrapper(buf); + expect(w.readFQDN()).toBe('a.'); + expect(w.readFQDN()).toBe('b.a.'); + expect(w.position).toBe(7); + }); +}); diff --git a/src/transport/ip/dnssd-readfqdn-fix.ts b/src/transport/ip/dnssd-readfqdn-fix.ts new file mode 100644 index 0000000..f8d4fa7 --- /dev/null +++ b/src/transport/ip/dnssd-readfqdn-fix.ts @@ -0,0 +1,44 @@ +/** + * Runtime fix for dnssd 0.4.1 (unmaintained): BufferWrapper.readFQDN follows DNS + * compression pointers without a cycle guard. A self-referencing or cyclic pointer + * in an incoming mDNS packet makes it loop forever at 100% CPU and blocks the + * event loop. This replaces readFQDN with the same logic plus a visited-pointer + * set and the RFC 1035 label limit (127). + */ + +/* eslint-disable @typescript-eslint/no-var-requires */ +const BufferWrapper = require('dnssd/lib/BufferWrapper'); + +BufferWrapper.prototype.readFQDN = function readFQDN(): string { + const labels: string[] = []; + const seenPointers = new Set(); + let len: number; + let farthest: number | undefined; + + while (this.remaining() >= 0 && (len = this.readUInt8())) { + if (len < 192) { + labels.push(this.readString(len)); + if (labels.length > 127) { + break; + } + } else { + const position = (len << 8) + this.readUInt8() - 0xc000; + if (!farthest) { + farthest = this.position; + } + if (seenPointers.has(position)) { + break; + } + seenPointers.add(position); + this.seek(position); + } + } + + if (farthest) { + this.seek(farthest); + } + + return `${labels.join('.')}.`; +}; + +export {}; diff --git a/src/transport/ip/http-connection.ts b/src/transport/ip/http-connection.ts index 53fd4d9..03d9fc0 100644 --- a/src/transport/ip/http-connection.ts +++ b/src/transport/ip/http-connection.ts @@ -360,7 +360,15 @@ export default class HttpConnection extends EventEmitter { ++this.a2cCounter; parser.execute(decryptedData); } catch (e) { - // pass + // Decryption failed: the stream is desynchronised (e.g. nonce mismatch after an + // unclean disconnect). `message` is only advanced on success, so swallowing the + // error spins this loop forever (100% CPU). Close the socket and reject so the + // caller fails fast and reconnects. On the event-reader path the promise is + // already resolved, so reject() is a no-op there. + debug(`${this.address}:${this.port} Decrypting message failed, closing connection`); + this.socket?.removeListener('data', bodyParser); + this.close(); + return reject(e); } } }; diff --git a/src/transport/ip/ip-discovery.ts b/src/transport/ip/ip-discovery.ts index 7caafae..3a50c7e 100644 --- a/src/transport/ip/ip-discovery.ts +++ b/src/transport/ip/ip-discovery.ts @@ -5,6 +5,7 @@ import { EventEmitter } from 'events'; import { Browser, Options, Service, ServiceType } from 'dnssd'; import { PairMethods } from '../../protocol/pairing-protocol'; +import './dnssd-readfqdn-fix'; /** * Service data structure returned by discovery methods