I have the following rule in web.config designed to identify and rewrite cookies for outgoing sessions using both secure and httpOnly flags:
<rewrite>
<outboundRules>
<preConditions>
<preCondition name="MatchSessionCookies">
<add input="{RESPONSE_SET_COOKIE}" pattern="." />
</preCondition>
</preConditions>
<rule preCondition="MatchSessionCookies" name="SecureSessionCookies" enabled="true">
<match serverVariable="RESPONSE_SET_COOKIE" pattern="^(.*sess.*)=(.+)$" />
<action type="Rewrite" value="{R:1}={R:2}; httpOnly; secure" />
</rule>
</outboundRules>
</rewrite>
This works as intended until the advent of httpErrors:
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/path/to/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
Thus, when accessing /a-page-that-exists.aspxdeleted cookies, outgoing ASPSESSIONID cookies are successfully overwritten with both protected and httpOnly flags.
Request URL: /a-page-that-exists.aspx
Status Code: 200 OK
Set-Cookie: ASPSESSIONIDABCDEFG=...; path=/; httpOnly; secure
The problem is access /a-page-that-does-NOT-exist.aspx. The request [404] seems to be internally “routed” to the path ExecuteURL, and my rules for rewriting the URLs that I have are completely excluded.
Request URL: /a-page-that-does-NOT-exist.aspx
Status Code: 200 OK
Set-Cookie: ASPSESSIONIDABCDEFG=...; path=/
, , [404], 404?