Skip to content

Commit

Permalink
[Fix](http)Enhanced Security Checks for Audit Log File Names
Browse files Browse the repository at this point in the history
## Purpose:

To improve the security of audit log files, a new method checkAuditLogFileName has been added to validate the file name and path to ensure they meet security requirements. This method is designed to prevent invalid file names and path traversal attacks, ensuring that only files within the designated directory can be accessed.↳

### Changes:

#### File Name Validation:

A regular expression check has been added to validate the file name: ^[a-zA-Z0-9._-]+$, restricting the file name to letters, numbers, dots, underscores, and hyphens.

 If the file name contains invalid characters (e.g., spaces, path traversal characters), a SecurityException is thrown with the message “Invalid file name.”
Path Validation:

The file name is resolved into a normalized path, and it is checked to ensure that it is within the allowed directory.

The path is constructed using Paths.get(Config.audit_log_dir).resolve(logFile).normalize().
If the path does not start with the specified audit log directory (Config.audit_log_dir), indicating an attempt to access outside the permitted directory (e.g., a path traversal attack), a SecurityException is thrown with the message “Invalid file path: Access outside of permitted directory.”
  • Loading branch information
CalvinKirs committed Nov 26, 2024
1 parent 530bee0 commit b4938b3
Showing 1 changed file with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -51,6 +53,23 @@
*/
@RestController
public class GetLogFileAction extends RestBaseController {
/**
* This method fetches internal logs via HTTP, which is no longer recommended and will
* be deprecated in future versions.
* <p>
* Using HTTP to fetch logs introduces serious security and performance issues:
* - **Security Risks**: Log content may expose sensitive information, allowing attackers to exploit the exposed
* HTTP endpoints.
* - **Performance Problems**: Frequent HTTP requests can cause significant system load, affecting the
* responsiveness and stability of the application.
* <p>
* It is strongly advised not to use this approach for accessing logs. Any new requirements should be
* handled using more secure, reliable, and efficient methods such as log aggregation tools (e.g., ELK, Splunk)
* or dedicated internal APIs.
* <p>
* **Note**: No new HTTP endpoints or types for log access will be accepted.
* Any further attempts to extend this HTTP-based log retrieval method will not be supported.
*/
private final Set<String> logFileTypes = Sets.newHashSet("fe.audit.log");

@RequestMapping(path = "/api/get_log_file", method = {RequestMethod.GET, RequestMethod.HEAD})
Expand Down Expand Up @@ -79,7 +98,13 @@ public Object execute(HttpServletRequest request, HttpServletResponse response)
String fileInfos = getFileInfos(logType);
response.setHeader("file_infos", fileInfos);
return ResponseEntityBuilder.ok();
} else if (method.equals(RequestMethod.GET.name())) {
}
if (method.equals(RequestMethod.GET.name())) {
try {
checkAuditLogFileName(logFile);
} catch (SecurityException e) {
return ResponseEntityBuilder.internalError(e.getMessage());
}
File log = getLogFile(logType, logFile);
if (!log.exists() || !log.isFile()) {
return ResponseEntityBuilder.okWithCommonError("Log file not exist: " + log.getName());
Expand All @@ -97,6 +122,17 @@ public Object execute(HttpServletRequest request, HttpServletResponse response)
return ResponseEntityBuilder.ok();
}

private void checkAuditLogFileName(String logFile) {
if (!logFile.matches("^[a-zA-Z0-9._-]+$")) {
throw new SecurityException("Invalid file name");
}
Path normalizedPath = Paths.get(Config.audit_log_dir).resolve(logFile).normalize();
// check path is valid or not
if (!normalizedPath.startsWith(Config.audit_log_dir)) {
throw new SecurityException("Invalid file path: Access outside of permitted directory");
}
}

private String getFileInfos(String logType) {
Map<String, Long> fileInfos = Maps.newTreeMap();
if (logType.equals("fe.audit.log")) {
Expand Down

0 comments on commit b4938b3

Please sign in to comment.