如何防止外部脚本调用我的asp.net C#代码XSS,?

问题描述 投票:0回答:1

html 脚本在页面加载事件后面调用我的 asp.net 代码,这是我的问题

我有以下 asp.net 页面:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="search_function" Codebehind="search_function.aspx.cs" %>

这是 ASP.NET C# 背后的代码:

 <code>protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        PerformSearch();
    }
    catch (Exception ex)
    {
        Util.SendErrorEmail(ex);
        PerformSearch();
    }
}

这个js文件有ajax调用search_function

 ` $.ajax({
                    url: BasePath + 'search_function.aspx',
                    data: 'action=search&value=' + t.value,
                    type: 'post',
                    
                    cache: false,
                    success: function (html) {
                        results.stop().show().fadeTo(250, 1);
                        results.html(html);
                        spinner.html("");
                        var addValue = 200;
                        if ($('#ctl00_cph_searchControl_hidIsIndex').val() == "1") {
                            addValue = -450;
                        }
                        results.css({
                            "position": "absolute",
                            "top":"107px"
                            //"left": $('#search').position().left + addValue + "px"
                        });
                       // alert(window.location);
                        BindResultsHover();  //bind our results hover listener to the new             results
                    }
                });`

这个工作正常,但下面的 html 脚本调用我的 Page_Load 事件并收到警报 1.:

<html>
<body>
<form action=http://localhost:51503/search_function.aspx method="POST">
<input type="hidden" name="action" value="search" />
<input type="hidden" name="value" value="1&quot;onfocus=&quot;alert`1`&quot;autofocus=&quot;" />
<input type="submit" value="Submit request" />
 </form>
 <script>
history.pushState('', '', '/');
document.forms[0].submit();
</script>
</body>
</html>

我已尝试在 page_load 事件中使用 IsValid,但该 XSS 脚本仍然调用我的页面。

javascript c# asp.net csrf xss
1个回答
0
投票

@user24846708:- 脚本并不是一件坏事,除非它们被用于恶意目的(XSS 就是其中之一)。 您需要清楚了解您的需求您需要完全清楚要编写什么代码以及在哪里编写

Ajax 调用 例如,可以提供性能优势,因为仅刷新页面的一部分而不是整个页面,并且您仍然可以获得所需的输出。

例如:- 如果有一个业务逻辑将验证用户的登录密码,那么在您的情况下,该代码应该在服务器上执行(使用 C#)。它还可能会调用数据库来检查密码。例如,您可以使用脚本来显示密码不能为空的验证。

脚本在浏览器上执行,而 C# 代码在服务器上执行。如果代码在服务器上执行,则逻辑对用户隐藏。

我相信您所问的是如何保护您的网站以及如何使用最佳安全实践编写代码。这是一个很大的话题,无法在这里解释。但是,我会提供一些链接供您参考。

XSS:- https://www.geeksforgeeks.org/what-is-cross-site-scripting-xss/

XSS 预防:- https://www.esecurityplanet.com/endpoint/prevent-xss-attacks/

C# 最佳安全实践:- https://learn.microsoft.com/en-us/dotnet/standard/security/https://www.c-sharpcorner.com/article/c-sharp-security -安全编码的最佳实践/

© www.soinside.com 2019 - 2024. All rights reserved.