Arbitrary File Read
Introduction
This article covers ways to secure the code from Arbitrary File Read vulnerability. This includes applying a proper function to check for the userβs input.
How to secure
In general, we recommend never allowing users to fully control the path to the local files that will be viewed. Also, always put a prefix and a suffix value in the formatted value that can be partially controlled by the users.
Since reading local files is a sensitive action in the first place, it should be protected by some kind of permission and a nonce check. We can also try to limit what files are being viewed using a whitelist or regex check and also the sanitize_file_name
function to prevent path traversal when reading local files: