Skip to content

Commit

Permalink
feat: add support for tld targets in dns tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Mar 29, 2024
1 parent b5afcf8 commit 3428e91
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/measurement/schema/command-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
} from '../../lib/malware/client.js';
import {
joiValidateDomain,
joiValidateDomainForDns,
joiValidateTarget,
whenTypeApply,
globalIpOptions,
Expand Down Expand Up @@ -83,7 +84,7 @@ const allowedDnsTypes = [ 'A', 'AAAA', 'ANY', 'CNAME', 'DNSKEY', 'DS', 'HTTPS',
const allowedDnsProtocols = [ 'UDP', 'TCP' ];

// Dns
const dnsDefaultTargetSchema = Joi.custom(joiValidateDomain()).custom(joiValidateTarget('domain')).required().messages(schemaErrorMessages);
const dnsDefaultTargetSchema = Joi.custom(joiValidateDomainForDns()).custom(joiValidateTarget('domain')).required().messages(schemaErrorMessages);
const dnsPtrTargetSchema = Joi.string().ip(globalIpOptions).custom(joiValidateTarget('ip')).required().messages(schemaErrorMessages);
const dnsTargetSchema = Joi.when(Joi.ref('..measurementOptions.query.type'), { is: Joi.string().insensitive().valid('PTR').required(), then: dnsPtrTargetSchema, otherwise: dnsDefaultTargetSchema });

Expand Down
20 changes: 20 additions & 0 deletions src/measurement/schema/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ export const joiValidateDomain = () => (value: string, helpers: CustomHelpers):
return value;
};

export const joiValidateDomainForDns = () => (value: string, helpers: CustomHelpers): string | ErrorReport => {
const options = {
allow_underscores: true,
};

if (value === '.') {
return value;
}

if (validator.isFQDN(value, options)) {
return value;
}

if (validator.isFQDN('example' + value, options)) {
return value.substring(1);
}

return helpers.error('domain.invalid');
};

export const joiValidateTarget = (type: string) => (value: string, helpers?: CustomHelpers): string | ErrorReport | Error => {
if ([ 'ip', 'any' ].includes(type) && isIpPrivate(value)) {
if (helpers) {
Expand Down

0 comments on commit 3428e91

Please sign in to comment.