简体   繁体   English

这个客户端应用程序安全吗?

[英]Is this client side application secure?

Here is a simplified version of a program I am using to work with a file using javascript on the client side.这是我用来在客户端使用 javascript 处理文件的程序的简化版本。

HTML: HTML:

<input id='myinput' type='file' accept='.png, .jpg, .jpeg' /> 

Javascript: Javascript:

 var myinput = document.getElementById('myinput');
 myinput.addEventListener('change', function(e) {

   /* 1. capture the file */
   var file = e.target.files[0];

   /* 2. make a fileReader object */
   var reader = new FileReader();

   /* 3. the load event listener */
   reader.addEventListener('load', function(e) {
     var fileContentAsText = e.target.result; // <-- is this line safe?
     /* 5. functions for manipulating the file would go here */
   }, false); 

   /* 4. passing the file to the filereader object */
   reader.readAsText(file);

 });

More or less, my program is for manipulating png or jpg type files, manipulating them, then making the modified version available for download.或多或少,我的程序用于处理 png 或 jpg 类型的文件,处理它们,然后使修改后的版本可供下载。

Everything happens on the client side.一切都发生在客户端。

Since nothing is being sent to the server, are there any security vulnerabilities that I should be concerned about?由于没有任何内容被发送到服务器,是否有任何我应该关注的安全漏洞?

If I was sending it to the server, almost everything I would have done to validate the file would have been in php, and I would have been reasonably assured that the operation was secure enough.如果我将它发送到服务器,几乎我为验证文件所做的一切都将在 php 中进行,并且我可以合理地确保操作足够安全。

Since I am not sending it to the server, none of those php mechanisms I would have applied are applicable.由于我没有将它发送到服务器,因此我将应用的那些 php 机制都不适用。

Actual questions:实际问题:

  1. Given that everything will happen on the client side, do I need to validate the file?鉴于一切都将在客户端发生,我是否需要验证文件?
  2. If so, why?如果是这样,为什么? And what actions can I take?我可以采取什么行动?

What comes to mind are text boxes that set the innerHTML of other elements, or where src/onerror attributes can be exploited for nefarious purposes.想到的是设置其他元素的innerHTML的文本框,或者可以利用src/onerror属性进行恶意目的的文本框。 Are these types of attacks something I need to watch out for?我需要注意这些类型的攻击吗? Because everything I have read on this matter concerns validating a file that goes to the server.因为我在这件事上读到的所有内容都涉及验证发送到服务器的文件。

You have to be aware of vulnerabilities like for example cross-site scripting, open redirection, sensitive information storage (not a comprehensive list), which may also affect client-only applications.您必须注意诸如跨站点脚本、开放重定向、敏感信息存储(不是全面列表)等漏洞,这些漏洞也可能影响仅限客户端的应用程序。

In practice this means the important part is point 5. in the comments - what happens to the loaded file.实际上,这意味着重要的部分是注释中的第5.点 - 加载的文件会发生什么。 For example you could store part of it after processing in say localStorage, which may present a risk if "sensitive" info is stored (be it whatever in your context).例如,您可以在 localStorage 中处理后存储它的一部分,如果存储“敏感”信息(无论在您的上下文中),这可能会带来风险。 Or for example if a part is written back to the client (which I think is the case if I understand correctly), it could pose an injection threat.或者例如,如果一个零件被写回给客户端(如果我理解正确的话,我认为是这种情况),它可能会构成注入威胁。 The most straightforward injection would be XSS if you for example write somehing to html, like the comment from the image exif.最直接的注入是 XSS,例如,如果您将一些内容写入 html,例如图像 exif 中的注释。 But you should also consider what happens to the resulting file after the user receives the result.但是您还应该考虑在用户收到结果后结果文件会发生什么。 Will it be displayed in an application that may be vulnerable to some kind of an injection, or for example buffer overflow?它是否会显示在可能容易受到某种注入攻击或例如缓冲区溢出的应用程序中? Consider an image viewer that has a known buffer overflow vulnerability.考虑一个具有已知缓冲区溢出漏洞的图像查看器。 Say a malicious user prepares an image and gives it to the victim.假设恶意用户准备了一个图像并将其提供给受害者。 This image may be crafted in a way that it does not directly result in buffer overflow, but after the transformations your application does to it, it exploits vulnerabilities in the client that displays it.此图像的制作方式可能不会直接导致缓冲区溢出,但在您的应用程序对其进行转换后,它会利用显示它的客户端中的漏洞。 Sure, it's a vulnerability of the 3rd party client software, but your application was used to hide the exploit and facilitate an attack.当然,这是 3rd 方客户端软件的一个漏洞,但您的应用程序被用来隐藏漏洞并促进攻击。

So try to consider the system as a whole, your apllication may just be a building block in a complex attack, and your goal should be to minimize the usefulness for an attacker.因此,尝试将系统视为一个整体,您的应用程序可能只是复杂攻击的构建块,您的目标应该是最大限度地减少对攻击者的有用性。 This is vague advice, but it all depends on what you actually do to the uploaded file.这是一个含糊的建议,但这完全取决于您对上传的文件实际执行的操作。

Another thing that comes to mind is a malicious uploaded image may hang the browser process, a denial of service on the client.想到的另一件事是恶意上传的图像可能会挂起浏览器进程,这是对客户端的拒绝服务。 You should be able to avoid this.您应该能够避免这种情况。 (Sure, the user uploads it for herself, but may not be aware of what it is she's uploading, having received it from somebody else - may or may not be valid in your scenario.) (当然,用户为自己上传了它,但可能不知道她上传的是什么,从其他人那里收到它 - 在您的场景中可能有效,也可能无效。)

Aldo during processing, parts of the image info may be used for say querying things, for example to find the camera vendor from the exif info or whatever.在处理过程中,部分图像信息可能用于查询事物,例如从 exif 信息中查找相机供应商或其他任何内容。 Such queries may also be susceptible to injection, resulting in query forgery via a malicious image.此类查询也可能容易受到注入,从而导致通过恶意图像伪造查询。 So anything you read from the file during processing should be treated as untrusted in general, the same as if it was done on the server.因此,您在处理期间从文件中读取的任何内容通常都应该被视为不受信任,就像在服务器上完成的一样。

Client side will never be safe.客户端永远不会安全。 Even though you're using accept attribute in input type="file" , it will only identify the open dialog to identify the given types and show them only.即使您在input type="file"使用accept属性,它也只会识别打开的对话框以识别给定的类型并仅显示它们。 But user can still choose Select All option there and and select any type of file.但是用户仍然可以在那里Select All选选项并选择任何类型的文件。 And reader.readAsText(file);reader.readAsText(file); will read it as it is and will not validate.将按原样读取它并且不会验证。 This means hacker can upload anything desired and can inject in the application.这意味着黑客可以上传任何想要的东西并可以注入应用程序。 Thus, always consider making validation through server side language.因此,始终考虑通过服务器端语言进行验证。

then making the modified version然后制作修改版

This will not make any sense since application can upload anything rather than the specified file types.这没有任何意义,因为应用程序可以上传任何内容而不是指定的文件类型。

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

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