|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWhat the article explainsThe article explains how an HTTP module can be used to combat leech requests to resources on your domain through GET or POST HTTP request methods. The code snippet provided defines a class that implements the This technique can be used to prevent the outside world from posting links to resources on your domain without authorization to do the same. For example, if you maintain a list of sites that have been authorized to provide links to your website, then you can put into effect a selective screening routine. On the other hand, if there are domains that you do not want to be referred from, this technique will again, prove equally useful. I have not provided the code to these ends though. My intent here is to simply illustrate a basic possibility. But you could mail me if you are interested to know how to go about it, but I believe you should be able to pull it off without much ado after reading through this article. BackgroundASP.NET PipelineThe ASP.NET pipeline represents a series of extensible objects that work in a sequential-chain and act on an incoming request, one after the other. As the requests pass through the pipeline, they are modified and filtered until they are finally handed over to a handler object that emits a suitable response back to the client. Through the use of these modules and handlers, we can effectively extend the capabilities our web server, just like what ISAPI extensions and filters are used to do for IIS. HttpModulesAn HTTP module is an assembly that handles events raised by an <httpModules>
<add name="OutputCache"
type="System.Web.Caching.OutputCacheModule"/>
<add name="Session"
type="System.Web.SessionState.SessionStateModule"/>
<add name="WindowsAuthentication"
type="System.Web.Security.WindowsAuthenticationModule"/>
<add name="FormsAuthentication"
type="System.Web.Security.FormsAuthenticationModule"/>
<add name="PassportAuthentication"
type="System.Web.Security.PassportAuthenticationModule"/>
<add name="UrlAuthorization"
type="System.Web.Security.UrlAuthorizationModule"/>
<add name="FileAuthorization"
type="System.Web.Security.FileAuthorizationModule"/>
</httpModules>
The number of modules that get to intercept the request is based upon these settings within the host machine's machine.config file and the application's web.config file. Note that, HTTP modules are typically executed for every request irrespective of the file type. When an HTTP module is hooked into the pipeline (via an entry in web.config), the ASP.NET runtime calls the module's To make a class act as a module, it should implement the member signatures of the
HttpApplicationThe The events that are raised by the
Using the codeRefererFilter.vbThis is our HTTP module class. It implements the AddHandler Application.BeginRequest, AddressOf Me.Application_BeginRequest
The When the Carrying on, if the Public Class RefererFilter
Implements IHttpModule
Private Application As HttpApplication
Public Sub Init(ByVal Application As HttpApplication)_
Implements IHttpModule.Init
'Register our Filter method with the HttpApplication
'object to receive notification of its BeginRequest event.
AddHandler Application.BeginRequest, _
AddressOf Me.Application_BeginRequest
End Sub
'This method is called by the HttpApplication object when
'the HttpContext object has been prepared for the URI request
Public Sub Application_BeginRequest(ByVal sender _
As Object, ByVal events As System.EventArgs)
Application = CType(sender, HttpApplication)
Dim Request As HttpRequest = Application.Context.Request
Dim hostName As String = Request.ServerVariables("HTTP_HOST")
Dim refName As String = Request.ServerVariables("HTTP_REFERER")
'Check if HTTP_REFERER holds a value
If Not refName Is Nothing Then
'Check if the value in refName is a authorized or valid
If Not IsValidReferer(hostName, refName) Then
TeminateRequest()
End If
End If
End Sub
' This function contains the Authorization or
' Validation logic to apply on the Referer URL
' Currently, the code below will only check to se if
' the host name is a part of the referer URL
Private Function IsValidReferer(ByRef host As String, _
ByRef referer As String) As Boolean
'referer will contain the previous url, while host
'contains only the current domain name.
Return IIf(referer.StartsWith("http://" & host),_
True, False) 'return TRUE if the domain is the same.
End Function
Private Sub TeminateRequest()
'YOU CAN SET AN ERROR RESPONSE IF YOU WISH
'OR REDIRECT THE REQUEST TO ANOTHER URI..
'your homepage could be a starting point.
'OR SIMPLY TERMINATE THE REQUEST...THIS IS NOT RECOMMENDED
'EXCEPT IF YOUR INTENT IS TO ILLUSTRATE.
'I choose to illustrate here the termination of a request
'before it flows to any more modules and ultimately to the handlers.
Application.CompleteRequest()
'The response in this case will be blank.
End Sub
Public Sub Dispose() Implements IHttpModule.Dispose
'Under ordinary circumstances, we don't really
'need to do anything here.
End Sub
End Class
Compile this class into an assembly and note the namespace, class and assembly name for registering the filter in the web.config file of your website as follows: - <httpModules>
<add name="RefererFilter" type="YourNamespace.RefererFilter,YourAssemblyName"/>
</httpModules>
Make sure you replace the type attribute value to an appropriate value. It should reflect this format: type="YourNamespace.ClassName,AssemblyName"
Code In ActionCase 1) Browse to your website by typing the URL in your browser's address field. Your website should conveniently respond with the contents of the resource that was requested. This happens because Case 2) Now we need to have a link on a page part of another domain that points to a URL on your website's domain. Pressing this hyperlink should result in our Filter terminating our request since ConclusionI suppose combating leech links is a concern for a whole lot of websites. Face it! Nobody enjoys being ripped off. If there is a way to prevent it, do so by all means. "Prevention is better than cure" Season's greetings to all!
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||