generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CNDIT-1708: Fix ATO security vulnerabilities for RTR services (#29)
- Loading branch information
Showing
5 changed files
with
145 additions
and
52 deletions.
There are no files selected for viewing
45 changes: 30 additions & 15 deletions
45
.../main/java/gov/cdc/etldatapipeline/investigation/config/LogDynamicFileAppenderConfig.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 |
---|---|---|
@@ -1,62 +1,77 @@ | ||
package gov.cdc.etldatapipeline.investigation.config; | ||
|
||
import ch.qos.logback.core.FileAppender; | ||
import lombok.Setter; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.TimeZone; | ||
|
||
public class LogDynamicFileAppenderConfig<E> extends FileAppender<E> { | ||
|
||
private String logFilePath; | ||
|
||
/** | ||
* Helper method used by Logback | ||
* Reading logFilePatch tag from dlt-logback.xml and return value | ||
* */ | ||
public void setLogFilePath(String logFilePath) { | ||
this.logFilePath = logFilePath; | ||
} | ||
* Helper method used by Logback | ||
* Reading logFilePatch tag from logback-config.xml and return value | ||
*/ | ||
@Setter | ||
private String logFilePath; | ||
|
||
/** | ||
* Purpose: Dynamically create log file if not exist | ||
* */ | ||
*/ | ||
@Override | ||
public void start() { | ||
if (logFilePath == null) { | ||
addError("Log file path is not configured"); | ||
return; | ||
} | ||
|
||
// Handle date formatting in the log file path | ||
if (logFilePath.contains("%d{")) { | ||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); | ||
dateFormat.setTimeZone(TimeZone.getDefault()); | ||
String formattedDate = dateFormat.format(new Date()); | ||
logFilePath = logFilePath.replace("%d{yyyy-MM-dd_HH-mm-ss}", formattedDate); | ||
} | ||
|
||
File logFile = new File(logFilePath); | ||
if (!logFile.isAbsolute()) { | ||
logFile = new File(System.getProperty("user.dir"), logFilePath); | ||
// Normalize and validate the log file path | ||
Path logDir = Paths.get(System.getProperty("user.dir")); // Define a safe directory for logs | ||
Path normalizedPath; | ||
try { | ||
// Resolve and normalize the logFilePath to ensure it's within the safe directory | ||
normalizedPath = logDir.resolve(logFilePath).normalize(); | ||
if (!normalizedPath.startsWith(logDir)) { | ||
addError("Log file path is outside the allowed directory"); | ||
return; | ||
} | ||
} catch (Exception e) { | ||
addError("Failed to resolve log file path", e); | ||
return; | ||
} | ||
|
||
// Create a File object using the validated and normalized path | ||
File logFile = normalizedPath.toFile(); | ||
|
||
try { | ||
if (!logFile.exists()) { | ||
Path parentDir = logFile.toPath().getParent(); | ||
Files.createDirectories(parentDir); | ||
if (parentDir != null && !Files.exists(parentDir)) { | ||
Files.createDirectories(parentDir); | ||
} | ||
Files.createFile(logFile.toPath()); | ||
} | ||
} catch (IOException e) { | ||
addError("Failed to create log file: " + logFilePath, e); | ||
addError("Failed to create log file: " + logFile.getAbsolutePath(), e); | ||
return; | ||
} | ||
|
||
setFile(logFile.getAbsolutePath()); // Set the File property with the log file path | ||
|
||
super.start(); | ||
} | ||
} | ||
} |
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
45 changes: 30 additions & 15 deletions
45
...rc/main/java/gov/cdc/etldatapipeline/observation/config/LogDynamicFileAppenderConfig.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
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
Oops, something went wrong.