Section A: E

Section A: Existing design/functionality

xisting Functionality

1. Security Issue: If a user clicks on a particular URL to any XXX site and then logs on, then any subsequent clicks on the same URL will open additional IE windows and automatically log on using the former user/password. The only way I have seen to inhibit this behavior is to open a dummy instance of IE (which will load whatever default home page the user has set) and then execute the URL again. This seems to break the IE session sequence.

 

Suppose I send you an email with XXX web site URL, You click the URL and login the system. The second time click will automatically login the system.

 

Please fix this problem.

 

 

上文是我几个月前收到的一封Mail中的一个小片段。(略去了其中的敏感信息)

测试发现在普通网页中含有超链接类似于http://localhot/yourAppSite/default.aspx

或在word2003 文档(其它版本的word我没有测试,估计也可能存在这个安全缺陷)均存在上述问题。编写HttpModule跟踪截获Request后发现,这两种安全缺陷居然在Request的次数不同。不过可以肯定的是这两种Request中都带有认证标识Cookie。那就禁用cookie吧,如何禁用?如果你不太了解asp.net安全机制的话,建议仔细阅读MSDN ^_^

 

web.config文件中有如下元素,如图

cookieless.JPG
不但要知道如何修正错误最好还能知道为什么这样修正错误。我们先来粗略地看看为什么
cookieles设为true后就cookie可以被禁用了。良好的代码阅读能力是必备。查看下

SessionModule.JPG

System.Web.SessionState.SessionStateModule的实现,在它的init方法中有如下代码。如图

 

再看看System.Web.SessionState.SessionStateSectionHandler.Config的实现

sessionConfig.JPG

.
这下就明白为什么这样改了吧?:)

Fix了这个安全缺陷后请仔细全面地测试下(三年养成一个好习惯J),你会发现有通过上述非正常方式登陆两次,同时打开两个界面,当一个logout后另一个再logout可能会有些小的毛病(UI显示问题)通过简单的加几句JS就可以修正了。具体要根据各自项目的界面风格之类的而论,这里我就不详述了(加几句JS很简单J

 

 

很好,我已经通过了第一关,可惜我无意中犯了大忌。因为我在fix这个安全缺陷的时候可能会导致另一个重大隐患的产生。这个隐患出现在一个月前的另外一个项目中。

 

 

为什么我的会话状态偶尔会丢失?

 

症状:为什么有时登录成功后又自动退出重新登录?为什么有时进行页面间跳转时会自动退出重新登录?我的会话状态为什么时而有效时而无效?

 

前一个月我有将近20天的时间一直被这个问题所困惑,检查过所写的每一行代码若干次。无论是cache类还是context类我都检查的几乎要把它们吞掉!得到的结论是――我的代码没有问题!OS的问题?Framework的问题?我开始怀疑MicrosoftMSDN上、在ASP.NET上,均找到了导致这个问题的原因。

先看看MSDN上的描述吧

The drawback of using cookieless sessions is that the session state is lost if an absolute URL is invoked. When cookies are used, you can clear the address bar, go to another application, and then return to the previous one and retrieve the same session values. If you do this when session cookies are disabled, the session data is lost. For example, the following code breaks the session:

<a runat="server" href="/code/page.aspx">Click</a>

If you need to use absolute URLs, resort to a little trick and manually add the session ID to the URL. You use the ApplyAppPathModifier method on the HttpResponse class.

<a runat="server" 
    href=<% =Response.ApplyAppPathModifier("/code/page.aspx")%> >Click</a>

The ApplyAppPathModifier method takes a string representing a URL and returns an absolute URL, which embeds session information.

看来我的结论是半对J,我的代码有问题,应用cookieless也有缺陷