forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix](http)Enhanced Security Checks for Audit Log File Names (apache#…
…44612) ## 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.” (cherry picked from commit c0b8478)
- Loading branch information
1 parent
fdff4a6
commit 95834ff
Showing
2 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
fe/fe-core/src/test/java/org/apache/doris/httpv2/GetLogFileActionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.apache.doris.httpv2; | ||
|
||
import org.apache.doris.common.Config; | ||
import org.apache.doris.httpv2.rest.GetLogFileAction; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
public class GetLogFileActionTest { | ||
|
||
@TempDir | ||
public File tempDir; | ||
|
||
@BeforeAll | ||
public static void before() { | ||
File tempDir = new File("test/audit.log"); | ||
tempDir.mkdir(); | ||
Config.audit_log_dir = tempDir.getAbsolutePath(); | ||
} | ||
|
||
@Test | ||
public void testCheckAuditLogFileName() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { | ||
//private method checkAuditLogFileName | ||
GetLogFileAction action = new GetLogFileAction(); | ||
Method method = GetLogFileAction.class.getDeclaredMethod("checkAuditLogFileName", String.class); | ||
method.setAccessible(true); | ||
method.invoke(action, "audit.log"); | ||
method.invoke(action, "fe.audit.log.20241104-1"); | ||
Assertions.assertThrows(InvocationTargetException.class, () -> method.invoke(action, "../etc/passwd")); | ||
Assertions.assertThrows(InvocationTargetException.class, () -> method.invoke(action, | ||
"fe.audit.log.20241104-1/../../etc/passwd")); | ||
Assertions.assertThrows(InvocationTargetException.class, | ||
() -> method.invoke(action, "fe.audit.log.20241104-1; rm -rf /")); | ||
|
||
|
||
} | ||
} |