473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

include security -what works, what dont?

please comment on the following methods of preventing cross site
scripting and/or other nastiness:

1:
$pages = array('home','contact','about','links' );
// could also build this array with readdir('MySafeDir') ??
if( in_array($_GET['page'], $pages) )
{ include $_GET['page'].".php";}
else {die("Nice Try."); }

2:
include "my_prefix_".$_GET['page'].".php";

3:
include "my_safe_dir/".$_GET['page'].".php";

4:
include_path=".:/myIncludes";
include $_GET['page'].".php";

--
thanks for your time
juglesh

Aug 10 '05 #1
4 1538
juglesh wrote:
please comment on the following methods of preventing cross site
scripting and/or other nastiness:

1:
$pages = array('home','contact','about','links' );
// could also build this array with readdir('MySafeDir') ??
if( in_array($_GET['page'], $pages) )
{ include $_GET['page'].".php";}
else {die("Nice Try."); }

2:
include "my_prefix_".$_GET['page'].".php";

3:
include "my_safe_dir/".$_GET['page'].".php";

4:
include_path=".:/myIncludes";
include $_GET['page'].".php";


You should see to remove all '..' from the paths, otherwise people could
navigate outside your secure directory.

The best IMHO is to use aliases for pages, and you hard code what the alias
mean, this way it will difficult to get the php script to display something
else than those pages you want.
//Aho
Aug 10 '05 #2
J.O. Aho wrote:
juglesh wrote:
please comment on the following methods of preventing cross site
scripting and/or other nastiness:

1:
$pages = array('home','contact','about','links' );
// could also build this array with readdir('MySafeDir') ??
if( in_array($_GET['page'], $pages) )
{ include $_GET['page'].".php";}
else {die("Nice Try."); }

2:
include "my_prefix_".$_GET['page'].".php";

3:
include "my_safe_dir/".$_GET['page'].".php";

4:
include_path=".:/myIncludes";
include $_GET['page'].".php";

You should see to remove all '..' from the paths, otherwise people could
navigate outside your secure directory.


so, just replace .. with nothing? and that would apply to #3? so, if
they go '../passwords.txt or whatever, that would make my include be
equivalent to "my_safe_dir/up a directory to
root/".$_GET['page'].".php" and they can include something on the
root, or however many ../ they use?
The best IMHO is to use aliases for pages, and you hard code what the alias
mean, this way it will difficult to get the php script to display something
else than those pages you want.


I see, so,
if($_GET['page']=home){include myhomepage.php;}
if($_GET['page']=contact){include mycontactpage.php;} ??

Aug 13 '05 #3
>> > 4:
> include_path=".:/myIncludes";
> include $_GET['page'].".php";
>
You should see to remove all '..' from the paths, otherwise people could
navigate outside your secure directory.


so, just replace .. with nothing? and that would apply to #3? so, if


My preference would be to return an error message to the user
(something similar to the FBI warning on video tapes) and
nothing else.
they go '../passwords.txt or whatever, that would make my include be
equivalent to "my_safe_dir/up a directory to
root/".$_GET['page'].".php" and they can include something on the
root, or however many ../ they use?


Yes.

I don't think I would want to let the user specify a file name, but
if they can, there's a few checks I would want to do:

- The file name (component) contains only acceptable characters,
which might be alpha, numeric, and maybe period, underscore, and
minus. NO slash, meaning all the files need to be in the same
directory.

- Check the component against a complete list of acceptable values
(no pattern-matching, a COMPLETE LIST of possible values, possibly
translating the value in the process).

For example, I occasionally have a page where you can select a sort
order with $_GET['order'], using a small set of named orders. The
names are supposed to make sense to the page maintainer and maybe
to a user reading the URL, but the user is really just supposed to
click a link with a longer description in the text, and not pay any
attention to the guts of the URL at all. I use a switch on
$_GET['order'] which sets a variable with the SQL order clause in
it. The name of the order has no necessary relationship to the SQL
fields involved (e.g. you might have order=date, order=datedesc and
order=name. The SQL fields involved might be signupdate, lastname,
and firstname).

