-
Notifications
You must be signed in to change notification settings - Fork 7
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
Changes from 16 commits
77dce55
84f32b9
cf34774
af366db
2ebe359
197bd85
1735091
d2d0176
d5740b3
aed1dbc
26c4830
bbb1edc
4dc300b
fb856d5
7390cb4
51c5fc4
aa13a24
ecf7bfa
dddcb2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
@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") | ||
alias(libs.plugins.fileversioning) | ||
} | ||
|
||
java { | ||
toolchain { | ||
languageVersion.set(JavaLanguageVersion.of(11)) | ||
} | ||
} | ||
|
||
spotless { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't doing anything There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I can delete all the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
} | ||
} | ||
|
||
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; | ||
|
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; | ||
|
||
|
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; | ||
|
||
|
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; | ||
|
@@ -16,9 +15,6 @@ public interface FileWeavingContext { | |
|
||
boolean madeWeaves(); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
||
|
@@ -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); | ||
|
@@ -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 java.util.Collections; | ||
import java.util.Objects; | ||
|
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; | ||
|
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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
@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 { | ||
} | ||
} | ||
|
||
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,16 @@ | ||
package io.codemodder.codemods; | ||
|
||
import io.codemodder.Changer; | ||
import java.util.List; | ||
|
||
/** | ||
* Give an ability for users to list all the codemods so they don't have to reference them | ||
* individually. | ||
*/ | ||
public final class DefaultCodemods { | ||
|
||
/** Get a list of all the codemods in our default set. */ | ||
public static List<Class<? extends Changer>> asList() { | ||
return List.of(SecureRandomCodemod.class); | ||
} | ||
} |
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(args, SecureRandomCodemod.class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.codemodder.codemods; | ||
|
||
import com.contrastsecurity.sarif.Region; | ||
import com.github.javaparser.ast.visitor.ModifierVisitor; | ||
import io.codemodder.ChangeConstructorTypeVisitor; | ||
import io.codemodder.Codemod; | ||
import io.codemodder.CodemodInvocationContext; | ||
import io.codemodder.FileWeavingContext; | ||
import io.codemodder.ReviewGuidance; | ||
import io.codemodder.RuleSarif; | ||
import io.codemodder.providers.sarif.semgrep.SemgrepJavaParserChanger; | ||
import io.codemodder.providers.sarif.semgrep.SemgrepScan; | ||
import java.util.List; | ||
import javax.inject.Inject; | ||
|
||
/** Turns {@link java.util.Random} into {@link java.security.SecureRandom}. */ | ||
@Codemod( | ||
id = "pixee:java/secure-random", | ||
author = "[email protected]", | ||
reviewGuidance = ReviewGuidance.MERGE_WITHOUT_REVIEW) | ||
public final class SecureRandomCodemod extends SemgrepJavaParserChanger { | ||
|
||
@Inject | ||
public SecureRandomCodemod( | ||
@SemgrepScan(pathToYaml = "/secure-random.yaml", ruleId = "secure-random") | ||
nahsra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
final RuleSarif sarif) { | ||
super(sarif); | ||
} | ||
|
||
@Override | ||
public ModifierVisitor<FileWeavingContext> createVisitor( | ||
final CodemodInvocationContext context, final List<Region> regions) { | ||
return new ChangeConstructorTypeVisitor( | ||
regions, "java.security.SecureRandom", context.codemodId()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
rules: | ||
- id: secure-random | ||
pattern: new Random() | ||
message: Insecure PRNG | ||
languages: | ||
- java | ||
severity: WARNING |
There was a problem hiding this comment.
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
andjava-library
plugins in a project. Do we need theapplication
plugin? I understand this is a library.There was a problem hiding this comment.
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. 😓
There was a problem hiding this comment.
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.