10. Prevention

Preventing file inclusion: 

Whitelist user input.

Preventing directory traversal: IF attackers control directory they can escape the webapp and attack something they are more familiar with or use a universal attack chain

Directory traversal allows attackers to do some example attacks:

Best way : use program lang or framework builtin tool to pull only filename. PHP for example has basename()

if implementing this yourself may run into a weird edgecase:

run the command cat .?/.*/.?/etc/passwd. You'll see Bash allows for the ? and * wildcards to be used as a .. Now type php -a to enter the PHP Command Line interpreter and run echo file_get_contents('.?/.*/.?/etc/passwd');. You'll see PHP does not have the same behaviour with the wildcards, if you replace ? and * with ., the command will work as expected.

assume we let php execute bash with system() function the attacker would be able to bypass directory traversal prevention if we use native functions to the framework we are using there's a chance other users would catch edge cases like thsi and fix it before it gets exploited in our webapp

sanitize user input to recursively remove any attempts of traversing directories:

while(substr_count($input, '../', 0)) {
    $input = str_replace('../', '', $input);
};

lfi / path traversal prevention web server configurations

php, examples

DOCKER usage is common to lock webapps to their webroot dir - preventing non-web-related files from being accessed

Ensure potentially dangerous modules are disabled like

WAF is a universal way to harden applications

important thing to avoid is false positives and blocking non-malicious requests ModSec minimizes false positives by offering a permissive mode which will only report incidences that would have been blocked, allowing defenders to tune rules and make sure no legit req is blocked.

Hardening =/= unhackable. Giving you a fighting chance at detecting attacks when they occur.