The best IMHO is to use aliases for pages, and you hard code what the alias
mean, this way it will difficult to get the php script to display something
else than those pages you want.


I see, so,
if($_GET['page']=home){include myhomepage.php;}
if($_GET['page']=contact){include mycontactpage.php;} ??


That's the idea, but I think you are missing some quotes.

Gordon L. Burditt
Aug 13 '05 #4

Gordon Burditt wrote:
> 4:
> include_path=".:/myIncludes";
> include $_GET['page'].".php";
>

You should see to remove all '..' from the paths, otherwise people could
navigate outside your secure directory.
so, just replace .. with nothing? and that would apply to #3? so, if


My preference would be to return an error message to the user


I've been hacking around, and I cant traverse(?) dirs with ../ if i use
a prefix, like "pre_".$GET['page'].".php" But, I'm not that
devious...

I want to guard against including remote xss nastiness, too. So, it
seems like the prefix thing works for that, they can try to include
'pre_http://badsite.com/badscript.php', but i dont see how thats going
to do anything. What if I str_replace('http',''), is there another way
around that?
- The file name (component) contains only acceptable characters,
which might be alpha, numeric, and maybe period, underscore, and
minus. NO slash, meaning all the files need to be in the same
directory.


yeah, for most of the easy page=home type pages, this works:
$_GET['page'] = ereg_replace("[^[:alnum:] ]","",$_GET['page']);

and then,
if (!is_file($_GET[page].".php"))
{header ("Location: http://www.domain.com/404.php"); die;}

The best IMHO is to use aliases for pages, and you hard code what the alias
mean, this way it will difficult to get the php script to display something
else than those pages you want.


I see, so,
if($_GET['page']=home){include myhomepage.php;}
if($_GET['page']=contact){include mycontactpage.php;} ??


That's the idea, but I think you are missing some quotes.


yup

Aug 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: googlegroupsneu.20.Jumbo1 | last post by:
Hallo, ich habe eine Frage zum Thema Einbinden von PHP-Dateien mittels include / require mit einem absoluten Pfad (http://...): Die Problemstellung: Auf unserer Website werden täglich News...
8
by: memento | last post by:
Hello, How to include file into an ASP program? I was trying with this command: <!-#include file="INC_file.asp" -> but unfortunately it does not work :( There isn't any error message. IIS...
8
by: Dave | last post by:
Everyone keeps telling me that I should protect my database connection information by defining the connection string in a separate file (presumably one that is not in the application directory)....
5
by: Tiraman | last post by:
Hi , can I use an asp (not aspx) file as include in my aspx files ? lets say that I have a regular asp file that hold few functions , params (dim x etc ....) and cost's and i don't want to...
3
by: indessen | last post by:
Hi, got the following problem: I need to include an HTML file that sits on another server B in an HTML file that sits on server A, and I need to include this at a particular place in the file....
1
by: joe10001 | last post by:
Hi all, I just installed CRELoaded (oscommerce fork) on my server and all work fine except that I have a little message at the bottom of the main page : Fatal error: main() : Security alert:...
11
by: McKirahan | last post by:
I am looking for feedback on an approach to using PHP. Below is a stripped down version of a Home page: "index.php". The content of the site is displayed in the middle of the page and is...
19
by: SteveT | last post by:
Is it possible to have an include file that resides outside of the web site parent folder? For instance, lets say you have several websites under Inetpub/global/websites/ but the include file is...
7
by: HopfZ | last post by:
This bookmarklet tries to load a local js file to a page. javascript:(function(){ var s=document.createElement('script'); s.setAttribute('src','file:///C:/abc.js');...
4
by: philleep | last post by:
Hi, i've just started on php. i want to split my site up into include files so that i can have seperate html files with my menu etc in and then include them in the php page. the code i've been...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.