Skip to content
Open
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
17 changes: 17 additions & 0 deletions __tests__/test-dnssd-readfqdn-fix.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
44 changes: 44 additions & 0 deletions src/transport/ip/dnssd-readfqdn-fix.ts
Original file line number Diff line number Diff line change
@@ -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<number>();
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 {};
10 changes: 9 additions & 1 deletion src/transport/ip/http-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};
Expand Down
1 change: 1 addition & 0 deletions src/transport/ip/ip-discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading