Author |
|
apishdad
Joined: 01 Jul 2019 Posts: 52 Location: Canada, Toronto
|
Posted: Tue 11 Feb '25 11:28 Post subject: How to stop a script tag from double insertion |
|
|
Hi,
I have a situation where I am trying to insert a third party widget which is basically a <script> tag, but the script tag gets inserted twice.
My setup is very similar to the following
<Proxy balancer://MyServer>
require all granted
BalancerMember http://server1 loadfactor=1 max=100 retry=60 connectiontimeout=300 timeout=300 route=Route1
BalancerMember http://Servr2 loadfactor=2 max=150 retry=60 connectiontimeout=300 timeout=300 route=Route2
ProxySet lbmethod=bytraffic stickysession=JSESSIONID|jsessionid scolonpathdelim=On
</Proxy>
ProxyPass "/" "balancer://MyServer/"
ProxyPassReverse "/" "balancer://MyServer/"
<LocationMatch ^/(.*)$>
FilterChain Replace Compress
RequestHeader unset Accept-Encoding
RequestHeader set Accept-Encoding identity
SubstituteMaxLineLength 5M
Substitute "s#</head>#\
<script>\
alert('Testing')\
</script>\
</head>\
#inq"
</LocationMatch>
What is happening is that when the page loads and I go to "View Source" for the page. I see something like this in the page:
<head>
<script>\
alert('Testing')\
</script>\
<script>\
alert('Testing')\
</script>\
</head>
i believe this is because on the first Proxypass it replaces the </head> tag with the whole <script>...</script></head> and on proxypassrevrese it replaces the </head> tag that is already in the first pass with another script block similar to above.
My question is this :
How can I avoid a second replacement of my </head> element with the script tag.
Thanks and greatly appreciated for your help. |
|
Back to top |
|
zhangdaoming
Joined: 08 Feb 2025 Posts: 3
|
Posted: Tue 11 Feb '25 11:57 Post subject: |
|
|
<LocationMatch ^/(.*)$>
FilterChain Replace Compress
RequestHeader unset Accept-Encoding
RequestHeader set Accept-Encoding identity
SubstituteMaxLineLength 5M
RequestHeader set MY_REPLACE_HEAD "true"
Substitute "s#</head>#\
<script>\
alert('One~')\
</script>\
</head>\
#inq" env=MY_REPLACE_HEAD
# clear run tag
RequestHeader unset MY_REPLACE_HEAD
</LocationMatch> |
|
Back to top |
|
apishdad
Joined: 01 Jul 2019 Posts: 52 Location: Canada, Toronto
|
Posted: Wed 12 Feb '25 22:51 Post subject: |
|
|
Thanks for the great suggestion. But I am getting this error:
AH00526: Syntax error on line 106 of D:/MyCode/Apache_Code.conf:
Substitute takes one argument, Pattern to filter the response content (s/foo/bar/[inf])
This error appears when I put the
env=MY_REPLACE_HEADER at the location where it says
#inq" env=MY_REPLACE_HEADER
When I take the env part I get "Syntax OK" check.
Any ideas? |
|
Back to top |
|
apishdad
Joined: 01 Jul 2019 Posts: 52 Location: Canada, Toronto
|
Posted: Thu 13 Feb '25 19:29 Post subject: |
|
|
Does anyone else has any ideas?
substitute command is not very friendly with Environment variables. |
|
Back to top |
|
James Blond Moderator

Joined: 19 Jan 2006 Posts: 7389 Location: EU, Germany, Next to Hamburg
|
Posted: Thu 13 Feb '25 23:08 Post subject: |
|
|
Maybe mod_proxy_html is your friend. |
|
Back to top |
|
apishdad
Joined: 01 Jul 2019 Posts: 52 Location: Canada, Toronto
|
Posted: Thu 13 Feb '25 23:37 Post subject: |
|
|
James Blond wrote: | Maybe mod_proxy_html is your friend. |
I just looked into that module, but I am not quite sure how to use it. Which command do you suggest in it |
|
Back to top |
|
apishdad
Joined: 01 Jul 2019 Posts: 52 Location: Canada, Toronto
|
Posted: Fri 14 Feb '25 1:36 Post subject: |
|
|
There is a different way to approach this too, but it leaves no </HEAD> tag.
The other way is to
use the substitute command as such without the </head> element in the .conf file but after that I am not quite sure how to just add a </HEAD> tag. So the code looks like
Substitute "s#</head>#\
<script>\
alert('One~')\
</script>\
#inq"
then I get
<script> alert('One~') </script>
in the HTML but I need a closing </head> tag. I dont know how to add that. |
|
Back to top |
|
apishdad
Joined: 01 Jul 2019 Posts: 52 Location: Canada, Toronto
|
Posted: Fri 14 Feb '25 10:16 Post subject: |
|
|
Figured it out...
At the beginning of my config file I had the following
FilterDeclare Replace
FilterProvider Replace Substitute "%{CONTENT_TYPE} =~ m#^application/(javascript|json|xml)#i"
FilterProvider Replace Substitute "%{CONTENT_TYPE} =~ m#^text/(css|html|javascript|plain|xml)#i"
I had to comment the line that says:
FilterProvider Replace Substitute "%{CONTENT_TYPE} =~ m#^text/(css|html|javascript|plain|xml)#i"
and it worked. |
|
Back to top |
|