github-advanced-security[bot] commented on code in PR #2936: URL: https://github.com/apache/groovy/pull/2936#discussion_r4208232830
########## subprojects/groovy-lsp/src/main/java/org/apache/groovy/lsp/internal/compile/GroovyCompiler.java: ########## @@ -0,0 +1,423 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.groovy.lsp.internal.compile; + +import groovy.lang.GroovyClassLoader; +import org.apache.groovy.lsp.internal.intelligence.StaticTypeCheckingPass; +import org.apache.groovy.lsp.internal.util.Uris; +import org.apache.groovy.lsp.internal.workspace.TextDocument; +import org.codehaus.groovy.control.CompilationFailedException; +import org.codehaus.groovy.control.CompilationUnit; +import org.codehaus.groovy.control.CompilerConfiguration; +import org.codehaus.groovy.control.SourceUnit; +import org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit; + +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.net.URLClassLoader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.PosixFilePermissions; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.function.Supplier; + +/** + * Compiles Groovy sources through semantic analysis (no class files) so + * the language server can offer diagnostics and navigation on a partial + * AST. Errors are collected rather than aborting the compile. + */ +public final class GroovyCompiler implements AutoCloseable { + + private static final String ASTTEST_ENABLE = "groovy.asttest.enable"; + private static final String GRAPE_ENABLE = "groovy.grape.enable"; + private static final Object PROCESS_FLAGS = new Object(); + + private GroovyClassLoader groovyLoader; + private URLClassLoader classpathLoader; + private LoaderKey loaderKey; + private Path javacOutput; + + /** + * Compiles {@code openDocuments} together with extra files from disk + * that are not already open. Open buffers overlay the filesystem. + * + * @param openDocuments open editor buffers + * @param extraFiles additional Groovy files + * @param settings compiler settings + * @param parentLoader parent class loader + * @return an immutable snapshot + */ + public CompilationSnapshot compile(final Collection<TextDocument> openDocuments, final Collection<Path> extraFiles, + final CompilerSettings settings, final ClassLoader parentLoader) { + synchronized (this) { + CompilerConfiguration configuration = settings.toConfiguration(); + return withProcessFlags(settings, () -> + compileUnit(openDocuments, extraFiles, settings, parentLoader, configuration)); + } + } + + private CompilationSnapshot compileUnit(final Collection<TextDocument> openDocuments, final Collection<Path> extraFiles, + final CompilerSettings settings, final ClassLoader parentLoader, + final CompilerConfiguration configuration) { + Map<URI, Integer> versions = new LinkedHashMap<>(); + Map<URI, String> texts = new LinkedHashMap<>(); + Map<URI, String> javaOverlays = new LinkedHashMap<>(); + List<File> javaFiles = collectJavaSources(openDocuments, extraFiles, versions, texts, javaOverlays); + LspJavaCompiler javaCompiler = javaFiles.isEmpty() ? null : new LspJavaCompiler(javaOverlays); + CompilationUnit unit = compilationUnit(settings, parentLoader, configuration, javaCompiler, !javaFiles.isEmpty()); + addOpenDocuments(unit, openDocuments, versions, texts); + addExtraFiles(unit, extraFiles, versions, texts); + if (unit instanceof JavaAwareCompilationUnit javaUnit && !javaFiles.isEmpty()) { + javaUnit.addSources(javaFiles.toArray(File[]::new)); + } + + try { + unit.compile(settings.getThroughPhase()); + } catch (CompilationFailedException ignored) { + // keep the partial AST and collected messages + } + StaticTypeCheckingPass.apply(unit); + + Map<URI, CompiledDocument> compiled = CompilationSnapshot.newMap(); + var sourceIterator = unit.iterator(); + while (sourceIterator.hasNext()) { + addCompiled(compiled, versions, texts, sourceIterator.next()); + } + Map<URI, List<JavaMessage>> javaMessages = javaCompiler == null ? Map.of() : javaCompiler.messages(); + for (Map.Entry<URI, String> entry : texts.entrySet()) { + compiled.computeIfAbsent(entry.getKey(), uri -> new CompiledDocument(uri, + versions.getOrDefault(uri, -1), entry.getValue(), null, null, null)); + } + for (Map.Entry<URI, List<JavaMessage>> entry : javaMessages.entrySet()) { + compiled.compute(entry.getKey(), (uri, current) -> { + CompiledDocument base = current == null + ? new CompiledDocument(uri, versions.getOrDefault(uri, -1), + texts.getOrDefault(uri, ""), null, null, null) : current; + return base.withJavaMessages(entry.getValue()); + }); + } + JavaSymbolIndex javaSymbols = javaCompiler == null ? JavaSymbolIndex.EMPTY : javaCompiler.index(); + return new CompilationSnapshot(compiled, javaSymbols); + } + + private CompilationUnit compilationUnit(final CompilerSettings settings, final ClassLoader parentLoader, + final CompilerConfiguration configuration, final LspJavaCompiler javaCompiler, + final boolean joint) { + if (!joint || javaCompiler == null) { + return new CompilationUnit(configuration, null, loaderFor(settings, parentLoader, configuration)); + } + Map<String, Object> jointOptions = new HashMap<>(); + jointOptions.put(CompilerConfiguration.MEM_STUB, Boolean.TRUE); + configuration.setJointCompilationOptions(jointOptions); + GroovyClassLoader loader = loaderFor(settings, parentLoader, configuration); + if (!prepareJavacOutput(configuration)) { + return new CompilationUnit(configuration, null, loader); + } + JavaAwareCompilationUnit unit = new JavaAwareCompilationUnit(configuration, loader); + unit.setCompilerFactory(javaCompiler); + return unit; + } + + private synchronized boolean prepareJavacOutput(final CompilerConfiguration configuration) { + deleteJavacOutput(); + try { + javacOutput = Files.createTempDirectory("groovy-lsp-javac-"); Review Comment: ## SonarCloud / Temporary files should not be created in publicly writable directories <!--SONAR_ISSUE_KEY:AaEWyq1fI3kxgnqHLHJM-->Make sure publicly writable directories are used safely here. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_groovy&issues=AaEWyq1fI3kxgnqHLHJM&open=AaEWyq1fI3kxgnqHLHJM&pullRequest=2936">SonarQube Cloud</a></p> [Show more details](https://github.com/apache/groovy/security/code-scanning/283) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
