Browse Source

Fix the input sanitation of the basecode directory name. Dots are now forbidden and file separators are automatically removed.

Timur Sağlam 5 years ago
parent
commit
463943a612

+ 1 - 1
jplag/src/main/java/de/jplag/CommandLineArgument.java

@@ -22,7 +22,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
 public enum CommandLineArgument {
     ROOT_DIRECTORY("rootDir", String.class, "The root-directory that contains all submissions"),
     LANGUAGE("-l", String.class, "Select the language to parse the submissions", LanguageOption.getDefault().getDisplayName(), LanguageOption.getAllDisplayNames()),
-    BASE_CODE("-bc", String.class, "Name of the directory which contains the base code (common framework used in all submissions)"),
+    BASE_CODE("-bc", String.class, "Name of the subdirectory of the root directory which contains the base code (common framework used in all submissions)"),
     VERBOSITY("-v", String.class, "Verbosity", "quiet", List.of("parser", "quiet", "long", "details")), // TODO SH: Replace verbosity when integrating a real logging library
     DEBUG("-d", Boolean.class, "(Debug) parser. Non-parsable files will be stored"),
     SUBDIRECTORY("-S", String.class, "Look in directories <root-dir>/*/<dir> for programs"),

+ 22 - 24
jplag/src/main/java/de/jplag/JPlag.java

@@ -13,6 +13,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
 import java.util.stream.Collectors;
 
 import de.jplag.options.JPlagOptions;
@@ -40,7 +41,7 @@ public class JPlag implements Program {
     // ERROR REPORTING:
     private String currentSubmissionName = "<Unknown submission>"; // TODO PB: This should be moved to parseSubmissions(...)
     private int errors = 0;
-    private ArrayList<String> errorVector = new ArrayList<>(); // Vector of errors that occurred during the execution of the program.
+    private List<String> errorVector = new ArrayList<>(); // Vector of errors that occurred during the execution of the program.
 
     /**
      * Creates and initializes a JPlag instance, parameterized by a set of options.
@@ -149,33 +150,30 @@ public class JPlag implements Program {
      * This method checks whether the base code directory value is valid.
      */
     private void checkBaseCodeOption() throws ExitException {
-        if (!this.options.hasBaseCode()) {
-            return;
-        }
-
-        String baseCodePath = this.options.getRootDirectoryName() + File.separator + this.options.getBaseCodeSubmissionName();
-
-        if (!(new File(this.options.getRootDirectoryName())).exists()) {
-            throw new ExitException("Root directory \"" + this.options.getRootDirectoryName() + "\" doesn't exist!", ExitException.BAD_PARAMETER);
-        }
-
-        File f = new File(baseCodePath);
-
-        if (!f.exists()) {
-            // Base code dir doesn't exist
-            throw new ExitException("Basecode directory \"" + baseCodePath + "\" doesn't exist!", ExitException.BAD_PARAMETER);
-        }
+        if (options.hasBaseCode()) {
+            if (!new File(options.getRootDirectoryName()).exists()) {
+                throw new ExitException("Root directory \"" + options.getRootDirectoryName() + "\" doesn't exist!", ExitException.BAD_PARAMETER);
+            }
 
-        if (this.options.getSubdirectoryName() != null && this.options.getSubdirectoryName().length() != 0) {
-            f = new File(baseCodePath, this.options.getSubdirectoryName());
+            String baseCode = options.getBaseCodeSubmissionName().replace(File.separator, ""); // trim problematic file separators
+            if (baseCode.contains(".")) {
+                throw new ExitException("The basecode directory name \"" + baseCode + "\" cannot contain dots!", ExitException.BAD_PARAMETER);
+            }
+            String baseCodePath = options.getRootDirectoryName() + File.separator + baseCode;
+            if (!new File(baseCodePath).exists()) {
+                throw new ExitException("Basecode directory \"" + baseCodePath + "\" doesn't exist!", ExitException.BAD_PARAMETER);
+            }
 
-            if (!f.exists()) {
-                throw new ExitException("Basecode directory doesn't contain" + " the subdirectory \"" + this.options.getSubdirectoryName() + "\"!",
-                        ExitException.BAD_PARAMETER);
+            String subdirectory = options.getSubdirectoryName();
+            if (subdirectory != null && subdirectory.length() != 0) {
+                if (!new File(baseCodePath, subdirectory).exists()) {
+                    throw new ExitException("Basecode directory doesn't contain" + " the subdirectory \"" + subdirectory + "\"!",
+                            ExitException.BAD_PARAMETER);
+                }
             }
+            options.setBaseCodeSubmissionName(baseCode);
+            System.out.println("Basecode directory \"" + baseCodePath + "\" will be used");
         }
-
-        System.out.println("Basecode directory \"" + baseCodePath + "\" will be used");
     }
 
     private ArrayList<Submission> filterValidSubmissions(ArrayList<Submission> submissions) {