csrf-fix: Improve parsing, corner cases

(cherry picked from commit 5818b8ea40a1fd40c3bc976775c42d1ff4ab9809)