I have been involved in two projects recently which have required the removal of all HTTP Headers which can identify the site as either an ASP.Net site or as a SharePoint site. This is normally as a result of Penetration Testing.
Normally I would use a combination of web.config changes to remove some headers and a custom HTTPModule to remove some of the SharePoint specific headers.
Before anyone gets on their high horse about whether or not stripping HTTP Headers is advisable, please bear in mind that we have two web app extensions, one of which is for our public facing site and it is only for this one these changes have been configured (search crawling and collaboration/site editing happen on other web application extensions) and also this is a client request to comply with the output of a penetration test so its not really open to architectural debate.
I have outlined the changes I normally make below and these have always worked fine for me. However recently I was directed to the following post which implies that when performing this action in a custom HTTPModule we should not use the PreSendRequestHeaders method, but rather we should choose an event earlier in the application lifecycle to avoid issues with the HTTPCache module:
I have attempted to test the method described in the post above, but when testing with SharePoint 2013 and IIS8 I have noticed some issues. The headers are not reliably removed in this scenario unless the PreSendRequestHeaders event is used. I have tried with several events in the lifecycle.
So my question is simple what is the correct way for this HTTPModule to work such that the heap corruption discussed above (is this a common issue, I have never seen it myself?) is not a problem and to ensure that the headers are consistently removed in IIS8 with SharePoint 2013.
NOTE: Probably warrants a second question but there appears to be two new HTTP Headers in SharePoint 2013 for SPRequestDuration and SPIisLatency. Anyone know what these are for and whether a HTTPModule is the correct place to remove them. What will this break if anything?
Current Solution
Web.Config Changes
To remove the 'X-AspNet-Version' header
<httpRuntime enableVersionHeader="False" />
To remove the 'X-Powered-By' and 'MicrosoftSharePointTeamServices' headers
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
<remove name="MicrosoftSharePointTeamServices" />
</customHeaders>
</httpProtocol>
Custom HTTPModule
To remove the additional unwanted HTTP Headers, adding this as an event handler for the PreSendRequestHeaders event.
private void RemoveHeaders(object sender, EventArgs e)
{
try
{
this._app.Response.Headers.Remove("Server");
this._app.Response.Headers.Remove("X-SharePointHealthScore");
this._app.Response.Headers.Remove("SPRequestGuid");
}
catch (Exception ex)
{
Trace.TraceError(string.Format("Failed to remove HTTP Headers. Error: {0}, Stack Trace: {1}", ex.Message, ex.StackTrace));
}
}