1

ブログサイトを開発しました。ユーザーは自分のブログを公開できます。

ユーザーは、javascript、c#、java などのコードと、strong タグなどの html タグを入力できます。

xssを回避するために、以下のように危険な文字をすべて置き換えたいのですが、このセキュリティは?

WebForm1.aspx.cs:

 public partial class WebForm1 : System.Web.UI.Page
    {
        public string V;
        protected void Page_Load(object sender, EventArgs e)
        {
            V = HtmlEncode("Get content from DB");

        }
        public string HtmlEncode(string theString)
        {
            theString = theString.Replace(">", ">");
            theString = theString.Replace("<", "&lt;");
            theString = theString.Replace(" ", "&nbsp;");
            theString = theString.Replace(" ", "&nbsp;");
            theString = theString.Replace("\"", "&quot;");
            theString = theString.Replace("\'", "'");
            theString = theString.Replace("\n", "<br/> ");
            ..................
            ..................
            ...................
            return theString;
        }
    }

WebForm1.aspx:

   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Test.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <%=V%>
    </div>
    </form>
</body>
</html>
4

1 に答える 1

3

解決策は、ユーザーがいわゆる「bb-code」を入力できるようにすることです。

Server.HtmlEncodeユーザーが実際のタグを入力できないようにするために引き続き使用しますが、置換の入力は許可します。

たとえば、ユーザーが次のように入力した場合:

 [b]test[/b]

サーバー側のコードでは、それを次のように置き換えます

 <strong>test</strong>

斜体、その他のスタイルや色についても同様です。このようにして、許可された HTML のサブセットを制御します。BB コードの使用は、フォーラムやブログでの標準的な方法です。

于 2013-09-19T01:26:15.527 に答える