Forum Discussion

martin_58353's avatar
Sep 11, 2017

STREAM::expression with special characters

I need inject code into JavaScript returned from server some code:

original code from server:

 

    
    this.initNewTransfer(t, n),
    

 

replaced/injected code:

 

    
    this.initNewTransfer(t, n),
    INJECTED-CODE,
    

 

iRule definition (with not correct $find and $replace variables):

 

    when HTTP_REQUEST {
      set path [HTTP::path]
      switch -glob $path {
        "/some-java-script.js" {
          HTTP::header remove "Accept-Encoding"
        }
      }
    }

    when HTTP_RESPONSE {
      switch -glob $path {
        "/some-java-script.js" {
           find string: "this.initNewTransfer(t, n),"
          set find "initNewTransfer\\(t, n\\),"
           replace string: "this.initNewTransfer(t, n), INJECTED-CODE,"
          set replace "initNewTransfer\\(t, n\\), INJECTED-CODE,"

          STREAM::disable
          STREAM::expression "@$find@$replace@"
          STREAM::enable
        }
      }
    }

 

The problem is with brackets in

\(

 

Can you please help me with this replacement? How can I match opening bracket in STREAM::expression?

  • I found correct strings for replacement:

     

    set find "initNewTransfer\\x28t, n\\)"
    set replace "this.initNewTransfer\x28t, n\), INJECTED-CODE"

    Is there any better solution?

     

  • I found correct strings for replacement:

     

    set find "initNewTransfer\\x28t, n\\)"
    set replace "this.initNewTransfer\x28t, n\), INJECTED-CODE"

    Is there any better solution?

     

    • cjunior's avatar
      cjunior
      Icon for Nacreous rankNacreous

      I honestly don't know why the first parenthesis causes disruption for the logic. This way, sometimes I workaround using hexa chars like you did, and in some cases I just ignore that char using a dot. Plus, I prevent command substitution with {}:

       

      set find {this\.initNewTransfer.t, n/),}
      set replace "this.initNewTransfer(t, n), INJECTED-CODE,"
      

      Just keep in mind the "find" is the regexp and "replace" a simple text.

       

      Let's wait a expert tell us what's the issue.

       

      Regards.

       

    • martin_58353's avatar
      martin_58353
      Icon for Cirrus rankCirrus

      As I wrote, solution of this is:

       

      set find "initNewTransfer\\x28t, n\\)"
      set replace "this.initNewTransfer\x28t, n\), INJECTED-CODE"

      The first bracked "(" is possible replace with \x28 or \050 (is the same char code with octal). But first bracket is not possible define with escape sequence (double backslash) like "\(", it is not working.

       

      The second bracket ")" is possible define with hex or oct code, but also escape sequence is correct "\)".

       

      I don't know if it's a feature or bug (I tested it with 15.1, 12.x and 13.1 sw version).