LLT-7558: Domain accessible after being removed from whitelist - #1931
LLT-7558: Domain accessible after being removed from whitelist#1931tomaszpatejko wants to merge 3 commits into
Conversation
e2bf5c3 to
67bc7b1
Compare
104cba8 to
60cccb8
Compare
60cccb8 to
df9c55a
Compare
df9c55a to
81b3f53
Compare
When whitelist changes, resolver's DNS cache should be flushed. This way when domain is reblocked, the DNS query for its IP address should not be handled from resolvers cache, but from tp-lite DNS server and result in NXDOMAIN.
The test, at its initial step, whitelists a domain and checks query result of the natlab tp-lite DNS server. The query response should contain a resolved IP address of the domain that is cached in resolver cache. Then the domain is removed for the whitelist. Subsequent query to the natlab tp-lite DNS server should result in NXDOMAIN.
8c5504f to
c8ac042
Compare
|
You can squash |
| /// but a cached answer would be returned without the query ever reaching | ||
| /// the firewall. | ||
| pub fn flush_cache(&self) { | ||
| self.resolver.clear_cache(); |
There was a problem hiding this comment.
Does this blocks until all requests in-flight are finished, and only than clears the cache?
There was a problem hiding this comment.
If I understand the code correctly, TrustDNS' resolver uses sync::Cache from moka-rs crate for its cache implementation, and the entire cache invalidation is delegated to that crate.
Moka-rs' sync::Cache::invalidate_all method does not remove any entries and just records current time as invalidation time for all the entries in the cache. Check the comment here: https://github.com/moka-rs/moka/blob/a616ec19e8d4ed938caf8b2c88090331d778d5da/src/sync/cache.rs#L1658
Actual removal of invalidated entries is delegated to Housekeeper that is triggered by get/insert methods. Housekeeper does not block reads but it blocks writes.
There was a problem hiding this comment.
But as I see it, it still can cause problems with requests-in-flight. Before flushing the cache, we should check, if there are some and either:
- Wait for them to finish and put to cache.
- Cancel them, for them not to end in cache.
There was a problem hiding this comment.
The only situation, where we can avoid this problem with request-in-flight is that cache time entry (used to invalidate entries) is actually request_started and NOT request_put_to_cache
There was a problem hiding this comment.
I think I don't understand the issue you are trying to point out here. Could you elaborate a bit more?
| ) -> bool { | ||
| let domains_changed = { | ||
| let mut configured_state = self.configured_state.write(); | ||
| let changed = configured_state.tp_lite_whitelisted_domains != domains; |
There was a problem hiding this comment.
Shouldn't we compare the redirects as well?
There was a problem hiding this comment.
I don't think redirects should be compared in this situation.
If I understand the comment for set_tp_lite_domain_whitelist, redirects contains pairs of DNS servers used for blocking or passing through a domain. So the change in this list does not drive the decision whether resolver cache should be flushed.
There was a problem hiding this comment.
My idea was to check both - the domains and redirects:
let changed_domains = configured_state.tp_lite_whitelisted_domains != domains;
let changed_redirects= configured_state.tp_lite_dns_redirects != redirects;
...
(changed_domains || changed_redirects)
redirects can influence DNAT usage, so my idea was to compare them as well.
There was a problem hiding this comment.
I think we can get rid of this discussion by flashing on every call to set_tp_lite_domain_whitelist. Realistically it's only called when: a) user added/or removed something from whitelist, b) user enabled or disabled tp. In both cases we want to flush the cache. So I don't think it's worth optimizing for some hard to imagine scenario.
Problem
Domain is still accessible after it has been removed from whitelist.
The following use case trigger the bug:
Solution
Flush DNS resolver's cache when whitelist is set. This was no stale cache entries will be present and correct TP-Lite DNS server with be consulted for blocked domain's IP address.
AI disclosure: Claude was used for the following:
The work was also human-reviewed.
☑️ Definition of Done checklist