php - $GET regarding filenames and paths (security related) -


I have created a simple download script like this in the afternoon as shown below.

Actually I pass pass file = 'whatever' and present the download to the user.

I started thinking about security later ... because anyone can pass their variable if they know the URL of the page ... I need to worry about something here is?

Can anyone have something like '.. / .. / ...... something ...... and actually my / home / < Back to the directory where they want they can download any file?

  if (isset ($ _GET ['file'])) {$ file = '/ home / test / user-data /' $ _ Session ['user'] ['ACCOUNT_ID']. ' / Download / '. $$ [' file ']; If (file_exists ($ file)) {// current TS $ now = time (); // set header header ('content-description: file transfer'); Header ('content-type: application / octet-stream'); Header ('content-dispute: attachment; filename ='. $ Now .'_file.csv '); Header ('content-transfer-encoding: binary'); Header ('expiration: 0'); Header ('cache-control: required-modified'); Header ('pragma: public'); Header ('content-length:'. File ($ file). ''); // Download readfile ($ file); // Unlink it ($ file); }}  

I am doing something on the lines of this:

< Pre> if (isset ($ _GET ['file'])) {// sanitize request and only name and extension $ file_path = $ _GET ['file']; $ Path_parts = pathinfo ($ file_path); $ File_name = $ path_parts ['basename']; $ File_ext = $ path_parts ['Extension']; // replaces the file location with a predetermined $ file_path = '/home/test/db-backups/'file_name; // Make sure the file exists (if_file ($ file_path)) {.....

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -