Note that there are some explanatory texts on larger screens.

plurals
  1. POC# - Literal control vulnerable to XSS attack
    primarykey
    data
    text
    <p>I'm using a literal to display some javascript on a product page control. Basically what I'm doing is in my code behind I'm declaring a new stringbuilder, writing the script while inserting some dynamic variables to populate the script then setting the literal text to the stringbuilder. This leaves me open to xss attacks. What can I do to prevent this?</p> <p>EDIT. Here is an example of the stringbuilder. when the page gets loaded the xss vulnerability occurs right after the javascript is generated.</p> <pre><code>System.Text.StringBuilder sb = new System.Text.StringBuilder(); //loop through items in the collection for (int i = 0; i &lt; _prod.ActiveProductItemCollection.Count; i++) { sb.Append("&lt;script type='text/javascript'&gt;"); //add +1 to each item sb.AppendFormat("mboxCreate(\"product_productpage_rec{0}\",", i+1); sb.Append("\"entity.id=" + _prodID + "\","); sb.Append("\"entity.categoryId=" + _categoryID + "\","); sb.Append("\"entity.name=" + _prod.ActiveProductItemCollection[i].Title + "\","); sb.Append("\"entity.pageURL=" + Request.Url.ToString() + "\","); //The following value has been taken from the productImageControl code behind. //Might have to refactor in future as a property of the image control. string filename = AppSettingsManager.Current.ProductImagePathLarge + _prod.ActiveProductItemCollection[i].Sku + AppSettingsManager.Current.ProductImageExtension; sb.Append("\"entity.thumbnailURL=" + filename + "\","); sb.Append("\"entity.inventory=" + _prod.ActiveProductItemCollection.Count + "\","); sb.Append("\"entity.value=" + _prod.ActiveProductItemCollection[i].ActualPrice + "\","); sb.Append("\"entity.ProductItemID=" + _prod.ActiveProductItemCollection[i].Sku + "\","); sb.Append("\"entity.addToCartImg=~/Images/Buttons/btn_AddToCartFlat.gif\");&lt;"); //The last line has to be /script. &lt; inserted on prev line. do not change it or bad things will happen. sb.Append("/script&gt;"); } this.LiteralMBoxScript.Text = sb.ToString(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload