Skip to content
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
25 changes: 15 additions & 10 deletions packages/code-analyzer-sfge-engine/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,30 @@
hamcrest = "3.0"
junit-jupiter = "5.13.4"
commons-cli = "1.10.0"
commons-collections4 = "4.5.0"
tinkergraph-gremlin = "4.0.0-beta.1"
gremlin-driver = "4.0.0-beta.1"
tinkergraph-gremlin = "3.7.7"
antlr-runtime = "3.5.3"
log4j-api = "2.25.1"
log4j-core = "2.25.1"
log4j-api = "2.25.5"
log4j-core = "2.25.5"
gson = "2.13.2"
guava = "33.4.8-jre"
guava = "33.8.0-jre"
jsr305 = "3.0.2"
json-simple = "1.1.1"
reflections = "0.10.2"
asm = "9.8"
mockito-core = "5.19.0"
mockito-junit-jupiter = "5.19.0"
netty-all = "4.1.127.Final"
# Versions below force patched releases of dependencies that arrive transitively (via gremlin-core). See sfge/build.gradle.kts.
commons-configuration2 = "2.15.1"
commons-beanutils = "1.11.0"
commons-lang3 = "3.21.0"
commons-text = "1.15.0"
snakeyaml = "2.7"

[libraries]
hamcrest = { module = "org.hamcrest:hamcrest", version.ref = "hamcrest" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
commons-cli = { module = "commons-cli:commons-cli", version.ref = "commons-cli" }
commons-collections4 = { module = "org.apache.commons:commons-collections4", version.ref = "commons-collections4" }
tinkergraph-gremlin = { module = "org.apache.tinkerpop:tinkergraph-gremlin", version.ref = "tinkergraph-gremlin" }
gremlin-driver = { module = "org.apache.tinkerpop:gremlin-driver", version.ref = "gremlin-driver" }
antlr-runtime = { module = "org.antlr:antlr-runtime", version.ref = "antlr-runtime" }
log4j-api = { module = "org.apache.logging.log4j:log4j-api", version.ref = "log4j-api" }
log4j-core = { module = "org.apache.logging.log4j:log4j-core", version.ref = "log4j-core" }
Expand All @@ -44,4 +45,8 @@ asm = { module = "org.ow2.asm:asm", version.ref = "asm" }
junit-platform-launcher = { module = "org.junit.platform:junit-platform-launcher" }
mockito-core = { module = "org.mockito:mockito-core", version.ref = "mockito-core" }
mockito-junit-jupiter = { module = "org.mockito:mockito-junit-jupiter", version.ref = "mockito-junit-jupiter" }
netty-all = { module = "io.netty:netty-all", version.ref = "netty-all" }
commons-configuration2 = { module = "org.apache.commons:commons-configuration2", version.ref = "commons-configuration2" }
commons-beanutils = { module = "commons-beanutils:commons-beanutils", version.ref = "commons-beanutils" }
commons-lang3 = { module = "org.apache.commons:commons-lang3", version.ref = "commons-lang3" }
commons-text = { module = "org.apache.commons:commons-text", version.ref = "commons-text" }
snakeyaml = { module = "org.yaml:snakeyaml", version.ref = "snakeyaml" }
2 changes: 1 addition & 1 deletion packages/code-analyzer-sfge-engine/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@salesforce/code-analyzer-sfge-engine",
"description": "Plugin package that adds 'Salesforce Graph Engine' as an engine into Salesforce Code Analyzer",
"version": "0.25.0",
"version": "0.26.0-SNAPSHOT",
"author": "The Salesforce Code Analyzer Team",
"license": "BSD-3-Clause",
"homepage": "https://developer.salesforce.com/docs/platform/salesforce-code-analyzer/overview",
Expand Down
69 changes: 64 additions & 5 deletions packages/code-analyzer-sfge-engine/sfge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@ repositories {
// See https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format
dependencies {
implementation(libs.commons.cli)
implementation(libs.commons.collections4)
implementation(libs.tinkergraph.gremlin)
implementation(libs.gremlin.driver)
implementation(libs.tinkergraph.gremlin) {
// commons-collections 3.x (pulled in by commons-beanutils) has known vulnerabilities and no patched release.
// SFGE and the Gremlin graph layer do not use it, so it is removed from the classpath entirely.
exclude(group = "commons-collections", module = "commons-collections")
// gremlin-shaded bundles (relocated) copies of Jackson and Kryo that are only used for graph import/export
// (GraphSON/Gryo/GraphML). SFGE only builds and traverses an in-memory graph, and the bundled Jackson version
// contains known vulnerabilities that cannot be patched via dependency constraints, so we do not ship it.
exclude(group = "org.apache.tinkerpop", module = "gremlin-shaded")
}
// These are used directly by SFGE source (they previously arrived only transitively), so declare them explicitly.
implementation(libs.commons.lang3)
implementation(libs.commons.text)
implementation(libs.antlr.runtime)
implementation(libs.log4j.api)
implementation(libs.log4j.core)
Expand All @@ -35,9 +44,16 @@ dependencies {
}
implementation(libs.reflections)
implementation(libs.asm)
// Force patched versions of transitive dependencies of gremlin-core (embedded in-memory graph).
constraints {
implementation(libs.netty.all) {
because("Force specific version of transitive dependency")
implementation(libs.commons.configuration2) {
because("Patched version of a transitive dependency of gremlin-core")
}
implementation(libs.commons.beanutils) {
because("Patched version of a transitive dependency of gremlin-core")
}
implementation(libs.snakeyaml) {
because("Patched version of a transitive dependency of gremlin-core")
}
}
implementation(files("lib/apex-jorje-lsp-sfge.jar"))
Expand Down Expand Up @@ -71,11 +87,54 @@ tasks.jar {
tasks.register<Copy>("copyDependencies") {
from(configurations.runtimeClasspath) // This includes all runtime dependencies
into(javaLibDir) // Target directory for dependencies
// The directory is also the output of the jar task (sfge-*.jar), so we can't use Sync. Instead, remove any stale
// dependency jars left over from earlier builds (e.g. ones since removed from the runtime classpath) so that an
// incremental build packs exactly the same dependency set as a clean build.
doFirst {
delete(fileTree(javaLibDir) { exclude("sfge-*.jar") })
}
}
tasks.build {
dependsOn("copyDependencies") // Ensure dependencies are copied during the build process
}

// Fail the build if a library that is known to carry unpatchable vulnerabilities (or that SFGE has no need for) sneaks
// back onto the runtime classpath, e.g., through a Gremlin upgrade. Everything on the runtime classpath gets shipped.
val bannedRuntimeDependencies = mapOf(
"io.netty" to "Only needed by gremlin-driver (remote Gremlin client), which SFGE does not use",
"software.amazon.awssdk" to "Only needed by gremlin-driver (remote Gremlin client), which SFGE does not use",
"org.apache.tinkerpop:gremlin-driver" to "SFGE only uses the embedded in-memory graph",
"org.apache.tinkerpop:gremlin-util" to "SFGE only uses the embedded in-memory graph",
"org.apache.tinkerpop:gremlin-shaded" to "Bundles Jackson/Kryo with known vulnerabilities that cannot be patched via constraints",
"commons-collections:commons-collections" to "3.x has known vulnerabilities and no patched release",
"org.apache.commons:commons-collections4" to "SFGE does not use it (the NonNull* collections are plain Java/Guava); sonatype-2024-3350 flags SetUniqueList in it"
)
tasks.register("verifyNoBannedDependencies") {
group = "verification"
description = "Fails if a banned dependency is on the runtime classpath (and therefore would be shipped)."
val resolvedModules = configurations.runtimeClasspath.map { config ->
config.incoming.resolutionResult.allComponents
.mapNotNull { it.moduleVersion }
.map { "${it.group}:${it.name}:${it.version}" }
}
doLast {
val violations = resolvedModules.get().flatMap { coordinates ->
val (group, name) = coordinates.split(":")
bannedRuntimeDependencies.filter { (banned, _) -> banned == group || banned == "$group:$name" }
.map { (_, reason) -> "$coordinates -> $reason" }
}
if (violations.isNotEmpty()) {
throw GradleException("Banned dependencies found on the runtime classpath:\n " + violations.joinToString("\n "))
}
}
}
tasks.check {
dependsOn("verifyNoBannedDependencies")
}
tasks.build {
dependsOn("verifyNoBannedDependencies")
}


// ======== TEST RELATED TASKS =========================================================================================
jacoco {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.salesforce.collections;

import java.util.ArrayList;
import org.apache.commons.collections4.PredicateUtils;
import org.apache.commons.collections4.list.PredicatedList;

/** An ArrayList that doesn't allow null values */
public class NonNullArrayList<E> extends PredicatedList<E> {
public class NonNullArrayList<E> extends NullRejectingList<E> {
private NonNullArrayList(ArrayList<E> list) {
super(list, PredicateUtils.notNullPredicate());
super(list);
}

static <E> NonNullArrayList<E> newInstance() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.salesforce.collections;

/** Rejects null values for the NonNull* collections. Throws the same exception type for every write path. */
final class NonNullGuard {
private NonNullGuard() {}

static <T> T check(T item) {
if (item == null) {
throw new IllegalArgumentException("Null is not allowed in this collection");
}
return item;
}

/** Checks everything up front so that a rejected bulk operation leaves the collection unchanged. */
static void checkAll(Iterable<?> items) {
for (Object item : items) {
check(item);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,96 @@
package com.salesforce.collections;

import com.google.common.collect.ForwardingMap;
import com.google.common.collect.ForwardingMapEntry;
import com.google.common.collect.ForwardingSet;
import com.google.common.collect.Iterators;
import java.util.HashMap;
import org.apache.commons.collections4.PredicateUtils;
import org.apache.commons.collections4.map.PredicatedMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
* A HashMap that doesn't allow null keys or values. The default Map methods (putIfAbsent, merge, compute, replace,
* ...) write through {@link #put}, and replaceAll writes through the entries returned by {@link #entrySet()}, so
* guarding those two covers every write path.
*/
public class NonNullHashMap<K, V> extends ForwardingMap<K, V> {
private final HashMap<K, V> delegate;

/** A HashMap that doesn't allow null keys or values */
public class NonNullHashMap<K, V> extends PredicatedMap<K, V> {
private NonNullHashMap(HashMap<K, V> map) {
super(map, PredicateUtils.notNullPredicate(), PredicateUtils.notNullPredicate());
this.delegate = map;
}

static <K, V> NonNullHashMap<K, V> newInstance() {
return new NonNullHashMap<>(new HashMap<>());
}

@Override
protected Map<K, V> delegate() {
return delegate;
}

@Override
public V put(K key, V value) {
return super.put(NonNullGuard.check(key), NonNullGuard.check(value));
}

@Override
public void putAll(Map<? extends K, ? extends V> map) {
NonNullGuard.checkAll(map.keySet());
NonNullGuard.checkAll(map.values());
super.putAll(map);
}

@Override
public Set<Map.Entry<K, V>> entrySet() {
return new NullRejectingEntrySet<>(super.entrySet());
}

private static final class NullRejectingEntrySet<K, V> extends ForwardingSet<Map.Entry<K, V>> {
private final Set<Map.Entry<K, V>> delegate;

NullRejectingEntrySet(Set<Map.Entry<K, V>> delegate) {
this.delegate = delegate;
}

@Override
protected Set<Map.Entry<K, V>> delegate() {
return delegate;
}

@Override
public Iterator<Map.Entry<K, V>> iterator() {
return Iterators.transform(delegate.iterator(), NullRejectingEntry::new);
}

// The inherited toArray methods would hand out the underlying (unguarded) entries.
@Override
public Object[] toArray() {
return standardToArray();
}

@Override
public <T> T[] toArray(T[] array) {
return standardToArray(array);
}
}

private static final class NullRejectingEntry<K, V> extends ForwardingMapEntry<K, V> {
private final Map.Entry<K, V> delegate;

NullRejectingEntry(Map.Entry<K, V> delegate) {
this.delegate = delegate;
}

@Override
protected Map.Entry<K, V> delegate() {
return delegate;
}

@Override
public V setValue(V value) {
return super.setValue(NonNullGuard.check(value));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
package com.salesforce.collections;

import com.google.common.collect.ForwardingSet;
import java.util.Collection;
import java.util.HashSet;
import org.apache.commons.collections4.PredicateUtils;
import org.apache.commons.collections4.set.PredicatedSet;
import java.util.Set;

/** An HashSet that doesn't allow null values */
public class NonNullHashSet<E> extends PredicatedSet<E> {
public class NonNullHashSet<E> extends ForwardingSet<E> {
private final HashSet<E> delegate;

private NonNullHashSet(HashSet<E> set) {
super(set, PredicateUtils.notNullPredicate());
this.delegate = set;
}

static <E> NonNullHashSet<E> newInstance() {
return new NonNullHashSet<>(new HashSet<>());
}

@Override
protected Set<E> delegate() {
return delegate;
}

@Override
public boolean add(E element) {
return super.add(NonNullGuard.check(element));
}

@Override
public boolean addAll(Collection<? extends E> collection) {
NonNullGuard.checkAll(collection);
return super.addAll(collection);
}
}
Loading
Loading