What is the meaning of pattern="." on preCondition property of IIS Url Rewrite?

Viewed 272

I have seen many examples of IIS URL Rewrite:

<!-- reference: https://im5tu.io/article/2017/06/ensuring-samesite-cookies-with-url-rewrite/ -->
<rewrite>
  <outboundRules> 
    <rule name="Ensure samesite Cookies" preCondition="Missing samesite cookie">
      <match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
      <action type="Rewrite" value="{R:0}; SameSite=strict" />
    </rule>
    <preConditions>
      <preCondition name="Missing samesite cookie">
        <!-- Dont remove the first line here, it does do stuff! -->
        <add input="{RESPONSE_Set_Cookie}" pattern="." />
        <add input="{RESPONSE_Set_Cookie}" pattern="; SameSite=strict" negate="true" />
      </preCondition>
    </preconditions>
  </outboundRules>
</rewrite>

Does anyone know why the line <add input="{RESPONSE_Set_Cookie}" pattern="." /> is necessary?

I tried searching on Google but did not find an answer.

2 Answers

The rule is only executed if it matches the pre-conditions.

The "pattern" is a regular expression, and . matches any character.
In this case, essentially saying "match if the cookie has a value".

The line below it negates executing the rule if the cookie already has the value which would be added.
Otherwise every time the rule ran, you would add the text again.

As to why it is necessary, sending an empty cookie should delete it.
In this case, it would ensure that there is actually a value within the cookie before attempting to change it with the rule.

Rewrite Module - Preconditions Collection
Creating outbound rules for URL rewrite module

It's a regular expression.

In regular expressions, . matches any single character. But that alone doesn't really explain things well. The important thing to understand is an empty string will NOT match, because there are no characters. Anything else — anything at all, no matter how long — WILL match, because processing will stop as soon as the match is confirmed with the first character.

In other words, this expression checks whether the cookie actually has a value.

Related