简体   繁体   English

如何解决在Java代码中创建具有指定名称的文件时的安全漏洞?

[英]How to solve a security break in creating File with a specified name in Java code?

The security code scan does not like the following Java code:安全码扫描不喜欢以下Java代码:

File localCsvFile = new File(MessageFormat.format(REPORT_ZIP_PATH_FORMAT,
            REPORT_LOCAL_PATH, str1, str2, count));

According to the scan the problem is in follows:根据扫描,问题如下:

The software allows user input to control or influence paths or file names that are used in filesystem operations.该软件允许用户输入来控制或影响文件系统操作中使用的路径或文件名。 This could allow an attacker to access or modify system files or other files that are critical to the application.这可能允许攻击者访问或修改对应用程序至关重要的系统文件或其他文件。

What is the best way to solve this problem?解决这个问题的最佳方法是什么? I am in Spring Boot Java application.我在 Spring Boot Java 应用程序中。 One of the proposed solution is to use AccessReferenceMap, how it works?建议的解决方案之一是使用 AccessReferenceMap,它是如何工作的?

This problem is discussed in OWASP security guidelines. OWASP 安全指南中讨论了此问题。 The solution can be a little bit different based on the application's host operating system, but here are some common guidelines -根据应用程序的主机操作系统,解决方案可能略有不同,但这里有一些通用准则 -

  • If possible try to avoid taking the file path from the user.如果可能,尽量避免从用户那里获取文件路径。 Evaluate if there are any alternative solutions without asking the user to enter the path on the server.在不要求用户输入服务器上的路径的情况下评估是否有任何替代解决方案。 I don't have the full context of your scenario but most of the time it is achievable.我没有你的场景的完整背景,但大多数时候它是可以实现的。 Maybe you can store the report on a server path based on user ID, example user1 files will be stored under /user/tmp/user1, this will eliminate asking the user to enter full path or absolute path.也许您可以根据用户 ID 将报告存储在服务器路径上,例如 user1 文件将存储在 /user/tmp/user1 下,这将消除要求用户输入完整路径或绝对路径的情况。

  • Validate the user input as much as possible on the server-side.在服务器端尽可能验证用户输入。 This will restrict user access as much as possible.这将尽可能限制用户访问。 For example, if the user is expected to access pdf then add validation to check the user input data to end with .pdf例如,如果希望用户访问 pdf,则添加验证以检查用户输入数据以.pdf结尾

  • Never pass user input directly to file system APIs.切勿将用户输入直接传递给文件系统 API。 Prefix it with a predefined path before sending it to file system APIs.在将其发送到文件系统 API 之前,使用预定义的路径为其添加前缀。

  • Make sure your file system APIs are reading/writing with a restricted operating system user having access to a specific path where you store only files required by the user.确保您的文件系统 API 正在读取/写入受限制的操作系统用户,该用户有权访问您仅存储用户所需文件的特定路径。

You can read more about this vulnerability and solutions here您可以在此处阅读有关此漏洞和解决方案的更多信息

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

相关问题 创建bat文件时如何解决java.lang.NoClassDefFoundError? - How to solve java.lang.NoClassDefFoundError when creating bat file? 使用用户输入的文件名创建Java编程来编写网页源代码 - Creating a java programming to webpage source code with user input file name 如何解决java.security.AccessControlException - How to solve java.security.AccessControlException 如何解决java.security.AccessControlException? - How to solve java.security.AccessControlException? 如果pdf从java代码生成,如何解决损坏的pdf文件? - How to solve corrupted file pdf, if pdf generate from java code? 如何解决-错误创建名称为'defaultController'的bean - How to solve - Error creating bean with name 'defaultController' 在Java应用程序中创建包含安全性的配置文件 - Creating a configuration file in a Java application including security 在 java 中创建一个具有指定名称的临时文件 - create a temporary file with a specified name in java 我如何通过java代码将单个文件上传到指定项目的指定分支,因为没有合适的api接口 - how can i upload a single file to specified branch of specified project by java code , as there is no suitable api interface 如何解决Java错误“系统找不到指定的路径”? - How to Solve Java Error “The system cannot find the path specified”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM