Skip to content

Commit a1995e8

Browse files
Potential fix for code scanning alert no. 114: Uncontrolled data used in path expression
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent e53dce8 commit a1995e8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

fj-doc-maven-plugin/src/main/java/org/fugerit/java/doc/project/facade/FeatureFacade.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.fugerit.java.doc.project.facade.flavour.ProcessEntry;
1212

1313
import java.io.*;
14+
import java.nio.file.Path;
1415
import java.util.HashMap;
1516
import java.util.Map;
1617

@@ -19,6 +20,18 @@ public class FeatureFacade {
1920

2021
private FeatureFacade() {}
2122

23+
/**
24+
* Checks that the given file is inside the baseFolder after normalization.
25+
* Throws IOException if not.
26+
*/
27+
private static void checkIfInBaseFolder(File baseFolder, File file) throws IOException {
28+
Path base = baseFolder.getCanonicalFile().toPath().normalize();
29+
Path target = file.getCanonicalFile().toPath().normalize();
30+
if (!target.startsWith(base)) {
31+
throw new IOException("File path " + file.getCanonicalPath() + " is not within permitted base folder " + baseFolder.getCanonicalPath());
32+
}
33+
}
34+
2235
public static void copyFlavourList( File baseFolder, String actualFlavour ) throws IOException {
2336
copyResourcesList( baseFolder, "flavour", actualFlavour );
2437
}
@@ -39,6 +52,13 @@ private static void copyResourcesList( File baseFolder, String mode, String id )
3952

4053
protected static void insureParent( File file ) throws IOException {
4154
File parentFile = file.getParentFile();
55+
// Defensive: check parent is within project's root as well
56+
if (parentFile != null) {
57+
File baseFolder = file.getParentFile().getParentFile();
58+
if (baseFolder != null) {
59+
checkIfInBaseFolder(baseFolder, parentFile);
60+
}
61+
}
4262
if ( !parentFile.exists() ) {
4363
log.info( "creates parent directory {}, mkdirs:? {}", parentFile.getCanonicalPath(), parentFile.mkdirs() );
4464
}
@@ -47,6 +67,8 @@ protected static void insureParent( File file ) throws IOException {
4767
protected static void copyFile(String path, File baseFolder, String basePath ) {
4868
SafeFunction.apply( () -> {
4969
File outputFile = new File( baseFolder, path );
70+
// Validate that the output file is inside the intended base folder
71+
checkIfInBaseFolder(baseFolder, outputFile);
5072
insureParent( outputFile );
5173
String fullPath = basePath+path;
5274
log.info( "copy path '{}' to file '{}'", fullPath, outputFile.getCanonicalPath() );

0 commit comments

Comments
 (0)