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

Add codemodder framework and replace 1 codemod #24

Merged
merged 19 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
contents: write
environment: Public Release
runs-on: "ubuntu-latest"
container:
image: returntocorp/semgrep
env:
ORG_GRADLE_PROJECT_pixeeArtifactoryUsername: ${{ secrets.PIXEE_ARTIFACTORY_USERNAME }}
ORG_GRADLE_PROJECT_pixeeArtifactoryPassword: ${{ secrets.PIXEE_ARTIFACTORY_PASSWORD }}
Expand Down
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ graalvm = "22.3.0"
jackson = "2.13.1"
javaparser-core = "3.25.0"
javaparser-symbolsolver = "3.15.15"
java-security-toolkit = "1.0.0"
javax-inject = "1"
commons-jexl = "3.2.1"
logback = "1.4.5"
maven = "3.8.7"
openpixee-toolkit = "1.0.0"
picocli = "4.7.0"
slf4j = "2.0.6"
guice = "5.1.0"

[libraries]
autovalue-annotations = { module = "com.google.auto.value:auto-value-annotations", version.ref = "auto-value" }
Expand All @@ -20,6 +23,7 @@ commons-collections4 = "org.apache.commons:commons-collections4:4.4"
contrast-sarif = "com.contrastsecurity:java-sarif:2.0"
graal-sdk = { module = "org.graalvm.sdk:graal-sdk", version.ref = "graalvm" }
gson = "com.google.code.gson:gson:2.9.0"
guice = { module = "com.google.inject:guice", version.ref = "guice" }
immutables = "org.immutables:value:2.9.0"
jackson-core = { module = "com.fasterxml.jackson.core:jackson-core", version.ref = "jackson" }
jackson-yaml = { module = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml", version.ref = "jackson" }
Expand All @@ -28,6 +32,8 @@ javaparser-core = { module = "com.github.javaparser:javaparser-core", version.re
javaparser-symbolsolver-core = { module = "com.github.javaparser:javaparser-symbol-solver-core", version.ref = "javaparser-core" }
javaparser-symbolsolver-model = { module = "com.github.javaparser:javaparser-symbol-solver-model", version.ref = "javaparser-symbolsolver" }
javaparser-symbolsolver-logic = { module = "com.github.javaparser:javaparser-symbol-solver-logic", version.ref = "javaparser-symbolsolver" }
java-security-toolkit = { module = "io.openpixee:java-security-toolkit", version.ref = "java-security-toolkit" }
javax-inject = { module="javax.inject:javax.inject", version.ref = "javax-inject"}
jetbrains-annotations = "org.jetbrains:annotations:23.0.0"
jfiglet = "com.github.lalyos:jfiglet:0.0.8"
juniversalchardet = "com.github.albfernandez:juniversalchardet:2.4.0"
Expand Down
44 changes: 44 additions & 0 deletions languages/codemodder-common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@Suppress("DSL_SCOPE_VIOLATION") // https://github.com/gradle/gradle/issues/22797
plugins {
id("io.openpixee.codetl.base")
id("io.openpixee.codetl.java-library")
id("io.openpixee.codetl.maven-publish")
id("application")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unusual to have the application and java-library plugins in a project. Do we need the application plugin? I understand this is a library.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand these so I just copied them all from the root to every new project. 😓

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • io.openpixee.codetl.base is our conventions on top of Gradle's base plugin. It defines things constant to all buildscripts like buildscript formatting. It's usually implied by other plugins e.g. java-library: I can't recall if that's the case here.
  • io.openpixee.codetl.java-library is our conventions on top of Gradle's java library plugin. It's used to define a library used by other Java projects.
  • application is Gradle's application plugin. We don't have conventions for this one, I don't think we use it I can't remember.
  • io.openpixee.codetl.maven-publish is our conventions on top of Gradle's maven publish plugin. It's for publishing artifacts to a Maven repository. We only include it if we need to publish the artifact to a Maven repository.

alias(libs.plugins.fileversioning)
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}

spotless {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't doing anything

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I can delete all the spotless kotlins from the new projects?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, the formatting is being applied by the plugins at the top of this build script.

java {
// do we need anything here?
nahsra marked this conversation as resolved.
Show resolved Hide resolved
}
}

publishing {
publications {
register<MavenPublication>("maven") {
from(components["java"])
artifactId = "codemodder-common"
}
}
}

dependencies {
compileOnly(libs.jetbrains.annotations)
implementation(libs.guice)
implementation(libs.contrast.sarif)
implementation(libs.java.security.toolkit)
implementation(libs.slf4j.api)

testImplementation(testlibs.bundles.junit.jupiter)
testImplementation(testlibs.bundles.hamcrest)
testImplementation(testlibs.assertj)
testImplementation(testlibs.jgit)
testImplementation(testlibs.mockito)
testRuntimeOnly(testlibs.junit.jupiter.engine)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.openpixee.java;
package io.codemodder;

import java.util.Collections;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.openpixee.java;
package io.codemodder;

import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.openpixee.java;
package io.codemodder;

import com.github.javaparser.ast.Node;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -16,9 +15,6 @@ public interface FileWeavingContext {

boolean madeWeaves();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took out this tight coupling to JavaParser as this class is intended to be used generally.

/** Intended to be used when dealing with AST objects. */
boolean isLineIncluded(Node n);

/** Intended to be used in non-JavaParser situations. */
boolean isLineIncluded(int line);

Expand Down Expand Up @@ -47,14 +43,6 @@ public boolean madeWeaves() {
return !weaves.isEmpty();
}

@Override
public boolean isLineIncluded(final Node n) {
if (n.getRange().isEmpty()) {
return true;
}
return includesExcludes.matches(n.getRange().get().begin.line);
}

@Override
public boolean isLineIncluded(final int line) {
return includesExcludes.matches(line);
Expand All @@ -65,4 +53,8 @@ static FileWeavingContext createDefault(
final File file, final IncludesExcludes includesExcludes) {
return new Default(includesExcludes.getIncludesExcludesForFile(file));
}

static FileWeavingContext createDefault(final LineIncludesExcludes includesExcludesForFile) {
return new Default(includesExcludesForFile);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.openpixee.java;
package io.codemodder;

import com.google.common.annotations.VisibleForTesting;
import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.openpixee.java;
package io.codemodder;

import java.util.Collections;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.openpixee.java;
package io.codemodder;

import java.io.File;
import java.nio.file.FileSystem;
Expand All @@ -7,13 +7,13 @@
import java.util.Objects;

/** This type is used in include/exclude logic for matching paths. */
final class PathMatcher {
public final class PathMatcher {

private final Integer line;
private final String repositoryRootPath;
private final java.nio.file.PathMatcher matcher;

PathMatcher(
public PathMatcher(
final FileSystem fs,
final File repositoryRoot,
final String pathPattern,
Expand All @@ -25,7 +25,7 @@ final class PathMatcher {
}

/** Return if this path matcher matches the given file. */
boolean matches(final File file) {
public boolean matches(final File file) {
String candidateFilePath = Path.of(file.getAbsolutePath()).normalize().toString();
String relativeCandidateFilePath = candidateFilePath.substring(repositoryRootPath.length());
if (!relativeCandidateFilePath.startsWith("/")) {
Expand All @@ -34,11 +34,11 @@ boolean matches(final File file) {
return matcher.matches(Paths.get(relativeCandidateFilePath));
}

Integer line() {
public Integer line() {
return line;
}

boolean targetsLine() {
public boolean targetsLine() {
return line != null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.openpixee.java;
package io.codemodder;

import java.util.List;
import java.util.Objects;
Expand Down
53 changes: 53 additions & 0 deletions languages/codemodder-default-codemods/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
@Suppress("DSL_SCOPE_VIOLATION") // https://github.com/gradle/gradle/issues/22797
plugins {
id("io.openpixee.codetl.base")
id("io.openpixee.codetl.java-library")
nahsra marked this conversation as resolved.
Show resolved Hide resolved
id("io.openpixee.codetl.maven-publish")
id("application")
alias(libs.plugins.fileversioning)
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}

application {
mainClass.set("io.codemodder.codemods.Runner")
}

spotless {
nahsra marked this conversation as resolved.
Show resolved Hide resolved
java {
// do we need anything here?
}
}

publishing {
publications {
register<MavenPublication>("maven") {
from(components["java"])
artifactId = "codemodder-default-codemods"
}
}
}

dependencies {
implementation(libs.javax.inject)
implementation(libs.contrast.sarif)
implementation(libs.slf4j.api)
implementation(libs.javaparser.core)
implementation(libs.javaparser.symbolsolver.core)
implementation(libs.javaparser.symbolsolver.logic)
implementation(libs.javaparser.symbolsolver.model)
implementation(project(":languages:codemodder-common"))
implementation(project(":languages:codemodder-framework-java"))
implementation(project(":languages:codemodder-semgrep-provider"))

testImplementation(testlibs.bundles.junit.jupiter)
testImplementation(testlibs.bundles.hamcrest)
testImplementation(testlibs.assertj)
testImplementation(testlibs.mockito)

testRuntimeOnly(testlibs.junit.jupiter.engine)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.codemodder.codemods;

import static io.codemodder.CodemodInvoker.run;

/** Invokes the codemod from a command line. */
public final class Runner {

public static void main(final String[] args) {
run(SecureRandomCodemod.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.codemodder.codemods;

import com.contrastsecurity.sarif.Result;
import com.contrastsecurity.sarif.SarifSchema210;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.visitor.ModifierVisitor;
import io.codemodder.ChangeConstructorTypeVisitor;
import io.codemodder.CodeDirectory;
import io.codemodder.Codemod;
import io.codemodder.FileWeavingContext;
import io.codemodder.JavaParserChanger;
import io.codemodder.ReviewGuidance;
import io.codemodder.Sarif;
import io.codemodder.providers.sarif.semgrep.SemgrepSarifProvider;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Turns {@link java.util.Random} into {@link java.security.SecureRandom}. */
@Codemod(
value = "pixee:java/secure-random",
author = "[email protected]",
reviewGuidance = ReviewGuidance.MERGE_WITHOUT_REVIEW)
public final class SecureRandomCodemod implements JavaParserChanger {

private final SarifSchema210 sarif;

@Inject
public SecureRandomCodemod(
final CodeDirectory codeDirectory, final SemgrepSarifProvider sarifProvider)
nahsra marked this conversation as resolved.
Show resolved Hide resolved
throws IOException {
this.sarif = sarifProvider.getSarif(codeDirectory.asPath(), "secure-random.semgrep");
}

@Override
public Optional<ModifierVisitor<FileWeavingContext>> createModifierVisitor(
nahsra marked this conversation as resolved.
Show resolved Hide resolved
final CodeDirectory codeDirectory, final Path path, final CompilationUnit cu) {
List<Result> results = Sarif.getResultsForCompilationUnit(sarif, path);
logger.trace("Found {} results in {} to change", results.size(), path);
if (!results.isEmpty()) {
return Optional.of(
new ChangeConstructorTypeVisitor(
Sarif.findRegions(results),
"java.security.SecureRandom",
"pixee:java/secure-random"));
}
return Optional.empty();
}

private static final Logger logger = LoggerFactory.getLogger(SecureRandomCodemod.class);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rules:
- id: secure-random
nahsra marked this conversation as resolved.
Show resolved Hide resolved
pattern: new Random()
message: Insecure PRNG
languages:
- java
severity: WARNING
53 changes: 53 additions & 0 deletions languages/codemodder-framework-java/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
@Suppress("DSL_SCOPE_VIOLATION") // https://github.com/gradle/gradle/issues/22797
plugins {
id("io.openpixee.codetl.base")
id("io.openpixee.codetl.java-library")
id("io.openpixee.codetl.maven-publish")
id("application")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

application isn't wanted here

alias(libs.plugins.fileversioning)
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}

spotless {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't doing anything

java {
// do we need anything here?
}
}

publishing {
publications {
register<MavenPublication>("maven") {
from(components["java"])
artifactId = "codemodder-framework-java"
}
}
}

dependencies {
compileOnly(libs.jetbrains.annotations)

api("io.github.pixee:codetf-java:0.0.2") // TODO bring codetf-java into the monorepo

implementation(libs.guice)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if some of these libraries are part of codemodder-framework's public API, then they should use the api configuration instead of implementation. For example, I think libs.contrast.sarif should use api, because we publish types from that library to custom codemods via the accessors in SemgrepSarif.

implementation(libs.contrast.sarif)
implementation(libs.javaparser.core)
implementation(libs.javaparser.symbolsolver.core)
implementation(libs.javaparser.symbolsolver.logic)
implementation(libs.javaparser.symbolsolver.model)
implementation(libs.logback.classic)
implementation(libs.maven.model)
implementation(libs.slf4j.api)
implementation(project(":languages:codemodder-common"))

testImplementation(testlibs.bundles.junit.jupiter)
testImplementation(testlibs.bundles.hamcrest)
testImplementation(testlibs.assertj)
testImplementation(testlibs.mockito)

testRuntimeOnly(testlibs.junit.jupiter.engine)
}
Loading