I do not agree completely. TLS is extremely useful. It should be used with caution, just as global characters should be used with caution; but to say that it should not be used at all is as ridiculous as to say that global symbols should never be used.
For example, I save the current active request in TLS. This makes it accessible from my logging class, without having to pass the request through every single interface, including many that are not interested in Django at all. It allows me to write records from anywhere in the code; logger outputs to the database table, and if the request becomes active when creating the log, it logs things like the active user and what is being requested.
If you do not want one thread to be able to modify the TLS data of another thread, set TLS to disable this, which probably requires the use of its own TLS class. However, I do not consider this argument convincing; if an attacker can execute arbitrary Python code as your backend, your system is already deadly compromised - it can decapitate everything that will be launched later, like another user, for example.
Obviously, you need to clear TLS at the end of the request; in Django, this means flushing it in process_response and process_exception in the middleware class.
Glenn Maynard Jul 12 2018-10-12T00: 00Z
source share