Excluding some urls for the STREAM conversion

I have the following iRule that converts http to https for external users only as defined on the internal_net

However, when the page contains calls to google.com, amazon.com, ebay.com it tries to put SSL on it.

Is there any way to include an exception list like the one that we have for the internal_net?

when HTTP_RESPONSE {

if { not ([matchclass [IP::client_addr] equals $::internal_net])}{

Enable debug logging to /var/log/ltm? 1=yes, 0=no

set debug 1

If the response content type is text, configure the default stream profile to replace http:// with https://

if {[string tolower [HTTP::header value Content-Type]] contains “text”}{

STREAM::expression @http://@https://@

STREAM::enable

}

Check if the response is a redirect (3xx)

if {[HTTP::is_redirect]}{

if {$debug}{log local0. “Original Location: [HTTP::header value Location], Updated Location: [string map {http: https:} [HTTP::header value Location]]”}

Use ‘string map’ to replace http: with https:

HTTP::header replace Location [string map {http: https:} [HTTP::header value Location]]

}

}

}

Hi Gerardo,

I assume the issue is with the stream profile rewriting references within the page content from http:// to https://. I expect the app isn’t redirecting with a 30x to google or other external sites.

You can use a negative lookbehind regex to specify the search string in the stream expression. There is an example on the STREAM::enable wiki page (Click here). I’ve added a few more examples to the STREAM::expression page (Click here) as well.

