note if data is truncated

This commit is contained in:
AJ ONeal 2017-10-09 13:48:54 -06:00
parent ef3f2ae941
commit 471a80638d
4 changed files with 33 additions and 11 deletions

View File

@ -65,6 +65,11 @@ function unpackQuestionLabels(opts) {
while (true) { while (true) {
if (total >= len) { if (total >= len) {
opts.trunc = true;
console.warn('');
console.warn('[WARNING] The label was truncated by byte length of message or rdata.');
console.warn('[WARNING] Depending on the Resource Record type, that may be a parse error.');
console.warn('');
break; break;
} }
labelLen = dv.getUint8(total, false); // additional count labelLen = dv.getUint8(total, false); // additional count
@ -72,8 +77,11 @@ function unpackQuestionLabels(opts) {
if (!labelLen) { if (!labelLen) {
break; break;
} }
if (192 === labelLen) { if (labelLen >= 0xc0) {
var pointer = dv.getUint8(total, false); // (11 000000 & whatever) signifies pointer
// (00 111111 & whatever) bitmask for potentially large pointer
// (00 000001 11111111) largest realistic pointer value (512 byte message size)
var pointer = ((labelLen & 0x3f) << 8) | dv.getUint8(total, false);
console.log('Found a pointer to' console.log('Found a pointer to'
+ ' 0x' + pointer.toString(16) + ' (' + pointer + ')' + ' 0x' + pointer.toString(16) + ' (' + pointer + ')'
+ ' at byte index' + ' at byte index'

View File

@ -26,11 +26,16 @@ exports.DNS_UNPACK_LABELS = function (ui8, ptr, q) {
len = ui8[total]; len = ui8[total];
if (len === undefined){ if (len === undefined){
// RDATA is terminated by undefined, not len === 0 // RDATA is terminated by undefined, not len === 0
q.trunc = true;
break; break;
} }
// Handle message compression pointers. See 4.1.4 of RFC1035 for details. // Handle message compression pointers. See 4.1.4 of RFC1035 for details.
// 0xc0 // 192 // parseInt('11000000', 2).toString(16)
if (len >= 0xc0) { if (len >= 0xc0) {
// Only the two highest bits are used to signify the pointer.
// The remaining bits may be used to specify the address of the pointer
// (it would seem that only 1 extra bit is actually used since the message size is 512 bytes)
var cpptr = ((ui8[total] & 0x3f) << 8) | ui8[total + 1]; var cpptr = ((ui8[total] & 0x3f) << 8) | ui8[total + 1];
// We're not coming back, so if this is the first time we're following one of // We're not coming back, so if this is the first time we're following one of

View File

@ -1,6 +1,10 @@
(function (exports) { (function (exports) {
'use strict'; 'use strict';
// FORMAT:
// name ttl class rr name
// foo. 15 IN NS www.example.com.
// Comes in variable lengths. It is the name of the primary Master for the Domain. // Comes in variable lengths. It is the name of the primary Master for the Domain.
// For example 'ns1.example.com' // For example 'ns1.example.com'
// It may be a label, pointer or any combination // It may be a label, pointer or any combination
@ -8,11 +12,12 @@ var unpackLabels = exports.DNS_UNPACK_LABELS || require('../dns.unpack-labels.js
exports.DNS_PARSER_TYPE_NS = function (ab , packet, record) { exports.DNS_PARSER_TYPE_NS = function (ab , packet, record) {
record.data = unpackLabels(new Uint8Array(ab), record.rdstart, { byteLength: 0, cpcount: 0, labels: [], name: '' }).name; var labelInfo = unpackLabels(new Uint8Array(ab), record.rdstart, { byteLength: 0, cpcount: 0, labels: [], name: '' });
if (record.trunc) {
throw new Error("RDATA type NS must be `null`-terminated, not truncated by rdata length.");
}
record.data = labelInfo.name;
return record; return record;
}; };
}('undefined' !== typeof window ? window : exports)); }('undefined' !== typeof window ? window : exports));

View File

@ -6,12 +6,16 @@
// to a host name. // to a host name.
// FORMAT: // FORMAT:
// ame ttl class rr name // name ttl class rr name
// 15 IN PTR www.example.com. // foo. 15 IN PTR www.example.com.
var unpackLabels = exports.DNS_UNPACK_LABELS || require('../dns.unpack-labels.js').DNS_UNPACK_LABELS; var unpackLabels = exports.DNS_UNPACK_LABELS || require('../dns.unpack-labels.js').DNS_UNPACK_LABELS;
exports.DNS_PARSER_TYPE_PTR = function (ab, pack, record) { exports.DNS_PARSER_TYPE_PTR = function (ab, pack, record) {
record.data = unpackLabels(new Uint8Array(ab), record.rdstart, { byteLength: 0, cpcount: 0, labels: [], name: '' }).name; var labelInfo = unpackLabels(new Uint8Array(ab), record.rdstart, { byteLength: 0, cpcount: 0, labels: [], name: '' });
return record; if (record.trunc) {
throw new Error("RDATA type PTR must be `null`-terminated, not truncated by rdata length.");
}
record.data = labelInfo.name;
}; };
}('undefined' !== typeof window ? window : exports)); }('undefined' !== typeof window ? window : exports));