Skip to content

Commit 79bdc80

Browse files
committed
update comments and documentation, validate output from getent with regex
1 parent ad037fe commit 79bdc80

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

backend/lib/ddns_resolver/ddns_resolver.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ const ddnsResolver = {
88

99
/**
1010
* Resolves the given address to its IP
11-
* @param {String} address
12-
* @param {boolean} forceUpdate: whether to force resolution instead of using the cached value
11+
* @param {String} domainName domain name of the dynamic DNS record
12+
* @param {boolean} forceUpdate option to force resolution instead of using the cached value
1313
*/
14-
resolveAddress: (address, forceUpdate=false) => {
15-
if (!forceUpdate && ddnsResolver._cache.has(address)) {
14+
resolveAddress: (domainName, forceUpdate=false) => {
15+
if (!forceUpdate && ddnsResolver._cache.has(domainName)) {
1616
// Check if it is still valid
17-
const value = ddnsResolver._cache.get(address);
17+
const value = ddnsResolver._cache.get(domainName);
1818
const ip = value[0];
1919
const lastUpdated = value[1];
2020
const nowSeconds = Date.now();
@@ -23,39 +23,35 @@ const ddnsResolver = {
2323
return Promise.resolve(ip);
2424
}
2525
}
26-
ddnsResolver._cache.delete(address);
26+
ddnsResolver._cache.delete(domainName);
2727
// Reach here only if cache value doesn't exist or needs to be updated
28-
let host = address.toLowerCase();
28+
let host = domainName.toLowerCase();
2929
return ddnsResolver._queryHost(host)
3030
.then((resolvedIP) => {
31-
ddnsResolver._cache.set(address, [resolvedIP, Date.now()]);
31+
ddnsResolver._cache.set(domainName, [resolvedIP, Date.now()]);
3232
return resolvedIP;
3333
})
3434
.catch((/*error*/) => {
3535
// return input address in case of failure
36-
return address;
36+
logger.error(`Failed to resolve IP for ${host}`);
37+
return domainName;
3738
});
3839
},
3940

4041

41-
/** Private **/
42-
// Properties
43-
/**
44-
* cache mapping host to (ip address, last updated time)
45-
*/
42+
/** Cache mapping host to (ip address, last updated time) */
4643
_cache: new Map(),
4744

48-
// Methods
4945
/**
50-
*
51-
* @param {String} host
52-
* @returns {Promise}
46+
* Uses execSafe to query the IP address of the given host
47+
* @param {String} host host to query
48+
* @returns {Promise} resolves to the IPV4 address of the host
5349
*/
5450
_queryHost: (host) => {
5551
return utils.execSafe('getent', ['ahostsv4', 'hosts', host])
5652
.then((result) => {
57-
if (result.length < 8) {
58-
logger.error(`IP lookup for ${host} returned invalid output: ${result}`);
53+
if (result.length < 8 || !/^(\d{1,3}\.){3}\d{1,3}$/.test(result)) {
54+
logger.error(`IPV4 lookup for ${host} returned invalid output: ${result}`);
5955
throw error.ValidationError('Invalid output from getent hosts');
6056
}
6157
const out = result.split(/\s+/);

0 commit comments

Comments
 (0)