# iRule Security 101 - #03 - HTML Comments

**URL:** <https://community.f5.com/t/irule-security-101-03-html-comments/66688>\
**Category:** F5 Technical Articles\
**Tags:** dev, big-ip, news, adn, security, 101-level, tech-tip, basic, getting-started\
**Created:** [August 29, 2007, 9:20pm UTC](https://community.f5.com/t/irule-security-101-03-html-comments/66688 "2007-08-29T21:20:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Pruitt](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/joe_pruitt/32/8438_2.png) [@Joe\_Pruitt](https://community.f5.com/u/Joe_Pruitt)\
**Post date:** [August 29, 2007, 9:20pm UTC](https://community.f5.com/t/irule-security-101-03-html-comments/66688/1 "2007-08-29T21:20:00Z")

</div>

In this session of iRules Security 101, I’ll walk you through on a process to strip unnecessary content from your outbound application responses.&nbsp; Section 3.2.5 of [RFC 1886 (The Hypertext Markup Language - 2.0)](http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt) allows for comments to be enclosed within HTML content.&nbsp; In certain cases, this could lead to unwanted information being exposed.

```quote
<body>
  ...
  <!-- Pull user info from Users table in database and format a list from that data -->
  ...
</body>

```

In this article, I’ll show you how to remove all those HTML comments from all HTTP traffic that’s leaving your network. Other articles in the series:

- [iRule Security 101 – #1 – HTTP Version](http://devcentral.f5.com/s/weblogs/Joe/archive/2007/07/27/2899.aspx)
- [iRule Security 101 – #02 – HTTP Methods and Cross Site Tracing](http://devcentral.f5.com/s/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/95/iRule-Security-101--02--HTTP-Methods-and-Cross-Site-Tracing.aspx)
- [iRule Security 101 – #03 – HTML Comments](http://devcentral.f5.com/s/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/98/iRule-Security-101--03--HTML-Comments.aspx)
- [iRule Security 101 – #04 – Masking Application Platform](http://devcentral.f5.com/s/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/99/iRule-Security-101--04--Masking-Application-Platform.aspx)
- [iRule Security 101 – #05 – Avoiding Path Traversal](http://devcentral.f5.com/s/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/104/iRule-Security-101--05--Avoiding-Path-Traversal.aspx)
- [iRule Security 101 – #06 – HTTP Referer](http://devcentral.f5.com/s/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/113/iRule-Security-101--06--HTTP-Referer.aspx)
- [iRule Security 101 – #07 – FTP Proxy](http://devcentral.f5.com/s/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/126/iRule-Security-101--07--FTP-Proxy.aspx)
- [iRule Security 101 – #08 – Limiting POST Data](http://devcentral.f5.com/s/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/148/iRule-Security-101--08--Limiting-POST-Data.aspx)
- [iRule Security 101 – #09 – Command Execution](http://devcentral.f5.com/s/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/162/iRule-Security-101--09--Command-Execution.aspx)

An early of every (respectable) developers training is to thoroughly comment ones code.&nbsp; Typically this is a safe process as source code is either compiled or obfuscated before being made available to the client.&nbsp; But, with the advent of web based applications, situations can occur that could [cause your source code to be leaked](http://http://www.techcrunch.com/2007/08/11/facebook-source-code-leaked/).&nbsp; While stripping out HTML comments will not completely secure all content breaches, it will do a small part in making sure that any internal information included in asp/jsp/html development is not allowed to reach the masses.

The following example will inspect all HTML responses for patterns matching HTML comments and replace those characters with spaces, effectively erasing them from the outside world.

```code
when HTTP_REQUEST {
  # Don't allow data to be chunked. This ensures we don't get
  # a comment that is spread across two chunked boundaries.
  if { [HTTP::version] eq "1.1" } {
    if { [HTTP::header is_keepalive] } {
      HTTP::header replace "Connection" "Keep-Alive"
    }
     HTTP::version "1.0"
  }
}
when HTTP_RESPONSE {
  # Ensure all of the HTTP response is collected
  if { [HTTP::header exists "Content-Length"] } {
     set content_length [HTTP::header "Content-Length"]
  } else {
     set content_length 1000000
  }
  if { $content_length > 0 } {
     HTTP::collect $content_length
  }
}
when HTTP_RESPONSE_DATA {
  # Find the HTML comments
  set indices [regexp -all -inline -indices {<![\r\n\t]*--([^\-]|[\r\n]|-[^\-])*[^/][^/]--[\r\n\t]*>} [HTTP::payload]]
  # Replace the comments with spaces in the response
  #log local0. "Indices: $indices"
  foreach idx $indices {
     set start [lindex $idx 0]
     set len [expr {[lindex $idx 1] - $start + 1}]
     #log local0. "Start: $start, Len: $len"
     HTTP::payload replace $start $len [string repeat " " $len]
  }
}

```

The special sauce in here is the regular expression used to search for the comments.&nbsp; I’ll leave it to you all to figure out how the regular expression works and possibly rehash it when I start the “iRules Ninja” series.

Bonus points to anyone who can comment on why I added the “[^/][^/]” towards the end of the regexp.

[Get the Flash Player](http://www.macromedia.com/go/getflashplayer) to see this player.

---

<div class="post-metadata">

**Author:** ![JRahm](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/jrahm/32/3327_2.png) [@JRahm](https://community.f5.com/u/JRahm)\
**Post date:** [August 30, 2007, 3:27pm UTC](https://community.f5.com/t/irule-security-101-03-html-comments/66688/2 "2007-08-30T15:27:15Z")

</div>

Is the trailing negated match to disqualify javascript comments?

---

<div class="post-metadata">

**Author:** ![Joe\_Pruitt](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/joe_pruitt/32/8438_2.png) [@Joe\_Pruitt](https://community.f5.com/u/Joe_Pruitt)\
**Post date:** [August 30, 2007, 8:02pm UTC](https://community.f5.com/t/irule-security-101-03-html-comments/66688/3 "2007-08-30T20:02:58Z")

</div>

Somehow I knew citizen\_elah would be quick to answer…

Yes, typically embedded javascript is enclosed in html comments and is every case I can think of, it is undesirable to erase the client side javascript before sending the page to the browser.

-Joe

---

<div class="post-metadata">

**Author:** ![Mike\_Lowell\_108](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Mike\_Lowell\_108](https://community.f5.com/u/Mike_Lowell_108)\
**Post date:** [September 4, 2007, 8:40pm UTC](https://community.f5.com/t/irule-security-101-03-html-comments/66688/4 "2007-09-04T20:40:46Z")

</div>

Actually that part of the regex seems incorrect. My memory is telling me that bracket expressions contain a list of characters, not a string, so the 2nd slash is redundant. “man 7 regex” seems to confirm this:

“If the list begins with `^', it matches any single character […]”

---

<div class="post-metadata">

**Author:** ![Mike\_Lowell\_108](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Mike\_Lowell\_108](https://community.f5.com/u/Mike_Lowell_108)\
**Post date:** [September 4, 2007, 8:49pm UTC](https://community.f5.com/t/irule-security-101-03-html-comments/66688/5 "2007-09-04T20:49:54Z")

</div>

Oh, and I almost forgot: the following PCRE has served me well…

```tcl
s///msg;

```

You’ll note that I took a different approach to matching. I’m not sure it’s better, bit it is different, for sure. 🙂 The idea was to make sure I was allowing “anything” except sequences that would mean something special. I settled on this approach while trying to match a bunch of fairly complex and malformed-looking comments. I’m not sure I solved the problem perfectly, but I did solve it for the test cases I was interested in.

---

<div class="post-metadata">

**Author:** ![josh4534\_106539](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@josh4534\_106539](https://community.f5.com/u/josh4534_106539)\
**Post date:** [April 9, 2011, 6:19pm UTC](https://community.f5.com/t/irule-security-101-03-html-comments/66688/6 "2011-04-09T18:19:40Z")

</div>

Is there an existing method to strip out the C-style comments of CSS?

/\* this is a comment \*/
