Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 109617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:05:51+00:00 2026-05-11T02:05:51+00:00

I want to make sure a file path set via query string does not

  • 0

I want to make sure a file path set via query string does not go outside of the desired subdirectory. Right now, I am checking that:

  1. The path does not start with ‘/‘, to prevent the user from giving an absolute path.
  2. The path does not contain ‘..‘, to prevent the user from giving a path that is outside of the desired subdirectory.
  3. The path does not contain ‘:‘, to prevent the use of a url (i.e. ‘http://‘, ‘ftp://‘, etc.). Should I ever run this script on a Windows server (not likely), this will also prevent absolute paths beginning with a drive specifier (i.e. ‘C:\‘). Note: I’m aware that a colon is a valid character in a Unix filenames, but I will never be using it in a filename.
  4. The path does not start with ‘\‘. Just in case I change my mind about running on a Windows server, this prevents Windows network paths from being specified (i.e. ‘\\someserver\someshare‘). Again, I’m aware that a backslash is a valid Unix filename character, but I also won’t be using it in any filenames.

Are these checks sufficient?

Background

I have a PHP script that takes (via query string) the path to a sample source file to be shown to a user. So I might give them a link like ‘view_sample.php?path=accounting_app/report_view.php‘ or ‘view_sample.php?path=ajax_demo/get_info.js‘.

The script looks basically like this:

$path = $_GET['path']; if(path_is_valid($path) && is_file('sample/$path')) {   header('Content-Type: text/plain');   readfile('sample/$path'); } 

My concern is that a malicious user would see the url and try to do something like ‘view_sample.php?path=../../database/connection_info.php‘ and gain access to a file which is not in the ‘sample’ directory.

Are the four checks I defined above (which would be implemented in the path_is_valid() function) sufficient to lock out a malicious user? (Also, I think checks 1, 3, and 4 are basically irrelevant since I am prepending a relative path, but if I didn’t do this would the checks be sufficient?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:05:52+00:00Added an answer on May 11, 2026 at 2:05 am

    Call

    $path = realpath('sample/$path'); 

    Then check that the resulting path starts with the directory you’re expecting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer From the docs: There is ambiguity in the formal syntax… May 11, 2026 at 2:09 pm
  • added an answer The way around this it seems is to use the… May 11, 2026 at 2:09 pm
  • added an answer In general, threads might be running on different CPUs/cores, with… May 11, 2026 at 2:09 pm

Related Questions

I want to make sure that a set of functions have the same signature
I want to make sure people can't type the name of a PHP script
I have an action handling a form post, but I want to make sure
I'm troubleshooting a problem with creating Vista shortcuts. I want to make sure that
In HTML, I can find a file starting from the web server's root folder
Because of strange cross domain AJAX problems, causing AJAX requests to fail when not
I am developing an app that needs to send text messages, so I have
Still 'diving in' to Python, and want to make sure I'm not overlooking something.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.