Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
fix(client) always inject localhost
Browse files Browse the repository at this point in the history
Treat the localhost name special by always injecting it, according
to RFC 6761: Section 6.3.3

fixes #50
  • Loading branch information
Tieske committed Aug 27, 2018
1 parent 83ad938 commit ae73c44
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Versioning is strictly based on [Semantic Versioning](https://semver.org/)
- Added: a new option `validTtl` that, if set, will forcefully override the
`ttl` value of any valid answer received. [Issue 48](https://github.com/Kong/lua-resty-dns-client/issues/48).
- Fix: remove multiline log entries, now encoded as single-line json. [Issue 52](https://github.com/Kong/lua-resty-dns-client/issues/52).
- Fix: always inject a `localhost` value, even if not in `/etc/hosts`. [Issue 54](https://github.com/Kong/lua-resty-dns-client/issues/54).

### 2.1.0 (21-May-2018) Fixes

Expand Down
57 changes: 57 additions & 0 deletions spec/client_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,63 @@ describe("DNS client", function()
assert.are.equal(#client.getcache(), 0) -- no hosts file record should have been imported
end)

describe("inject localhost:", function()

it("if absent", function()
local result, err, record
result, err = assert(client.init({
nameservers = { "8.8.8.8:53" },
resolvConf = {},
hosts = {},
}))
assert.is.True(result)
assert.is.Nil(err)
record = client.getcache():get("28:localhost")
assert.equal("[::1]", record[1].address)
record = client.getcache():get("1:localhost")
assert.equal("127.0.0.1", record[1].address)
end)

it("not if ipv4 exists", function()
local result, err, record
result, err = assert(client.init({
nameservers = { "8.8.8.8:53" },
resolvConf = {},
hosts = {"1.2.3.4 localhost"},
}))
assert.is.True(result)
assert.is.Nil(err)

-- IPv6 is not defined
record = client.getcache():get("28:localhost")
assert.is_nil(record)

-- IPv4 is not overwritten
record = client.getcache():get("1:localhost")
assert.equal("1.2.3.4", record[1].address)
end)

it("not if ipv6 exists", function()
local result, err, record
result, err = assert(client.init({
nameservers = { "8.8.8.8:53" },
resolvConf = {},
hosts = {"::1:2:3:4 localhost"},
}))
assert.is.True(result)
assert.is.Nil(err)

-- IPv6 is not overwritten
record = client.getcache():get("28:localhost")
assert.equal("[::1:2:3:4]", record[1].address)

-- IPv4 is not defined
record = client.getcache():get("1:localhost")
assert.is_nil(record)
end)

end)

end)


Expand Down
8 changes: 8 additions & 0 deletions src/resty/dns/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,14 @@ _M.init = function(options)
hosts = {}
end

-- treat `localhost` special, by always defining it, RFC 6761: Section 6.3.3
if not hosts.localhost then
hosts.localhost = {
ipv4 = "127.0.0.1",
ipv6 = "[::1]",
}
end

-- Populate the DNS cache with the hosts (and aliasses) from the hosts file.
local ttl = 10*365*24*60*60 -- use ttl of 10 years for hostfile entries
for name, address in pairs(hosts) do
Expand Down

0 comments on commit ae73c44

Please sign in to comment.