|
| 1 | +/** |
| 2 | + * Using the system's DNS resolver, try resolving both |
| 3 | + * allowed and blocked hostnames. |
| 4 | + * |
| 5 | + * Also test blocked hostnames for DNS cache poisoning by |
| 6 | + * trying to resolve a long, random (nonexisting) subdomain |
| 7 | + */ |
| 8 | + |
| 9 | +const { randomBytes } = require('node:crypto') |
| 10 | +const { promises: dns } = require('node:dns') |
| 11 | + |
| 12 | +const { TestGroup } = require('../template') |
| 13 | +const { FAMILY_VALUES, getCensorsString, getResultIcon } = require('../utils') |
| 14 | + |
| 15 | +class DnsTester extends TestGroup { |
| 16 | + /** |
| 17 | + * A test group to assess the system's DNS resolver |
| 18 | + */ |
| 19 | + constructor(globalConfig, globalResults) { |
| 20 | + super(globalConfig, globalResults, 'DNS') |
| 21 | + } |
| 22 | + |
| 23 | + static getTestTag() { |
| 24 | + return 'DNS' |
| 25 | + } |
| 26 | + |
| 27 | + static getPrereqs() { |
| 28 | + return ['Route'] |
| 29 | + } |
| 30 | + |
| 31 | + getDefaultResults() { |
| 32 | + return { |
| 33 | + IPv4: false, |
| 34 | + IPv6: false, |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + checkIfShouldSkip(globalResults) { |
| 39 | + /** |
| 40 | + * Skip this test if all routing tests failed |
| 41 | + */ |
| 42 | + let skip = true |
| 43 | + for (const family in FAMILY_VALUES) { |
| 44 | + if (Object.values(globalResults.Route[family]).includes(true)) { |
| 45 | + this.results[family] = {} |
| 46 | + skip = false |
| 47 | + } |
| 48 | + } |
| 49 | + if (skip) { |
| 50 | + return 'no routable networks' |
| 51 | + } |
| 52 | + return null |
| 53 | + } |
| 54 | + |
| 55 | + async startTest() { |
| 56 | + this.testPrefix = `${randomBytes(30).toString('hex')}.` |
| 57 | + console.log(`Using POISON test prefix: ${this.testPrefix}`) |
| 58 | + |
| 59 | + for (const family in FAMILY_VALUES) { |
| 60 | + if (this.results[family] === false) { |
| 61 | + continue // not routable |
| 62 | + } |
| 63 | + for (const host of this.config.allow) { |
| 64 | + this.startResolveTest(host, family, false) |
| 65 | + } |
| 66 | + for (const host of this.config.block) { |
| 67 | + this.startResolveTest(host, family, true) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + async startResolveTest(host, family, testPoison) { |
| 73 | + const testPrefs = [''] |
| 74 | + if (testPoison) { |
| 75 | + testPrefs.push(this.testPrefix) |
| 76 | + } |
| 77 | + for (const prefix of testPrefs) { |
| 78 | + this.startTestThread( |
| 79 | + DnsTester.resolveThread, |
| 80 | + [family, prefix + host], |
| 81 | + `${family}, ${host}${prefix ? ', POISON' : ''}`, |
| 82 | + this.config.timeout, |
| 83 | + ) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + logResults() { |
| 88 | + let resStr = '' |
| 89 | + for (const [family, results] of Object.entries(this.results)) { |
| 90 | + if (results === false) { |
| 91 | + continue |
| 92 | + } |
| 93 | + this.results[family] = true |
| 94 | + const allowList = this.config.allow |
| 95 | + const allowOkCnt = allowList.reduce((sum, host) => sum + (results[host] || 0), 0) |
| 96 | + const allowTotal = allowList.length |
| 97 | + let resIcon |
| 98 | + if (allowOkCnt === allowTotal) { // DNS can resolve |
| 99 | + resIcon = getResultIcon(true) |
| 100 | + } else if (allowOkCnt === 0) { // DNS can't resolve |
| 101 | + resIcon = getResultIcon(false) |
| 102 | + this.results[family] = false |
| 103 | + } else { // test inconclusive |
| 104 | + resIcon = getResultIcon(null, `resolved ${allowOkCnt}/${allowTotal}`) |
| 105 | + } |
| 106 | + resStr += `${family}: DNS ${resIcon}\n` |
| 107 | + |
| 108 | + const censors = [] |
| 109 | + const blockList = this.config.block |
| 110 | + const blockOkCnt = blockList.reduce((sum, host) => sum + (results[host] || 0), 0) |
| 111 | + const blockTotal = blockList.length |
| 112 | + if (blockOkCnt < blockTotal) { |
| 113 | + censors.push(`DNS blocking: ${blockTotal - blockOkCnt}/${blockTotal} blocked`) |
| 114 | + } |
| 115 | + |
| 116 | + const blockPoisonCnt = blockList.reduce((sum, host) => sum + (results[this.testPrefix + host] || 0), 0) |
| 117 | + if (blockPoisonCnt > 0) { |
| 118 | + censors.push(`DNS poisoning: ${blockPoisonCnt}/${blockTotal} poisoned`) |
| 119 | + } |
| 120 | + resStr += getCensorsString(censors) |
| 121 | + } |
| 122 | + return resStr |
| 123 | + } |
| 124 | + |
| 125 | + static async resolveThread(timeout, logger, results, family, host) { |
| 126 | + if (results[family] === false) { |
| 127 | + return // Not routable |
| 128 | + } |
| 129 | + results[family][host] = 0 // default to failed |
| 130 | + try { |
| 131 | + let records |
| 132 | + if (FAMILY_VALUES[family] === 4) { |
| 133 | + records = await dns.resolve4(host) |
| 134 | + } else { |
| 135 | + records = await dns.resolve6(host) |
| 136 | + } |
| 137 | + if (!timeout.isSet) { |
| 138 | + logger(`Got ${records.length} records`) |
| 139 | + results[family][host] = 1 |
| 140 | + } else { |
| 141 | + logger(`Timeout occurred for ${host}`) |
| 142 | + } |
| 143 | + } catch (error) { |
| 144 | + if (!timeout.isSet) { |
| 145 | + logger(`Failed with error: ${error.message}`) |
| 146 | + results[family][host] = 0 // explicitly set to failed |
| 147 | + } else { |
| 148 | + logger(`Timeout occurred for ${host}`) |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +function getClientTests() { |
| 155 | + return [DnsTester] |
| 156 | +} |
| 157 | + |
| 158 | +module.exports = { |
| 159 | + DnsTester, |
| 160 | + getClientTests, |
| 161 | +} |
0 commit comments