Squid: Update script for blacklists of the squid proxy buggy
I encountered problems while updating or adjusting the categories of the proxy blacklists. Currently, several categories of the following blacklists are active:
The python script for updating the above lists consumes 100% of cpu for a very long time (more than 30 minutes) which induced me to do some more investigation. I identified the following issues:
- The update process is not transactional. Multiple instances of the update script can be launched the same time which results in conflicts
- During the update process blacklists (files) are rebuilt while in use by squid :-(. I do not know whether this affects the running squid instance with its open file descriptors. But, interruption of the update process (e.g. restarting the opnsense) leaves the blacklists in an inconsistent state which prevents restarting the squid proxy (see following error message).
- Update script contains several off-by-one errors in comparison instructions (e.g. _if (len(self._url) > 8 and self.url[-7:] == '.tar.gz')
Nov 8 14:11:09 opnsense-host squid[37972]: FATAL: Bungled /usr/local/etc/squid/squid.conf line 32: acl remoteblacklist_UT2 dstdomain "/usr/local/etc/squid/acl/UT2"
- Opnsense 20.7.4 (and probably previous versions)
- Expected behaviour is a rapid processing of the whole update process in a transactional manner, which does not break squid cnfiguration.
- see also related topic in the Opnsense Forum
The locking we can fix (https://github.com/opnsense/core/commit/8487b23a227f5b6c06f8d6f7516102cd4e252891), the time it takes to generate an internal acl we can't (it needs to be cleansed and properly sorted in order for squid to accept it). Commercial solutions (like the one in our business edition or Sensei) often use different approaches to detach acl actions from the proxy itself.
I've spend quite some time in the past optimising this script, which doesn't mean there won't be bugs. The line in question could be simplified (str.endswith('')), but will still lead to the same result.
I am wondering that this ticket has been immediately closed without further review. The new introduced flock solves the first issue but not the others. Especially the third one is a bug which needs to be fixed in case code quality is a hard requirement.
I know that all FQDNs need to be checked for correctness, but I think sorting the generated lists is not neccessary because squid uses binary search trees for this data structure. I did some tests and added some random domains to the end of the generated lists followed by a "squid -k reconfigure" to force squid to re-read its config files. After this has been done the newly added domains will be blocked.
Additionally, I found new bug. In the generated lists all FQDNs begin with a dot (.) which means to block a FQDN and all of its subdomains. The latter is probably wrong and not the intention of the blacklists (e.g. shallalist).
@somova better open a new ticket for other issues in that case. I'm pretty sure the sorting isn't there for no reason, but if someone wants to work on it and propose improvements, I don't mind.
@AdSchellevis Thanks for your repsonse. Let me get this right. Do you like to split all above bullet points up into distinct tickets or do like to separate these from the first one which has been solved with an additional file lock. In my eyes the first approach does not make sense but the latter looks ok for me.