Skip to content

Commit

Permalink
dns/resource: add error messages
Browse files Browse the repository at this point in the history
These new error messages will help users
more easily craft Resources. The error messages
will be returned via the `validateresource` RPC.
  • Loading branch information
tynes committed Apr 16, 2020
1 parent 28fc645 commit 7bbfaa4
Showing 1 changed file with 56 additions and 32 deletions.
88 changes: 56 additions & 32 deletions lib/dns/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,11 @@ class Resource extends Struct {
}

fromJSON(json) {
assert(json && typeof json === 'object');
assert(Array.isArray(json.records));
assert(json && typeof json === 'object', 'Invalid json.');
assert(Array.isArray(json.records), 'Invalid records.');

for (const item of json.records) {
assert(item && typeof item === 'object');
assert(item && typeof item === 'object', 'Invalid record.');

const RD = stringToClass(item.type);

Expand Down Expand Up @@ -437,13 +437,19 @@ class DS extends Struct {
}

fromJSON(json) {
assert(json && typeof json === 'object');
assert(json.type === 'DS');
assert((json.keyTag & 0xffff) === json.keyTag);
assert((json.algorithm & 0xff) === json.algorithm);
assert((json.digestType & 0xff) === json.digestType);
assert(typeof json.digest === 'string');
assert((json.digest.length >>> 1) <= 255);
assert(json && typeof json === 'object', 'Invalid DS record.');
assert(json.type === 'DS',
'Invalid DS record. Property "type" must be "DS".');
assert((json.keyTag & 0xffff) === json.keyTag,
'Invalid DS record. Property "keyTag" must be a u16.');
assert((json.algorithm & 0xff) === json.algorithm,
'Invalid DS record. Property "algorithm" must be a u8.');
assert((json.digestType & 0xff) === json.digestType,
'Invalid DS record. Property "digestType" must be a u8.');
assert(typeof json.digest === 'string',
'Invalid DS record. Propery "digest" must be a String.');
assert((json.digest.length >>> 1) <= 255,
'Invalid DS record. Property "digest" is too large.');

this.keyTag = json.keyTag;
this.algorithm = json.algorithm;
Expand Down Expand Up @@ -495,9 +501,12 @@ class NS extends Struct {
}

fromJSON(json) {
assert(json && typeof json === 'object');
assert(json.type === 'NS');
assert(isName(json.ns));
assert(json && typeof json === 'object',
'Invalid NS record.');
assert(json.type === 'NS',
'Invalid NS record. Property "type" must be "NS".');
assert(isName(json.ns),
'Invalid NS record. Property "ns" must be a valid name.');

this.ns = json.ns;

Expand Down Expand Up @@ -554,10 +563,13 @@ class GLUE4 extends Struct {
}

fromJSON(json) {
assert(json && typeof json === 'object');
assert(json.type === 'GLUE4');
assert(isName(json.ns));
assert(IP.isIPv4String(json.address));
assert(json && typeof json === 'object', 'Invalid GLUE4 record.');
assert(json.type === 'GLUE4',
'Invalid GLUE4 record. Property "type" must be "GLUE4".');
assert(isName(json.ns),
'Invalid GLUE4 record. Property "ns" must be valid name.');
assert(IP.isIPv4String(json.address),
'Invalid GLUE4 record. Property "address" must be a IPv4 address.');

this.ns = json.ns;
this.address = IP.normalize(json.address);
Expand Down Expand Up @@ -615,10 +627,13 @@ class GLUE6 extends Struct {
}

fromJSON(json) {
assert(json && typeof json === 'object');
assert(json.type === 'GLUE6');
assert(isName(json.ns));
assert(IP.isIPv6String(json.address));
assert(json && typeof json === 'object', 'Invalid GLUE6 record.');
assert(json.type === 'GLUE6',
'Invalid GLUE6 record. Property "type" must be "GLUE6".');
assert(isName(json.ns),
'Invalid GLUE6 record. Property "ns" must be valid name.');
assert(IP.isIPv6String(json.address),
'Invalid GLUE6 record. Property "address" must be a IPv6 address.');

this.ns = json.ns;
this.address = IP.normalize(json.address);
Expand Down Expand Up @@ -677,9 +692,11 @@ class SYNTH4 extends Struct {
}

fromJSON(json) {
assert(json && typeof json === 'object');
assert(json.type === 'SYNTH4');
assert(IP.isIPv4String(json.address));
assert(json && typeof json === 'object', 'Invalid SYNTH4 record.');
assert(json.type === 'SYNTH4',
'Invalid SYNTH4 record. Property "type" must be "SYNTH4".');
assert(IP.isIPv4String(json.address),
'Invalid SYNTH4 record. Property "address" must be a IPv4 address.');

this.address = IP.normalize(json.address);

Expand Down Expand Up @@ -737,9 +754,11 @@ class SYNTH6 extends Struct {
}

fromJSON(json) {
assert(json && typeof json === 'object');
assert(json.type === 'SYNTH6');
assert(IP.isIPv6String(json.address));
assert(json && typeof json === 'object', 'Invalid SYNTH6 record.');
assert(json.type === 'SYNTH6',
'Invalid SYNTH6 record. Property "type" must be "SYNTH6".');
assert(IP.isIPv6String(json.address),
'Invalid SYNTH6 record. Property "address" must be a IPv6 address.');

this.address = IP.normalize(json.address);

Expand Down Expand Up @@ -812,13 +831,18 @@ class TXT extends Struct {
}

fromJSON(json) {
assert(json && typeof json === 'object');
assert(json.type === 'TXT');
assert(Array.isArray(json.txt));
assert(json && typeof json === 'object',
'Invalid TXT record.');
assert(json.type === 'TXT',
'Invalid TXT record. Property "type" must be "TXT".');
assert(Array.isArray(json.txt),
'Invalid TXT record. Property "txt" must be an Array.');

for (const txt of json.txt) {
assert(typeof txt === 'string');
assert(txt.length <= 255);
assert(typeof txt === 'string',
'Invalid TXT record. Entries in "txt" Array must be strings.');
assert(txt.length <= 255,
'Invalid TXT record. Entries in "txt" Array must be <= 255 in length.');

this.txt.push(txt);
}
Expand Down

0 comments on commit 7bbfaa4

Please sign in to comment.