STREAM::expression {@http://(?!(?:www\.)?amazon\.com|google\.com|ebay\.com)@https://@}

Would match the bolded sections for these test cases:

http://example.com

http://test.com

I’m not sure how much drain this would add on CPU and/or memory. If you can positively define what strings you want to rewrite with a simpler regex, it would be less resource intensive.

Also, it would be a good idea to disable the stream filter using STREAM::disable by default and only enable it when the content-type header value matches your test. This ensures that the stream filter isn’t applied to subsequent HTTP responses on the same TCP connection. There is an example of this on most of the stream wiki pages.

Aaron

I guess it will be better to have the inclusion stream instead of exclusion.

Could you assist me with that?

I want to convert only calls in the page that make reference to whatever.mycompany.com

Whatever may be any number of names including one, two

Example

one.one.mycompany.com

one.two.mycompany.com

two.one.mycompany.com

three.mycompany.com

regional.london.mycompany.com

london.mycompany.com

africa.mycompany.com

new.uk.mycompany.com

… (you get the point)

This is the one that I use

when HTTP_RESPONSE {

Disable the stream filter by default

STREAM::disable

Check if response type is text

if {[HTTP::header value Content-Type] contains “text”}{

Replace any http:// instance with https:// only if the original string is http://*example.com:

STREAM::expression {@http://(?:.*?example\.com)@https://@}

Enable the stream filter for this response only

STREAM::enable

}

}

All the https have https:///folderone/image.gif instead of https://example.com/folderone/image.gif

How to fix this?

This is what I got in the log file

TCL error: Rule nameoftherule HTTP_RESPONSE - Illegal argument line 9 invoked from within STREAM::expression {http://?:.*?example.com}

I would have thought you could use a stream expression with a single token. Here is a more tested example with the unnecessary backreference in the regex removed:

when HTTP_RESPONSE {  
    
      Disable the stream filter by default  
     STREAM::disable  
    
      Check if response type is text  
     if {[HTTP::header value Content-Type] contains "text"}{  
    
         Match any http:// instance and replace it with nothing 
        STREAM::expression {@http://.*?example\.com@@} 
    
         Enable the stream filter for this response only  
        STREAM::enable  
     }  
  }   
  when STREAM_MATCHED {  
     log local0. "[IP::client_addr]:[TCP::local_port]: matched: [STREAM::match], replaced with: [string map {http:// https://} [STREAM::match]]"  
     STREAM::replace "[string map {http:// https://} [STREAM::match]]"  
    
  }

And the output:

Rule stream_expression_rule : 10.0.0.1:3413: matched: http://test.example.com, replaced with: https://test.example.com

Rule stream_expression_rule : 10.0.0.1:3413: matched: http://example.com, replaced with: https://example.com

Aaron

That worked great, thanks

Now I need more help

With more modifications to the same iRule to exclude some stuff.

Where can I find more information about the Regular Expressions and their validation?

I’m sorry it did not work.

If you include a url in your test with the name somethingelse.com it will convert that one too not only *.example.com

Here are some link to learn more about regular expressions

Click here

Click here

Click here

I think hoolio rule shouldn’t match anything else, maybe you’ve done some typo.

Do you try the exact same iRule or did you update it ? if yes please post your code

This is the iRule that I have, I just changed the name of the url to the one for the company.

This is the version BIG-IP 9.2.4 Build 13.1

when HTTP_RESPONSE {

if { not ([matchclass [IP::client_addr] equals $::internal_net])}{

Enable debug logging to /var/log/ltm? 1=yes, 0=no

set debug 1

STREAM::disable

If the response content type is text, configure the default stream profile to replace http:// with https://

if {[string tolower [HTTP::header value Content-Type]] contains “text”}{

STREAM::expression {@http://.*?example\.com@@}

STREAM::enable

}

Check if the response is a redirect (3xx)

if {[HTTP::is_redirect]}{

if {$debug}{log local0. “Original Location: [HTTP::header value Location], Updated Location: [string map {http: https:} [HTTP::header value Location]]”}

Use ‘string map’ to replace http: with https:

HTTP::header replace Location [string map {http: https:} [HTTP::header value Location]]

}

}

}

when STREAM_MATCHED {

log local0. “[IP::client_addr]:[TCP::local_port]: matched: [STREAM::match], replaced with: [string map {http:// https://} [STREAM::match]]”

STREAM::replace “[string map {http:// https://} [STREAM::match]]”

}

The stream portion of the rule should be working fine to only rewrite the http:// references to https:// for *.example.com. The issue is likely to be there isn’t any checking on 30x redirects. If you change this line:

if {[HTTP::is_redirect]}{

To:

if {[HTTP::is_redirect] && [string match -nocase “http://*.example.com*” [HTTP::header value Location]]}{

You can restrict the Location header rewriting to redirects which match http://*.example.com*

If this doesn’t work, try adding logging to the rule, reproduce the issue and then repost your current rule with the logging and the log output from /var/log/ltm. If you have any problems debugging the rule, let us know.

Aaron

Here are some examples of non working things

image.weather.com, w3.org, plumtree.com are not in part of my domain

Is there any way to check row by row in the code instead of chuncks of code?

Rule stageiRuleGerardopart3 STREAM_MATCHED: 161.51.3.239:3366: matched: http://image.weather.com/web/services/oap/magnet\_hgeneric.jpg WIDTH=270 HEIGHT=175TRTD VALIGN=TOPTABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=270 BACKGROUND=TRTDIMG SRC=http://image.weather.com/web/blank.gif WIDTH=1 BORDER=0 HEIGHT=1 alt=/TDTDIMG SRC=http://image.weather.com/web/blank.gif WIDTH=96 BORDER=0 HEIGHT=1 alt=/TDTDIMG SRC=http://image.weather.com/web/blank.gif WIDTH=168 BORDER=0 HEIGHT=1 alt=/TD/TRTRTDIMG SRC=http://image.weather.com/web/blank.gif WIDTH=1 BORDER=0 HEIGHT=20 alt=/TDTD ALIGN=CENTER COLSPAN=2 VALIGN=MIDDLEIMG SRC=http://image.weather.com/web/blank.gif WIDTH=1 BORDER=0 HEIGHT=1 alt=A title=As reported at Houston Wb City, TX. Jul 24 12:05 p.m. CT target=blank HREF=Weather Forecast and Conditions for Houston, Texas - The Weather Channel | Weather.com CLASS=GENXHcityInfoBHouston, TX

Rule stageiRuleGerardopart3 STREAM_MATCHED: 161.51.3.239:3366: matched: http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd span xmlns:pt=http://www.plumtree.com/xmlschemas/ptui/ script type=text/javascriptif typeofMyKbr==object MyKbr.styleSheetFix/script script type=text/javascript function MyKbr_footerOpenContactInfoevent { if !MyKbr_footerCancelCloseContactInfo { var e=document.getElementByIdMyKbr_footerContactInfo e.style.left=event.clientXdocument.body.scrollLeft10 e.style.top=event.clientYdocument.body.scrollTop-e.offsetHeight-10 e.style.visibility=visible } } function MyKbr_footerCancelCloseContactInfo { if MyKbr_footerCloseContactInfo.timerHandle { window.clearTimeoutMyKbr_footerCloseContactInfo.timerHandle MyKbr_footerCloseContactInfo.timerHandle=null return true }

Rule stageiRuleGerardopart3 STREAM_MATCHED: 161.51.3.239:3550: matched: http://www.plumtree.com/xmlschemas/ptui/ script language=javascriptdocument.cookie=mykbrexp=/script table style=BACKGROUND-COLOR: white border=0 cellpadding=0 cellspacing=0 width=100 height=40 tr td width=150 valign=topdiv style=width:150height:50overflow:hidden img style=position:relativeleft:-20top:-13 id=imgLogo src=http://static.stage.regional.mykbr.com, replaced with: https://www.plumtree.com/xmlschemas/ptui/ script language=javascriptdocument.cookie=mykbrexp=/script table style=BACKGROUND-COLOR: white border=0 cellpadding=0 cellspacing=0 width=100 height=40 tr td width=150 valign=topdiv style=width:150height:50overflow:hidden img style=position:relativeleft:-20top:-13 id=imgLogo src=https://static.stage.regional.mykbr.com

OK, I think someone needs to help me here

This is the new iRule and it works following the first example here

http://devcentral.f5.com/wiki/default.aspx/iRules/STREAM\_\_expression.html

I don’t know very well regular expressions but this is my solution.

when HTTP_RESPONSE {

if { not ([matchclass [IP::client_addr] equals $::internal_net])}{

set debug 1

STREAM::disable

STREAM::expression {@http://stage.example.com@https://stage.example.com@ @http://static.regional.example.com@https://static.regional.example.com@ @http://static.stage.regional.example.com@https://static.stage.regional.example.com@ @http://static.example.com@https://static.example.com@ }

STREAM::enable

}

}

If you think you can improve it plese do it.

One importan thing here is that I cannot convert all the *.example I need to have a set of domains with example not all of them.

For example stage.server1.example.com should be excluded and maybe others.

That’s interesting… It looks like the ? in the regex might not be performing a lazy match on the string being searched.

In a quick test, the ? works for regexp to restrict the match to the shortest possible string:

when RULE_INIT { 
    log local0. "[regexp -inline {test.*?example\.com} {test1.example.com text text test2.example.com}]" 
 }

Log output:

Rule : test1.example.com

Likewise, using the last rule I posted worked for a test below:

when HTTP_RESPONSE {  
    
     Disable the stream filter by default  
    STREAM::disable  
    
     Check if response type is text 
    if {[HTTP::header value Content-Type] contains "text"}{  
  
       log local0. "[IP::client_addr]:[TCP::client_port]: Setting stream expression, enabling filter"   
        Replace any http:// instance with https:// only if the original string is http://*example.com:  
       STREAM::expression {@http://.*?example\.com@@} 
  
        Enable the stream filter for this response only  
       STREAM::enable 
    } 
 }   
 when STREAM_MATCHED {  
    log local0. "[IP::client_addr]:[TCP::local_port]: matched: [STREAM::match], replaced with: [string map {http:// HTTPS://} [STREAM::match]]"  
    STREAM::replace "[string map {http:// HTTPS://} [STREAM::match]]"  
 }

Here is the log output for a request which was rewritten:

: 10.1.1.1:4683: matched: http://test.example.com, replaced with: HTTPS://test.example.com

: 10.1.1.1:4683: matched: http://example.com, replaced with: HTTPS://example.com

And here is the updated response content:

HTTPS://test.example.com

http://nochange.com

To compare, this is a request direct to the server:

http://test.example.com

http://nochange.com

I’m not sure why the rule isn’t working when you’re testing against your application. I would suggest opening a case with F5 Support referencing this post, as the example is working on a simple test in 9.4.4 and should work for your scenario.

On your second point, if you need to only match some subdomains but not all, you could either modify the regex in the stream expression, or add logic to the STREAM_MATCHED event. For the second option, you can check if the matched string is in a list or datagroup of subdomains to not rewrite. In that case, just don’t call STREAM::replace.

Aaron

Thinking about this again, I’m guessing the reason you were seeing strings being matched which were much longer than a single domain was the regex you were testing with was wrong.

Here is an example that should work to allow you to check all http://*.example.com strings but rewrote only those you want to.

when RULE_INIT {    
        
        List of *.example.com subdomains that should not be rewritten (in lowercase)    
       set ::subddomains_to_preserve [    
          "keep1.example.com" \    
          "keep2.example.com" \    
          "keep3.example.com" \    
       ]    
    }    
    when HTTP_RESPONSE {      
        
        Disable the stream filter by default      
       STREAM::disable      
        
        Check if response type is text     
       if {[HTTP::header value Content-Type] contains "text"}{      
        
          log local0. "[IP::client_addr]:[TCP::client_port]: Setting stream expression, enabling filter"       
           Replace any http:// instance with https:// only if the original string is http://*example.com:      
          STREAM::expression {@http://.*?example\.com@}     
        
           Enable the stream filter for this response only      
          STREAM::enable     
       }     
    }       
    when STREAM_MATCHED {    
        
        Check if the current string matched by the stream filter is part of the list of subdomains to preserve.    
       if {[matchclass [string tolower [STREAM::match]] equals $::subddomains_to_preserve]}{    
          log local0. "[IP::client_addr]:[TCP::local_port]: found [STREAM::match], but did not modify it."      
       } else [    
          log local0. "[IP::client_addr]:[TCP::local_port]: found [STREAM::match], replaced with: [string map {http:// HTTPS://} [STREAM::match]]"      
          STREAM::replace "[string map {http:// https://} [STREAM::match]]"      
       }    
    }

You could also change the list in RULE_INIT from subdomains to preserve to those you want to rewrite to https. The list could also be replaced with a separate class.

Edit: you’ll also want to use an HTTP profile with response chunking set to rechunk.

SOL6422: Using the Stream profile with HTTP traffic can prevent the client from displaying all of the data

Aaron

Hi everybody, I been copying and pasting the last irule shown here, because I had a similar issue. First I had to modify that irule becouse gave to me some problems whit the syntax and other stuff. I’m new on f5 so I starting to google it how to fix it and finally I got this:

when RULE_INIT {    

    Lista de los domininios que NO se reemplazaran 
  set static::listado [ list /http://www.fapesp.br ]
    }

when HTTP_RESPONSE {      

    Desabilitar el filtro stream 
   STREAM::disable      

    Check if response type is text     
   if {[HTTP::header value Content-Type] contains "text"}{      

      log local0. "[IP::client_addr]:[TCP::client_port]: Setting stream expression, enabling filter"       
       Remplazar cualquier http:// por https://      
      STREAM::expression {@http://*@}     

       Habilitar el filtro stream solo si corresponde
      STREAM::enable     
   }     
}       
when STREAM_MATCHED {    

    Revisar if el string actual obtenido por el filtro stream esta en la lista de dominios a conservar
   if { [lsearch -glob $static::listado [string tolower [STREAM::match]] ] } {    
      log local0. "[IP::client_addr]:[TCP::local_port]: found [STREAM::match], but did not modify it."      
   } else {   
      log local0. "[IP::client_addr]:[TCP::local_port]: found [STREAM::match], replaced with: [string map {http:// HTTPS://} [STREAM::match]]"      
      STREAM::replace "[string map {http:// https://} [STREAM::match]]"      
   }    
}

This rule does not works and show this error: " Rule /Common/iRule_Scielo_EXCEPTIONS : 192.168.210.36:19119: found http://, but did not modify it"

So where is my problem, I only want excluding some urls from stream conversion. Thank