Forum Moderators: coopster & phranque

Message Too Old, No Replies

How do you validate the referrer?

in PERL

         

th1chsn

11:48 pm on Apr 3, 2003 (gmt 0)

10+ Year Member



Hello,

I have a script for a program running on a separate domain from my members area. I want my members to only be allowed to access this script only if they are coming from inside my members area. The scripts are written in perl and I was trying to modify this code that I received. I put this code in the beginning part of the script. It didn't work. I was still able to get in from outside the members area. Any suggestions would be greatly appreciated.

@AllowedDomains = (ip.ip.ip.ip,'anysite.com');
@TestDomains = map { my $revdomain = reverse $_;
$revdomain =~ s/\.*$/./; $revdomain; } @AllowedDomains;

#$referrer = 'http://anysite.com';
#$referrer = $ENV{HTTP_REFERER};

($type, $nothing, $host) = split m{/}, $referrer;
$revhost = reverse $host;
$revhost =~ s/\.*$/./;

@matched = grep { 0 == index $revhost, $_ } @TestDomains;

if(! @matched ) {
# here's where you send the error message, without the
# list of allowed strings, and quit
print "Content-type: text/html\n\n";
print qq¦

<html>
<center>
<br><br><br><br><br><br><br><br>
<font color="red" size=5>
<b>Access Denied. This page is reserved exclusively for Members.</b>
</font></center>
</html>¦;
exit 0;
}

# if the user is authorized the script will begin here

SinclairUser

12:47 am on Apr 4, 2003 (gmt 0)

10+ Year Member



Is this the code you actually have installed - if so I think you need to modify it before it will work. In your case you really only need a simple check for the correct referrer against you known domain.

like this:

if ($ENV{'HTTP_REFERER'} eq "your_domain_name")
{ print "welcome"; }
else
{ print "go away!"; }

Obviously, you will need to do more than just print a message but it should give you the basic idea...

C.

DrDoc

1:05 am on Apr 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just keep in mind that you can't trust the referer. It is easy to spoof a referer manually. Also, some user agents don't report the referer correctly.

th1chsn

1:35 am on Apr 4, 2003 (gmt 0)

10+ Year Member



Thanks for the input. Do you know of another way to accomplish this task that might be more secure?

andreasfriedrich

1:44 am on Apr 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld [webmasterworld.com] th1chsn.

I´d suggest reading Marcia`s WebmasterWorld Welcome and Guide to the Basics [webmasterworld.com] post which contains a lot of useful information.

Use whatever login mechanism you use for your members area for this script as well.

Andreas