javascript - 防止javascript中的url受到xss攻击

标签 javascript jquery jsp security xss

我正在研究数据安全网络应用程序。我试图将参数从一个jsp页面传递到另一个页面,它很容易受到XSS攻击。如何防止jsp中的攻击

我尝试了以下操作:

1) 我的网址是:“localhost:8080/samplejsp.jsp?employeeId=16”,如果我尝试传递类似“localhost:8080/samplejsp.jsp?employeeId=16”的网址? employeeId=alert(%27Hello%27)' 显示alert('Hello')。

2)我有一个按钮,当我单击该按钮时,我将打开一个新的jsp页面,将employeeId传递给新的JSP “localhost:7070/samplejsp1.jsp?employeeId=16

3)我尝试限制employeeId,但验证给定输入只能是数字的正则表达式,否则我不会重定向。

4)现在,当我尝试单击传递 "employeeId=alert(%27Hello%27)" 的按钮时,它仍然显示警报消息('Hello'),但它没有重定向到新的jsp页面

我的问题是,如果发生 XSS(跨站脚本)攻击,如何停止警报消息?提前致谢

最佳答案

我的回答是基于你的说法:

3) I tried to restrict the employeeId but validating the regular expression that given input should only be number otherwise i am not redirecting.

由于您要专门处理数字,因此可以使用实数解析技术,而不是充满错误的正则表达式。

这是一种更好的方法。

int number = -1; //initializing with invalid numerical value for your application
try
{
   number = Integer.parseInt(userInputString);
}
catch(Exception ex)
{
   // could not convert the number
   response.sendRedirect("./error.jsp");
   return;
}

或者,如果由于您已经向响应写入内容而进行重定向为时已晚:

if(number == -1) // whatever is an invalid numerical value for your application
{
    out.print("There was an error with the number");
    return;
}

如果数字需要为 double 型,则 Double.parseDouble(str) 或 float Float.parseFloat(str)

对于整数,像“1d”这样的字符串值会在解析时抛出错误。但对于 double 和 float,数字后跟“d”或“f”(如“1d”和“1f”)将通过(即在数字变量中变为 1.0)。因此,请确保在解析数字后使用数字变量而不是用户输入的字符串。

关于javascript - 防止javascript中的url受到xss攻击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46457517/

相关文章:

javascript - 自动将焦点设置在动态加载的输入字段上

jquery - 在 JQueryUI 选项卡选择上加载 ASP.NET MVC 部分 View

java.lang.IllegalStateException : Neither BindingResult nor plain target object for bean name 'command' available as request attribute 错误

javascript - 为什么 date() 之间的差异是 NaN?

javascript - 将选择器光标设置到 contenteditable div 中的新位置

javascript - 如何卡住javascript中的嵌套对象?

javascript - AJAX 调用后未触发语义 UI 复选框事件

javascript - 添加 json2.js 时出现语法错误

java - 下载文件作为附件

javascript - AngularJs - 在 Controller 函数中获取 ng-src 属性的值