The generic solution is to URL-encode strings before inclusion into HTTP headers such as Location or Set-Cookie.
Typical examples of sanitization include casting to integers or aggressive regular expression replacement. Most modern server-side scripting languages and runtimes, like PHP since version 5.1.21 and Node.js since 4.6.0 (previous versions supported it, but the protection could've been bypassed, which was discovered in 2016)2 as well as Web frameworks, such as Django since version 1.8.43 support sanitization of HTTP responses against this type of vulnerability.
"PHP: PHP 5.1.2. Release Announcement". The PHP Group. Retrieved 2014-11-13. //php.net/releases/5_1_2.php ↩
"CVE-2016-5325 | Snyk Vulnerability Database". Learn more about debian:9 with Snyk Open Source Vulnerability Database. Retrieved 2024-01-16. https://security.snyk.io/ ↩
"CVE-2015-5144 | Snyk Vulnerability Database". Learn more about pip with Snyk Open Source Vulnerability Database. Retrieved 2024-01-16. https://security.snyk.io/ ↩