mafintosh / dns-packet

An abstract-encoding compliant module for encoding / decoding DNS packets
MIT License
205 stars 71 forks source link

ERR_OUT_OF_RANGE #57

Closed camhart closed 4 years ago

camhart commented 4 years ago

I get this whenever I attempt to use Google's DoH endpoint. I don't get it from cloudflare. Any ideas?

internal/buffer.js:77
  throw new ERR_OUT_OF_RANGE(type || 'offset',
  ^

RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range. It must be >= 0 and <= 785. Received 858
    at boundsError (internal/buffer.js:77:9)
    at Buffer.readUInt16BE (internal/buffer.js:323:5)
    at Object.question.decode (C:\Users\Cam\projects\playground\base64url\node_modules\dns-packet\index.js:1417:31)
    at decodeList (C:\Users\Cam\projects\playground\base64url\node_modules\dns-packet\index.js:1537:19)
    at Object.exports.decode (C:\Users\Cam\projects\playground\base64url\node_modules\dns-packet\index.js:1477:12)
    at IncomingMessage.<anonymous> (C:\Users\Cam\projects\playground\base64url\node_modules\@sagi.io\dns-over-https\index.js:78:31)
    at IncomingMessage.emit (events.js:321:20)
    at IncomingMessage.Readable.read (_stream_readable.js:508:10)
    at flow (_stream_readable.js:979:34)
    at resume_ (_stream_readable.js:960:3) {
  code: 'ERR_OUT_OF_RANGE'
}
pusateri commented 4 years ago

The doh example was written during the time that DOH was experimental and before RFC 8484 was finalized. It needs updated with the standard URLs. This patch makes it work. I'll submit a pull request.

diff --git a/examples/doh.js b/examples/doh.js
index 37ef19f..b9a6b91 100644
--- a/examples/doh.js
+++ b/examples/doh.js
@@ -26,12 +26,12 @@ const buf = dnsPacket.encode({
 })

 const options = {
-  hostname: 'dns.google.com',
+  hostname: 'dns.google',
   port: 443,
-  path: '/experimental',
+  path: '/dns-query',
   method: 'POST',
   headers: {
-    'Content-Type': 'application/dns-udpwireformat',
+    'Content-Type': 'application/dns-message',
     'Content-Length': Buffer.byteLength(buf)
   }
camhart commented 4 years ago

The error I posted is from decoding a response from Google dns--not encoding one to be sent.

pusateri commented 4 years ago

Send sample code if you want someone to look at it where we can reproduce it. This doesn’t sound DoH related anymore.

camhart commented 4 years ago

const doh = require('@sagi.io/dns-over-https')

async function run() { let r1 = await doh.query({ name: 'google.com', method: 'GET', hostname: 'dns.google.com', path: '/dns-query', port: 443, userAgent: '@sagi.io/dns-over-https', type: 'A', klass: 'IN', useHttps: true, }).catch(err => { console.log(err)})

console.log(rl) }

run()

That explodes with:

C:\Users\Cam\projects\playground\base64url>node bug.js
internal/buffer.js:77
  throw new ERR_OUT_OF_RANGE(type || 'offset',
  ^

RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range. It must be >= 0 and <= 785. Received 858
    at boundsError (internal/buffer.js:77:9)
    at Buffer.readUInt16BE (internal/buffer.js:323:5)
    at Object.question.decode (C:\Users\Cam\projects\playground\base64url\node_modules\dns-packet\index.js:1417:31)
    at decodeList (C:\Users\Cam\projects\playground\base64url\node_modules\dns-packet\index.js:1537:19)
    at Object.exports.decode (C:\Users\Cam\projects\playground\base64url\node_modules\dns-packet\index.js:1477:12)
    at IncomingMessage.<anonymous> (C:\Users\Cam\projects\playground\base64url\node_modules\@sagi.io\dns-over-https\index.js:78:31)
    at IncomingMessage.emit (events.js:321:20)
    at IncomingMessage.Readable.read (_stream_readable.js:508:10)
    at flow (_stream_readable.js:979:34)
    at resume_ (_stream_readable.js:960:3) {
  code: 'ERR_OUT_OF_RANGE'
}
camhart commented 4 years ago

Just realized it must be a bug with how @sagi.io/dns-over-https is using it. Opened a bug on their repo (https://github.com/sagi/dns-over-https-node/issues/2).