Skip to content

Commit fc195f9

Browse files
github-actions[bot]KB Bot
andauthored
Added new kb article autocompletebox-html-encoding-items (#723)
Co-authored-by: KB Bot <[email protected]>
1 parent ca56603 commit fc195f9

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: HTML Encoding Items in RadAutoCompleteBox Control
3+
description: Learn how to handle HTML encoding in the RadAutoCompleteBox control to securely display special characters while preventing script injection vulnerabilities.
4+
type: how-to
5+
page_title: Handle HTML Encoding in RadAutoCompleteBox Items
6+
meta_title: Handle HTML Encoding in RadAutoCompleteBox Items
7+
slug: autocompletebox-html-encoding-items
8+
tags: autocompletebox, ui for asp.net ajax, html encoding, xss, security
9+
res_type: kb
10+
ticketid: 1702307
11+
---
12+
13+
## Environment
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>UI for ASP.NET AJAX AutoCompleteBox</td>
19+
</tr>
20+
<tr>
21+
<td>Version</td>
22+
<td>All</td>
23+
</tr>
24+
</tbody>
25+
</table>
26+
27+
## Description
28+
29+
I want to display text with special characters like `<`, `>`, and `'` in the [RadAutoCompleteBox](https://docs.telerik.com/devtools/aspnet-ajax/controls/autocompletebox/overview) control for UI for ASP.NET AJAX. Adding raw text results in special characters being rendered as HTML, which may expose the site to script injection vulnerabilities. Encoding the text ensures safe display but causes the tags to appear encoded. I need to know how RadAutoCompleteBox handles HTML encoding and how to prevent vulnerabilities while displaying special characters as literal text.
30+
31+
This knowledge base article also answers the following questions:
32+
- How to encode special characters in RadAutoCompleteBox?
33+
- Does RadAutoCompleteBox automatically encode HTML?
34+
- How to prevent XSS vulnerabilities in RadAutoCompleteBox?
35+
36+
## Solution
37+
38+
RadAutoCompleteBox does not automatically HTML encode the text of its items. You must handle the encoding or sanitization of data before adding it to the control to prevent cross-site scripting (XSS) vulnerabilities. Follow these solutions:
39+
40+
Utilize the `DropDownItemTemplate` feature to encode item text dynamically at runtime.
41+
42+
````ASP.NET
43+
<telerik:RadAutoCompleteBox runat="server" ID="auto" DataTextField="Text" DataValueField="Value">
44+
<DropDownItemTemplate>
45+
<%# System.Web.HttpUtility.HtmlEncode(DataBinder.Eval(Container.DataItem, "Text")) %>
46+
</DropDownItemTemplate>
47+
</telerik:RadAutoCompleteBox>
48+
````
49+
50+
````C#
51+
protected void Page_Load(object sender, EventArgs e)
52+
{
53+
List<Item> items = new List<Item>
54+
{
55+
new Item() { Value = "1", Text = "<script>alert('xss')</script>" },
56+
new Item() { Value = "2", Text = "name <>" },
57+
new Item() { Value = "3", Text = "'single-quote'@example.com" }
58+
};
59+
60+
auto.DataSource = items;
61+
}
62+
63+
public class Item
64+
{
65+
public string Value { get; set; }
66+
public string Text { get; set; }
67+
}
68+
````
69+
70+
If you prefer plain text without any HTML tags, use regular expressions to remove tags.
71+
72+
````C#
73+
string userInput = "<b>Hello</b>";
74+
string plainText = Regex.Replace(userInput, "<.*?>", string.Empty);
75+
RadAutoCompleteBox1.Entries.Add(new AutoCompleteBoxEntry(plainText, null));
76+
````
77+
78+
Always validate and sanitize user input on the server side before saving or displaying it. This adds additional protection against XSS attacks.
79+

0 commit comments

Comments
 (0)