IISのWebサーバーでURLに"+" "%2B" が含まれていると「要求にダブル エスケープ シーケンスが含まれていました。」 / "The request filtering module is configured to deny a request that contains a double escape sequence." エラーが発生する - IIS

IISのWebサーバーでURLに"+" "%2B" が含まれていると「要求にダブル エスケープ シーケンスが含まれていました。」エラーが発生する現象について紹介します。

現象

IISのWebサーバーでURLに"+" や "%2B" が含まれていると以下のエラーが発生します。
エラーメッセージ
HTTP Error 404.11 - Not Found
要求フィルター モジュールが、ダブル エスケープ シーケンスを含む要求を拒否するように構成されています。
可能性のある原因
要求にダブル エスケープ シーケンスが含まれていました。要求フィルターはダブル エスケープ シーケンスを拒否するように Web サーバーで構成されています。
対処方法
applicationhost.config または web.confg ファイルにある configuration/system.webServer/security/requestFiltering@allowDoubleEscaping 設定を確認します。


エラーメッセージが英語で表示される場合もあります。英語の場合は次のエラーメッセージが表示されます。
エラーメッセージ
HTTP Error 404.11 - Not Found The request filtering module is configured to deny a request that contains a double escape sequence.
Most likely causes:
The request contained a double escape sequence and request filtering is configured on the Web server to deny double escape sequences.
Things you can try:
Verify the configuration/system.webServer/security/requestFiltering@allowDoubleEscaping setting in the applicationhost.config or web.confg file.

対処法1:URLに"+" "%2B" を含まないようにする

いくつかの対処方法があります。
アプリケーションの仕様を変更してURLに"+"や"%2B"の文字列を含まないようにします。

対処法2:パラメーターに"+" "%2B" を含まないようにする

URLに"+" "%2B" が含まれる場合にエラーが発生しますが、パラメーターに "+"や"%2B"が含まれている場合はエラーが発生しません。
アプリケーションを変更して"+"の文字をパラメータで渡すようにします。
エラーが発生するURL
http://www.ipentec.com/testapp/chapter/aa+
http://www.ipentec.com/testapp/chapter/aa%2B
エラーが発生しない
http://www.ipentec.com/testapp/chapter?page=aa+
http://www.ipentec.com/testapp/chapter?page=aa%2B

対処法3:web.configを変更してダブルエスケープシーケンスを許可する

web.configファイルを変更することで、IISの設定を変更し、ダブルエスケープシーケンスを許可できます。
requestFiltering タグの allowDoubleEscapingallowDoubleEscaping="true" を記述することでダブルエスケープシーケンスを許可できます。
web.config (変更後)
<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <!-- To customize the asp.net core module uncomment and edit the following section. 
  For more info see https://go.microsoft.com/fwlink/?linkid=838655 -->

  <system.webServer>
    <!--
    <handlers>
      <remove name="aspNetCore"/>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
     -->
    <security>
      <requestFiltering allowDoubleEscaping="true" />
    </security>
  </system.webServer>
</configuration>

対処法4:"+"を全角にする

"+"の文字を "+" の全角文字に変更することで回避できます。

対処法5:Base64エンコーディングする

"+"の文字をURLエンコーディングしても "%2B" となるため、通常のURLエンコードではエラーになります。 Base64エンコーディングすることでエラーを回避できます。

著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2022-12-19
改訂日: 2022-12-19
作成日: 2020-06-16
iPentec all rights reserverd.