Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gradle wrapper security warning #1989

Merged
merged 1 commit into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import org.eclipse.jdt.ls.core.internal.ServiceStatus;
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
import org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator;
import org.eclipse.lsp4j.CodeActionOptions;
import org.eclipse.lsp4j.CodeLensOptions;
import org.eclipse.lsp4j.DocumentFilter;
Expand Down Expand Up @@ -233,11 +235,15 @@ public IStatus runInWorkspace(IProgressMonitor monitor) {
long start = System.currentTimeMillis();
connection.sendStatus(ServiceStatus.Starting, "Init...");
SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
Preferences preferences = preferenceManager.getPreferences();
if (preferences.isImportGradleEnabled()) {
WrapperValidator.putSha256(preferences.getGradleWrapperList());
}
try {
ProjectsManager.setAutoBuilding(false);
projectsManager.initializeProjects(roots, subMonitor);
projectsManager.configureFilters(monitor);
ProjectsManager.setAutoBuilding(preferenceManager.getPreferences().isAutobuildEnabled());
ProjectsManager.setAutoBuilding(preferences.isAutobuildEnabled());
JavaLanguageServerPlugin.logInfo("Workspace initialized in " + (System.currentTimeMillis() - start) + "ms");
connection.sendStatus(ServiceStatus.Started, "Ready");
} catch (OperationCanceledException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.eclipse.buildship.core.GradleDistribution;
Expand Down Expand Up @@ -76,58 +73,10 @@ private void updateProject(ProjectsManager projectsManager, IProject project, bo

private boolean hasAllowedChecksumsChanged(Preferences oldPreferences, Preferences newPreferences) {
if (!Objects.equals(oldPreferences.getGradleWrapperList(), newPreferences.getGradleWrapperList())) {
putSha256(newPreferences.getGradleWrapperList());
WrapperValidator.putSha256(newPreferences.getGradleWrapperList());
return true;
}
return false;
}

private void putSha256(List<?> gradleWrapperList) {
List<String> sha256Allowed = new ArrayList<>();
List<String> sha256Disallowed = new ArrayList<>();
for (Object object : gradleWrapperList) {
if (object instanceof Map) {
Map<?, ?> map = (Map<?, ?>) object;
final ChecksumWrapper sha256 = this.new ChecksumWrapper();
sha256.allowed = true;
map.forEach((k, v) -> {
if (k instanceof String) {
switch ((String) k) {
case "sha256":
if (v instanceof String) {
sha256.checksum = (String) v;
}
break;
case "allowed":
if (v instanceof Boolean) {
sha256.allowed = (Boolean) v;
}
break;
default:
break;
}
}
});
if (sha256.checksum != null) {
if (sha256.allowed) {
sha256Allowed.add(sha256.checksum);
} else {
sha256Disallowed.add(sha256.checksum);
}
}
}
}
WrapperValidator.clear();
if (sha256Allowed != null) {
WrapperValidator.allow(sha256Allowed);
}
if (sha256Disallowed != null) {
WrapperValidator.disallow(sha256Disallowed);
}
}

class ChecksumWrapper {
private String checksum;
private boolean allowed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -312,4 +313,53 @@ public static Set<String> getDisallowed() {
return Collections.unmodifiableSet(allowed);
}

public static void putSha256(List<?> gradleWrapperList) {
List<String> sha256Allowed = new ArrayList<>();
List<String> sha256Disallowed = new ArrayList<>();
for (Object object : gradleWrapperList) {
if (object instanceof Map) {
Map<?, ?> map = (Map<?, ?>) object;
final ChecksumWrapper sha256 = new ChecksumWrapper();
sha256.allowed = true;
map.forEach((k, v) -> {
if (k instanceof String) {
switch ((String) k) {
case "sha256":
if (v instanceof String) {
sha256.checksum = (String) v;
}
break;
case "allowed":
if (v instanceof Boolean) {
sha256.allowed = (Boolean) v;
}
break;
default:
break;
}
}
});
if (sha256.checksum != null) {
if (sha256.allowed) {
sha256Allowed.add(sha256.checksum);
} else {
sha256Disallowed.add(sha256.checksum);
}
}
}
}
WrapperValidator.clear();
if (sha256Allowed != null) {
WrapperValidator.allow(sha256Allowed);
}
if (sha256Disallowed != null) {
WrapperValidator.disallow(sha256Disallowed);
}
}

private static class ChecksumWrapper {
private String checksum;
private boolean allowed;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -137,4 +139,34 @@ public void testMissingSha256() throws Exception {
assertEquals(size, WrapperValidator.size());
}
}

@Test
public void testPreferences() throws Exception {
WrapperValidator wrapperValidator = new WrapperValidator(100);
Set<String> allowed = WrapperValidator.getAllowed();
Set<String> disallowed = WrapperValidator.getDisallowed();
File file = new File(getSourceProjectDirectory(), "gradle/gradle-4.0");
wrapperValidator.checkWrapper(file.getAbsolutePath());
int size = WrapperValidator.size();
List list = new ArrayList();
Map map = new HashMap();
map.put("sha256", "41c8aa7a337a44af18d8cda0d632ebba469aef34f3041827624ef5c1a4e4419d");
map.put("allowed", Boolean.TRUE);
list.add(map);
try {
ValidationResult result = wrapperValidator.checkWrapper(file.getAbsolutePath());
assertFalse(result.isValid());
assertNotNull(result.getChecksum());
WrapperValidator.clear();
WrapperValidator.putSha256(list);
result = wrapperValidator.checkWrapper(file.getAbsolutePath());
assertTrue(result.isValid());
} finally {
WrapperValidator.clear();
WrapperValidator.allow(allowed);
WrapperValidator.disallow(disallowed);
wrapperValidator.checkWrapper(file.getAbsolutePath());
assertEquals(size, WrapperValidator.size());
}
}
}