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

Trailing dot in name gets an unfilled byte #62

Open
thenewwazoo opened this issue Dec 27, 2020 · 2 comments
Open

Trailing dot in name gets an unfilled byte #62

thenewwazoo opened this issue Dec 27, 2020 · 2 comments

Comments

@thenewwazoo
Copy link

thenewwazoo commented Dec 27, 2020

When a question's name field has a trailing dot (e.g. 1.0.0.127.in-addr.arpa.), the length is computed before the trailing dot is dropped. This results in a buffer that's one byte longer than necessary, and which has a trailing byte that goes unfilled. For example,

        var reversed = '151.1.168.192.in-addr.arpa.';
        resolver.query({
            flags: 1 << 8 | 1 << 5,
            id: getRandomInt(1, 65534),
            questions: [
                {
                    name: reversed,
                    type: 'PTR',
                    class: 'IN'
                },
            ],
            additionals: [
                {
                    name: '.',
                    type: 'OPT',
                    udpPayloadSize: 0x1000,
                },
            ],
        });

results in a buffer len of 56, but only 55 bytes are written.

The relevant code is here:

name.encode = function (str, buf, offset) {
  if (!buf) buf = Buffer.allocUnsafe(name.encodingLength(str)) // <-- allocate based on str len
  if (!offset) offset = 0
  const oldOffset = offset

  // strip leading and trailing .
  const n = str.replace(/^\.|\.$/gm, '') // <-- mutate str
  if (n.length) {
    const list = n.split('.') // <-- loses the last byte

This was discovered while diagnosing #52 (also trying to diagnose mafintosh/multicast-dns#13).

As a workaround, manually dropping the trailing dot in constructing the address seems to work (but not to fix my issue, oh well.)

@mafintosh
Copy link
Owner

Ah, I see. Good find. Wanna PR a fix?

@thenewwazoo
Copy link
Author

I’ve gotten started but the encodeLength method is called in several places and I need to be confident I’m not going to break anything and that the tests are correct (plus it’s slow going since I’m not very familiar with JS :) ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants