WDLHandler implements LanguageHandlerInterface { public void checkForRecursiveHTTPImports(String content, Set<String> currentFileImports) throws IOException { String[] lines = StringUtils.split(content, '\n'); for (String line : lines) { Matcher m = IMPORT_PATTERN.matcher(line); while (m.find()) { String match = m.group(1); if (match.startsWith("http: if (currentFileImports.contains(match)) { throw new CustomWebApplicationException(ERROR_PARSING_WORKFLOW_YOU_MAY_HAVE_A_RECURSIVE_IMPORT, HttpStatus.SC_BAD_REQUEST); } else { URL url = new URL(match); try (InputStream is = url.openStream(); BoundedInputStream boundedInputStream = new BoundedInputStream(is, FileUtils.ONE_MB)) { String fileContents = IOUtils.toString(boundedInputStream, StandardCharsets.UTF_8); Set<String> importsForThisPath = new HashSet<>(currentFileImports); importsForThisPath.add(match); checkForRecursiveHTTPImports(fileContents, importsForThisPath); } } } } } }  static void checkForRecursiveLocalImports(String content, Set<SourceFile> sourceFiles, Set<String> absolutePaths, String parent); @Override Version parseWorkflowContent(String filepath, String content, Set<SourceFile> sourceFiles, Version version); Optional<String> reportValidationForLocalRecursiveImports(String primaryDescriptorContent, Set<SourceFile> sourcefiles,
            String primaryDescriptorFilePath); VersionTypeValidation validateEntrySet(Set<SourceFile> sourcefiles, String primaryDescriptorFilePath, String type); void checkForRecursiveHTTPImports(String content, Set<String> currentFileImports); @Override VersionTypeValidation validateWorkflowSet(Set<SourceFile> sourcefiles, String primaryDescriptorFilePath); @Override VersionTypeValidation validateToolSet(Set<SourceFile> sourcefiles, String primaryDescriptorFilePath); @Override VersionTypeValidation validateTestParameterSet(Set<SourceFile> sourceFiles); @Override Map<String, SourceFile> processImports(String repositoryId, String content, Version version,
            SourceCodeRepoInterface sourceCodeRepoInterface, String filepath); @Override String getContent(String mainDescName, String mainDescriptor, Set<SourceFile> secondarySourceFiles,
            LanguageHandlerInterface.Type type, ToolDAO dao); static final Logger LOG; static final String ERROR_PARSING_WORKFLOW_YOU_MAY_HAVE_A_RECURSIVE_IMPORT; static final String ERROR_PARSING_WORKFLOW_RECURSIVE_LOCAL_IMPORT; }
@Test public void testRecursiveImports() throws IOException { final File recursiveWdl = new File(ResourceHelpers.resourceFilePath("recursive.wdl")); final WDLHandler wdlHandler = new WDLHandler(); String s = FileUtils.readFileToString(recursiveWdl, StandardCharsets.UTF_8); try { wdlHandler.checkForRecursiveHTTPImports(s, new HashSet<>()); Assert.fail("Should've detected recursive import"); } catch (CustomWebApplicationException e) { Assert.assertEquals(ERROR_PARSING_WORKFLOW_YOU_MAY_HAVE_A_RECURSIVE_IMPORT, e.getErrorMessage()); } final File notRecursiveWdl = new File(ResourceHelpers.resourceFilePath("valid_description_example.wdl")); s = FileUtils.readFileToString(notRecursiveWdl, StandardCharsets.UTF_8); wdlHandler.checkForRecursiveHTTPImports(s, new HashSet<>()); }