[1.7.x] Tweaked fix for refs #23946; thanks Claude.

Backport of 5c773447c3239a297b22929f0df15d2971d72e77 from master
[1.7.x] Added some directories on Jenkins to flake8 exclude.

Backport of 53908c1f934b32e83e9fa34c14910b1837f7f610 from master
[1.7.x] Fixed #23939 -- Moved session verification out of middleware.

Thanks andrewbadr for the report.