Skip to content

Commit a08debf

Browse files
authored
Merge pull request #549 from fugerit-org/alert-autofix-114
Potential fix for code scanning alert no. 114: Uncontrolled data used in path expression
2 parents e53dce8 + 431d47a commit a08debf

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

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

Lines changed: 25 additions & 3 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+
public 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( String.format( "File path %s is not within permitted base folder %s", file.getCanonicalPath(), baseFolder.getCanonicalPath() ) );
32+
}
33+
}
34+
2235
public static void copyFlavourList( File baseFolder, String actualFlavour ) throws IOException {
2336
copyResourcesList( baseFolder, "flavour", actualFlavour );
2437
}
@@ -37,16 +50,25 @@ private static void copyResourcesList( File baseFolder, String mode, String id )
3750
}
3851
}
3952

40-
protected static void insureParent( File file ) throws IOException {
53+
public static void insureParent( File file ) throws IOException {
4154
File parentFile = file.getParentFile();
42-
if ( !parentFile.exists() ) {
43-
log.info( "creates parent directory {}, mkdirs:? {}", parentFile.getCanonicalPath(), parentFile.mkdirs() );
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+
if ( !parentFile.exists() ) {
62+
log.info( "creates parent directory {}, mkdirs:? {}", parentFile.getCanonicalPath(), parentFile.mkdirs() );
63+
}
4464
}
4565
}
4666

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() );
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package test.org.fugerit.java.doc.project.facade;
2+
3+
import org.fugerit.java.doc.project.facade.FeatureFacade;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
10+
class TestFeatureFacade {
11+
12+
@Test
13+
void checkIfInBaseFolderTestOk() throws IOException {
14+
File baseFolder = new File( "." );
15+
File file = new File( "pom.xml" );
16+
FeatureFacade.checkIfInBaseFolder( baseFolder, file );
17+
Assertions.assertTrue( file.exists() );
18+
}
19+
20+
@Test
21+
void checkIfInBaseFolderTestKo() {
22+
File baseFolder = new File( "fj-doc-base" );
23+
File file = new File( "pom.xml" );
24+
Assertions.assertThrows( IOException.class, () -> FeatureFacade.checkIfInBaseFolder( baseFolder, file ) );
25+
}
26+
27+
@Test
28+
void insureParentTestNull1() throws IOException {
29+
File root = File.listRoots()[0];
30+
FeatureFacade.insureParent( root );
31+
Assertions.assertTrue( root.exists() );
32+
}
33+
34+
@Test
35+
void insureParentTestNull2() throws IOException {
36+
File root = File.listRoots()[0];
37+
if ( root != null ) {
38+
File[] list = root.listFiles();
39+
if ( list != null && list.length > 0 ) {
40+
FeatureFacade.insureParent( list[0] );
41+
Assertions.assertTrue( root.exists() );
42+
}
43+
}
44+
}
45+
46+
}

0 commit comments

Comments
 (0)