简体   繁体   English

安全存储按钮点击数据的方法

[英]Safe way to store data for button clicks

I am trying to find out what the safest way to store data for use when the user clicks on a button. 我正在尝试找出最安全的方式来存储用户单击按钮时使用的数据。

I know that you can store data in attributes(either the value attribute or a data- attribute) of the button tag like so: 我知道您可以将数据存储在按钮标签的属性(值属性或数据属性)中,如下所示:

<button type="button" value="1" data-value="1">Click me!</button>

But the problem with this is that the user(probably really only advanced users) can manipulate the value with firebug or some other app and THEN click the button and send over different data. 但是,这样做的问题是用户(可能实际上只有高级用户)可以使用Firebug或其他应用程序操纵值,然后单击按钮并发送不同的数据。 I fully understand that I need to check the input before I try to do anything with the sent data. 我完全理解,在尝试对发送的数据执行任何操作之前,需要检查输入。

I also found out that I could use jQuery's .data() to attach data to dom elements, which seems a bit more useful. 我还发现我可以使用jQuery的.data()将数据附加到dom元素,这似乎更有用。 I'm not exactly sure how the data is stored, but I assume its harder to manipulate. 我不确定数据的存储方式,但是我认为它更难处理。

What got me really interested in this question was when I was looking through Soundcloud's code in firebug, I saw that none of the "like" buttons had data attached to the buttons. 使我对这个问题真正感兴趣的是,当我在Firebug中浏览Soundcloud的代码时,我发现“赞”按钮中没有一个附加数据。 I tried deleting/manipulating elements/data and the buttons still worked. 我尝试删除/操作元素/数据,但按钮仍然有效。 So it got me thinking that they are probably using a similar process to what jquerys data() is doing. 因此,我想到他们可能正在使用类似于jquerys data()正在执行的过程。

I just want to know if there is a safer way to store data or a way so that the user can't manipulate the data before clicking the button. 我只想知道是否存在一种更安全的数据存储方式或一种使用户无法在单击按钮之前操纵数据的方式。

Consider this function: 考虑以下功能:

function setupPrivateData(element) {
  var private = 1; 
  element.setPrivate = function ( d ) { private = d; }
  element.getPrivate = function ( ) { return private; }
}

When called with some DOM element it will add two methods to it: .setPrivate(val) and .getPrivate() . 当使用某些DOM元素调用时,它将向其中添加两个方法: .setPrivate(val).getPrivate()

These are the only methods that will allow you to access and modify that private variable associated with the element. 这些是允许您访问和修改与元素关联的私有变量的唯一方法。

The user can always manipulate data. 用户可以随时操纵数据。 Nothing stops an advanced user to access object properties or call a jquery.data() on their own. 没有什么可以阻止高级用户访问对象属性或自行调用jquery.data()的。

Something you could do in vanilla js would be: 您可以在Vanilla JS中执行的操作是:

  var div = document.getElementById("test"); div.something = "hidden value"; div.addEventListener("click", function() { alert(this.something); }); 
 <div id="test">click me</div> 

The best way would to be a serverside verification if the sent data is valid or not. 最好的方法是在服务器端验证发送的数据是否有效。

Besides that, you could try to wrap your code in an anonymous function to deny the user access to the object: 除此之外,您可以尝试将代码包装在匿名函数中,以拒绝用户访问该对象:

(function() {
    var data = {};
    data.something = "test";
})()

But even that fails as soon as the user manipulates your files and adds for instance a debugger statement. 但是,即使这样,一旦用户操作您的文件并添加例如调试器语句,该操作也会失败。

You can obfuscate your javascript but the only validation has to be done on your server. 您可以混淆JavaScript,但是唯一的验证必须在服务器上完成。 For example, I tried to get the weather from theweathernetwork . 例如,我试图让从天气theweathernetwork They have hidden their API call using multiple files and callbacks. 他们使用多个文件和回调隐藏了API调用。 In my opinion, it's just more challenging (funnier) if you want to reverse-engineer their site. 我认为,如果您要对他们的网站进行反向工程,这将更具挑战性。

Javascript can't be secure. JavaScript不能保证安全。 Never trust user input 永远不要相信用户输入

If you are logging button clicks, the safest way to keep track is to save and validate on the server side. 如果您记录的是按钮单击,那么进行跟踪的最安全方法是在服务器端进行保存和验证。

For example, when you click a like button on Soundcloud, it makes an HTTP request to Soundcloud's server, records that you clicked the button, and marks it as a favorite. 例如,当您单击Soundcloud上的“喜欢”按钮时,它将向Soundcloud的服务器发出HTTP请求,记录您单击该按钮并将其标记为收藏。 This way, if the same user clicks the button anytime in the future, it can check before incrementing the number of favorites. 这样,如果将来同一时间单击同一用户,则可以在增加收藏夹数之前进行检查。

The number displayed in the button is also pulled in from the database when the view is rendered. 呈现视图时,按钮中显示的数字也会从数据库中提取。

This is a huge topic, and you have a lot to learn, far too much for a comment here. 这是一个巨大的话题,您有很多东西要学习,这里的评论太多了。 Anything "stored" in an attribute in the HTML source is absolutely not secure, it can be changed very very easily. HTML源代码中“存储”在属性中的任何内容绝对是不安全的,可以非常轻松地对其进行更改。

The most common way of dealing with this would be to use a cookie, but even with some effort these can be manipulated. 解决此问题的最常见方法是使用cookie,但即使付出一些努力,也可以对其进行操作。

If security is important, find some way of identifying your users (possibly by IP, but even that isn't fool proof!) and keep the data on your server, linked to a user ID which can be retrieved after the button is clicked. 如果安全性很重要,请找到某种方法来识别您的用户(可能是通过IP,但这还不是万无一失!),然后将数据保存在服务器上,并链接到用户ID,单击该用户ID可以在单击该按钮后对其进行检索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM