首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ExecuteURL中使用web.config作为404处理程序将绕过URL重写。( outboundRules)在使用其他responseModes时不会

在ExecuteURL中使用web.config作为404处理程序将绕过URL重写。( outboundRules)在使用其他responseModes时不会
EN

Stack Overflow用户
提问于 2016-03-16 19:17:12
回答 1查看 2.1K关注 0票数 3

web.config中有以下规则,用于识别和重写具有安全标志和httpOnly标志的出站会话cookie:

代码语言:javascript
复制
<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>

这按照预期工作,直到httpErrors发挥作用为止:

代码语言:javascript
复制
<httpErrors>
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" prefixLanguageFilePath="" path="/path/to/404.aspx" responseMode="ExecuteURL" />
</httpErrors>

因此,在访问/a-page-that-exists.aspx时,被写入的出站ASPSESSIONID cookie将被成功地用安全标志和httpOnly标志重写。

代码语言:javascript
复制
Request URL: /a-page-that-exists.aspx
Status Code: 200 OK

Set-Cookie: ASPSESSIONIDABCDEFG=...; path=/; httpOnly; secure

问题是访问/a-page-that-does-NOT-exist.aspx。看来404请求在内部被“路由”到ExecuteURL路径,并且我的URL重写规则被完全绕过。

代码语言:javascript
复制
Request URL: /a-page-that-does-NOT-exist.aspx
Status Code: 200 OK

Set-Cookie: ASPSESSIONIDABCDEFG=...; path=/

对于如何修改我的出站重写规则,以便在提交给我的404处理程序之前,它们可以应用于404请求,有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-16 21:00:20

看来,我们必须使用IIS重写版本的IIS <httpErrors />处理程序,但它有效:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <!-- Remove existing 404 handler -->
        <httpErrors>
            <remove statusCode="404" subStatusCode="-1" />
        </httpErrors>

        <rewrite>
            <outboundRules>
                <preConditions>
                    <preCondition name="MatchSessionCookies">
                        <add input="{RESPONSE_SET_COOKIE}" pattern="." />
                    </preCondition>
                </preConditions>

                <!-- Does NOT work with ExecuteURL 404 handler -->
                <rule preCondition="MatchSessionCookies" name="SecureSessionCookies" enabled="true">
                    <match serverVariable="RESPONSE_SET_COOKIE" pattern="^(gsm|.*sess.*)=(.+)$" />
                    <action type="Rewrite" value="{R:1}={R:2}; httpOnly; secure" />
                </rule>
            </outboundRules>
            <rules>
                <!-- Re-implement ExecuteURL 404 handler as URL Rewrite -->
                <rule name="Handle404" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAny">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/path/to/404.aspx?404;{PreserveSchema:{HTTPS}}{HTTP_HOST}{UNENCODED_URL}" />
                </rule>
            </rules>
            <rewriteMaps>
                <!-- http://stackoverflow.com/a/10227936/901156 -->
                <rewriteMap name="PreserveSchema" defaultValue="OFF">
                    <add key="ON" value="https://" />
                    <add key="OFF" value="http://" />
                </rewriteMap>
            </rewriteMaps>
        </rewrite>
    </system.webServer>
</configuration>

以及答复:

代码语言:javascript
复制
Request URL: /a-page-that-does-NOT-exist.aspx
Status Code: 200 OK

Set-Cookie: ASPSESSIONIDABCDEFG=...; path=/; httpOnly; secure
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36045022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档