views:

61

answers:

3

The host that the majority of my script's users are on forces an text ad at the end of every page. This code is sneaking into my script's AJAX responses. It's an HTML comment, followed by a link to their signup page. How can I strip this comment and link from the end of my AJAX responses?

A: 

Regular Expressions

My first suggestion would be to find a regular expression that can match and eliminate that trailing information. I'm not the greatest at writing regular expressions but here's an attempt:

var response = "I am the data you want. <strong>And nothing more</strong> <!-- haha -> <a href='google.com'>Sucker!</a>";
var myStuff = response.replace("/\s+?<!--.*>$/gi", "");

Custom Explosion String

What would be an easy and quick solution would be to place a string at the end of your message ("spl0de!"), and then split the ajax response on that, and only handle that which comes before it.

var myStuff = response.split("spl0de!")[0];

This would remove anything anybody else sneaks onto the end of your data.

Jonathan Sampson
That looks good, thanks! Someone suggested I do something similar, but didn't specify any code. My knowledge of JavaScript doesn't go much further than the basic syntax and a few traversing functions.
soren121
@soren121, I've got a fair bit of knowledge when it comes to Javascript, but regular expressions are still very new to me. I'm only beginning to understand them with any great deal of certainty. I'm glad to see that this helped you.
Jonathan Sampson
+1  A: 

Typically those scripts basically look for text/html content and just shove the code into the stream. Have you tried setting the content type to something else such as text/json, text/javascript, text/plain and see if it gets by without the injection?

epascarello
Well, sometimes I'm actually sending HTML, so...
soren121
Does that really matter? You read it as text with responseText. I am sure it is just a fragment and not an entire valid document.
epascarello
6 months later, I ended up sending responses encoded in JSON strings, sent with the text/json header. Gets through fine.
soren121
A: 

you see a lot of this with hand-generated xml, it isn't valid , so consumers try to fix-up the broken xml with hand-rolled regex -- its completely the wrong approach. you need to fix this at the source, at the broken host.

tish
The broken host won't listen, and there's not really a better host for the price (of free).
soren121